首页 > 解决方案 > pyvisa得到不完整的响应

问题描述

我正在通过 TCP/IP 链接控制远程仪器。当我远程登录到设备时,交互没有问题:

% telnet 12.34.56.78 5024
Trying 12.34.56.78...
Connected to cpe-12-34-56-78.san.res.rr.com.
Escape character is '^]'.
Welcome to Keysight's 34972A Switch/Measure Unit
34972A> *IDN?
Agilent Technologies,34972A,MY49002127,1.17-1.10-02-02
34972A> DATA:POINTS?
+0
34972A> 
telnet> q
Connection closed.

但是当我尝试从 Python 脚本中获得相同的交互时,它似乎无法刷新当前输出或导致显示先前输出的东西。例如:

% python           
Python 3.8.2 (default, Dec 21 2020, 15:06:04) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvisa
>>> import time
>>> pyvisa.log_to_screen()
>>> rm=pyvisa.ResourceManager('@py')
2021-03-24 21:24:23,808 - pyvisa - DEBUG - SerialSession was not imported No module named 'serial'.
2021-03-24 21:24:23,809 - pyvisa - DEBUG - USBSession and USBRawSession were not imported No module named 'usb'.
2021-03-24 21:24:23,819 - pyvisa - DEBUG - TCPIPSession was correctly imported.
2021-03-24 21:24:23,830 - pyvisa - DEBUG - GPIBSession was not imported No module named 'gpib'.
2021-03-24 21:24:23,830 - pyvisa - DEBUG - Created library wrapper for py
2021-03-24 21:24:23,831 - pyvisa - DEBUG - Created ResourceManager with session 3068185
>>> device = rm.open_resource("TCPIP::12.34.56.78::5024::SOCKET")
2021-03-24 21:24:33,555 - pyvisa - DEBUG - TCPIP0::12.34.56.78::5024::SOCKET - opening ...
2021-03-24 21:24:33,581 - pyvisa - DEBUG - TCPIP0::12.34.56.78::5024::SOCKET - is open with session 8175711
>>> device.read_termination='\r\n'
>>> device.write_termination='\r\n'
>>> print(device.read(encoding='latin1'))
2021-03-24 21:24:59,497 - pyvisa - DEBUG - TCPIP0::12.34.56.78::5024::SOCKET - reading 20480 bytes (last status <StatusCode.success_max_count_read: 1073676294>)
ÿûÿûWelcome to Keysight's 34972A Switch/Measure Unit
>>> print(device.query("*IDN?"))
2021-03-24 21:25:08,145 - pyvisa - DEBUG - TCPIP0::12.34.56.78::5024::SOCKET - reading 20480 bytes (last status <StatusCode.success_max_count_read: 1073676294>)
34972A> *IDN?
>>> print(device.query("DATA:POINTS?"))
2021-03-24 21:25:16,767 - pyvisa - DEBUG - TCPIP0::12.34.56.78::5024::SOCKET - reading 20480 bytes (last status <StatusCode.success_max_count_read: 1073676294>)
Agilent Technologies,34972A,MY49002127,1.17-1.10-02-02
>>> device.close()
2021-03-24 21:25:23,791 - pyvisa - DEBUG - TCPIP0::12.34.56.78::5024::SOCKET - closing
2021-03-24 21:25:23,791 - pyvisa - DEBUG - TCPIP0::12.34.56.78::5024::SOCKET - is closed
>>> 

请注意,它使用“*IDN?”` 命令进行print(device.query("*IDN?"))响应。34972A> *IDN?print(device.query("DATA:POINTS?" prints the result of the

对于and ,我已经尝试了\n、'\r' 和 '\r\n' 的所有九种组合。read_terminationwrite_termination

有什么建议么?

标签: pythonpyvisa

解决方案


我找到了一种解决方法,尽管我不认为它是一个很好的解决方案。

简而言之,该单元正在响应命令,然后是响应。device.query()获取回显命令也是如此;响应保留在输出缓冲区中,直到下一次读取操作。

解决方法是进行初始读取以获取(并忽略)回显的命令,然后进行第二次读取以获取结果:

def query_ignoring_echo(device, command):
    device.write(command)
    echoed = device.read()  # read and ignore echoed command
    return device.read()

推荐阅读