【发布时间】:2017-07-28 23:07:41
【问题描述】:
我想使用 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
我使用以下代码在我的 Raspberry 上获取 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()-function,这让我陷入了一个循环。
如果有人可以帮助我,我会很高兴。
编辑:
错误的输出实际上是 0x0096 = 150 这并没有错,但通常是我发送的最后一个请求。
【问题讨论】:
标签: python raspberry-pi usb paramiko