简体   繁体   中英

pySerial readline reads previously written data

Hy,

I am writing some data through serial port, and i have an issue reading the response from that device.

Example:

ser.write('Hello World')
print(ser.readline()) #Instead of reading the response from the 
                      #other device, it reads 'Hello world'

I have made a workaround for this issue, by calling twice the readline(), and not caring about the first one. In this way (works) it reads the data sent from the other device

ser.write('Hello World')
ser.readline()
print(ser.readline())

It seems for me that the data sent it remains in the buffer? and when calling readline() it reds that first?!

Can somebody explain for me what i am doing wrong?

Thank you!

Not likely, that there is no "problem with data remaining in the buffer". But more likely that the device is sending everything it receives back, then responds. This behavior allow one using a terminal to talk to a serial device, to see that the device is receiving ok. It allows to correct typos and verifies that both host and device are set for the same baud rate.

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