How to flush serial.read() receiver buffer,

Hello,

I have been having this problem trying to clear the serial receive buffer. I know there was a function called flush in the older Arduino version but not in the new one. I am using 1.0.4 and I can not figure out how to keep extraneous characters out of the buffer I an reading the string to. I do a Serial.available() and get the number of characters there and proceed to read them all only keeping the first part of the string into the local buffer variable. The problem is that on my next read I see characters from the previous Serial.read.Is there a good way to clear the Serial.read() buffer?

StanK

This is a frequently asked question. Clearing the input buffer is simple to do, but it is never a good design choice.

I can not figure out how to keep extraneous characters out of the buffer I an reading the string to.

There are two approaches that work: 1. Don't send them. Or, 2. Consume them properly as they are sent.

Needing to clear the buffer is a sure sign of a design issue elsewhere, which we can help you with if you post your code (in code tags, please).

Good luck with your project,

-br

I am using 1.0.4 and I can not figure out how to keep extraneous characters out of the buffer I an reading the string to.

Then, throwing away random amounts of unread data is not going to help.

You need to provide a lot more detail on your problem - what is sending the data, how much, how often, what code is reading the data, what "extraneous characters" are sneaking in, etc.

Hello, To start with I am using a GMS/GPRS module by comsat with the SIM900 chip. The library is modified to use the GSM Shield,
developed by www.open-electronics.org
(Arduino GSM shield - Open Electronics - Open Electronics)
and based on SIM900 chip,
with the same commands of Arduino Shield,
based on QuectelM10 chip. I am using a UNO. Serial rate is 9600 baud,

I want to read 2 strings from a terminal for the phone number and the message to send a text message. I want to read up to a 16 charter length string for the phone number, save it to a buffer and then read up to a 60 char string for the message. If the user types a phone number string longer than the buffer I need to clear any characters that may still be unread or the next string to read, message sees left over character from the phone number string and stores them in the phone message to send buffer. I read all the characters and save only the first part determined by the string length assignment operator.I have added some delay to allow the data to show up and that seems to help.

 if  (phoneNumberGood == false)
 { 
    Serial.print("To send message, Type Phone number of sendee, "); 
   while (Serial.available() == 0)  {delay(500);}
    byte character_count = Serial.available();    
    char  phone_number_to_call[17] = "";
    byte i = 0;
    while (character_count > 0) 
    {
     charRead = Serial.read();
     character_count = character_count - 1;
      if (i < 16)
      { 
        phone_number_to_call[i] = charRead;
        i = i + 1;
      } 
    }
    phoneNumberGood = true; 
    Serial.println(phone_number_to_call);
   
  }
if (phoneNumberGood == true)
  {
    Serial.println("Type message to send, 60 characters maximum, "); 
    while(Serial.available() == 0){delay(500);}    
    byte character_count = Serial.available();
    char  phone_message_to_send[61] = "";
    byte i = 0;
    while (character_count > 0) 
    {
   charRead = Serial.read();
      character_count = character_count - 1;
     if (i < 60)
      { 
        phone_message_to_send[i] = charRead;
        i = i + 1;
      }
    }   
  phoneNumberGood = false; 
 Serial.println(phone_message_to_send);  
  
  }

Right now the code loops and asks for the same information again. I hope this helps

StanK

If the user types a phone number string longer than the buffer I need to clear any characters that may still be unread or the next string to read, message sees left over character from the phone number string and stores them in the phone message to send buffer.

And, if the user types less than 16 characters? Aren't you then going to use part of the message as the phone number?

If there is some delimiter that defines the end of the phone number, you should be reading, and storing or not, the data until that delimiter arrives, regardless of how many characters that is.

Hi PaulS,

I think I have a handle on the problem but it still seems weird to me. I am sure it is operator error. I will use Serial.readBytesUntil() and I think that will fix it. I appreciate your feed back.

StanK