我正在通过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
>>> 请注意,print(device.query("*IDN?"))使用34972A> *IDN?和print(device.query("DATA:POINTS?" prints the result of the "*IDN?"`命令进行响应。
对于read_termination和write_termination,我已经尝试了\n、'\r‘和'\r\n’的所有九种组合。
有什么建议吗?
发布于 2021-03-26 01:29:40
我找到了一个变通方法,尽管我不认为它是一个很好的解决方案。
简而言之,该单元正在回显命令,然后是响应。因此,device.query()正在获取回显的命令;响应将保留在输出缓冲区中,直到下一次读取操作。
解决方法是执行初始读取以获取(并忽略)回显命令,然后进行第二次读取以获得结果:
def query_ignoring_echo(device, command):
device.write(command)
echoed = device.read() # read and ignore echoed command
return device.read()https://stackoverflow.com/questions/66793174
复制相似问题