LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to accurately read from multiple serial ports at the same time.

I am trying to read from three serial ports (one actual serial port, the other two are setup through a USB to Serial converter) simultaneously using LabView and the VISA VI.  I use this VI three times, one for each of the devices.  If I set all of the VISA resource names to the same port (say COM3), then the problem does not appear, it is only when I have two or more different COM ports selected that the problem arises.  So what is the problem...

 

It appears that the information coming from one of the other sensors is getting mixed in with the current data stream.  I think that I am running into some sort of race condition with the way the serial buffer is implemented, but am not sure.  I have inserted the VI that clears the buffer after each message comes in and this helps (the problem does not occur as frequently), but it is not a perfect fix (meaning it still occurs).  Is there something special I need to do to run multiple serial devices at the same time?  Any help is greatly appreciated.

0 Kudos
Message 1 of 10
(6,351 Views)

I have never had problems using different serial ports at the same time.  Each serial port would have its own hardware buffer and also its own software buffer that is part of the serial port driver.  I don't know of any way these datastreams could be getting mixed.  I suspect there is something not quite right with the way you are using this VI.  Is it a subVI part of a larger program?  Are you opening and closing the VISA references on each call of this VI?

 

Please post your code so we can take a look at it.

0 Kudos
Message 2 of 10
(6,348 Views)
I open the Serial ports outside of a loop and then write to the buffer inside a while loop.  To close the serial port, I have a button that ends the while loop and closes each port.  When I run it with the same port selected on each VISA (meaning all three look at COM3), I do not have the problem.  The code I am referencing is attached, with the sub-vi used.  I am fairly new to LabView, so excuse the un-cleaned up design.
Download All
0 Kudos
Message 3 of 10
(6,335 Views)

First, I don't know how you can say there is no problem if all 3 look at COM3.  In that case, you are only looking at a single com port, and the data will be scattered across the 3 instances of that com port being used.

 

In your main VI, you do the configuring of the com port before the loop.  That is good.  I think the problem is in your magnetometer VI where each instance of it is being executed on each loop iteration.  In there you are setting a some VISA properties in a couple of locations.  You also have a couple of flush buffers going on.  I don't think there is ever a good reason to need to do a flush buffer.  I think with the couple of flush buffers, and the setting of the VISA properties on every iteration of the loop, you are losing data.  Move the VISA property nodes to the main VI prior to the start of the loop.  Eliminate the flush buffers.  Just read in the loop.

 

Since the VI is non-reentrant, only one instance of it can run at a time.  So instance 1 runs, when it finishes, then instance 2,  when it finishes, then instance 3.  Not necessarily in this order, and the order could change from loop iteration to loop iteration.  Since data could be coming into the Com port 1 while the instance of the subVI for com port 2 or 3 is running,  very likely if the data is coming in at the same rate on all 3 com ports, you are losing data coming in on a port while the other 2 are running because it gets flushed before it is read.

 

Get rid of the flush buffers, move the property nodes outside the subVI and before the loop in the main VI, and also make the subVI reentrant, I think your problem will go away.

0 Kudos
Message 4 of 10
(6,332 Views)
Thanks, I will move the subvi out and get rid of the buffer flushes (I added those as an attempt to fix the problem anyways).  I cannot test it until tomorrow when I get back in the lab, hopefully this clears it up.
0 Kudos
Message 5 of 10
(6,328 Views)

Ravens Fan,

 

That worked perfectly.  Thank you for the help.

0 Kudos
Message 6 of 10
(6,287 Views)

Ravens Fan,

 

I am trying to run 5 ports at the same time. It is kind the same concept as this Magnetometer example. But in my application ports can be reset, or I can start running one port and keep open the others as I need them. I have an Apply button that would add more ports, and the reset button that would clear all the ports.

 

Does the ports need to be closed everythime I reset them?

 

Thank you,

Cosmica 

0 Kudos
Message 7 of 10
(6,088 Views)

I am not quite clear on what you mean by "resetting" the ports.

 

Each VISA port is independent of the others.  So you can open or close one while leaving the others open.

 

It's a good idea to close ports if you are done using them, but you shouldn't need to close them if you plan on continuing to use them.

0 Kudos
Message 8 of 10
(6,082 Views)

Reset: 

I have an array that holds the ports that are being used, and it disables them so they cannot be selected twice. Now, If I want to use a specific port and this is being used, then I reset the ports so It can become free to be selected.

 

I am trying to run 5 ports simultaneoulsy, but they seem to only work sometimes. Do all the ports need to be in the same loop and opened outside of it?

 

Thank you,

Cosmica

0 Kudos
Message 9 of 10
(6,078 Views)
No. They shouldn't all have to be in the same loop. I'd recommend starting a new thread and attach your VI to it.  State what is going wrong.  This should cause your message to get more attention compared to putting it in this older message thread.
Message 10 of 10
(6,065 Views)