Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Strange RS232 question

Hi,

I am supposed to read some data from the RS232 port and plot it on the waveform. The data looks like this: (ASCII Strings seperated by return)
134
343
12F
12
...


To test, I wrote a Labview program to send out this kind of data to the RS232 port. It is a for loop and for each loop step, it will translate the data into an ASCII string and send it to the RS232 port. For example, number 48 will be translated to "48".

I made a loopback on the RS232 port, and wrote another Labview program to read the RS232 data in, translate into numerical and plot on waveform. It is a while loop, for each loop step, it will check the bytes on the serial port, if it is bigger than 0, it will read the bytes and translate into number and plot it.
This works fine in loopback mode, it can capture every line of string and plot it perfectly.

But then I switched to the real environment, the data is sent by others, I checked the data by Hyperterminal and it is in accordance with the above format, but the strange thing is that now my receiving program got the data not line by line, instead, it got the data several lines per read. How can this be? And what can I do to make it read line by line while not losing other data? Not every sample data is of the same length.
0 Kudos
Message 1 of 4
(3,620 Views)
Is is possible that multiple lines of data are being received between each read by your program? VISA will buffer the data, and when you issue the read you'll get all of the bytes in the buffer. I can how the behavior would change between your test app and the real instrument since the test app is running on the same system as the receiver app and thus may be "in sync" where as the instrument is truely async to your program.
0 Kudos
Message 2 of 4
(3,620 Views)
Well, I am not using VISA...... What I used is "Serial Port Init", "Bytes at Serial Port", "Serial Port Read"....

What do you mean by "sync"? If I test with loopback and with two programs running on the same machine, I can let the sending program running all the time, while the receiving program start at anytime.

Another question is how this serial port works in Labview on Windows? Is the buffer a FIFO like thing? Do I need to clear the buffer after reading? Or when should I clear the buffer?

Maybe VISA will do better than the basic serial port APIs, because it can designate terminator, thus I can make sure that it only read one line if the terminator is set to be "\n".
0 Kudos
Message 3 of 4
(3,620 Views)
By 'sync' I mean that the test app writes a few bytes, the receiver app reads a few bytes, etc. Since the two applications are running on the same machine they may be in sync timing-wise so that the reciever program happens to work as expected.

The serial port contains multiple buffers. For non-VISA it is a HW buffer and driver buffer. VISA may add an additional buffer.

When the computer receives a byte at a serial port, the serial driver grabs the byte from the hardware buffer and stores it in a software buffer. When you call Bytes at Serial Port, you're asking how many bytes are in the software buffer. Then you perform a read and get N bytes from the software buffer.

What I would do is make a routine that just reads from
the serial port and puts the resulting data in a buffer you define in LabVIEW (perhaps a queue of some sort?). Then your main program parses the buffer and processes the data accordingly.
0 Kudos
Message 4 of 4
(3,620 Views)