LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

serial port

In the attached program, I write '1' to the port to get back the info I read from the serial port. But , it seems that the stuff I am reading at the indicator of the serial read is not the one I requested as soon as I write '1'; its the previous info present at the serial port. So, in short, everytime I read the past information, not the current one which I requested.

 

Any help is appreciated. Thank you for your time.

 

Regards,

Mozill

0 Kudos
Message 1 of 6
(3,336 Views)
I had what I think may be a similar problem, but using the VISA commands--

Have you tried toying with introducing a delay between the write and read, and/or flushing the buffer?

here was my (short) thread:
http://forums.ni.com/ni/board/message?board.id=170&message.id=239258


Message Edited by JJoe on 04-03-2007 05:38 PM

0 Kudos
Message 2 of 6
(3,331 Views)
Hi JJoe,

I read your thread and according to Dennis, a read followed by write wont create any problems. I will try putting a small delay between the two. I am not sure of how you flush a buffer. Does flushing means reading from a port?


Thanks for your time.

Regards,
Mozill
0 Kudos
Message 3 of 6
(3,312 Views)
There is a Flush function in the VISA palette, but that won't do you any good. Your problem is a simple timing problem. You're assuming the instrument you're talking to provides a response far more quickly than it probably does. Remember that the computer is going to be much faster than the serial port. Here's what's probably happening:

1. You write a "1".
2. You check the number of bytes at the serial port. There's nothing there, so you read nothing.
3. You write a "1" again, since you're in a loop.
Meanwhile, the instrument has received the first "1", processed the command, and sent the response, but you missed it because you tried reading too quickly.

This has nothing to do with needing to flush the serial port. You need to slow down your program. A delay after the write will do it, but a better solution is to keep reading until you see some text to indicate the end of a response from the instrument.

Also, you really should be using the VISA functions directly. The functions you're using are actually just wrappers around the VISA functions - just open them up and you'll see. NI provided them for backward compatibility to existing programs.
Message 4 of 6
(3,300 Views)
I'd like to clarify a little the statement about a read followed by a write won't be a problem. This is true only when you have a well behaved serial instrument and you are using the termination character. For example, if the instrument appends a CR or LF at the end of the data it transmits, then you just have to specify the number of bytes to read. The VISA Read will wait (for the timeout value) until that number of bytes is read or the termination character is detected. When the instrument does not send a termination character and you are forced to use VISA Bytes at Serial Port, you should not assume that the first reading is correct as smercurio mentions. If you want to do it without fixed wait statements, put the VISA Bytes at Serial Port in a loop and check until the number of bytes isgreater than 0. Then do a read and keep checking the bytes available until it is 0. Then you know that you've got all of the data.
Message 5 of 6
(3,287 Views)
Hi M

if you check out the examples in Labview
then
you will be able to use the :
Advanced Serial write and Read .

Note that the timing is set at 5ms.

xseadog

Message 6 of 6
(3,285 Views)