LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect when a VISA serial port has been closed

I am in the middle of programming for a couple of instruments that will use the VISA serial communication VIs for communication with LabVIEW and have recently discovered during the course of debugging the following:

1) I can initialized the serial port (baud rate & so forth) and read & write to the instrument without problem.

2) I can manually use (run them stand-alone) the VI's that I have put together for reading & writing without problem.

3) I can cause the VISA comm-port serial resource to be closed (initially this action was inadvertent; now I just want to figure out how to detect it).

4) I then find that, with the port closed, I can run the WRITE VI without error (I think, under the circumstances, it should generate an error).

5) I also find that, with the port closed, when I try to run the READ VI I get a timeout error. (I wish the error were more along the lines "Hey dummy, your port is closed!" than a timeout error).

6) a VISA "INSTR" (read) property node connected to the VISA resource doesn't report any errors when the resource has been closed and I have not discovered any properties which indicate a difference between an open port resource and a close port resource.

So far the only indication I have found that I might be dealing with a closed VISA serial port is that a READ operation will timeout but that error is not exclusive to a closed port.

Does anyone know of another indicator?
Message 1 of 5
(3,110 Views)
A couple of versions ago, VISA was changed so that if you did a VISA operation without first doing an Open, a session was automatically opened so I believe you are seeing expected behavior. It is still recomended to explicitly do an Open and Close. I have confirmed this new behavior with a VISA Write but I assume it is the same with a VISA Read. Could it be that the only reason your VISA Read times out is because there is no data to be read?
Message 2 of 5
(3,095 Views)
Thanks Dennis,
I checked and you are correct. If I make sure that there is indeed something to read at the port then the read does work without error and without having to re-open the port if it has been closed.
Message 3 of 5
(3,073 Views)
I will also mention that while I totally agree with Dennis on explicitly opening and closing VISA sessions there is also an option under the Tools, Options ..., Miscellaneous, to "Automatically close Visa sessions". Not sure that would be a good idea, it sort of encourages some sloppy practices that might spill over to other areas of code but for what its worth, there it is. So you don't have to open to write, or close when your done. Don't know how it tells your done or keeps track but ...


P.M.
Putnam
Certified LabVIEW Developer

Senior Test Engineer North Shore Technology, Inc.
Currently using LV 2012-LabVIEW 2018, RT8.5


LabVIEW Champion



Message 4 of 5
(3,066 Views)


@LV_Pro wrote:
I will also mention that while I totally agree with Dennis on explicitly opening and closing VISA sessions there is also an option under the Tools, Options ..., Miscellaneous, to "Automatically close Visa sessions". Not sure that would be a good idea, it sort of encourages some sloppy practices that might spill over to other areas of code but for what its worth, there it is. So you don't have to open to write, or close when your done. Don't know how it tells your done or keeps track but ...


P.M.




This option means that LabVIEW closes any VISA refnums as soon as the top level VI of the hierarchy which did open the port (either explicit through a VISA Open or implicit by passing a valid VISA resource identifier to a VISA VI) goes idle. It does indeed promote sloppy coding practice.

Rolf Kalbermatter
Rolf Kalbermatter
My Blog
Message 5 of 5
(3,055 Views)