我仍然是python和仪器控制的新手,我面临着一些我还找不到答案的问题。我正在使用PyVisa通过rs232控制一个单色仪(光谱产品dk240)。(Python3.5,PyVisa 1.8)
我可以通过设置正确的终止字符来写入命令并读取响应。问题是,有时仪器响应是一个没有终止的单字节,然后我得到一个超时(即使我在端口监视器上看到了我想要的响应)。
我尝试使用read_raw来获取单字节,但它不起作用。下面是我的代码的一个简单版本:
import pyvisa
rm = pyvisa.ResourceManager()
instrument = rm.open_resource('ASRL1::INSTR')
instrument.baud_rate= 9600
instrument.data_bits=8
instrument.stop_bits = pyvisa.constants.StopBits.one
instrument.parity = pyvisa.constants.Parity.none
instrument.read_termination = chr(24) #specified in the manual
instrument.write(chr(33)) # command to get the serial number
instrument.read() # this works!
instrument.write(chr(27)) # echo command
# instrument replies one byte echo (seen on port monitor)
instrument.read_raw(1) # I get a timeout here以及错误:
raise errors.VisaIOError(ret_value)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.我还尝试将终止字符设置为"none“,这样visa就不会等待,但仍然会超时。此外,我尝试用read_raw(1)读取序列号,但从仪器中得到的不是一个字节,而是完整的答案,这是为什么?
任何帮助都将不胜感激!
发布于 2016-07-18 19:48:41
现在可能有点晚了,但是当我自己使用一个依赖于bytes_in_buffer属性的函数遇到这个问题时,我帮了自己一个忙。
def read_all(devicehandle):
with devicehandle.ignore_warning(constants.VI_SUCCESS_DEV_NPRESENT, constants.VI_SUCCESS_MAX_CNT):
try:
data , status = devicehandle.visalib.read(devicehandle.session, devicehandle.bytes_in_buffer)
except:
pass
return data备注:它不适用于以太网连接。该属性不存在。
https://stackoverflow.com/questions/36678063
复制相似问题