Search code examples
pythonvisa

How to return a wrong message if there is no device connected when I use VISA?


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?


Solution

  • 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)')