PyVisa读取时间

2024-04-19 18:39:28 发布

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

我对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

错误是:

^{pr2}$

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

任何帮助都将不胜感激!!!在


Tags: rm答案nonereadraw字符one仪器
1条回答
网友
1楼 · 发布于 2024-04-19 18:39:28

这可能有点晚了,但是我在自己的函数中遇到了这个问题,这个函数依赖于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

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

相关问题 更多 >