Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing serial stream

I have a device that streams serial data continuously. It has no hardware or software flow control, and it does not respond to any commands. Within the stream are fixed width ASCII tokens, and the whole thing ends with a "CRLF" combo and then repeats. The stream is 121 characters long, and it occurs at 64Hz on a 115200 baud port.

I set up a serial VISA input using the Instrument I/O Assistant under Labview 7.1. The issue I have is that when I run the "Read and Parse," I get a totally random chunk of the stream. There isn't any alignment that I can find anywhere. I've messed with all sorts of things but can't figure out how to get the stream aligned so that I am always parsing correctly. Any ideas?

When I open the stream in Hyperterminal, it looks just fine since it clears the screen every CR/LF. When I execute a "read" in the "basic I/O" part of the VISA Session, the data appears to be correctly aligned about 90% of the time.
0 Kudos
Message 1 of 2
(3,347 Views)
Ian,

When you lost synchronisation (at startup or when any received character was lost or damaged), there's no change to determine, what character might be the next. So you need a mechanism to find complete messages. Either search for the token or start a new message with the character following CRLF (btw, this is 0x0d0a).
So what I would do is something like this pseudocode:
Open serial port
Clear a string shift register SR
While not finished do
wait some time
N=Bytes at serial port
Read N bytes
append them to whatever is in SR
delete all characters in SR before the first set of CRLF
find the position of CRLF in SR starting at pos 2
If found, remove the substring from pos 0 to foundposition-2
process the removed substring
put the remainder back into SR
WhileEnd
Close serial port

This way you'll have some data in SR that always start with CRLF. You remove and process complete messages, but those do start now with CRLF instaed of ending with. But that's not a problem.
If processing the data is complicated or time consuming, it is better to separate data reading and processing. Put the processing into a parallel independent while loop. Stop it with a local variable of the stop for the first while loop. Use a named queue to transfer those messages from the reader to the processor. Don't forget a wait in both loops.
Greetings from Germany!
--
Uwe
0 Kudos
Message 2 of 2
(3,341 Views)