3

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

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

But what if the device is not connected to the computer? What I want to do is that before I initialize the device, firstly I want to check whether the device is connected properly? How to achieve that?

animuson
  • 53,861
  • 28
  • 137
  • 147

2 Answers2

4

You could do it two ways:

1) Check if it is in the 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 to occur

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)')
mikeb
  • 66
  • 3
1

Use get_instruments_list to make sure that the instrument you want to connect to is available.

michel-slm
  • 9,438
  • 3
  • 32
  • 31