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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top