So, I hava a question about Comparing Strings.
I know in the IDE have some example, but I do, but,
I will write down the question at below.
In the a SIM module, use the "AT" request some command.
When request, the Mudle will return some information.(betwen --------- are the information send and return)
Send:AT+CGATT?
Return:+CGATT: 1
OK
I will judge the return information YES or NO.
I use receive the information returned. it is ok, the information can be receive all.
But when I want to Comparing it. I do know how to solution it.
Because the information inculde some '\r' "\n" in it.
How can I make a Srting that can use be Comparing with return information.
GPSSerial.print("AT+CGATT?");
if (GPSSerial.readString() == "the String") {
Serial.println("CNMP = 13 ok");
}else{
Serial.println("Command has some misstake, you need do it again"); }
don't use the String class, stick to cString (null terminated char arrays). Using the String class might appear to make your life easy initially but on the long run it might poke hole in your SRAM and your code will crash and it will hard to debug.
Learn to deal with asynchronous input in an asynchronous way. You send a command to your device and expert and answer on Serial, then let the loop build up that answer and once received use the functions you learnt about in #2 (strstr or strcmp or strtok for example) to parse / check what you received back. A good tutorial to follow to deal with Serial input is Serial Input Basics
don't use the String class, stick to cString (null terminated char arrays). Using the String class might appear to make your life easy initially but on the long run it might poke hole in your SRAM and your code will crash and it will hard to debug.
Learn to deal with asynchronous input in an asynchronous way. You send a command to your device and expert and answer on Serial, then let the loop build up that answer and once received use the functions you learnt about in #2 (strstr or strcmp or strtok for example) to parse / check what you received back. A good tutorial to follow to deal with Serial input is Serial Input Basics
But, I do not know still that hwo to Comparing the two strings.
forexample when in send AT+CGATT? commadn to the module, it will return two different infromation
one: (the module work right)
+CGATT: 1
OK
two:(the module has some problem)
+CGATT: 0
OK
so, i will compareit the receive information with the a srting, but i do not how make the string.
like: +CGATT: 0\r\nOK or +CGATT: 0\rOK or +CGATT: 0\nOK or +CGATT: 0\r\nOK\r\n …………
you create a char array buffer (cString) in which you stuff all the answer from your device until you have received 'OK\r\n' and then you parse the answer checking if you received a +CGATT: and if yes then you can extract the 0 or 1 that is after that (for example)
or you create a char array buffer (cString) in which you stuff all the answer from your device until you have received '\n' (just a line) and you parse the answer checking if you received a +CGATT: and if yes then you can extract the 0 or 1 that is after that (for example) - and ignore all the other answer
challenge with the second approach (which is simpler) is that you don't know when you parse if the OK has been received or not
Note: you can use the [url=http://www.cplusplus.com/reference/cstring/strstr/]strstr()[/url] function to see if a cstring is within another one (this way you don't care where really it is in the answer of if you have \n or \r, you just need to know it's there and bonus point the function returns a pointer to the first occurrence of the string your are searching for in the first string so you know where to go to extract the 0 or 1 that will be after the +CGATT:
here is a quick example for testing
const byte maxBufferSize = 100;
char receivedBuffer[maxBufferSize + 1];
const char * searchedPattern1 = "+HELLO:";
const char * searchedPattern2 = "+CGATT:";
void setup() {
Serial.begin(115200);
strcpy (receivedBuffer, "some garbage here\r\n+CGATT: 33\r\n\r\nOK\r\nsome more garbage\r\n"); // simulate reading from your device
Serial.println(F("I've received :\n---------------------"));
Serial.print(receivedBuffer);
Serial.println(F("---------------------"));
char * pointer1 = strstr(receivedBuffer, searchedPattern1); // http://www.cplusplus.com/reference/cstring/strstr/
if (pointer1) {
int returnedValue = atoi(pointer1 + strlen(searchedPattern1) + 1); // the number is at the end of that string, http://www.cplusplus.com/reference/cstdlib/atoi/
Serial.print(F("I've found "));
Serial.print(searchedPattern1);
Serial.print(F(". The returned value is: "));
Serial.println(returnedValue);
} else {
Serial.print(F("Could not find "));
Serial.println(searchedPattern1);
}
Serial.println(F("---------------------"));
char * pointer2 = strstr(receivedBuffer, searchedPattern2); // http://www.cplusplus.com/reference/cstring/strstr/
if (pointer2) {
int returnedValue = atoi(pointer2 + strlen(searchedPattern2) + 1); // the number is at the end of that string, http://www.cplusplus.com/reference/cstdlib/atoi/
Serial.print(F("I've found "));
Serial.print(searchedPattern2);
Serial.print(F(". The returned value is: "));
Serial.println(returnedValue);
} else {
Serial.print(F("Could not find "));
Serial.println(searchedPattern2);
}
Serial.println(F("---------------------"));
}
void loop() {}
if you run this you'll see in the Serial monitor
[sub][color=purple]I've received :
---------------------
some garbage here
[color=red]+CGATT:[/color] [b][color=green]33[/color][/b]
OK
some more garbage
---------------------
Could not find +HELLO:
---------------------
I've found [color=red]+CGATT:[/color]. The returned value is: [b][color=green]33[/color][/b]
---------------------
[/color][/sub]
so it does find your key phrase within the buffer and can extract the data (33 here, in your case would be 0 or 1) into a variable
combine that with the Serial Input Basics tutorial and you've a starting point for what you want to achieve