Serial.flush() does not work?

Hello,

I'm starting exploring the arduino Mega 2560 world and did some programming. So far I'm fine but the Serial.flush() command seems not to work for whatever reason. Hereunder is a tiny programm. The programm waits for an serial input and direct after the serial input buffer should be flushed. But obviously it is not flushed because I can see in the serial monitor the messages "LED is on/off" when I send a "0" or a "1"

What did I wrong?

int ledPin=22; void setup() { Serial.begin(9600);// create Serial Object pinMode(ledPin,OUTPUT); }

void loop() { while(Serial.available() == 0);// Have the arduino wait to receive input Serial.flush(); int val = Serial.read()-'0'; // Read the input if(val==1) { Serial.println("LED is On"); digitalWrite(ledPin,HIGH); } else if (val==0) { Serial.println("LED is off"); digitalWrite(ledPin,LOW); } else { Serial.println("Invalid!"); }

}

'flush' does not mean 'clear'; it will just block while sending all the data that was not transmitted yet.

And after that you are printing your 'LED is On'; why would that not show in the serial monitor?

PS: please use code tags when posting code

okay, just to make shure that I understood it right (I'm not a native english): "flush" does not mean to empty (clear) the serial input buffer. "flush" blocks ??? I've not really understood which data are blocked.

But how to empty (clear) the seriell input buffer?

Your second remark: the serial monitor indeed shows the text "LED is on" and that is for me the reason to assume that the buffer is not empty

Apparently the purpose of Serial.flush() changed. Now it is only relevant when sending data - its purpose is to cause a program to wait until all the data in the Serial output buffer has been sent.

If you want to empty the Serial input buffer you can do it with

while (Serial.available() >0 ) {
   Serial.read();
}

...R

Apparently the purpose of Serial.flush() changed.

In most common usage, flush() refers to emptying an output stream. cout, for instance, is an output stream that buffers data, since an application can write data faster than a teletype machine or monitor can display it. The application writes data to the buffer, and the buffered data is sent to wherever it is supposed to go. To make sure that all messages written have been seen, one uses flush to assure that that has happened.

The concept of dumping unread serial data, using flush(), was an abomination that finally got corrected.

It is somewhat unfortunate that there was no dumpRandomAmountsOfUnreadData() function created to replace it. But, really, why would one NEED such a function?

okay, understood. It works now.

Concerning the NEED of emptying an input buffer: If a programm is listening to an serial input and find a desired text or number in the input stream you might wish to emty the remaining content of the buffer. I could imagine that there are other intelligent methods to overcome this situation. So, really need : No but maybe helpful in rare cases.

Robin2:
Apparently the purpose of Serial.flush() changed. Now it is only relevant when sending data - its purpose is to cause a program to wait until all the data in the Serial output buffer has been sent.

If you want to empty the Serial input buffer you can do it with

while (Serial.available() >0 ) {

Serial.read();
}




...R

Too much work :P, if you are feeling lazy, just use the simpler version:

while(Serial.read() > -1);

If a programm is listening to an serial input and find a desired text or number in the input stream you might wish to emty the remaining content of the buffer.

But, the random amount of unread data in the buffer might include the start of the next useful packet. Just dumping the unread data is almost never the right thing to do. THAT is why there is no function to do it for you.

If you want to dump random amounts of unread data, you write the code to do it.

while(Serial.available() == 0);// Have the arduino wait to receive input
Serial.flush();
int val = Serial.read()-'0';  // Read the input

Even supposing that flush() did do exactly what you want (clear the input buffer), what on earth are you hoping to achieve by reading something you just emptied?

Jiggy-Ninja: while(Serial.available() == 0);// Have the arduino wait to receive input Serial.flush(); int val = Serial.read()-'0';  // Read the input

Even supposing that flush() did do exactly what you want (clear the input buffer), what on earth are you hoping to achieve by reading something you just emptied?

If you have an empty buffer, a Serial.read() is a nice obfuscated way to get a quick -1 :P

pYro_65: Too much work :P, if you are feeling lazy, just use the simpler version:

while(Serial.read() > -1);

You can be lazier :D

Serial.readString();

The function returns the whole buffer as String. The snippet dumps that by not storing the return :)

theintelligentboy: You can be lazier :D

Serial.readString();

The function returns the whole buffer as String. The snippet dumps that by not storing the return :)

How long does that function take to complete ?

...R

Robin2: How long does that function take to complete

About 2 years ;) That is how old this thread is :(