Hi how do I flush a specific number of bytes in the Input buffer?

I don't want to use flushInput() because it removes all bytes. I just want to remove a certain size. What command can be used?


8 Years
Discussion Span
Last Post by jlm699

Couldn't you just read(x); x number of bytes into some garbage variable?

Hi I do use read, here is part of my code.

for x in range (0,100):
s = ser.read(15)
print repr(s)

The problem is that the output does not change value although the input to the serial port is changed. When I did the flushInput() the input to the serial line reflected. Which leads me to two possible theories

1. The data queued in the input buffer must be a lot that is why the rate of "reading" by python is not enough
2. The input buffer remains static and is not accepting new inputs that is why it outputs the same values.

How does the read command work in respect to reading the input buffer? Does it update the buffer everytime data is read?


The input buffer works exactly as any other type of buffer. It fills up with whatever it is reading and stores the information until you read it.

read(size=x) reads x number of bytes from the buffer.
inWaiting() tells you how many bytes are in the buffer waiting to be read
flushInput() clears the buffer and discards all of its contents.

Read over the pyserial informational wiki and you'll see much information to help you. A big concern of mine is how you've set up your serial port. Make sure you set the proper parameters (Baud rate, non-blocking, etc.). Again, pay attention to the wiki page for more details on this.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.