简体   繁体   中英

How to read serial port output in Python using pySerial?

A device is connected to my PC and I need to write a command via COM Port and get the output. Here is my code

from time import sleep
import serial

ser = serial.Serial()
ser.baudrate = 115200
ser.port = 'COM20'

ser.open()
ser.write("system\r")
result = ser.read(100)
print result

ser.close()

This is working perfectly. However, different commands return different strings. so I need to read it without mentioning no of bytes in ser.read(100) .

How can I achieve that? Appreciate any help. Thank you

在while循环中一次读取一个字节,并在找到所需序列时中断,即协议起始和结束控制字节之间的数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM