Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

difference result between exception message and result from VisaStatusCode

Solved!
Go to solution

Hi,

first time poster here. Just started learning the ins and outs of instrument control.

 

I have been using Visual Studio 2010 to create a C# program to configure and use a digital power meter instrument
(via a USB connection), and I have noticed that sending a faulty Query() (on purpose) to an instrument will result in an
exception (as expected).More specifically "Timeout expired before operation completed.  VISA error code -1073807339 (0xBFFF0015), ErrorTimeout"

For communication with the instrument I use the following library: NationalInstruments.VisaNS

 

But when I use VisaStatusCode (LastStatus) in the catch of the exception, to see if it gives the same error code,
it only returns "Success".

The exception error message includes everything that I need, but I was wondering why I am getting a different result.

Is the LastStatus value not supposed to be used for this? Can anyone suggest what is going on here?

0 Kudos
Message 1 of 12
(4,235 Views)

Hello HZW,

 

Can you post an excerpt of your code (especially the part related to the timeout) so that I can take a look at it?

Kind Regards,
Thierry C - CLA, CTA - Senior R&D Engineer (Former Support Engineer) - National Instruments
If someone helped you, let them know. Mark as solved and/or give a kudo. 😉
0 Kudos
Message 2 of 12
(4,228 Views)

Thanks for the reply!

 

It's not the timeout error that troubles me (because I am sending an invalid command), but the difference between the
information in the exception, and the value returned by doing a VisaStatusCode status request - although it is inside the catch...

 

 

private static MessageBasedSession mbSession;

 

// here I go looking for USB devices

string[] resources = ResourceManager.GetLocalManager().FindResources("USB?*");

//... skipping checks for valid string array; line below is wrapped in a try...catch

mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open(resources[0]);

 

// also inside a try..

responseString = mbSession.Query("*IDN?\n"); // a valid call which returns a correct reply from the device

 

// this is the nonsensical command
try{responseString = mbSession.Query("*IBB?\n");}

catch(Exception exp) // <-- gets me an exception string which contains info as described previously

{
// this is the value that I am investigating

VisaStatusCode status = mbSession.LastStatus; // <-- returns 'Success' (?)

}

 

// some cleanup afterwards

0 Kudos
Message 3 of 12
(4,225 Views)

Hello HZW,

 

I'm currently at home so I can't do all the wanted tests.

 

When I'm back at the office I'll try to reproduce this behavior and see why it behaves like this.

 

Kind Regards,
Thierry C - CLA, CTA - Senior R&D Engineer (Former Support Engineer) - National Instruments
If someone helped you, let them know. Mark as solved and/or give a kudo. 😉
0 Kudos
Message 4 of 12
(4,220 Views)

Hello HZW,

 

Can you try the code without the catching of the exception?

Do you then still get LastStatus equal to "Success"?

 

Kind Regards,
Thierry C - CLA, CTA - Senior R&D Engineer (Former Support Engineer) - National Instruments
If someone helped you, let them know. Mark as solved and/or give a kudo. 😉
0 Kudos
Message 5 of 12
(4,209 Views)

I'm not sure I understood you correctly, but if I remove the try...catch, it only makes my application crash.

("Testprogram has encountered a problem and needs to close. .."). Is it not so that when an exception
in a method call is not caught in a try..catch, it just triggers an exception in the next higher level ?

 

But what I hadn't tried was using mbSession.Write() to send the command, and use ReadString() to read the
reply back. If I do that, the exception is thrown during mbSession.ReadString(). And if I do a mbSession.LastStatus

before the ReadString(), it still returns "Success".

(Thank you for taking time to help me understand this betterSmiley Wink)

0 Kudos
Message 6 of 12
(4,204 Views)

Hello HZW,

 

I think I know understand your question clearly and what is troubling you.

 

Basically, if you don't handle a certain exception by using a "try ... catch" and no catch block is found anywhere on the call stack, then it will produce an unhandled exception.

Because this exception occurs while calling your method query() or read string(), it can and most probably is the case that the LastStatus will not update, because the exception occured while calling the function.

 

What I think/suspect is happening is the following:

- You call for example a query() method (1) without an exception occuring and the LastStatus gets updated.

- You're calling the query() (2) or read string() (2) method

- This method throws the exception (while executing it)  and does not update the LastStatus value (because the exception was thrown)

- mbSession.LastStatus shows the LastStatus value that was written to it by query() method (1) and not 2.

 

In my past (before working for NI) I have seen this type of behavior quite often and have seen similar things happen with other libaries also.

 

Because of this I personally don't like to depend on the data that might (or might not) have have been changed when an exception is thrown inside/by a function.

But of course that's only my personal opninion.

 

Unfortunately I currently don't have Visual Studio installed on my local pc, so I cannot test this myself now in the same circumstances.

 

Kind Regards,
Thierry C - CLA, CTA - Senior R&D Engineer (Former Support Engineer) - National Instruments
If someone helped you, let them know. Mark as solved and/or give a kudo. 😉
Message 7 of 12
(4,193 Views)

Thank you for the update.

Your explanation makes sense. The Exception information is very useful (a number a message and the meaning of the number);

I will use that to interpret the result of a failed call.Although I'm left wondering what the use of a status variable is, if it's not updated when

something goes wrong. Or perhaps the status variable is supposed to be used in programming languages that don't have the try..catch feature.

 

Kind regards,

HZW

0 Kudos
Message 8 of 12
(4,191 Views)

Hello HZW,

 

After some further searching and troubleshouting I think I have found something new.

 

I think what you're encountering might be similar to this issue.

Can you try this approach (adapted to your functionality) as a work-around?

Kind Regards,
Thierry C - CLA, CTA - Senior R&D Engineer (Former Support Engineer) - National Instruments
If someone helped you, let them know. Mark as solved and/or give a kudo. 😉
0 Kudos
Message 9 of 12
(4,185 Views)

Hi,

 

I think not all of your message got through. Did you post a link?

I do not see a name or link associated with 'this issue'.

0 Kudos
Message 10 of 12
(4,183 Views)