Write variable number of bytes to BUSOUT with fixed length array

Started by crankshaft, Jan 31, 2022, 01:01 AM

Previous topic - Next topic

crankshaft

Is there a more efficient way of doing this ??

I have a byte array SERDATA[1], this array is populated with varying length data from 3 to 10 and I want to send the non-null bytes via Hbusout (or other similar commands) and the only way I can find of doing this is to calc the data length (SDLEN) and use this in a switch as below.

In this application I am starting from byte 2 as bytes 0,1 of SERDATA are the DEVID and ADDR.

This works, but I wondered if there is a more efficient way with less code space.

    DEVID = SERDATA[0]
    ADDID = SERDATA[1]

    Select SDLEN
        Case 3
            HBusOut DEVID,ADDID,[SERDATA[2]]
        Case 4
            HBusOut DEVID,ADDID,[SERDATA[2],SERDATA[3]]
        Case 5
            HBusOut DEVID,ADDID,[SERDATA[2],SERDATA[3],SERDATA[4]]
        Case 6
            HBusOut DEVID,ADDID,[SERDATA[2],SERDATA[3],SERDATA[4],SERDATA[5]]
        Case 7
            HBusOut DEVID,ADDID,[SERDATA[2],SERDATA[3],SERDATA[4],SERDATA[5],SERDATA[6]]
        Case 8
            HBusOut DEVID,ADDID,[SERDATA[2],SERDATA[3],SERDATA[4],SERDATA[5],SERDATA[6],SERDATA[7]]
        Case 9
            HBusOut DEVID,ADDID,[SERDATA[2],SERDATA[3],SERDATA[4],SERDATA[5],SERDATA[6],SERDATA[7],SERDATA[8]]
        Case 10
            HBusOut DEVID,ADDID,[SERDATA[2],SERDATA[3],SERDATA[4],SERDATA[5],SERDATA[6],SERDATA[7],SERDATA[8],SERDATA[9]]
    EndSelect


I thought about looping over the SERDATA array and calling HBUSOUT for each active byte, but that is also going to generate an ACK for each one and I would also prefer to send this as a single event.

I also considered using the Str function, but the problem with that is the length cannot be a variable:

Str SERDATA \4
Str SERDATA \SDLEN

top204

For more control, use the individual Bus commands, so the Start, Stop, Ack etc, commands can be send, along with whatever data is being sent or received in an loop.