How can I make sure that the Serial UART is clean

Dear All,

First I am goinf to explain my code. It's not big. (It's a sample of my code)

// Reading String
char buffer[200];
char inChar;
int index;

void setup
{
  Serial.println(F("Testing the power status"));

  Serial.println("AT");

  read_String();
}
void loop
{
[Some code]
}

void read_String() 
{
  index=0;
  // Here I clean the buffer variable, it has still value
  buffer[0]='\0';
  while(Serial.available() > 0) // Don't read unless
    // there you know there is data
  {
    if(index < 199) // One less than the size of the array
    {
      inChar = Serial.read(); // Read a character
      buffer[index] = inChar; // Store it
      index++; // Increment where to write next
      buffer[index] = '\0'; // Null terminate the string
    }
  }
Serial.println(buffer);
}

The

Serial.println(buffer);

also print

Testing the power status

before the return value of the AT command.

(I hope I am clear enought)

So my problem. You may have understand it :o)
When I sent an "AT" command, it return me somethink like

OK
Call Ready
Gps Ready

This is good, it also display

Testing the power status
OK
Call Ready
Gps Ready

and I do not want to have the first line. It's like a "dust" :o)

In my raed_String function, it's possible to clean Serial.aailable and/or Serial.read in order to read only what happen the "AT" command?

Do you understand my need or should I provide more information?

Thank a lot

and I do not want to have the first line. It's like a "dust" :o)

The one that this line prints?

  Serial.println(F("Testing the power status"));

The code that you blame for printing that line is not the code actually responsible.

You haven't said what device is at the other end of the Serial connection - I suspect that is rather important.

It looks like that device echoes back whatever you send it so a simple way to stop getting text you don't want is not to send the text in the first place. Or is that just too simple?

...R

Hello,

Here is an exemple with other code. I should have shown this one. But there more code :slight_smile:
Thank for your reading

In powerOnSim908() look at sendAtCommand() then go to sendAtcomand for detail of my problem (my code works in spite of this

(@Paul, I take care to put the { in a line, as you told me)
(I put my comments in CAPITAL)

boolean powerOnSim908(void)
{
  if(debug)
  {
    Serial.println(F("Powering up SIM908"));  
  }
  boolean turnedON = false;
  //uint8_t answer=0;
  int cont;
   	
  for (cont=0; cont<3; cont++)
  {
    digitalWrite(pin_power,HIGH);
    delay(1500);
    digitalWrite(pin_power,LOW);

    // PAY ATTENTION AT THIS LINE REGARDING MY WORRY
    Serial.println(F("Testing if modeule is up"));

    if(sendATcommand("AT", "OK", 5000))          // GO TO HAVE A LOOK AT sendAtCommand() NOW
    {
	cont = 4; // Leave the loop
	turnedON = true;
    }
    else
    {
      turnedON = false;
      if(debug)
      {
	Serial.println(F("\nTrying agin to turn on SIM908"));  
      }
    };
  }
	
  if(turnedON)
  {
    if(debug)
    {
      Serial.println(F("Module is tunrned on\n"));
    }
  }
  else
  {
      if(debug)
      {
	Serial.println(F("Module is NOT tunrned ON\n"));  
      }
   }	
    return turnedON;
}

The sendAtCommande wait 5sec for an OK. This work.
Now look at my sendAtCommand(). I change it a little bit to store a buffer the return value. Look...

boolean sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout)
{
    uint8_t x=0;
    bool answer=false;
    //buffer[0]='\0';
    unsigned long previous;

    //memset(buffer, '\0', 100);    // Initialice the string
    
    delay(100);

    // I DO NOT UNDERTSAND THAT LINE, BECAUSE APPERENTLY IS NOT CLEENING THE INPUT BUFFER, SEE BELOW   
    while( Serial.available() > 0) Serial.read();    // Clean the input buffer
 	
    if (ATcommand[0] != '\0')
    { 
       // HERE IS SEND THE AT COMMAND
        Serial.println(ATcommand);    // Send the AT command   
    }

    x = 0;
    previous = millis();
    
    index=0;

    do
    {
     // FROM THAT POINT THE BUFFER SHOULD BE CLEAN AND PRINT THE RESULT OF THE "AT" COMMAND
      if(Serial.available() > 0) 
      {
        if(index < 199) // One less than the size of the array // Same as buffer size
        {
          // HERE, IT STORE THE SERIAL DATAS GENERATE BY THE AT COMMAND INTO buffer
          inChar = Serial.read(); // Read a character
          buffer[index] = inChar; // Store it
          index++; // Increment where to write next
          //Serial.println(index);
          buffer[index] = '\0'; // Null terminate the string
        }
      }
    }while(((millis() - previous) < timeout));


    if(strstr(buffer,"NORMAL POWER DOWN") != NULL)
    {
       answer = false;
    }
    else if (strstr(buffer, expected_answer) != NULL)    // check if the desired answer (OK) is in the response of the module
    {
      // HERE I PRINT THE BUFFER CONTENT.
      Serial.println(F("### BUFFER")); 
      Serial.println(buffer);
      Serial.println(F("### END BUFFER"));
       answer = true;
    }
    else
    {
      answer = false;
    }	
            	  	
	if(debug)
        {
    	  if(answer)
      	  {
    		//Serial.println(F("Expected answer : OK!\n"));
    	  }
    	  else
    	  {
    		//Serial.println(F("Expected answer : KO!\n"));
    	  };
  }   	
  // IT RETUR TRUE OF IT GET "OK" OR FALSE IF NOT
  return answer;
}

While the powerOnSim908 is lunched,
This is printed : 'Testing if modeule is up'
the AT is sent.
BUT In my terminal, Serila.println(buffer) print me that:

BUFFER

Testing if modeule is up
AT

OK

Call Ready

GPS Ready

END BUFFER

It includes 'Testing if modeule is up' and it should not. It why I am asking if that part of code

while( Serial.available() > 0) Serial.read();    // Clean the input buffer

it really cleaning the buffer

Serial.println(buffer) should print

AT

OK

Call Ready

GPS Ready

and not

Testing if modeule is up
AT

OK

Call Ready

GPS Ready

Do you see my worries?

Thank a lot

In other word,
how can I make sure that it this step

Serial.available()

or/and

Serial.read()

the Serial is clean with no other previous print, like generated by

Serial.println(F("Testing if modeule is up"));

Many thank for your clarification

pierrot10:
Do you see my worries?

Did you see Reply #2 ?

...R

Hello Robin

I do not understand your question.
I am using arduino uno and TEL051 shield

TEL0051 shield has a SIM908 (GPS and GSM/GPRS)

While I send an AT command I would like to read the data that the AT command return.

For exemple, actually I am working on my issue with

Serial.println("AT+CGPSSTATUS?");

I would like to print in a buffer, all that command return, excluding all "dust" that can genereate a Serial.println, that I am using for debuging.

Did I answer to your qestion?

pierrot10:
I do not understand your question.
I am using arduino uno and TEL051 shield

This is the first time that important piece of information has been provided.

Now can you post links to the datasheet for the TEL051 and the datasheet for the SIM908 (GPS and GSM/GPRS)

Your objective should be to ensure we have as much information about your project as you have.

...R

Dear Robin,

Thank for coming back. I am going to close that topic.
In fact I discovered there is always data "runing" in serial.

I red the datasheet of At command Sim908 and it state

The "AT" or "at" prefix must be set at the beginning of each Command line. To terminate a
Command line enter <CR>.
Commands are usually followed by a response
that includes. "<CR><LF><response><CR><LF>"
Throughout this document, only the responses are presented, <CR><LF> are omitted
intentionally.

Then now, I am concentrated n how to make into a buffer . I am going to open a new post
http://forum.arduino.cc/index.php?topic=266073.msg1876089#msg1876089
with my investigation with all infrmation, as you mentionned.

Thank a lot