[SOLVED]Flush serial input buffer

How to properly flush serial input buffer?
While(serial.available()) serial.read();
Is not flushing emptying properly.

As per @nickgammon suggestion. 10ms delay solved the problem.


By reading buffer after flush.

By reading buffer after flush.

Let's see the code that you used


Sure here it is with description.


Im sending via serial monitor


as requested:

bool receive_command(char answer[]);
void serial_flush(void);

void setup() {
  // put your setup code here, to run once:
  for(byte i=0;i<3;i++){
 if(receive_command("OK")) Serial.println("OK found");

void loop() {
  // put your main code here, to run repeatedly:


void serial_flush(void) {
  while (Serial.available()) Serial.read();

bool receive_command(char answer[]) {
  unsigned long _time = 0;
  const int max_time = 10000;
  bool flag = 0;

  _time = millis();
  Serial.print("in Serial buffer=");
  Serial.println(" left in serial buffer");}
  while (Serial.available() < 2) {
    if ((millis() - _time) > max_time) {
      Serial.println("waited too long nothing received");
      return flag;
  do {
    if (Serial.findUntil(answer, '\n')) return flag = 1;
  } while (Serial.available());
  return flag;


via serial monitor with CR+LF line ending I`m sending this string:
26chars+OK+26chars+CR+LF=56 chars


in Serial buffer=0
OK found
in Serial buffer=1
b left in serial buffer
in Serial buffer=0
waited too long nothing received

after OK is found serial_flush() is executed but after second run there serial.available() shows there is something in serial buffer. right at that moment it was char ‘b’ but it also can show char ‘d’ and ‘c’ depending how long it will run.

why serial_flush() is not clearing the buffer?

"why serial_flush() is not clearing the buffer?"

well because serial_flush() does not clear the buffer. I believe all it does is WAIT until you finish sending data on that serial.

Serial.flush() does not clear buffer but serial_flush() which is my defined function suppose to do that.

Also posted at:

If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. When you post links please always use the chain links icon on the toolbar to make them clickable.

the second parameter of findUntil(target, terminator) is of type char* not char

use Serial.findUntil(answer, "\n")

The concept of emptying the Serial Input Buffer is meaningless unless you know for sure that the sending device has stopped sending data to the Arduino BEFORE you empty the buffer and does not send any more data AFTER you have emptied the buffer.

The examples 2 and 3 in Serial Input Basics seem to work fine without any attempt to empty the buffer. The 3rd example just ignores any data between an end-marker and the next start-marker.


This is not cross posting. First i posted here. Look at the date and time. Then not getting answer decide to Open new account and ask in stackexchange too. And also mentioned right in the first line of my post in stackexchange that this question is also posted in forum.arduino.cc you are right in one thing that i should have been linked that sentence to this thread which i didnt do because couldnt find that button in editor.

I did send 1 long string of 56 chars and nothing else so sending device stopped sending string right after i hit “send” button in seria monitor.

Then why there is 1 char left in the buffer and that char is not “z” or any other letter but “b”? Why all other chars were cleared except “b”?

I've answered it here.

1 Like

There is no indication on the Stack Exchange thread that you have read or ingested the answers given there. There is no point spraying off the same question onto multiple platforms if you aren't then going to put the effort into indicating whether or not the answers have helped you.

Let me give you an analogy: Say you go out into the garden and pull up all the weeds. Then a few days later you complain because more weeds have grown. It takes time for the weeds to grow and "flushing" them at any particular point won't remove ones that grow in the future. Same with the serial port. You can't flush things that haven't arrived yet.

1 Like

Then why there is 1 char left in the buffer and that char is not “z” or any other letter but “b”? Why all other chars were cleared except “b”?

Because you are only reading one thing:

  Serial.println(" left in serial buffer");}

If you had put "while" instead of "if" you would have seen more things. Not all of them, because some of them would arrive in the future.

1 Like

Before if(serial.available()) im printing whatever is in buffer via Serial.println(Serial.available()) shouldnt it show me if there are 10 chars or more left? it shows only 1 char is left.

Robin`s method is working without any delay but that doesnt explain why my method isnt. I have to add delay as you said and things are getting better.

as always we are getting far from the main question
if while(serial.available()) serial.read(); is considered as a way of flushing the input buffer in many articles and @spycatcher also wondered why it isnt working for me I wanted first to show that it isnt working and then to understand why.