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: 

VISA open, close and connection error handling

Hi,

 

     I am using a VISA object to connect to a Keysight N5225A PNA. The VISA connection is able to connect, setup, trigger and retrieve measurement data. However, after repeating [trigger+data send] for several times (or hours), an error will pop up and my VISA wont work anymore. I've tried to close the connection and open it again, but this wont solve it. 

 

Some of the errors I might see are 

−1073807346

The given session or object reference is invalid.

−1073807194

The connection for the given session has been lost.

 

Since there are many probable causes, I would like to ask for an example on how to properly make several repetitive measurements using VISA objects, and how to programatically handle connection errors that may occur (e.g. TCP connection is down, then wait until its back, or how to reset the errors listed above)

 

Thanks,

Rodrigo

0 Kudos
Message 1 of 9
(4,056 Views)

Please post your code, judging by the errors you are receiving your VISA session is becoming invalid.

 

 But in general you do this:

  1. Open a VISA session at the beginning of your program
  2. Communicate with instrument reusing the same VISA sesion via shift registers in your main loop
  3. Close the VISA session at the end of your program

 

DO NOT:

  • Open a VISA Session, communicate with the instrument, and close the VISA session every single time you communicate with the instrument 

 

========================
=== Engineer Ambiguously ===
========================
Message 2 of 9
(4,036 Views)

RTSLVU,

 

 

    Thanks for your quick answer, It was useful.

 

    I was doing it wrong, opening and closing the VISA connection every time. Now, I have removed the open and close, and just pass the connection using shift register as you suggested, I believe the program behaves better than before. However, I am still interested in knowing how to handle timeout errors, or broken connections, in that way I'll be able to recover from unwanted communications temporal disconnections.

 

   The code is part of a labview class that has many dependent vi's, I've attached the files. The vi of interest is the one that triggers the sample and gets the data from the PNA is called "PNASweepAndLog.vi". If the file is badly exported, can you please instruct me on how to do it better. 

 

 

0 Kudos
Message 3 of 9
(4,015 Views)

It's hard to say exactly how to handle errors like this, as each case will be different. Ask yourself, for your application, what  would you LIKE it to do? In many time-critical applications, losing the connection should trigger an Abort condition and alert someone that the system has gone down. In other, less critical applications, you can just capture the error- when you see an error that the connection has gone down, try to reinitialize it once every minute (or second or whatever makes sense).

 

Unfortunately I don't know of a way to get a list of ALL potential "lost my VISA reference" errors, other than to manually trigger each one and record them. You could try yanking the cable, powering off the device, allowing Windows to go to sleep, turning off your router (if it's a TCP connection), etc. Your program can then say "OK, if it's one of [these] errors, just try to reconnect, no need to worry" and can remake the connection after some delay.

 

Gmail actually has a pretty solid way of doing this, since email isn't exactly time critical. If you load up Gmail on your computer and pull the network, it'll say "Lost connection, trying again in 10, 9, 8..." and will try again. If that fails, it waits 30 seconds. If that fails, it waits 60 seconds, then 5 minutes, and so on (numbers are made up for example purposes).

 

You could have some sort of popup say "Hey, lost connection- trying again in x minutes, or click here to manually reconnect". If the program can reestablish connection on its own, then it can close the popup. (Note: the popup will need to be non-blocking for this to work, so you'll need to maintain a communication reference to it)

0 Kudos
Message 4 of 9
(4,012 Views)

 BertMcMahan,

 

      Yes, I agree, a list of ALL potential "lost my VISA reference" errors is not feasible. However, I did mentioned two common errors that I often see. My question is how do I tell labview, "hey, it's ok, probably is a loose cable, just wait until it works fine again".

 

How do i, programmatically, catch the error and attempt to reinitialize the connection until it is back?. I tried to erase the error, then close the vi and open it again, but it didn't work, even if the connection has returned the VISA connection cannot be reestablished using that procedure. Whenever I have a VISA connection error, I have to close labview and open it again.

0 Kudos
Message 5 of 9
(4,004 Views)

Could the source of your issue be that you are dealing with two copies of your resource name and that you may not be handling them properly throughout the code? I am not sure why you have two copies of your resource name. You have VISA resource name and PNA.VISA resource name in your class private data for your N5225A class. Whenever I see two copies of the same data I get suspicious about not using them correctly and making sure they stay in sync with each other. I would recommend only using a single copy of the resource name.

 

Also, when you detect an error in with your communications you need to feed that back to your state machine for handling.

 

You are also passing your class by value so when you close it in one spot, you may not be reflecting that back on other branches of the class wire. If you notice the notes say that the Hardware Configuration cluster should contain references to your hardware. You have include the actual classes in that cluster. You are branching that cluster all throughout your code you you are not always operating on the same data. By value is good and works, but you cannot keep state information in classes that you branch/queue the class wire. This effectively makes a copy of the class. An update on one branch will not be reflected in a different branch of that same class wire. I think your problems are that you are trying to maintain state information in your class but you pass that around like a hot potato. You send copies of it via queues and you branch the wire. Either you need to be very consistent and use a single wire for your class or you need to store the state information in DVRs so that you pass a reference to the state and not pass it by value.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 6 of 9
(3,983 Views)

Well I can not decipher your code, I have studied that QMH example program before an couldn't make heads nor tails of how it works.

 

Your VISA session is getting lost in that shuffle someplace... good luck

 

One thing you can do once you figure out what is causing your errors is when an error occurs check the error code and if it matches an error you know how to handle. You can clear the error from the cluster and then handle the error, reopen VISA , or whatever. 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 7 of 9
(3,946 Views)

In my experience, there are some VISA errors that are unrecoverable.

I assume that you have checked if there is an updated VISA driver.

 

---------------------------------------------
Certified LabVIEW Developer (CLD)
There are two ways to tell somebody thanks: Kudos and Marked Solutions
Message 8 of 9
(3,934 Views)

Mark_Yedinak,

 

      I very much appreciate the feedback on my approach for the architecture of the program, you took your time to go through it. 

 

     Yes, I have two controls for the VISA resource, it is because i was looking for procedures to reinitialize the VISA when an error occurred, I understand this is not good programming, it is prone to errors, and I will remove it.  But definitely this double resource issue is not the only cause of errors, because I have them even before. 

 

    "You are also passing your class by value so when you close it in one spot, you may not be reflecting that back on other branches of the class wire." This is a very good observation, I didn't think about it, I will correct it.

 

   I believe I have received valuable insights on how to properly manage VISA resources. However, I am still confused on how to reinitialize a VISA after an error occurred.

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