[Python] Timeout error with pyvisa when using GPIB connection

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently using a DPO 7054 Digital Phospore Oscilloscope to record the voltage of two single-photon detectors. A GPIB connection allows for the transfer of the waveforms towards my PC from which I analyse the data. I use pyvisa to communicate with the oscilloscope.

    My problem relies on the fact that I sometimes get a timeout error such as: "VI_ERROR_TMO (-1073807339): Timeout expired before operation completed". I do not understand why or when it happens, but after this error I can not re-communicate with the oscilloscope unless I turn it off and on again.

    Here is a code I use to start and stop the communication with the resource and check errors :

    import pyvisa
    import time
    def p():
    time.sleep(1)
    return (print("Toc"))

    rm = pyvisa.ResourceManager() #Create Ressource Manager object
    rm.close()
    rm = pyvisa.ResourceManager() #Create Ressource Manager object
    print("\n",rm.list_resources()) #Gives tuple with available resources
    print(rm,": Ressource Manager Object\n") # Check what backend is used and the location of the shared library used, if relevant
    print('\a')
    #%%
    os = rm.open_resource('GPIB0::1::INSTR')
    os.flush(pyvisa.constants.VI_READ_BUF)
    os.clear()
    os.close()
    #%%
    rm = pyvisa.ResourceManager()
    os = rm.open_resource('GPIB0::1::INSTR',
    read_termination = '\n')

    os.write("*IDN?")
    print(os.read_raw())
    #data = os.read_values(1000)
    print(os.query("*IDN?"))

    os.write("ACQuire:STATE STOP")
    os.write("ACQuire:STATE RUN")

    print(("xinc ",os.query("WFMPRE:XINCR?")))
    print("xincr ",float(os.query("WFMPRE:XINCR?")))
    print("xzero ",os.query("WFMPRE:XZERO?"))
    print("pt_off ",os.query("WFMPRE:pT_OFF?"))
    print("horiz pos ",os.query("HORIZONTAL:pOS?"))

    print("ymult ",os.query('WFMPRE:YMULT?')) # y-axis least count,volts / level
    print("yzero ",os.query('WFMPRE:YZERO?')) # y-axis zero error,reference voltage
    print("yoff ",os.query('WFMPRE:YOFF?')) # y-axis offset,reference position (level)


    if False:


    print(rm.session)
    os.timeout = 1e4 #in ms. For infinite timeout, set None or float('+inf')
    print(os.query('*IDN?'))
    os.write('*IDN?')
    print(os.read_raw(1))

    print(rm.session)

    os.write('*ESR?')
    print(os.read_raw(1))
    a = time.time()
    os.write("ALLEV?")
    b = time.time()
    print(os.read_raw(1))
    c = time.time()

    print("\n",(b-a)*1e3,(c-a)*1e3)
    os.clear()
    os.close()
    rm.close()


    If I have already encountered a TIMEOUT error, then the code will only print:

    ('ASRL 3::INSTR', 'ASRL4::INSTR', 'GPIB0::1::INSTR')
    Resource Manager of Visa Library at C:\WINDOWS\system32\visa32.dll : Ressource Manager Object


    and send the following traceback :

    Traceback (most recent call last):

    File C:\ProgramData\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

    File c:\users\python\close gpib.py:31
    os.write("*IDN?")

    File ~\AppData\Roaming\Python\Python312\site-packages\pyvisa\resources\messagebased.py:196 in write
    count = self.write_raw(message.encode(enco))

    File ~\AppData\Roaming\Python\Python312\site-packages\pyvisa\resources\messagebased.py:156 in write_raw
    return self.visalib.write(self.session, message)[0]

    File ~\AppData\Roaming\Python\Python312\site-packages\pyvisa\ctwrapper\functions.py:2795 in write
    ret = library.viWrite(session, data, len(data), byref(return_count))

    File ~\AppData\Roaming\Python\Python312\site-packages\pyvisa\ctwrapper\highlevel.py:226 in _return_handler
    return self.handle_return_value(session, ret_value) # type: ignore

    File ~\AppData\Roaming\Python\Python312\site-packages\pyvisa\highlevel.py:251 in handle_return_value
    raise errors.VisaIOError(rv)

    VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.


    What am I doing wrong? How can I stop the TIMEOUT error from happening? Or if it happens, how can I re-run my code without having to turn on and off the oscilloscope?

    Thank you for your time.

    Continue reading...

Compartilhe esta Página