11-13-2023 10:16 PM
Hi.
Using the protocol below as a start point can anyone indicate a LV for reading (such binary data).
Protocol;
STX (0x02)
Fixed length message of say 20 bytes
ETX (0x03)
My expectation is that this is a pretty general question (with a general response ?).
The data integrity (at this stage,) would be from STX at start, ETX at end, with the appropriate fixed space between them.
Regards JC.......
11-14-2023 01:30 AM - edited 11-14-2023 01:39 AM
One difficulty here is likely that there can be 0x02 and 0x03 bytes in the binary data and that they will be escaped by prepending some specific byte in front, making your fixed length data not really very fixed anymore. This would be in embedded systems typically solved by reading byte by byte and deciding on the spot in a mini state machine.
I would probably still use Serial Port Init with termination character 0x03.
When the VISA Read returns, check for escaped bytes and correct them, then check you got a full message and if not, repeat the rest until you have an unescaped 0x03 at the end. This is your data frame. Is it not long enough? You got a partial or corrupted frame!
11-14-2023 05:06 AM - edited 11-14-2023 05:07 AM
Are the 20 bytes ASCII data or binary? It is not normal to see the STX and ETX with an ASCII protocol, so it should be mostly safe to assume binary here.
What I did in this situation is read 1 byte at a time until the STX (0x02) is read. At that point, read 21 bytes (20 data bytes and the ETX). Verify the last byte is 0x03. If it is, pass the data on. If not, throw out what you read. Regardless, go back to reading 1 byte at a time looking for the STX.
11-14-2023 11:19 AM
As you specifically indicate that the message is fixed length, I would just read the fixed length message (with the STX and EXT lengths counted in the total message length). And then decode the message once the entire message is read.
-Dave
11-14-2023 12:04 PM
@dr2 wrote:
As you specifically indicate that the message is fixed length, I would just read the fixed length message (with the STX and EXT lengths counted in the total message length). And then decode the message once the entire message is read.
-Dave
What if the port is initialized when the sender is half way through the message? You will be out of sync of the data frame forever. You need to sync up to the start byte before you can do anything. Even then, you have to verify your frame actually did end correctly. If not, then you have to assume you got out of sync somehow (bytes dropped, cable unplugged, intermittent connection in the hardware, etc.).
11-19-2023 05:42 AM
Hi. Thanks all for the input. I was initially try to do the task using the termination characters. Then perform interpretations on the different ways the data can arrive.
Crossrulz presentation was the approach I went with. This does not use termination characters. It is below and looks similar to the previous presentation.
To note is that I did have issue with the syncing out of phase (with my actual test data that I was using). My solution was to move forward in the arriving data by 1 byte. This is the lower RHS read.
If I expand this with a byte dedicated to parity then we can be absolutely sure of the correctness of the syncing.
Regards JC.....