LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

VISA Serial Read Timing

Hi all,

 

I'm running into a bit of trouble with a program I'm developing. Background: this program will be used to control an automotive dynamometer. It uses RS-232 communication. It will write commands and read the responses if necessary.

 

This problem is this: I have two while loops running in parallel, one holding an event structure which handles button pushes (write variables, read variables). The other is always monitoring the dyno status as long as it's connected. With the monitor loop writing a status request and reading back the answer every 500 ms there is occasionally a situation where LabVIEW will write a variable or read request while the other loop is trying to read. I thought that LabVIEW would sort out the timing, but it doesn't appear to be the case. I'll attach a transmission snippet to show what I mean. 

 

Written data (COM3)

b4 11 44 05
Read data (COM3)
02 31 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 17 31 34
Written data (COM3)
b4 11 45 32 30 32 05
Read data (COM3)
24

 

The first write is a status request and the first read is the answer. The problem is that the second write happens before the termination character (24) is received. This causes the read request to see a termination and nothing else. It's an intermittent problem and only occurs one in a while, when the timing of the two loops syncs up. 

 

It's an intermittent problem, but 'd like to fix it if it won't be a ton of work. Before starting this project I spent hours researching the appropriate architecture to do what I need, but came up thoroughly confused. At a certain point I had to get on with it and make it work somehow, even if I went about it in a less than ideal way. That said, this is the most complex VI I've written to date so please excuse me if I missed some obvious solution.

 

Thanks!

0 Kudos
Message 1 of 5
(3,201 Views)

Dyno.png

0 Kudos
Message 2 of 5
(3,197 Views)

The secret seems to be not to have two loops trying to access the same resource at once.  Channel all read write commands to that device through a single loop.  Send commands to it through a queue.  Have it do the write and read of the response and send that response back to the loop that requested it.

0 Kudos
Message 3 of 5
(3,176 Views)

While I completely agree with RavensFan, there is another option with less impact on your code: use a synchronization object, for example one sempahore with size 1 (this is the default size). This object acts as a mutex assuring that only one loop at a time can access the serial port. To do so, you need to Acquire the semaphore (with a sensible timeout) before sending any command and Release it after the answer was received.

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
0 Kudos
Message 4 of 5
(3,115 Views)

nevermind I was not following your program very well

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 5 of 5
(3,107 Views)