【问题标题】:Can't receive reply using PySerial but hyperterminal works无法使用 PySerial 接收回复,但超级终端可以工作
【发布时间】: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())

【问题讨论】:

    标签: python pyserial


    【解决方案1】:
    1. 找到活动端口后添加中断,
    2. 尝试将不同的 eol 值传递给 readline()、“\r”或“\r\n”。

    【讨论】:

    • 我让它与这两者结合使用。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多