首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyVisa读取超时

PyVisa读取超时
EN

Stack Overflow用户
提问于 2016-04-17 22:50:22
回答 1查看 4.8K关注 0票数 2

我仍然是python和仪器控制的新手,我面临着一些我还找不到答案的问题。我正在使用PyVisa通过rs232控制一个单色仪(光谱产品dk240)。(Python3.5,PyVisa 1.8)

我可以通过设置正确的终止字符来写入命令并读取响应。问题是,有时仪器响应是一个没有终止的单字节,然后我得到一个超时(即使我在端口监视器上看到了我想要的响应)。

我尝试使用read_raw来获取单字节,但它不起作用。下面是我的代码的一个简单版本:

代码语言:javascript
复制
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

以及错误:

代码语言:javascript
复制
raise errors.VisaIOError(ret_value)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.

我还尝试将终止字符设置为"none“,这样visa就不会等待,但仍然会超时。此外,我尝试用read_raw(1)读取序列号,但从仪器中得到的不是一个字节,而是完整的答案,这是为什么?

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2016-07-18 19:48:41

现在可能有点晚了,但是当我自己使用一个依赖于bytes_in_buffer属性的函数遇到这个问题时,我帮了自己一个忙。

代码语言:javascript
复制
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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36678063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档