what's the difference with Serial.flush() and...

Dear All,

I have a doubt.

What's the difference between

_cell.flush()

and

while(_cell.available() > 0) _cell.read();

The second clean _cell,

but ehat does the first? It cleans as well?

The first flushes the output buffer, the second flushes the input buffer.

Ok, thank for your answer.
I am sorry, :confused: but what the difference between the input and the output.

I tell you why my stupid question.
I am using openlog. When run the append command, I always have 3 specials caracters.
Exemple of the 3 special caracters and text pasted in a word document
I eart that thoses 3 special caracter are heximal caracter.

With Openlog, to return to the command mode, you have to do

_openlog.print(26); // 26 is an exa
_openlog.print(26);
_openlog.print(26);

Then , I think my 3 special caracters have a relation with .openlog.print(26).

It's the reason why, I though about cleaning _openlog. But following your answer, I do not know if I have to clean the input or the output.

So, if I have to use

_openlog.flush()

or

while(_openlog.available() > 0) _openlog.read();

I already tried

_openlog.print(26); // 26 is an exa
_openlog.print(26);
_openlog.print(26);
_openlog.flush()

but it did not help.

FYI: Here is my append function

uint8_t WI968C::sd_append(char * fileName, char * string, bool isNewFile)
{
  uint8_t answer = 0;

  //delay(5000);

  #ifdef SD_ACTIVE
    if(isSDok)
    {
      #ifdef DEBUG
          sprint(F("\nSD APPEND "));
          sprint(fileName);
          sprint(F(" "));
          sprintln(string);
      #endif

      _openlog.listen();
      delay(100);
      if (_openlog.isListening())
      {
    
        if(sd_gotoCommandMode())
        {

          // APPEND TO THE FILE GPSLOG
          _openlog.print(F("append "));
          _openlog.print(fileName); 
          //_openlog.print('\r');
          _openlog.write(13);
          
          unsigned int timeout = 3000; //10000
          unsigned long previous;
          previous = millis();

          do{
            if(_openlog.available())
            {
              if(_openlog.read() == '<')
              {
                answer = 1;
                isCommandMode = false;
              }
            }
          }while((answer <= 0) && ((millis() - previous) < timeout));

          if(answer == 1)
          {
            //isCommandMode = false;
            if(strlen(string) > 0)
            {
              // Write the fix to the file
              _openlog.print("{");
              _openlog.print(string);
              _openlog.print("}");
              _openlog.write(13);
            }
            else
            {
              //_openlog.write(13);
              _openlog.print('\r');
            }
            //Return to command mode
            sd_gotoCommandMode();
//          _openlog.write(26);
//          _openlog.write(26);
//          _openlog.write(26);

            #ifdef DEBUG_SD
             
                sprint(F("SD Append\t\t"));
                sprintln(OK_RESPONSE);
      
            #endif

          } //end if answer
          else
          {
            #ifdef DEBUG_SD
    
                  sprint(F("SD Append\t\t"));
                  sprintln(KO_RESPONSE);
                
            #endif

            isCommandMode = true;

          } // end if answer

        } // sd_gotoCommandMode
        else
        {
          answer = -51;
        }

        // Listen _cell
        do
        {
          _cell.listen();
          #ifdef DEBUG_SD
            sprintln(SD_LISTING_CELL);
          #endif
          delay(100);
        }while(!_cell.isListening());

      } // end listing
      else
      {
        #ifdef DEBUG
          sprintln(SD_ERR_LISTING_OPENLOG);
        #endif
        //isCommandMode = false;
        answer = -52;
      } // end listening
    
      //return answer;
    }
    else
    {
      #ifdef DEBUG
        sprintln(SD_KO);
      #endif
      answer = -53;
    }

  #else
    #ifdef DEBUG
      sprintln(SD_UNACTIVE);
    #endif
    answer = -54;
  #endif

  return answer;
  }

Stuff being sent TO the Arduino goes in the input buffer. Stuff being sent FROM the Arduino goes in the output buffer. The flush() method blocks, waiting for the outgoing buffer to be empty. Typically it is only used if the Arduino needs to send data before going to sleep.

Why would you print() 26 to the OpenLog device? It might make sense to write() 26 to the device.

Hello Pauls,

Excuse me, it's a error.
My code is not with print(26) but write(26)

pierrot10:
Ok, thank for your answer.
I am sorry, :confused: but what the difference between the input and the output.

I tell you why my stupid question.
I am using openlog. When run the append command, I always have 3 specials caracters.
Exemple of the 3 special caracters and text pasted in a word document
I eart that thoses 3 special caracter are heximal caracter.

With Openlog, to return to the command mode, you have to do

_openlog.write(26); // 26 is an exa

_openlog.write(26);
_openlog.write(26);




Then , I think my 3 special caracters have a relation with .openlog.write(26).

It's the reason why, I though about cleaning _openlog. But following your answer, I do not know if I have to clean the input or the output.

So, if I have to use


_openlog.flush()



or


while(_openlog.available() > 0) _openlog.read();




I already tried


_openlog.write(26); // 26 is an exa
_openlog.write(26);
_openlog.write(26);
_openlog.flush()



but it did not help.

FYI: Here is my append function


uint8_t WI968C::sd_append(char * fileName, char * string, bool isNewFile)
{
  uint8_t answer = 0;

//delay(5000);

#ifdef SD_ACTIVE
    if(isSDok)
    {
      #ifdef DEBUG
          sprint(F("\nSD APPEND "));
          sprint(fileName);
          sprint(F(" "));
          sprintln(string);
      #endif

_openlog.listen();
      delay(100);
      if (_openlog.isListening())
      {
   
        if(sd_gotoCommandMode())
        {

// APPEND TO THE FILE GPSLOG
          _openlog.print(F("append "));
          _openlog.print(fileName);
          //_openlog.print('\r');
          _openlog.write(13);
         
          unsigned int timeout = 3000; //10000
          unsigned long previous;
          previous = millis();

do{
            if(_openlog.available())
            {
              if(_openlog.read() == '<')
              {
                answer = 1;
                isCommandMode = false;
              }
            }
          }while((answer <= 0) && ((millis() - previous) < timeout));

if(answer == 1)
          {
            //isCommandMode = false;
            if(strlen(string) > 0)
            {
              // Write the fix to the file
              _openlog.print("{");
              _openlog.print(string);
              _openlog.print("}");
              _openlog.write(13);
            }
            else
            {
              //_openlog.write(13);
              _openlog.print('\r');
            }
            //Return to command mode
            sd_gotoCommandMode();
//          _openlog.write(26);
//          _openlog.write(26);
//          _openlog.write(26);

#ifdef DEBUG_SD
           
                sprint(F("SD Append\t\t"));
                sprintln(OK_RESPONSE);
     
            #endif

} //end if answer
          else
          {
            #ifdef DEBUG_SD
   
                  sprint(F("SD Append\t\t"));
                  sprintln(KO_RESPONSE);
               
            #endif

isCommandMode = true;

} // end if answer

} // sd_gotoCommandMode
        else
        {
          answer = -51;
        }

// Listen _cell
        do
        {
          _cell.listen();
          #ifdef DEBUG_SD
            sprintln(SD_LISTING_CELL);
          #endif
          delay(100);
        }while(!_cell.isListening());

} // end listing
      else
      {
        #ifdef DEBUG
          sprintln(SD_ERR_LISTING_OPENLOG);
        #endif
        //isCommandMode = false;
        answer = -52;
      } // end listening
   
      //return answer;
    }
    else
    {
      #ifdef DEBUG
        sprintln(SD_KO);
      #endif
      answer = -53;
    }

#else
    #ifdef DEBUG
      sprintln(SD_UNACTIVE);
    #endif
    answer = -54;
  #endif

return answer;
  }