3

How to be sure what is end of an AT command send to GSM module?

I need some character or string that represent end of AT command for every case due to finding when whole response is received.

I guess it have something with "\r\n" sequence but, this sequence could be at the beginning of AT command response?

alk
  • 69,737
  • 10
  • 105
  • 255
viktor.radovic
  • 508
  • 2
  • 9
  • 20
  • What about EOF/CTRL+Z ? http://en.wikipedia.org/wiki/Control-Z – rkosegi Nov 08 '12 at 09:26
  • So you are actualy **not** looking for something indicating the end of the command **but** for something indicating the end of its response (if any)? – alk Nov 08 '12 at 09:31
  • 1
    @alk Yes, i am trying to indicate the end of response to an AT command – viktor.radovic Nov 08 '12 at 09:34
  • When I last handled AT commands this was with analog modems back in the 90s and they used to respond with `OK` at least. – alk Nov 08 '12 at 09:38

5 Answers5

3

Like you suppose it's \r\n.
This defines the end of a line.

And it makes only sense to process complete lines.

Some commands respons with only OK\r\n, some with some data.
So you should build at first a parser, who can detect complete lines, so they can be processed with a function that handles responses.

Sometimes you can get even responses you don't request, like change events net login or the sim status #QSS: 2\r\n.

So you need to know what answer you expect and wait until you get this

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Yes, excellent advice. AT command responses should always be [parsed line by line](https://stackoverflow.com/a/36899432/23118). – hlovdal Nov 03 '17 at 16:29
2

Here is what I used in Python. It works. There might be some other better solutions.

ser = serial.Serial(com, baudrate, timeout=5)
while True:
# a simple AT return result parser
    new_char = ser.read(1)
    byte_flow += new_char

    if len(byte_flow) >=4 and byte_flow[-4:] == b'\r\nOK':
        break
    elif len(byte_flow) >= 7 and byte_flow[-7:] == b'\r\nERROR':
        break
byte_flow += ser.read(2)  # the final '\r\n'

You can add more elif if there is any other patterns.

LanternD
  • 73
  • 8
1

Depending on mode and command I.e.

<CR> or <CR><LF>

http://www.3gpp.org/ftp/Specs/html-info/27007.htm

Morpfh
  • 4,033
  • 18
  • 26
1

Referring the original Hayes command set Wikipedia is stating:

<CR> Carriage return character, is the command line and result code terminator character, which value, in decimal ASCII between 0 and 255, is specified within parameter S3. The default value is 13.

So it should be possible to set any character to indicate the end of the response (and as well the end of any command sent to the modem a well).


Anyhow <CR>s are not only returned as last character by many modem's responses.

So it might be an interesting experiment whether writing a different character value to S3 would just change the last character sent at the end of the modem's response, or if this would change any <CR> sent during the modem's response to be the value stored in S3.

alk
  • 69,737
  • 10
  • 105
  • 255
0

The response format is command specific. While there are certain similarities ("OK\r\n"), in the end you need explicit handling for each.

Simon Richter
  • 28,572
  • 1
  • 42
  • 64