SIM900 AT 命令响应解析 [英] SIM900 AT Commands response parsing

查看:31
本文介绍了SIM900 AT 命令响应解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用连接到 Arduino Uno 的 sim900 gps/gprs 模块扩展板,我将如何解析我的 AT 命令的响应?或者如何在发送 AT 命令后删除串行中打印的第一行?

i am using sim900 gps/gprs module shield connected to an Arduino Uno, how will i be able to parse the response of my AT commands? Or how will i be able to remove the 1st line printed in the serial after sending an AT command?

AT+CMGL="ALL"

+CMGL: 1,"REC READ","+XXXXXXXXXX","","16/04/25,15:20:59+32"
Hilp akp si ralphh the pogi one mmalit mi pizza hehehehehe

+CMGL: 2,"REC READ","+XXXXXXXXXX","","16/04/25,21:51:33+32"
Yow!!!

OK

上面输出的例子,我想去掉 AT+CMGL="ALL" 然后解析剩下的数据,解析的最好方法是什么

example on the output above, i want to get rid of the AT+CMGL="ALL" and then parse the data left, what is the best way in parsing

推荐答案

我将如何解析我的 AT 命令的响应?

How will i be able to parse the response of my AT commands?

是的,这是一个正确的问题.

Yes, this is the right question to ask.

发送 AT 命令后,如何去除串口打印的第一行?

How will i be able to remove the 1st line printed in the serial after sending an AT command?

不,这是一个错误的问题,因为如果您关心回声是否打开,那么您就做错了.

No, this is the wrong question to ask, because if you care about whether echo is on or not you are doing it wrong.

解析AT命令输出的正确策略如下:

The correct strategy for parsing AT command output is as follows:

  • 发送 AT 命令行(以 "\r" 正确终止).
  • 读取从调制解调器接收到的一个和一个字符,直到您有一整行以 "\r\n" 结尾,然后解析该行.
    • 如果该行等于最终结果代码,则命令行的所有输出都已完成(并且调制解调器已准备好接收新命令).这必须是您测试的第一件事!
    • 如果正在运行的 AT 命令具有其信息文本响应行的前缀(几乎全部都有),请检查该行是否以该前缀开头,如果是,则处理该行,否则忽略它.
    • 如果运行的 AT 命令没有前缀,您可能希望在收到最终结果代码之前打印所有内容.这仅适用于诸如 ATI 之类的旧命令,并且对于解析这些命令,您可能会合理地关心 echo 与否.
    • Send the AT command line (correctly terminated with "\r").
    • Read one and one character received from the modem until you have a complete line terminated with "\r\n" and then parse that line.
      • If the line equals a final result code, then all output from the command line is finished (and the modem is ready to receive new commands). This must be the first thing you test for!
      • If the AT command running has a prefix for its information text response lines (almost all have) check if the line starts with that, and if so process the line else ignore it.
      • If the AT command running does not have a prefix you probably want to print everything until the final result code is received. This applies only for legacy commands like ATI, and for parsing these you might legitimately care about echo or not.

      现在对于 AT+CMGL 命令,它需要做更多的工作,因为响应被分成多行.

      Now for the AT+CMGL command it is a little bit more work since the responses are split on multiple lines.

      首先,最好的信息来源应该是制造商特定的 AT 文档,其次是官方的 3GPP 27.005 规范AT+CMGL 命令.

      First of all, the best source of information should be the manufacturer specific AT documentation, the second best being the official 3GPP 27.005 specification that standardize the AT+CMGL command.

      文本模式下AT+CMGL的响应指定为

      The response for AT+CMGL in text mode is specified as

      +CMGL: <index>,<stat>,<oa/da>,[<alpha>],[<scts>][,<tooa/toda>,
      <length>]<CR><LF><data>[<CR><LF>
      +CMGL: <index>,<stat>,<da/oa>,[<alpha>],[<scts>][,<tooa/toda>,
      <length>]<CR><LF><data>[...]]
      

      因此,在收到以+CMGL:"开头的行后,接下来的所有行直到您读到一个空行(\r\n")都属于这一行.

      hence after receiving a line starting with "+CMGL: " all the lines following until you read a blank line ("\r\n") belongs to this.

      请参阅这个答案,了解一般代码结构和流程,尽管如上面写的响应的多行属性需要多一点处理.我会使用以下内容(未经测试的代码):

      See this answer on the general code structure and flow, although as written above the multi-line property of the response needs a bit more handling. I would have used something like the following (untested code):

      enum CMGL_state {
          CMGL_NONE,
          CMGL_PREFIX,
          CMGL_DATA
      };
      
      // Extra prototype needed because of Arduino's auto-prototype generation which often breaks compilation when enums are used.
      enum CMGL_state parse_CMGL(enum CMGL_state state, String line);
      enum CMGL_state parse_CMGL(enum CMGL_state state, String line)
      {
          if (line.equals("\r\n") {
              return CMGL_NONE;
          }
          if (line.startsWith("+CMGL: ") {
              return CMGL_PREFIX;
          }
          if (state == CMGL_PREFIX || state == CMGL_DATA) {
              return CMGL_DATA;
          }
          return CMGL_NONE;
      }
      
      ...
      
      write_to_modem("AT+CMGL=\"ALL\"\r");
      CMGL_state = CMGL_NONE;
      goto start;
      do {
          CMGL_state = parse_CMGL(CMGL_state, line);
          switch (CMGL_state) {
          case CMGL_PREFIX:
              process_prefix(line); // or whatever you want to do with this line
              break;
          case CMGL_DATA:
              process_data(line); // or whatever you want to do with this line
              break;
          case CMGL_NONE:
          default:
              break;
          }
      start:
          line = read_line_from_modem();
      } while (! is_final_result_code(line))
      

      这篇关于SIM900 AT 命令响应解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆