我可以通过Putty向串行端口发送命令字符串并获得响应。然而,当我使用Python PySerial读/写尝试同样的操作时,我无法发送读/写命令。
腻子终端:示例1:
<command_string>
response = Success
示例2:
<incorrect_command_string>
response = Fail
Python代码:
serialData = serial.Serial(port=2, baudrate=921600, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
serialData.write(b'<command_string>')
print(serialData.in_waiting)
print(serialData.read(serialData.in_waiting))
代码输出:
0
b''
有什么建议吗?
找到它:响应字符串具有"\r"。所以我可以像下面这样使用read_util来消除睡眠。
serialData.read_until("r".encode('utf-8'))