代码之家  ›  专栏  ›  技术社区  ›  fearless_fool

pyvisa得到不完整的响应

  •  0
  • fearless_fool  · 技术社区  · 3 年前

    我正在通过TCP/IP连接控制一个远程仪器。当我通过telnet连接到设备时,我在交互方面没有任何问题:

    % 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?”`命令。

    \n 、“\r”和“\r\n”表示 read_termination write_termination .

    0 回复  |  直到 3 年前
        1
  •  0
  •   fearless_fool    3 年前

    我找到了一个解决办法,尽管我认为这不是一个很好的解决办法。

    简言之,部队在回应命令,然后是回应。所以呢 device.query() 正在获取回音命令;在下一次读取操作之前,响应将保留在输出缓冲区中。

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

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