LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabView Read binary fixed length serial data with VISA

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.......

0 Kudos
Message 1 of 6
(930 Views)

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!

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 6
(900 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 6
(870 Views)

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

0 Kudos
Message 4 of 6
(822 Views)

@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.).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 6
(808 Views)

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.

 

pushkin_1-1700397198589.png

 

 

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.....

0 Kudos
Message 6 of 6
(755 Views)