LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing and reading form a serial port at the same instant

Hello,

 

I am trying to read temperature measurement from a serial device. The way I can do this is to write "t1" to the device and listen to its response. The data manual for the device says when I input "t1" + "Enter", I would get the instantaneous response of "+21.75" and a "*" to conform that a response is sent. The problem I is I am only able to read the "*" and not the "+21.75". I suspect that the device is sending the response before the code is getting a chance to read from the device.

 

I tried the following case structures:

1. "write to serial device>figure out bytes at port>read from the device

2. "write + read in parallel"

3. "write + read in parallel" with a constant for bytes at port "20", in this instance, the connection times out

4. the difference in time elapsed is of the order "0.0002" sec so I added a delay in between write and read

 

The baud rate for the communication is 9600.

 

I would really appreciate any help.

 

Thanks,

Rahul

 

Download All
0 Kudos
Message 1 of 6
(4,873 Views)

First problem is don't use Bytes at Port.  It is the wrong thing to use 95% of the time.  In all likelihood, only 1 byte as arrived when you check it.  Since it looks like your device uses termination characters, just read a large number of bytes.  More than you expect to get in a message.

 

Second, you say write and read at the same instant.  But in reality nothing is instantaneous.  The device is not going to respond with a message until it has received the command you have written.  So there is no point having the write and read in parallel.  Use the error wire and the VISA resource wire to serialize those two things so the Write occurs before the Read.  Write now you have a race conditioning between the two, and adding a time delay ahead of the bytes at port is a pure guess on your part as to how long it takes for the message to come back and exactly what line of code wins the race between the write and the check port/read.

 

You really can't enter a time delay of 0.0002 seconds into that express VI.  Underneath it all, it multiplies by 1000 and enters that many milliseconds as an integer into the Wait function.  Your finest resolution is 1 millisecond.  LabVIEW won't let you wait 0.2 milliseconds.

0 Kudos
Message 2 of 6
(4,868 Views)

RavenFan, thanks for your reply. I tried to use a constant number of bytes to read and serialized write and read before. But like I stated in my problem, the program is missing the initial few bytes. Yes, the output from the serial device sends out termination characters. When I write "T1 E " where "E" is the termination character, the output I receive is "* E " as opposed to "+21.75 E * E". I do not receive any errors.

 

I compare the time elapsed in the write and read sequences to try and time my read right after my write. I did not know that LabVIEW wont let me wait 0.2ms. 

0 Kudos
Message 3 of 6
(4,799 Views)

Have you serialized the Write and the Read?

 

Once you fix that and the timing, there is no reason in the LabVIEW code for it to miss the early bytes.  I would suspect that something is wrong with the device you are talking to.

0 Kudos
Message 4 of 6
(4,795 Views)

I don't think this will solve your issue, but if you did need to wait 0.2 ms:

 

Time less than ms_BD.png

steve

----------------------------------------------------------------------------------------------------------------
Founding (and only) member of AUITA - the Anti UI Thread Association.
----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 5 of 6
(4,787 Views)

Have you solved the issue? 

 

I was thinking of a version of parallel loops with FIFO data structures to make sure that the data flow works properly. (Something like producer-consumer)

0 Kudos
Message 6 of 6
(4,734 Views)