Get AT command response

I am using an Arduino Uno with a sim900 gps/gprs module and I'm using at commands, how can I get the response of at command (i.e OK, ERROR) so that I can do something if response == "OK" or response == "ERROR"


Solution 1:

If you have not yet read all of chapter 5 in V.250 specification stop reading here and do so immediately, it is an important basis for the rest of this answer, I'll wait until you'll come back. Some of the references to alphabets (short version: ignore this/treat it as ASCII) and S-registers might seem cryptic but don't give up, it will grow on you quickly.


The only correct way to process modem output is to divide the output into complete lines and iterate one full line at the time. This is universal and applies to absolutely all AT commands (with only one single exception that I can think of1).

Let me emphasis that: you should only split modem response text on strict end of line boundaries ("\r\n"), and process the resulting line string in one single operation. So you really should go and implement the read_line_from_modem function I suggested in my previous answer.

This means that whenever you want to check for the OK result code you should only use strcmp(line, "OK\r\n") and not strstr or similar because you know you are processing a complete, full line which should start with the final result code at the very beginning and it will always be followed by "\r\n"2.

Now there are more final result codes than just OK and ERROR, and instead of trying to figure out everything by yourself3 I suggest looking at is_final_result_code or isFinalResponseSuccess as listed in this answer.

Summary

  1. Always read modem output line by line, and use a separate function do the reading, returning/modifying a string/buffer to contain the line.
  2. The first thing you do after having read a line is to check if it is a final result code. Write a separate function that takes the line as a parameter and checks if it is.
  3. If the line was not a final result code, do whatever is appropriate for the AT command(s) that is (are) executing.
  4. Read the next line and go back to 2.

1 The "\r\n> " prefix for AT+CMGS is the only place you do something a bit different, i.e. start processing modem response on something other than a strict line boundary.

2 Unless you have misconfigured S3 and S4 which you never should do.

3 The list in V.250 is not complete, there exist a couple more defined in 27.005 and 27.007.

Solution 2:

You can use following code to read response from SIM900 GSM Module just after issuing AT command.

char response[200];
for(int i = 0 ; Serial.available() > 0 && i<200 ; i++) {
   response[i++] = Serial.read();
}

After reading response you can use strstr() function to check whether it is 'OK' or 'ERROR', as given below:

if(strstr(responce, "OK"){
   /*Do your code to handle OK response*/
}
else if(strstr(responce, "ERROR"){
   /*Do your code to handle ERROR response*/
}
else {
   /* You got some other response*/
}

Solution 3:

I just wanted to add that K.H.A.J.A.S answer helped me but I had to modify it before it worked like so:

char response[200];
for(int i = 0 ; Serial.available() > 0 && i<200 ; i++) {
    response[i] = Serial.read();
}

Whereas his answer had [i++] so it was incrementing i in the for loop and then incrementing i again when storing that char in response index i.