这是我的功能。我正在尝试将传感器一词发送到COM端口,然后返回结果。
def serialportcommunication():
ser = serial.Serial(
port='COM5',
baudrate=115200
)
print('Writing SENSORS command to COM7')
ser.write(b"sensors")
time.sleep(4)
print('Reading 10 bytes from serial buffer')
read_val = ser.read(size=10)
print(read_val)
print('Done printing output')
输出我得到上面的代码:
将SENSORS命令写入COM7
从串行缓冲区读取10个字节
b'sensors \ r \ n'
完成打印输出
如果我使用像Putty这样的终端程序执行命令“sensors”到COM端口,我会从目标设备上获得一个文本墙(示例输出如下所示,我不得不尽管白了大部分输出。)
这篇文章我正在读回来,我想用Python上面的ser.read(size = ??)的teh命令在Python中读取它,但是我没有读回来。
如何阅读?
答案 0 :(得分:0)
已在评论中解决:
Do you have to hit Enter when typing the command manually? Then you need to do the same here - ser.write(b"sensors\r") (or maybe \n, or \r\n). – jasonharper Yes, when i enter the command in Putty, I to hit enter. Thank you thank you. I added the '\n in the end and it works now!!!! – Neil Dey