Skip to main content
 首页 » 编程设计

python之使用 visa 与 GPIB 接口(interface)总是给我 VisaIOError

2025年05月04日14wuhuacong

我正在尝试在 Python 中导入 visa 并与 GPIB 交互以控制设备。 我使用的设备名称是"GPIB0::9::INSTR",我想这个应该没有问题。

我在 2.7.3 Python Shell 中运行了以下代码

>>> from visa import * 
>>> a = instrument("GPIB0::9", timeout = 20) 
>>> a.write("*IDN?") 
>>> print a.read() 
 
Traceback (most recent call last): 
  File "<pyshell#53>", line 1, in <module> 
    print a.read() 
  File "C:\Python27\lib\site-packages\pyvisa\visa.py", line 433, in read 
    return self._strip_term_chars(self.read_raw()) 
  File "C:\Python27\lib\site-packages\pyvisa\visa.py", line 407, in read_raw 
    chunk = vpp43.read(self.vi, self.chunk_size) 
  File "C:\Python27\lib\site-packages\pyvisa\vpp43.py", line 840, in read 
    visa_library().viRead(vi, buffer, count, byref(return_count)) 
  File "C:\Python27\lib\site-packages\pyvisa\vpp43.py", line 398, in check_status 
    raise visa_exceptions.VisaIOError, status 
VisaIOError: VI_ERROR_TMO: Timeout expired before operation completed. 

以上是系统报错。 其实一开始,我把Timeout设置为3,就显示了这个errot。但是我如上图改成20后还是不行。

有人可以帮帮我吗?

请您参考如下方法:

有不同的问题可能会导致超时。 首先,您应该检查您的设备是否支持 *IDN? 查询。它是 IEEE-488.2 标准命令,因此它受支持的可能性很高(如果不支持,请查看您的手册中的命令)。

然后你应该检查你的通信设置,特别是终止字符和 EOI。

如果您使用了错误的终止字符,visa 将继续读取并最终超时。

注意:如果您使用的是可查询命令(它是写入和读取的组合),您可以使用 pyvisa 的 ask 函数。

import visa 
 
# If you've got just one gpib card installed, you can ommit the 0. 
# ASsuming the EOI line should be asserted and a termination character 
# of '\n' 
instrument = visa.instrument('GPIB::9', term_chars='\n', send_end=True) 
# use ask to write the command and read back the response 
print instrument.ask('*IDN?')