LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

serial port read problem

I read the data of PLC register through a serial port. The first running of the reading vi always return the previous operation's result, that means each time I have to run the vi twice to get correct data.
Please help!
Thanks
0 Kudos
Message 1 of 5
(2,739 Views)
It is difficult to offer help without seeing the code.  Please post the code.Smiley Wink
0 Kudos
Message 2 of 5
(2,733 Views)
Here is my code. I do not close the VISA after reading, because I need to do a conituously reading by repeating the readD vi.
Download All
0 Kudos
Message 3 of 5
(2,698 Views)

Try putting a small wait (perhaps 100ms, you may want to experiment) between your VISA write and your read Loop subVI in the main program, or at the beginning of your subVI before you start the read loop.

What I think is happening:

You issue a write command, the data goes out of the serial port.  It takes a small amount of time for the device to get the message and begin to transmit the response.  Before the response begins, your sub VI runs.  Since no data has yet hit the input buffer (bytes at port =0), the subVI ends and you have no data the first time.  Next time you execute, the device has since returned data from the previous execution and put it in the serial port buffer, the subVI runs and collects this data out of the buffer.

Alsom you may want to put a small wait statement inside the loop inside your Read Loop subVI.  Without it, the loop runs as fast as possible and there is a slight chance (which would increase with slower baud rates), that the bytes in buffer could equal zero if it happens to check the buffer before the next byte has arrived.

Message Edited by Ravens Fan on 08-20-2007 12:09 AM

0 Kudos
Message 4 of 5
(2,690 Views)

Thanks for your suggestions. It seems work.

 

0 Kudos
Message 5 of 5
(2,677 Views)