Go Down

Topic: Serial.flush() does not work? (Read 4157 times) previous topic - next topic

Zuse99

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!");
  }

}

sterretje

#1
Sep 20, 2016, 11:28 am Last Edit: Sep 20, 2016, 11:29 am by sterretje
'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
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Zuse99

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


Robin2

#3
Sep 20, 2016, 11:45 am Last Edit: Sep 20, 2016, 11:46 am by 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
Code: [Select]
while (Serial.available() >0 ) {
   Serial.read();
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Quote
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?
The art of getting good answers lies in asking good questions.

Zuse99

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.

pYro_65

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
Code: [Select]
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);
Forum Mod anyone?
https://arduino.land/Moduino/

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

Jiggy-Ninja

Code: [Select]
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?
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

pYro_65

Code: [Select]
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
Forum Mod anyone?
https://arduino.land/Moduino/

theintelligentboy

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

while(Serial.read() > -1);
You can be lazier :D

Code: [Select]
Serial.readString();

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

Robin2

You can be lazier :D

Code: [Select]
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
Two or three hours spent thinking and reading documentation solves most programming problems.

sterretje

#12
Aug 11, 2018, 08:41 am Last Edit: Aug 11, 2018, 11:30 am by sterretje
How long does that function take to complete
About 2 years ;) That is how old this thread is :(
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up