Electronic – How to handle nested final result code in AT-command text

at commandsgsmmodemserial

I am writing a parser (a finite state machine to be more precise) for AT-commands and I don't see how to handle a particular corner case:

Here is the specifications of the modem communication:
When the DTE sends an AT-command terminated by <CR> (like ATE0<CR>), the DCE will optionally answer an information text response between <CR><LF> characters and finally give the final result code (like <CR><LF>OK<CR><LF>).

My algorithm is like this:

do {
    line = readLine(serial_modem);
} while (! is_final_result_code(line))

Here is the corner case:
Suppose I send the command to read an SMS message and that the information text response (i.e. the SMS text) itself contains the sequence <CR><LF>OK<CR><LF>.

Then my parser will think wrongly that the first <CR><LF> terminates the information text response while the next part is the final result code.

Do I miss an important information on how to handle this case properly ?

Best Answer

In text mode you can't have <CR><LF> inside the received message. While sending a text message with the AT command set, the message is already terminated with the first occurence of <CR><LF>. Hence you haven't to check for this case while receiving messages in text mode.

For sending and receiving formatet messages you must use the PDU mode. In PDU mode the message is packed into a frame which contains also a field for the message length.

I don't know if there's an underlying protocol layer that unsures that you will only get well formated PDU frames.