From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Locking access to serial ports

I have two instruments on serial ports, and two independant VIs to control them. One VI may be running, or both, at any one time. The VIs may be running under LabView, or executables.
I would like some simple and elegant way of ensuring that when I run the second VI, it cannot access whichever port is being used by the first VI. I have experimented with using the VISA Lock Async.vi, which should do exactly that. However, it seems to be possible to have two VIs lock access to the same port, without generating an error. I can only assume that the "lock" is for the application (LabView), not the VIs.
I have looked at the synchronisation functions for a solution, but I can't see any reasonable way of using them. Semaphores are ok if you want to control access to a single port, but not the undefined list of ports that may be available on any given machine.
I could use a global array variable containing a list of ports in use. Not very elegant though...
 
 
0 Kudos
Message 1 of 9
(3,321 Views)
If I remember, one way of preserving port state is to always start by trying to open a VISA session using the VISA open.vi, with duplicate session set to true and access mode set to 1 (lock). If another vi is already using the port, an error is returned.
Chilly Charly    (aka CC)

         E-List Master - Kudos glutton - Press the yellow button on the left...
        
0 Kudos
Message 2 of 9
(3,302 Views)
Since every serial port has differing Serial Resource names (ASRL4::INSTR or ASRL1::INSTR, etc) just be sure you pass the correct resource name to your subvi's. You can pass it as a global, part of a cluster, whatever you want. Just be sure the right serial resource name is passed and I can't see how there would be a problem.
0 Kudos
Message 3 of 9
(3,302 Views)
ViLabWorks,
The question was about vis or aplications trying to use the same serial port...
Chilly Charly    (aka CC)

         E-List Master - Kudos glutton - Press the yellow button on the left...
        
Message 4 of 9
(3,292 Views)
It isn't exactly clear to me why this is about one port. How much does one serial port cost versus programming time? Or even if it is about 4 serial ports the same applies. For me there is no question, doing something perhaps difficult versus spending next to nothing to solve the problem.
0 Kudos
Message 5 of 9
(3,291 Views)
Thanks, guys, but I don't think you have understood my problem. Read the first sentence - 2 completely independant VIs, and 2 or more ports...
 
I have written a program today using the globals option, which works very well. There should be a better way though.
 
Chris
 
PS. Forgot to mention - this is in Labview 6.1 in Windows.
 
0 Kudos
Message 6 of 9
(3,270 Views)


CDancer a écrit:
Thanks, guys, but I don't think you have understood my problem. Read the first sentence - 2 completely independant VIs, and 2 or more ports...
I have written a program today using the globals option, which works very well. There should be a better way though.
Chris





ViLabWorks a écrit:
It isn't exactly clear to me why this is about one port. How much does one serial port cost versus programming time? Or even if it is about 4 serial ports the same applies. For me there is no question, doing something perhaps difficult versus spending next to nothing to solve the problem.





Well...
Sometimes you need to access the same equipement from different places in your application, or from different applications...
Usually I simply set/read a global "busy" boolean.
One problem I would like to adress is the automatic detection of specific serial hardware. How is it possible to find the port on which some equipment has been connected ? Of course, a solution would be to try to connect to this equipment by sending a command and waiting for the proper answer. However, before doing that, you need to detect the available ports (VISA Find Ressource), then scan each port after setting the proper baud rate, byte length, parity, etc..., then close the port to make it available to another application. Unfortunately, within LabVIEW, this procedure may interfere with other vis already connected to other serial equipments, with impredictable results.
Apart the solution proposed in my first post, I don't know a way of deciding if a port is already in use. May be I missed something ?
Chilly Charly    (aka CC)

         E-List Master - Kudos glutton - Press the yellow button on the left...
        
0 Kudos
Message 7 of 9
(3,262 Views)

Chris, I really don't see why you would have a problem, most of the devices I know while surely give you an error if two seperate programs tried to access the same device while one session is open on the other program. So by default you don't need to do anything. Perhaps looking for errors..... Not only will you have device errors if you tried with most serial devices, but visa and windows will bark at you as well in most cases I suspect. I just don't see what you are fishing for really but I don't know your serial device. Also if extra ports help make the programming easy then most neighborhood PC shops can get you an extra port, and IT depts may have PC's lying around that you can get a port from too. Serial ports at computer shows go for nothing. I don't know your port situation, or real programming situation, but I don't see why simple regular careful visa programming in your LV 6.1 won't do the job.

Best of luck with it.

0 Kudos
Message 8 of 9
(3,256 Views)

That was my first thought, it should generate an error if two VIs try to use the same port, but as I said, "it seems to be possible to have two VIs lock access to the same port, without generating an error".

Lack of serial ports is not the issue.

My solution is to have two global string arrays, one called Ports and one called Owners. When a VI takes control of a port, it adds the port name to Ports and adds its own name to Owners. Every VI checks these globals to make sure a port is free before trying to access it. The VIs can also release the port by deleting its name and port name from the arrays. I wrote one sub-vi to perform the functions "Check port is free", "Claim port if free" and "Release port if I own it".

It works very well. I just have to remember not to have two copies of the global variables VI   🙂

Chris

 

0 Kudos
Message 9 of 9
(3,237 Views)