I’m working on an Arduino and String.substring does not appear to be operating correctly, so I’m wondering what I may be doing wrong….
My function is as follows:
boolean processSerial()
{
String buf;
int iter = 0;
char thisChar;
while(iter < 1000){
if (Serial.available()) {
#if ARDUINO >= 100 //For Arduino v1.0+
{
thisChar = Serial.read();
buf += thisChar;
//if(DEBUG){Serial.print(thisChar);}
}
#else //For Arduino v0023 or earlier
thisChar = Serial.read();
#endif
}
iter++;
}
Serial.print(buf);
Serial.print(buf.substring(0,10));
if(buf.substring(1) == "GPGGA"){
Serial.println("FOUND IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
Serial.println("And we're done");
}
The pattern “GPGGA” is coming up but substring is not returning true.
The output from my Serial is as follows:
$GPGGA,053540.000,3804.1237,N,07615.5232,W,1,7,1.39,117.5,M,-33$GPGGA,053And we're done
Thus buf clearly has ‘GPGGA’ in the string. The last little bit that shows ‘GPGGA’ again is the printing of charcters 0-10 in the string. Why is my if statement not returning true?
buf.substring(1)returns the substring starting at position one and continuing all the way to the end of the string. This will start withGPGGA, but it will not equalGPGGA.If you know the string you compare to is 5 characters in length, you could use
Also note that it’s not the
substring()function that is expected to return true, but the comparison operator==.