当我在仪器上运行pyvisa的命令写入命令时,如何检索控制台输出(串行连接)?
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论