06-29-2010 04:42 PM
Hi,
I've been trying to to create a VI that can send and receive text data to a device via RS-232. I started out with the Basic Serial Read and Write.vi that comes as an example with LabView and that works. However, all it does is wait for specified amount of time before trying to read incoming data. I thought to make that example more robust by polling the serial port for incoming characters. I did some Googling and I came upon this page:
http://digital.ni.com/public.nsf/allkb/179E61F87CF8B340862571EA0037CFA3
Preventing LabVIEW from Continuously Polling the Serial Port.
When I modified the Basic ... VI to include this however, I get all manner of errors. Sometimes, I don't get any response at all. Sometimes, I get response data from a previous request (but not all of it), and sometimes the device I'm communicating with tells me it's seeing a framing error. I don't think I've ever actually received a correct response. Can someone tell me if I've implemented this incorrectly? Does the page above ONLY apply to PXI controllers or can I use this for RS-232 communication? Do I need to be waiting on a different event?
Thanks,
Jason Mazzotta
06-29-2010 05:18 PM
I think the CLR function was a bad idea- unless you also RESET your instrument. by blowing away the IO buffer data your chopping off a lot of stuff that may be happening- kind of like crashing into a tree- and leaving the device in a unknown state.
06-29-2010 05:27 PM
Post your vi instead of a screen shot. I can't see a thing because the picture is too small and grainy.
Also, with a vi, we can see all of the other cases not shown.
06-29-2010 05:55 PM
Here is a sample vi that writes to the serial port and then waits for a minimum amount of bytes to be present at the serial port before it reads. The wait loop will abort if there is an error or if the timeout value is reached so that you don't get stuck in an infinite loop in case there is no response or in case of an error.
06-30-2010 10:12 AM
06-30-2010 10:17 AM
If I don't flush the I/O buffers each time I read and write to the serial port, how do I make sure there's nothing in those buffers the next time I try to read and write to the serial port?
06-30-2010 11:08 AM
tbob. Thanks again. That does seem to work. I'm going to see if I can modify it to read out the characters individually. I'd still be interested to find out why those events don't seem to behave they're advertised (or maybe I'm just misinterpreting the advertisement).
06-30-2010 11:16 AM
@jazzdman wrote:
If I don't flush the I/O buffers each time I read and write to the serial port, how do I make sure there's nothing in those buffers the next time I try to read and write to the serial port?
Generally, use some form of flow control. a temination character is one common approach so that you read until a specific character is seen (0x0A, linefeed is very common). the RS-232 recommendation (RS stands for Recommended Standard) allows for several handshaking method of flow control as well. Perhaps your equipment uses one of these methods?
06-30-2010 12:05 PM
@jazzdman wrote:
... I'm going to see if I can modify it to read out the characters individually. ....
Why do you want to read the characters individually? Why not just read the whole thing and then if you want to process the characters individually you can use string subset to do this. It is much quicker to read once and get a 100 character string than to read 100 times. Parsing the string is very quick, quicker than a serial read.
07-01-2010 04:04 AM
Seems like your expected string is actually terminated by a linefeed character. VISA supports character terminated reads and even enables that for serial ports by default.
The entire loop in the middle and the "wait for event "can be simply replaced by a single VISA Read when you let VISA use read termination mode. It even uses line feed by default as termination character but you can easily modify that at the VISA Serial Init function.