python - 无法使用 PySerial 接收回复,但 super 终端有效

标签 python pyserial

我有一个设备 (Pololu Wixel),我正尝试使用 USB 串行连接与之通信。 Hyperterminal 工作正常,但我正在尝试使用 Python 以获得更大的灵 active 。我可以向设备发送命令,但是当我尝试接收所有内容时,我得到的只是我刚刚发送的命令。但是,如果我打开 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”。

关于python - 无法使用 PySerial 接收回复,但 super 终端有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28476467/

相关文章:

python - 通过串行通信自动登录操作

python - 如何将 dict.get() 与多维字典一起使用?

python - 如何关闭 SQLAlchemy session ?

python - 在python中设置搜索程序的目录

python - Python 串行导入错误

python - 使用 python、cherrypy 和 pyserial 进行线程化

python - Pandas - 从列中提取多个数据

python - 从python中的数组中选择3个随机元素

python - 奇怪的 (py) 串行 linux 损坏

python - Numpy/Python 数组值错误