Get AT command response

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.

Leave a Comment