How to check if an AT command executed successfully or failed

1.9k views Asked by At

How do I place a check programmatically if the AT command executed successfully or failed.

I have placed following check:

boolean success = response.endsWith("OK");
boolean failed = response.endsWith("ERROR");

I just want to be sure if this check can be placed universally, or atleast on AT+CUSD command. I cannot place contains check since the ussd response can itself contain 'ok' or 'error' strings.

1

There are 1 answers

3
hlovdal On BEST ANSWER

Excellent step on your behalf to investigate how to do things properly instead of just stopping on "well, this seems to work by trial and error".

Yes, OK and ERROR are universal but ERROR might be substituted with something else and there exists other responses as well. The V.250 specification defines most of the Final Result Codes but additionally 27.007 defines +CME ERROR: and 27.005 defines +CMS ERROR:.

You can look at the code for atinout for an example for a combined is_final_result function, although the two split isFinalResponseError and isFinalResponseSuccess functions in ST-Ericsson's U300 RIL seems closer to your usage. Note however that CONNECT is not a final result code, it is an intermediate result code, so the name isFinalResponseSuccess is not 100% correct and you most likely do not want to include that.

Regarding contains versus endsWith it does not matter; the final result codes always comes on a line all by itself1, so you should always check that. In other words you should always only read and buffer response data from the modem until you have received a terminating \r\n pair of bytes and first then start parsing that received line (the absolutely only exception is when waiting for the 4 byte response from AT+CMGS before sending the payload).

So your structure should look something like

Write(port, "AT+SOMECMD\r");
do {
         input = ReadLine(port, responseTimeout);
} while (!isFinalResultCode(input));

If you want to consume any intermediate information text responses before the final result code comes you need to change the loop a little (e.g. check for final result code first before continuing processing the intermediate response).


1 Provided that the end of line configuration has not been messed up, e.g. you should always let S3 and S4 be \r and \n as well as using ATV1.