How to return an incorrect message if there is no connected device when I use VISA?

For exmaple, as follows, I can simply initialize my device using the following code if my device is connected correctly.

from visa import *
my_instrument = instrument("GPIB::14")

But what if the device is not connected to the computer? I want to do this before initializing the device, firstly, I want to check if the device is connected correctly? How to achieve this?

+3
source share
2 answers

You can do this in two ways:

1) Check if it is in get_instruments_list ()

from visa import *
my_instrument_name = "GPIB::14"
if my_instrument_name in visa.get_instruments_list():
    print('Instrument exists connecting to it')
    my_instrument = instrument(my_instrument_name)
else:
    print('Instrument not found, not connecting')

2) Try to connect and catch the exception, you will need to wait for the timeout

from visa import *
my_instrument_name = "GPIB::14"
try:
    my_instrument = instrument(my_instrument_name)
    print('Instrument connected')
except(visa.VisaIOError):
    print('Instrument not connected (timeout error)')
+4
source

Use get_instruments_listto make sure that the tool you want to connect to is available.

+1
source

All Articles