I recall using a flush() once for baudrate detection, before my Arduino period started ..
I entered AAAAAAAAAAAAAAAA until the application detected it. "arduinized" code was something like this:
long baudRates[] = { 1200,2400,9600, ..., -1} // -1 is a sentinel
long setBaudRate()
{
for (int i=0; baudRates[i] != -1; i++)
{
long br = baudRates[i];
Serial.begin(br);
Serial.flush(); // remove junk from buffer
while (!Serial.Available());
c = Serial.Read();
if (c== 'A') return br;
}
There are other methods to detect baudrate by checking edges; these are especially usefull to detect non standard baudrates, or deviating clocks.
Furthermore flush() can make sense after a soft reset in application,
what to think about a parameterized flush() ???
void flush (uint8_t n=MAXBUF)
{
if (n > available() ) _rx_buffer->head = _rx_buffer->tail;
else while (n-- > 0 && available()) read(); // can be done in one formula no doubt
}