Page 1 of 1

Reading multiple bytes

Posted: Mon Jun 11, 2012 5:55 pm
by MJU
When I read a device sometimes it sends multiple bytes back (serial protocols).
But what is the best way to receive and store all the data that is send back?

Sometimes a 1-wire device sends it 64bits code, sometimes a I2C device sends 3 byte and so on.
How can I make sure I can receive them, and use the data bytes in a Flowcode?

Do I need to make a loop for the amount of bytes I will receive and make store each received byte in a variable?

Any help is welcome..

Re: Reading multiple bytes

Posted: Tue Jun 12, 2012 9:42 am
by Benj
Hello,

A good option is sometimes to use an array.

For example here is a receive loop for a RS232 data receive.

idx = 0
rx = 0
while rx < 255
{
rx = RS232 readbyte (10)
decision (rx < 255)
yes: data[idx] = rx
no: data[idx] = 0
idx = idx + 1
}

It is basically loading each byte into the byte array or string variable data.

When a timeout is received (255) a 0 is added to the end of the array to signify the end of the data.

The idx byte is used as the array pointer and once outside the loop can also be used for the number of bytes that were received.