当我在仪器上运行pyvisa的命令写入命令​​时,如何检索控制台输出(串行连接)?

发布于 2025-02-10 20:28:44 字数 2308 浏览 1 评论 0原文

我正在尝试使用Pyvisa的串行通信将写命令发送到仪器。发送命令后,仪器将运行一个内部文件并将日志返回到仪器的Linux终端。我想将此文件的执行日志返回到计算机的终端。

使用SSH通信,我可以通过下面的代码(使用stdout.readlines())将其返回:

def create_connection_ssh(self):
        self.ssh_amp = paramiko.SSHClient()
        self.ssh_amp.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh_amp.connect(self.ip_amp, username=self.username_amp, password=self.password_amp)

def amplifier_clock_set(self):
        stdin, stdout, stderr = self.ssh_amp.exec_command('../../opt/scripts/config/amplifier_clock_set.sh')

        day = datetime.today().strftime('%d')
        month = datetime.today().strftime('%m')
        year = datetime.today().strftime('%Y')
        hour = datetime.today().strftime('%H')
        minute = datetime.today().strftime('%M')
        weekday = datetime.today().strftime('%a')
        montname = datetime.today().strftime('%b')
        
        stdin.write(f'{day}\n')
        stdin.write(f'{month}\n')
        stdin.write(f'{year}\n')
        stdin.write(f'{hour}\n')
        stdin.write(f'{minute}\n')

        result = ' '.join(stdout.readlines())
        error = ' '.join(stderr.readlines())
        print('Output: '+result+error)

以及下面用于串行通信的代码:(问题在这里)

def create_connection_serial(self):

        RscManager = visa.ResourceManager()
        try:
            self.amplifier = RscManager.open_resource(self.amplifier_serial)
            self.amplifier.baud_rate = 115200
            self.amplifier.write_termination = '\n'
            self.amplifier.read_termination = '\n'
            time.sleep(0.5)

def amplifier_clock_set(self):
        self.amplifier.write('./amplifier_clock_set.sh')

        day = datetime.today().strftime('%d')
        month = datetime.today().strftime('%m')
        year = datetime.today().strftime('%Y')
        hour = datetime.today().strftime('%H')
        minute = datetime.today().strftime('%M')

        self.amplifier.write(f'{day}')
        self.amplifier.write(f'{month}')
        self.amplifier.write(f'{year}')
        self.amplifier.write(f'{hour}')
        self.amplifier.write(f'{minute}')

        print(self.amplifier.read()) #In this part, I need a command or a way to return the console output that is in the terminal of the instrument that ran the file.

I'm trying to send a write command to an instrument using serial communication through PyVisa. After sending the command, the instrument runs an internal file and returns a log to the instrument's linux terminal. I would like to return the execution log of this file to the machine's terminal.

With SSH communication, I can return it through the code below (using stdout.readlines()):

def create_connection_ssh(self):
        self.ssh_amp = paramiko.SSHClient()
        self.ssh_amp.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh_amp.connect(self.ip_amp, username=self.username_amp, password=self.password_amp)

def amplifier_clock_set(self):
        stdin, stdout, stderr = self.ssh_amp.exec_command('../../opt/scripts/config/amplifier_clock_set.sh')

        day = datetime.today().strftime('%d')
        month = datetime.today().strftime('%m')
        year = datetime.today().strftime('%Y')
        hour = datetime.today().strftime('%H')
        minute = datetime.today().strftime('%M')
        weekday = datetime.today().strftime('%a')
        montname = datetime.today().strftime('%b')
        
        stdin.write(f'{day}\n')
        stdin.write(f'{month}\n')
        stdin.write(f'{year}\n')
        stdin.write(f'{hour}\n')
        stdin.write(f'{minute}\n')

        result = ' '.join(stdout.readlines())
        error = ' '.join(stderr.readlines())
        print('Output: '+result+error)

And the code used for serial communication below: (The problem is here)

def create_connection_serial(self):

        RscManager = visa.ResourceManager()
        try:
            self.amplifier = RscManager.open_resource(self.amplifier_serial)
            self.amplifier.baud_rate = 115200
            self.amplifier.write_termination = '\n'
            self.amplifier.read_termination = '\n'
            time.sleep(0.5)

def amplifier_clock_set(self):
        self.amplifier.write('./amplifier_clock_set.sh')

        day = datetime.today().strftime('%d')
        month = datetime.today().strftime('%m')
        year = datetime.today().strftime('%Y')
        hour = datetime.today().strftime('%H')
        minute = datetime.today().strftime('%M')

        self.amplifier.write(f'{day}')
        self.amplifier.write(f'{month}')
        self.amplifier.write(f'{year}')
        self.amplifier.write(f'{hour}')
        self.amplifier.write(f'{minute}')

        print(self.amplifier.read()) #In this part, I need a command or a way to return the console output that is in the terminal of the instrument that ran the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文