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.
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
andERROR
are universal butERROR
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 splitisFinalResponseError
andisFinalResponseSuccess
functions in ST-Ericsson's U300 RIL seems closer to your usage. Note however thatCONNECT
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 fromAT+CMGS
before sending the payload).So your structure should look something like
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
andS4
be\r
and\n
as well as usingATV1
.