Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyVisa读取超时_Python_Python 3.x_Visa - Fatal编程技术网

Python PyVisa读取超时

Python PyVisa读取超时,python,python-3.x,visa,Python,Python 3.x,Visa,我还不熟悉python和仪器控制,我面临着一些我还没有找到答案的问题。 我使用PyVisa通过rs232控制单色仪(光谱产品dk240)。 (Python 3.5,PyVisa 1.8) 我可以通过设置正确的终止字符来编写命令和读取响应。问题是,有时仪器响应是一个没有终止的单字节,然后我会得到一个超时(即使我在端口监视器上看到我想要的响应) 我尝试使用read_raw来获取单个字节,但它不起作用。 以下是我的代码的简单版本: import pyvisa rm = pyvisa.ResourceM

我还不熟悉python和仪器控制,我面临着一些我还没有找到答案的问题。 我使用PyVisa通过rs232控制单色仪(光谱产品dk240)。 (Python 3.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.
我还尝试将终止字符设置为“无”,这样visa就不会等待它,但仍然会得到超时。 此外,我试图用read_raw(1)读取序列号,但从仪器中得到的不是一个字节,而是完整的答案,这是为什么


任何帮助都将不胜感激

这可能有点晚了,但我自己解决了这个问题,因为我自己的函数依赖于bytes\u in\u 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

备注:它不适用于以太网连接。属性不存在。

谢谢您。我遇到了一个奇怪的工具,它在使用
instrument.query(…)
时超时,但在
instrument.visalib.read(instrument.session,1)
的无限循环中工作正常,当返回值为
“\r”
时,我会中断该循环。你的解决方案是一个很好的解决办法。