Arduino Forum

Topics => Education and Teaching => Topic started by: aditya123 on Apr 18, 2014, 02:25 pm

Title: Clearing serial buffer solved
Post by: aditya123 on Apr 18, 2014, 02:25 pm
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
Title: Re: Clearing serial buffer solved
Post by: Riva on Apr 18, 2014, 02:46 pm
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();
  }
}   
Title: Re: Clearing serial buffer solved
Post by: retrolefty on Apr 18, 2014, 03:16 pm
  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
Title: Re: Clearing serial buffer solved
Post by: arduinoenigma on May 10, 2015, 12:49 am
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 (http://arduinoenigma.blogspot.com)
Title: Re: Clearing serial buffer solved
Post by: GoForSmoke on May 10, 2015, 03:44 am
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.
Title: Re: Clearing serial buffer solved
Post by: ORPARGA on Jan 27, 2016, 09:50 pm
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;
  }
}
Title: Re: Clearing serial buffer solved
Post by: Robo-T on Apr 02, 2017, 10:50 pm
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.

Title: Re: Clearing serial buffer solved
Post by: multimedia on Aug 10, 2017, 01:09 am
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!
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Aug 10, 2017, 04:52 am
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. 
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Aug 10, 2017, 07:50 pm
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? 
Title: Re: Clearing serial buffer solved
Post by: multimedia on Aug 10, 2017, 08:04 pm
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.
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Aug 10, 2017, 11:07 pm
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. 
Title: Re: Clearing serial buffer solved
Post by: multimedia on Aug 11, 2017, 12:41 am
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!
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Aug 11, 2017, 12:49 am
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. 
Title: Re: Clearing serial buffer solved
Post by: multimedia on Aug 11, 2017, 12:55 am
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.
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Aug 11, 2017, 01:06 am
Im a noob at c
Not me, I'm pretty damned good at this if I must say so. 


please give me a really basic example,
After you called me a cunt earlier?  I see you took it down, but I saw it.  Maybe a sorry would help. 

the serial buffer normally gets outputted to the lcd screen,
No, not normally.  Normally the two have nothing to do with one another.  Nothing gets sent to the lcd unless you send it there in your code.  That's why I asked for context.  Are you talking about a serial lcd?  Are you talking about the send buffer or receive buffer? 


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 don't send so much to the lcd.  You have control over what you send from your code.

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.
I have no idea what that code is either because I know practically zero about your project or which components you have or the code you are using.  If you want help, then you'll have to be forthcoming about those details. 

And don't call people ugly names. 



Title: Re: Clearing serial buffer solved
Post by: multimedia on Aug 11, 2017, 01:12 am
i dam well just told you lcd 16,2 standard arduino kit screen thats it and esp 8266
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Aug 11, 2017, 01:18 am
There are many kinds of LCD.  Which one do you have?  How does the board communicate with it?  Serial?  I2C?  SPI?  Parallel?

I'm still not clear on the connection between the serial buffer and the LCD in your program.  That's something else you should explain.

So we know you have some kind of LCD and an eap8266 and that's all you've given me.  And I'm supposed to know what code you need to do some as yet unnamed thing just from that?  Are you serious? 

Or you can just keep being combative and get nowhere.  I'm stuck at an airport so I honestly have nothing better to do. So I really don't care.
Title: Re: Clearing serial buffer solved
Post by: multimedia on Aug 11, 2017, 01:22 am
omfg, soooooooo many questions and they are dumb ones! its a standard means theres no model number no make nothing just google standard 16,2 arduino lcd kit and it dont matter they are all same except mine has i2c board again a standard thing look for the most common i have that, simples! h/w dont even matter its a software thing!
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Aug 11, 2017, 01:37 am
Hardware determines how you write the software. 

Sorry, but all lcds are not the same.  They really aren't.  And there's no such thing as a "standard" one.

But that's not the really big question. The real question is about what the code is doing with this serial data and what's being put on the LCD.

I'm not being obtuse. I honestly don't know what it is that you're trying to do with this code.  I can tell you I've never had a problem writing stuff to any LCD. 

But hey, if you don't want to answer these things then don't. I can tell you that you won't get a solution that way but that's your prerogative.

Actually, come to think of it I'm pretty much done with this.  You're combative, don't want to answer simple questions, and you called me an ugly name.  So I'm out. Maybe if you post some details someone else will help you. But as for me I'm pretty much done with you.  A slightly different tact on your end and you'd have working code by now. But you're just a waste of time I think.  Good luck figuring it out.
Title: Re: Clearing serial buffer solved
Post by: multimedia on Aug 11, 2017, 02:16 am
no the ide compiles depending on options! just give me code and i will test :) plz
Title: Re: Clearing serial buffer solved
Post by: NischalSehrawat on May 22, 2018, 11:01 pm
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();
  }
}   

Hi, Can you please explain how will this line of code empty the serial buffer?
Title: Re: Clearing serial buffer solved
Post by: zoomx on May 23, 2018, 12:03 pm
void serialFlush(){
  while(Serial.available() > 0) {   //while there are characters in the serial buffer, because Serial.available is >0
    char t = Serial.read();         // get one character
  }




Title: Re: Clearing serial buffer solved
Post by: multimedia on Jun 06, 2018, 11:59 pm
I basically made an lcd display that uses a file / path requests from a form all to save having to use sd card for displaying things but want to be able to rewrite it and the program know if new input detected if yes then change sort of thing but im novice and not got a clue the best way to do this, it sort of works but not stangle due to after a long while it clears itself anyone help?
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Jun 07, 2018, 12:49 am
What's your native language OP?
Title: Re: Clearing serial buffer solved
Post by: multimedia on Jun 07, 2018, 11:48 am
irrelevent stick to the topic!
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Jun 07, 2018, 03:09 pm
irrelevent stick to the topic!
Relevant, beccause you aren't making much sense when you type and I thought if you are not a native English speaker I would help you find the forum where they speak your language. 

If English is your native language then please take a little more care when you type to make something that is readable by others.  Incude maybe some punctuation or something.  You're asking a bunch of professionals for help, you're not typing a text message to your boyfriend.  Try to look at least mildly intelligent if you want anything other than the dregs to try to help you.
Title: Re: Clearing serial buffer solved
Post by: GolamMostafa on Jun 10, 2018, 02:20 am
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 , [...].
Unfortunately, I could never figure out the meaning/significance ofemptying the serial buffer.

A: Emptying the Serial Buffer of the Receiver Section
Data comes to the receiver one character at a time. When the character arrives, it is immediately read upon and is saved in an user defined array. If so, how a serial buffer can get full and such we need to empty/flush it after reading the data. It is only one byte long buffer as I understand. If we don't read the character that has  just arrived, there is a possibility that the character byte might be overwritten by the arrival of the next character. Therefore, the speed of data read must be higher than the speed (the Bd) at which data arrives.

B:  Serial Buffer of the Transmit Section.
In UART Protocol, one character is transmitted at a time. If so, the execution of the command Serial.print("Arduino"); requires that there should be an intermediate buffer to hold the characters of the message Arduino. As I understand, the transmission of Serial.print("Arduino"); is composed of the following 7 sub transmissions:
Code: [Select]
Serial.print('A');
Serial.print(r');
Serial.print('d');
Serial.print('u');
Serial.print('i');
Serial.print('n');
Serial.print('o');

 

How much (in byte) is the length of the serial buffer of the Transmit Section?
 
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Jun 10, 2018, 02:37 am
Both buffers are 64 bytes on most boards. 


It's defined in HardwareSerial.h

Code: [Select]
#if !defined(SERIAL_TX_BUFFER_SIZE)
#if ((RAMEND - RAMSTART) < 1023)
#define SERIAL_TX_BUFFER_SIZE 16
#else
#define SERIAL_TX_BUFFER_SIZE 64
#endif
#endif
#if !defined(SERIAL_RX_BUFFER_SIZE)
#if ((RAMEND - RAMSTART) < 1023)
#define SERIAL_RX_BUFFER_SIZE 16
#else
#define SERIAL_RX_BUFFER_SIZE 64
#endif
#endif
#if (SERIAL_TX_BUFFER_SIZE>256)
typedef uint16_t tx_buffer_index_t;
#else
typedef uint8_t tx_buffer_index_t;
#endif
#if  (SERIAL_RX_BUFFER_SIZE>256)
typedef uint16_t rx_buffer_index_t;
#else
typedef uint8_t rx_buffer_index_t;
#endif
Title: Re: Clearing serial buffer solved
Post by: GolamMostafa on Jun 10, 2018, 03:00 am
Delta_G, it is just an academic query:

If the buffer size of the Transmit Section is 64 byte, only 64 characters of the following message should be transferred and the remaining characters are either lost or queued somewhere. Practically, it is observed that the whole text has appeared on the Se rail Monitor.

Code: [Select]
Serial.println("//put your setup code here, // put your setup code here, to run once: // put your setup code here, to run once: // put your setup code here, to run once:");
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Jun 10, 2018, 03:38 am
Yes, print will block if the buffer is full.  cant post the code from print.h right now but it's there. 

This is the reason Serial print from an interrupt is so dangerous.  Nothing leaves the buffer because the interrupts are off and print deadlocks waiting for room in the buffer. 
Title: Re: Clearing serial buffer solved
Post by: pepelevamp on Nov 23, 2018, 03:48 pm
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. 
I'm gonna call you on this. You shouldn't be calling scenarios like that stupid, making him feel bad.

I present a simple counter-example:
Random data which made its way through 8N1 encoding when your RX receiver was listening to data on a bus intended for another receiver.

This is the case in my project, and I'm not doing something 'stupid'. I'm using a single wire for both RX and TX, and when in RX, the baud rate is different from the TX baud rate.  Some times you need to send out a command to change TX properties and reinitialize your receiver, clearing prior data you know is junk.

I too was looking for a simple method to clear the input buffer aside from a while loop.
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Nov 23, 2018, 06:04 pm
Quote
Random data which made its way through 8N1 encoding when your RX receiver was listening to data on a bus intended for another receiver.
If you are throwing out the whole buffer, how do you know where the random data ends and the good data starts?  I'll say it again, throwing away data without knowing what it is that you are throwing away is foolish.  I'm sure you'll find some corner case where it makes sense to you, but it is still foolish from a design perspective.

If your receiver is picking up random garbage from some other bus that it wasn't supposed to be listening to then you have a design issue, not a need to throw away data. 
Title: Re: Clearing serial buffer solved
Post by: multimedia on Nov 23, 2018, 06:25 pm
In my case the lcd screen only holds x number of chars, so all good i wana be able to clear it to then print new input when thats the case but not if no new input comes in.
Title: Re: Clearing serial buffer solved
Post by: pepelevamp on Nov 23, 2018, 09:40 pm
If you are throwing out the whole buffer, how do you know where the random data ends and the good data starts?  I'll say it again, throwing away data without knowing what it is that you are throwing away is foolish.  I'm sure you'll find some corner case where it makes sense to you, but it is still foolish from a design perspective.

If your receiver is picking up random garbage from some other bus that it wasn't supposed to be listening to then you have a design issue, not a need to throw away data. 
If the given design dictates that you know its all garbage, then its not foolish from a design perspective. And there's no 'design issue' - theres imposed constraints.

Don't be so righteous - there is a world of scenarios out there you haven't heard of and likely won't. You're not going to win this one - just take it on the nose that you shouldn't tell people they're foolish for their choices when you have no backstory.

There is a million scenarios where you are listening to garbage but you can TX out a command to clear it. Radio encounters jokes like this all the time. Be helpful.

Title: Re: Clearing serial buffer solved
Post by: Delta_G on Nov 23, 2018, 09:50 pm
whatever kid. 
It's just like the mailbox.  If you throw away without knowing why it is then how you know it wasn't a big check from Ed McMahon?   

If you know it is garbage then that isn't throwing it away randomly.  But the question becomes how do you know it is garbage?   

And yes, if you're having this problem then show me the design and I can fix it so you don't need to do something like this.  If you do it right you don't have to throw away data you don't know what it is.  If you find yourself with random garbage data then there is a better way to do whatever you are doing. 


If you work for me and come to me with a design where you don't know where signals are coming from and can't control things then you'll probably be in the unemployment line real soon.   And I'll get someone who knows what they're doing to rewrite it so that things are under control and we know where our signals are coming from. 

Title: Re: Clearing serial buffer solved
Post by: pepelevamp on Nov 23, 2018, 09:57 pm
oh yeah big man the employer.

You've just been caught out beating on a newbie and giving him bad advice. All that matters is that you should be helpful and learn that you're wrong somewhere.

It never occurred to you that there might be a side-channel which indicates when data is garbage or understandable. It never occurred to you that the flow control mechanism might actually make it deterministic so you can infer the entire buffer is garbage.

You just didn't think of these things. You just assumed it must be wrong, and because of some principal you know of, it MUST be other people's designs - not your rule of thumb yer sticking to.

You need to stop beating your chest and allow people to teach you things.

Title: Re: Clearing serial buffer solved
Post by: Delta_G on Nov 23, 2018, 10:11 pm
If you've got a side channel that tells you the data is garbage then why are you putting it in the buffer?  Why aren't you tossing that data as it arrives?   You're wasting resources.  I'll get someone competent to write this.  Better luck next time. 
Title: Re: Clearing serial buffer solved
Post by: multimedia on Nov 23, 2018, 10:11 pm
Ok its for an lcd display using i2c and i dont wish to use any sd card with that in mind heres the code...


https://forum.arduino.cc/index.php?topic=494077.0

is the link to my question please post as reply there or here, up to you.
Title: Re: Clearing serial buffer solved
Post by: Delta_G on Nov 23, 2018, 10:25 pm
Your problems there have nothing to do with this thread.  That thread is old as hell.  You didn't post your code right.  Your errors are about really simple stuff like undefined functions.  If that's your level of coding then you really have absolutely no place trying to school me on anything. 

If you still need help on that thread then bump it, add some code tags, and wait for someone you haven't spent the last few posts trying to insult. 
Title: Re: Clearing serial buffer solved
Post by: pepelevamp on Nov 23, 2018, 10:26 pm
If you've got a side channel that tells you the data is garbage then why are you putting it in the buffer?  Why aren't you tossing that data as it arrives?   You're wasting resources.  I'll get someone competent to write this.  Better luck next time.  
Do you know what the problem is here being solved? Or are you telling someone how to solve the problem. You're acting a mix of both. Which is very poor form. 'Wasting resources' aye. Now you're attributing worth to things without any justification because you don't understand the problem/scenario/constraints.

Look at how much work you gotta to do save face now. Much easier to just learn stuff & be kind aint it?