【发布时间】:2015-02-12 19:45:51
【问题描述】:
我有一个设备 (Pololu Wixel),我正尝试使用 USB 串行连接与之通信。超级终端工作正常,但我正在尝试使用 Python 以获得更大的灵活性。我可以向设备发送命令,但是当我尝试接收时,我得到的只是我刚刚发送的命令。但是,如果我打开超级终端,我将在那里收到对脚本发送的命令的回复。我的代码如下。我有点茫然,看起来这应该相当简单。感谢您的帮助。
import serial
import time
'''
Go through 256 COM ports and try to open them.
'ser' will be the highest port number. Fix this later.
'''
for i in range(256):
currentPort = "COM" + str(i+1)
try:
ser = serial.Serial(currentPort,baudrate=115200,timeout=5)
print("Success!!")
print(ser.name)
except:
pass
print(ser.isOpen())
str = "batt" #Command to request battery levels.
ser.write(str.encode())
x = ser.inWaiting()
print(x)
while ser.inWaiting() > 0:
out = ser.readline()
print(out.decode())
【问题讨论】: