无法使用 paramiko 从其他设备获取 USB 输出



我想使用 Python 2.7 控制远程设备的频率合成器。

直接使用 Rapberry Pi 的 USB 端口进行通信是通过终端完成的。命令可在手册中找到。有两种命令,set 和 get,例如:

echo 0E > /dev/ttyACM0    # 0E is the code to reset
echo 04 > /dev/ttyACM0|head</dev/ttyACM0    # 04 will return the frequency

我使用以下代码在我的树莓树上获取 python 的输出:

print(os.popen(echo 04 > /dev/ttyACM0|head</dev/ttyACM0))

终端命令可以使用来自其他设备的 ssh 执行。

现在,当我尝试使用 Python 时,我可以毫无问题地使用 set 命令,但 get 命令不会带来正确的输出。

我使用以下功能:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
global Con # nected
con=0

def SSHConnection():
global con
while True:
try:
ssh.connect(IP,username=User,password=Password)
con=1
break
except:
tkMessageBox.showwarning("Error!", "Device not found or wrong Login")
break
def outin(command):
global con
output="sudo echo "+command+"> /dev/ttyACM0|head</dev/ttyACM0"
if remote.get()==0:  # switches between remote and direct output
answer=os.popen(output).readlines()
else:
if con==0:  #  checks wether a connection has already 
SSHConnection()  #  connects
else:
pass
stdin,stdout,stderr = ssh.exec_command(output)
answer=stdout.read()
return answer

我在网上发现我的函数可能会在我的命令执行之前执行.read()。 我在网上找到的所有解决方案都使用channel.recv_exit_status()函数,这让我陷入了循环。

如果有人能帮助我,我会很高兴。

编辑: 错误的输出实际上是0x0096= 150,这不是那么错误,但通常是我发送的最后一个请求。

问题是某种时机。 我做了一个解决方法,在远程设备上调用python代码将输出保存在不同的文件中,然后调用该文件。 在大约 98% 的呼叫中工作..

最新更新