Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading and writing simultaneously on a serial port

Is it possible to have a serial port read and write running at the same time without them deadlocking?

The structure I want is two while loops running in parallel. The first one waits for return to be pressed in an input field, writes the string to the serial port, and goes round again. The second one blocks on a serial port read until some data arrives, displays it, and goes back to waiting. Unfortunately the write in the first loop blocks if the second loop is waiting for data to read.

Some of the commands sent to the attached box will produce no output, some a known amount of output, and some will make it produce output until another command stops it. The program doesn't need to maintain any state about
the box, just read and display any data it sends, so this separation of read and write seems the cleanest way to handle things. Any better ideas would be welcome though.

I'm using the serial port compatability VIs because the attached box is a homemade device that sends binary data rather than ASCII formatted into lines.


Michael
0 Kudos
Message 1 of 2
(3,191 Views)
Michael,
I've used serial pseudo simultanious R/W before with LabVIEW.
Indeed, the serial read may be a problem since it waits on the bytes to arrive from the external device.
If you know how many bytes you will receive, just call the number of bytes at port vi often.
If this number is equal of exceeds the byte-length, then you are certain the bytes have arrived and can be copied from the serial input port (read port).

Another method I use is defining a serial buffer (in a vi). I regularly look for number of bytes at serial port. If > 0 add these bytes to the buffer.
You can do the decoding on the buffer, which stops your deadlock.

Good luck, Patrick de Boevere
0 Kudos
Message 2 of 2
(3,191 Views)