Storing and Using Byte Received with FullDuplexSerial
coryco2
Posts: 107
I am trying to understand how to utilize the Rx functions of FullDuplexSerial in order to receive different-length strings of bytes of data and store them in byte-sized variables for use in other methods.
I started with this
Which was fine, except for all 16 bytes of the buffer had to be filled before the code continued. Some of the strings I want to receive are less than 16 bytes, and the code just waited and started filling the leftover bytes with the next incoming string, which just made a mess. I tried substituting serial.rxcheck, but was uncertain how to use it--would I have to add an IFstatement after every line to see if a -1 was returned instead of a data byte, and then jump to the code that uses the variables and pass the byte number of the highest non -1 received byte? And flush the Rx buffer above that somehow to get rid of the stale data? I don't grasp how to do this efficiently.
What I am looking to do is to have an Rx loop that will receive from 1 to 16 bytes of data, assigning the value of each byte to a separate variable, and the number of bytes of the received string to another variable. Each string will end with the same value: 255. Once the variables values are evaluated by other code, I want to return to the Rx loop, flush the Rx buffer and repeat the process.
It doesn't seem like it should be that difficult, but I am having a mental block, and would appreciate any suggestions. Thanks!
I started with this
repeat ReceivedByte[0] := serial.rx ReceivedByte[1] := serial.rx ReceivedByte[2] := serial.rx ReceivedByte[3] := serial.rx ReceivedByte[4] := serial.rx ReceivedByte[5] := serial.rx ReceivedByte[6] := serial.rx ReceivedByte[7] := serial.rx ReceivedByte[8] := serial.rx ReceivedByte[9] := serial.rx ReceivedByte[10] := serial.rx ReceivedByte[11] := serial.rx ReceivedByte[12] := serial.rx ReceivedByte[13] := serial.rx ReceivedByte[14] := serial.rx ReceivedByte[15] := serial.rx
Which was fine, except for all 16 bytes of the buffer had to be filled before the code continued. Some of the strings I want to receive are less than 16 bytes, and the code just waited and started filling the leftover bytes with the next incoming string, which just made a mess. I tried substituting serial.rxcheck, but was uncertain how to use it--would I have to add an IFstatement after every line to see if a -1 was returned instead of a data byte, and then jump to the code that uses the variables and pass the byte number of the highest non -1 received byte? And flush the Rx buffer above that somehow to get rid of the stale data? I don't grasp how to do this efficiently.
What I am looking to do is to have an Rx loop that will receive from 1 to 16 bytes of data, assigning the value of each byte to a separate variable, and the number of bytes of the received string to another variable. Each string will end with the same value: 255. Once the variables values are evaluated by other code, I want to return to the Rx loop, flush the Rx buffer and repeat the process.
It doesn't seem like it should be that difficult, but I am having a mental block, and would appreciate any suggestions. Thanks!
Comments
Look at StringIn in PST. I think PST also has a DecIn which I think is what you are after.
If you don't use the PST methods I mentioned, you'd want to use a loop instead of the way your doing it. Something like this:
The loop exits with the string including the terminating 255.
I've tried every way I could think of to make this work (e.g. adding another "if" statement to detect when the value of serial.rxCheck is -1, etc), but everything seems to screw up the timing to receive bytes when they are in the buffer.
Any suggestions?
And last but not least, do you have a simple test case?