How to clear out or flush the arduino serial buffer

For anyone curious about the serial.flush() code change…

New (Arduino 1.0+ Serial.Flush())

void HardwareSerial::flush()
{
  while (_tx_buffer->head != _tx_buffer->tail)
    ;
}

Old (Arduino 23- Serial.Flush())

void HardwareSerial::flush()
{
  _rx_buffer->head = _rx_buffer->tail;
}

What I did to return this function to my current Arduino version, was add a new function to HardwareSerial.h.

virtual void flushRX(void);

Then, add the above (old) flush() function to HardwareSerial.cpp (but name it flushRX). Worked like a charm.

void HardwareSerial::flushRX()
{
  _rx_buffer->head = _rx_buffer->tail;
}

I did not realize they changed the function, and have been debugging code that USED to work… and suddenly stopped working.

Good Luck!