Reading in serial data and using strcmp

I have a function that reads in the NMEA strings from a GPS module. In order to just focus on the data that I am interested in I read in the first 5 characters and then perform a strcmp to determine if it is the appropriate sentence. If not it ignores the rest of the sentence. Here is the function:

//this just reads data in one character at a time until we have a complete sentence
boolean checkforSentence()
{
  char c;
  while(mySerial.available())
  {
    last_data_received = millis() + 2000;
    c = mySerial.read();
    //Serial.print(c);
    
    if(sentenceBegins && c == '\r') //we have a full sentence
    {
      sentenceBegins = false;
      Serial.println(buffer);
      return true;
    }
    
    if(sentenceBegins) //store characters to buffer
    {
      buffer[index] = c;
      index++;
      buffer[index] = '\0';
      /*
      we need something to segregate out the RMC and GGA sentences and ignore
      everything else.  Originally I was sending a command to the GPS to
      only send the string that I was interested in, but for some reason the GPS
      is not accepting my commands anymore.  It happened pretty suddenly so f'
      it.  I will just ignore everything that I don't want to see.
      */
      if(index==5)
      {
        if(!(strcmp(buffer, "GPGGA") == 0 && data_index==true))
        {
          sentenceBegins = false;
          Serial.println(buffer);
        }
        if(!(strcmp(buffer, "GPRMC") == 0 && data_index == false))
        {
          sentenceBegins = false;
          Serial.println(buffer);
        }
      }
    }
    
    if(c == '

I have a feeling strcmp may be looking for a new line and carriage return character at the end of buffer but doing this:

if(!(strcmp(buffer, 'GPRMC') == 0 && data_index == false))

results in:

invalid conversion from 'int' to 'const char*'

The result of the Serial.println is:

GPGGA
GPGGA
GPGSA
GPGSA
GPGSV
GPGSV
GPRMC
GPGGA
GPGGA
GPGSA
GPGSA
GPGSV
GPGSV
GPRMC

which suggests that it is checking the appropriate number of characters. Any suggestions?) //beginning of sentence...start saving to buffer
    {
      sentenceBegins = true;
      index = 0;
    }
 
  }
  return false;
}


I have a feeling strcmp may be looking for a new line and carriage return character at the end of buffer but doing this:

§DISCOURSE_HOISTED_CODE_1§


results in:

§DISCOURSE_HOISTED_CODE_2§


The result of the Serial.println is:

§DISCOURSE_HOISTED_CODE_3§


which suggests that it is checking the appropriate number of characters. Any suggestions?
 'GPRMC'

BZZZT

I think it needs null terminated strings.

If you just want to compare a few characters maybe the simplest thing would be to write your own function.

...R

AWOL:

 'GPRMC'

BZZZT

I tried:

if(!(strcmp(buffer, 'GPRMC') == 0 && data_index == false))

and got the error message:

invalid conversion from 'int' to 'const char*'

Robin2:
I think it needs null terminated strings.

If you just want to compare a few characters maybe the simplest thing would be to write your own function.

...R

It is null terminated.

if(sentenceBegins) //store characters to buffer
    {
      buffer[index] = c;
      index++;
      buffer[index] = '\0';

Each increment adds the null termination. In this case, the null termination would be at buffer[5] when the strcmp happens.

jerseyguy1996:

AWOL:

 'GPRMC'

BZZZT

I tried:

if(!(strcmp(buffer, 'GPRMC') == 0 && data_index == false))

and got the error message:

invalid conversion from 'int' to 'const char*'

Yes, that's correct.
You can't convert an "int" to any kind of "char" pointer.

Double quotes for strings. You're using single quotes.

I have a feeling strcmp may be looking for a new line and carriage return character at the end of buffer

No, it isn't. Forget about data_index for the moment. Just print the value in buffer and the value returned by strcmp().

It is quite likely that you need to be using strncmp() instead.

Well I managed to figure it out thanks to all of the great replies on this thread! Basically I had set up a situation where at least one of those conditional statements would always evaluate to true and reset my string builder. I changed the function to:

//this just reads data in one character at a time until we have a complete sentence
boolean checkforSentence()
{
  char c;
  while(mySerial.available())
  {
    last_data_received = millis() + 2000;
    c = mySerial.read();
    //Serial.print(c);
    
    if(sentenceBegins && c == '\r') //we have a full sentence
    {
      sentenceBegins = false;
      Serial.println(buffer);
      return true;
    }
    
    if(sentenceBegins) //store characters to buffer
    {
      buffer[index] = c;
      index++;
      buffer[index] = '\0';
      /*
      we need something to segregate out the RMC and GGA sentences and ignore
      everything else.  Originally I was sending a command to the GPS to
      only send the string that I was interested in, but for some reason the GPS
      is not accepting my commands anymore.  It happened pretty suddenly so f'
      it.  I will just ignore everything that I don't want to see.
      */
      if(index==5)
      {
        if(data_index==true)
        {
          if(!(strcmp(buffer, "GPGGA") == 0))
          {
            sentenceBegins = false;
            //Serial.println(buffer);
          }
        }
        
        if(data_index==false)
        {
          if(!(strcmp(buffer, "GPRMC") == 0))
          {
            sentenceBegins = false;
            //Serial.println(buffer);
          }
        }
      }
    }
    
    if(c == '

which works great now!) //beginning of sentence...start saving to buffer
    {
      sentenceBegins = true;
      index = 0;
    }
 
  }
  return false;
}


which works great now!