Go Down

Topic: Clearing serial buffer solved (Read 84252 times) previous topic - next topic

aditya123

Hi all,
          i had this question a few weeks back , after the change in flush() method it had become quite difficult to empty the serial buffer , so to all those who require to send large amounts of data from processing or serial monitor to arduino, just use this code. Only prerequisite  is that the data must be sent as a bulk, no delays are required.

void loop()
{
   if (Serial.available())
   {
     val="";
     while(Serial.available())
     {
         char t= Serial.read();
         val=val+t;
     }
     Serial.println(val);
     Serial.end();    // Ends the serial communication once all data is received
     Serial.begin(9600);  // Re-establishes serial communication , this causes deletion of anything previously stored in the buffer                             //or cache
  }
}
later u can just use the substring method to extract from val

Riva

Would it be better to write a small function to clear the buffer instead of doing serial.end/serial.begin as this may fragment memory.

Code: [Select]
void serialFlush(){
  while(Serial.available() > 0) {
    char t = Serial.read();
  }
}   
Don't PM me for help as I will ignore it.

retrolefty

  I'm still of the opinion that any attempt to 'flush' the serial input buffer is flawed thinking as one has no control over what and when new serial data my arrive, possibly even between the ending of the flushing function but before returning to further processing that assumes the input buffer is empty when it's possible it is not, esp at the higher baud rates.

  I think this kind of requirement is better met by 'building' it into the serial protocol so that one can simply throw away all characters until a valid start of packet character is read and then save characters until you get a valid end of packet character. So build the structure into the protocol rather then assuming what may or may not arrive or assuming what data you don't need even though you haven't determine what those flushed characters might represent.

Recall that the older arduino flushing function did clear the serial input buffer, but they changed it around IDE => 1.0 to flush only the output transmit buffer and left no function to clear the input buffer. If it was a useful or logical function why would they remove that functionality? I certainly am open to what others may have to say on this subject.

Lefty

arduinoenigma

I tried all the methods in this thread, with mixed results.

What worked for me was the following, the value of 10 on the loop was arbitrary, this can be tuned for your needs:

 //clear out the serial buffer

 byte w = 0;

 for (int i = 0; i < 10; i++)
 {
   while (Serial.available() > 0)
   {
     char k = Serial.read();
     w++;
     delay(1);
   }
   delay(1);
 }

arduinoenigma.blogspot.com

GoForSmoke

Strange. My serial code generally has output waiting, it no-blocks serial input and never reads more than 1 char per loop() it's generally a lot of loops() between serial available even at 115200 baud.

Isn't flush() supposed to flush the transmit buffer? That's the one you can load 1000's of times faster than it can send.

The only serial input you should have trouble keeping up with is SPI.

But if you do have problems keeping up with serial, you might want to find out what's blocking in your code.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

ORPARGA

I recently had a similar problem to send data to Arduino Unity and I developed this little demonstration.

It has the advantage of adjusting the write speed of Arduino speed at which empties buffe ...

Please note that is just a small demonstration.

Code: [Select]


int MaxBytesAvailable;
int CurrentAvailable;
int delayNumber=1000;
int n;
String messageOut;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  MaxBytesAvailable=Serial.availableForWrite();
}

void loop() {
  // put your main code here, to run repeatedly:
  messageOut="";
  messageOut+=Serial.availableForWrite();
  messageOut+=" Current Delay =";
  messageOut+=delayNumber;
  Serial.println(messageOut);
  for(n=0;n<delayNumber;n++)
  {
    if((CurrentAvailable=Serial.availableForWrite())>=MaxBytesAvailable) break;
   
  }
  if (CurrentAvailable>(MaxBytesAvailable/2))
  {
    delayNumber-=100;
    if(delayNumber<0)delayNumber=0;
   
  }
  else
  {
    delayNumber+=100;
    if(delayNumber>65000) delayNumber=65000;
  }
}

Robo-T

#6
Apr 02, 2017, 10:50 pm Last Edit: Apr 02, 2017, 11:32 pm by Robo-T
What about just adding a simple while loop whenever you want to clear the buffer?
while(Serial.available()){Serial.read();}
In my design, there shouldn't be any data in the buffer, but I include this just in case there was some old data that didn't get read for some reason.


multimedia

all that is so complicated and abit long winded, why cant it just be serial.clear, or if (serial.read = "clear" ) {
} serial.clear}
or something along them lines! omfg!

Delta_G

all that is so complicated and abit long winded, why cant it just be serial.clear, or if (serial.read = "clear" ) {
} serial.clear}
or something along them lines! omfg!
Because throwing away serial data without any regard for what is there is generally considered "stupid as f&%k" and the people who wrote the language weren't generally that stupid.  If you're throwing away random amounts of data you should at least be looking for valid packet start markers while you do it.  But really, if you find yourself just blasting through throwing much away then you probably have a seriously flawed program or protocol and you should really fix that first. 
If at first you don't succeed, up - home - sudo - enter.

Delta_G

If you know what's in there then you can just read it out.  Why should there be a second function to do the same thing? 
If at first you don't succeed, up - home - sudo - enter.

multimedia

To output to an lcd :)and then to enter more data you overload buffer therefore i need the serial buffer cleared b4 putting a new string in.

Delta_G

To output to an lcd :)and then to enter more data you overload buffer therefore i need the serial buffer cleared b4 putting a new string in.
That doesn't make any sense without context.  Writing to the LCD and receiving serial have nothing to do with one another. 
If at first you don't succeed, up - home - sudo - enter.

multimedia

Its a project im working on dw about it. how can i clear it with a small command not a 10 mile long bit of bloated code, who can help without asking 20 questions as im in a poker tour right now!

Delta_G

#13
Aug 11, 2017, 12:49 am Last Edit: Aug 11, 2017, 12:50 am by Delta_G
Its a project im working on dw about it. how can i clear it with a small command not a 10 mile long bit of bloated code, who can help without asking 20 questions as im in a poker tour right now!
A while loop.  Write the function you want.  Fix your design so you don't have to.  There's three options that don't require 10 miles of code. 
If at first you don't succeed, up - home - sudo - enter.

multimedia

Im a noob at c so all I see is long code and that's all i have to work with, please give me a really basic example, the serial buffer normally gets outputted to the lcd screen, but the lcd is 16, 1 and fills up and stops any more input and even puts in strange chars if i try to add more in so maybe i need to use the lcd.clear but then it might just fill with the serial buffer again and it wont achieve any clearing, so i think clearing the serial buffer aswell as lcd.clear will work a lot better but have no idea what the code to do that is.

Go Up