无法使用 PySerial 接收回复,但超级终端有效

Can't receive reply using PySerial but hyperterminal works

我有一个设备 (Pololu Wixel),我正尝试使用 USB 串行连接与之通信。超级终端工作正常,但我正在尝试使用 Python 以获得更大的灵活性。我可以向设备发送命令,但是当我尝试接收所有内容时,我得到的只是我刚刚发送的命令。但是,如果我打开 Hyperterminal,我会在那里收到对从脚本发送的命令的回复。我的代码如下。我有点不知所措,看起来这应该相当简单。感谢您的帮助。

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())
  1. 找到活动端口后添加中断,
  2. 尝试将不同的 eol 值传递给 readline()、“\r”或“\r\n”。