python - 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_termination
write_termination
有什么建议么?
解决方案
我找到了一种解决方法,尽管我不认为它是一个很好的解决方案。
简而言之,该单元正在响应命令,然后是响应。device.query()
获取回显命令也是如此;响应保留在输出缓冲区中,直到下一次读取操作。
解决方法是进行初始读取以获取(并忽略)回显的命令,然后进行第二次读取以获取结果:
def query_ignoring_echo(device, command):
device.write(command)
echoed = device.read() # read and ignore echoed command
return device.read()
推荐阅读
- inno-setup - Inno Setup - 如何在 TUninstallProgressForm 中修复 StatusLabel 中过长的路径
- sql-server - 为什么在这种情况下@sql 是一个 nvarchar(max) 变量,这对语句有何影响?(SQL 服务器)
- c# - 如何在 Umbraco 8 中使用 DocumentType(模型)制作主布局模板?
- laravel - Laravel 授权更新用户
- javascript - 动态添加多个表单控件
- mysql - 如何在表中实现位数据类型
- containers - 如何使用 Garden-RunC 后端手动进入应用容器
- react-native - 如何在电源按钮上运行功能 3 次单击?
- angular - ag-grid 中的行编辑
- python - python3 queue.put() 阻塞 main