1

我可以使用 miniterm.py 读写信号发生器。流中没有 read_termination 字符(根据设备制造商的手册)。我附上了 miniterm 的调试输出以显示一些事务。我是否缺少让 pyvisa 读取任意长度的返回字符串而不终止的东西?

------------------------miniterm 输出 ------------ --

设置:/dev/ttyUSB0 9600,8,N,1 RTS:活动 DTR:活动 BREAK:不活动 CTS:不活动 DSR:不活动 RI:不活动 CD:不活动 软件流控制:不活动 硬件流控制:不活动 串行输入编码:UTF- 8串口输出编码:UTF-8 EOL:LF过滤器:调试默认直接

[TX:'R'] [TX:'M'] [TX:'W'] [TX:'\n'] [RX:'3'] 3 [RX:'6'] 6 [TX:'R '] [TX:'F'] [TX:'W'] [TX:'\n'] [RX:'0'] 0 [TX:'R'] [TX:'M'] [TX:' A'] [TX:'\n'] [RX:'2'] 2 [RX:'0'] 0 [RX:'0'] 0 [TX:'R'] [TX:'F'] [ TX:'A'] [TX:'\n'] [RX:'1'] 1 [RX:'0'] 0 [RX:'0'] 0 [RX:'0'] 0


任何指向正确方向的指针都会有所帮助。这是一种非常奇怪的设备。我尝试过的大多数事情都以“正在使用的设备”或“超时”错误结束。

先感谢您。

罗宾

4

2 回答 2

0

我在测量仪器上遇到了同样的问题,花了一段时间才找到解决方案:

device.bytes_in_buffer为您提供缓冲区内的字节数和

answer = device.read_bytes( device.bytes_in_buffer )返回缓冲区内的字节。

之后,您可以调用answer.decode()以获取设备返回给您的 ASCII 字符串。下面是一个较长的 Python 类示例,我为使与设备的通信更容易而编写:

import pyvisa as visa
import logging as log
import time
from typing import Final, Union

class interface:  # provide write(), read(), read_raw(), ask() and ask_raw() function

    _device = None

    def __init__(self, device) -> None:
        self._device = device

    def write(self, command) -> None:
        self._device.write("%s" % ( command))

    def read(self) -> str:
        return self.read_raw().decode()

    def read_raw(self) -> str:
        return self._device.read_bytes( self._device.bytes_in_buffer )

    def ask(self, command) -> str:
        return self.ask_raw(command).decode()

    # flush buffer, this is necessary to prevent from errors because the last SCPI command took to long to be received
    def flush_buffer(self) -> None:
        self._device.flush(visa.constants.VI_READ_BUF)

    def ask_raw(self, command) -> str:
        self.flush_buffer()
        self.write(command)
        time.sleep(0.2) # this is needed for the device to process the SCPI command
        return self.read_raw()

    def boolean_property(
        self,
        scpi_cmd: str,                  # command name of scpi option
        err_description: str,           # generate usefull error messages
        value: Union[bool, None] = None # True or False -> set scpi option to value, None -> get current value of scpi option
    ) -> Union[bool, None]:

        # if parameter value is None -> get current state from scpi option
        if value == None:
            return {
                "ON": True,
                "1": True,
                "OFF": False,
                "0": False
            }[str(
                self.ask("%s?" % (scpi_cmd.upper()))
            ).upper()]   # uppercase conversion is used to prevent issues with unexpected lowercase letters

        # check wether the parameter is a boolean
        if not type(value) == bool:
            return log.error("%s: Cannot assign \"%s\" for %s, must be bool(True) or bool(False)" % (self._device, value, err_description))

        # write the scpi command
        self.write("%s %s" % (
            scpi_cmd,
            {
                True: "ON",
                False: "OFF"
            }[value]
        ))
于 2022-03-01T10:56:34.300 回答
0

下面的 read_data() 函数在一定程度上解决了这个问题。现在要弄清楚为什么背靠背写入会失败,除非在写入之间不使用大约 0.2 秒的 time.sleep(x)。

def read_data(cmd):
    data=""
    byte=""
    looper = True
    dds.write(cmd)
    while(looper==True):
       try:
          byte = dds.read_bytes(1)
          data += byte.decode()
       except:
          looper = False
    return(data)
于 2020-10-04T03:10:03.757 回答