pyvisa.errors.VisaIOError:VI_ERROR_TMO(1073807339):操作完成前超时

2024-04-27 04:28:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python3进行一些自动化工作,以控制Keysight仪器,例如UXA(频谱分析仪)和开关控制器(Keysight 11713C)

我的代码可以运行一段时间,但我经常在几分钟后看到visa超时错误。e、 g.我捕获100个不同频率的频谱,其中一些开关活动在一个环路中,它可能运行20-50个环路,并开始显示visa超时错误

一些文章说,我需要关闭资源管理器以使运行更加顺畅,因此我添加了一些额外的代码,以便在资源管理器检测到visa超时错误时关闭资源管理器,但当超时发生时,此“关闭资源管理器”活动也会失败

下面是我的代码简化版的一个示例:

rm = visa.ResourceManager();

sa_inst = rm.open_resource('TCPIP0::10.10.10.1::hislip0::INSTR',write_termination= '\n', read_termination='\n')

sa = SA(sa_inst)

switch_controller_inst = rm.open_resource('TCPIP0::10.10.10.5::INSTR',write_termination= '\n', read_termination='\n')

Switch_Control = SwitchControl(switch_controller_inst)

counter = 100 

if counter >0:
   try:
        Switch_Control.TurnOnSwitch(switch_controller_inst, switch_H1_H2)
        sa_inst.write('CALC:MARK:MAX;X?;Y?') 
        counter = counter - 1    
   except visa.VisaIOError as e:
        rm.close()
        time.sleep(2)
        rm = visa.ResourceManager();

        sa_inst = >rm.open_resource('TCPIP0::10.10.10.1::hislip0::INSTR',write_termination='\n',read_termination='\n')

        sa = SA(sa_inst)

        switch_controller_inst = rm.open_resource('TCPIP0::10.10.10.5::INSTR',write_termination='\n',read_termination='\n')
        Switch_Control = SwitchControl(switch_controller_inst)

这是非常令人沮丧的,这个错误发生随机,而不是每次都出现。任何帮助都将不胜感激,例如,作为解决方法或调试方法的任何技巧

以下是错误消息的示例:

Traceback (most recent call last):
  File "C:/1_Python_Automation/mycode/test.py", line 594, in <module>
    Switch_Control.TurnOnSwitch(switch_controller_inst, switch_H1_H2)
  File "C:\1_Python_Automation\mycode\Instrument_Automation\Power_Supplies\SwitchController11713C.py", line 39, in TurnOnSwitch
    inst.write(":ROUTe:CLOSe (@" + str(switch_port) +")");
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\resources\messagebased.py", line 197, in write
    count = self.write_raw(message.encode(enco))
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\resources\messagebased.py", line 157, in write_raw
    return self.visalib.write(self.session, message)[0]
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\ctwrapper\functions.py", line 2796, in write
    ret = library.viWrite(session, data, len(data), byref(return_count))
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\ctwrapper\highlevel.py", line 222, in _return_handler
    return self.handle_return_value(session, ret_value)  # type: ignore
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\highlevel.py", line 251, in handle_return_value
    raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/1_Python_Automation/mycode/test.py", line 698, in <module>
    rm.close()
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\highlevel.py", line 3096, in close
    resource.close()
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\resources\resource.py", line 334, in close
    self.visalib.close(self.session)
  File "C:\Users\bNB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\ctwrapper\functions.py", line 637, in close
    return library.viClose(session)
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\ctwrapper\highlevel.py", line 222, in _return_handler
    return self.handle_return_value(session, ret_value)  # type: ignore
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\highlevel.py", line 251, in handle_return_value
    raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_CLOSING_FAILED (-1073807338): The VISA driver failed to properly close the session or object reference. This might be due to an error freeing internal or OS resources, a failed network connection, or a lower-level driver or OS error.
Exception ignored in: <function Resource.__del__ at 0x00000263BC9AF550>
Traceback (most recent call last):
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\resources\resource.py", line 199, in __del__
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\resources\resource.py", line 333, in close
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\resources\resource.py", line 327, in before_close
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\resources\resource.py", line 345, in __switch_events_off
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\resources\resource.py", line 495, in disable_event
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\ctwrapper\functions.py", line 662, in disable_event
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\ctwrapper\highlevel.py", line 222, in _return_handler
  File "C:\Users\NB\AppData\Local\Programs\Python\Python38\lib\site-packages\pyvisa\highlevel.py", line 251, in handle_return_value
pyvisa.errors.VisaIOError: VI_ERROR_INV_OBJECT (-1073807346): The given session or object reference is invalid.