如果您尚未阅读第 5 章的全部内容V.250规范 http://www.itu.int/rec/T-REC-V.250-200307-I/en请停止阅读此处并立即阅读,这是本答案其余部分的重要基础,我会等到你回来。一些对字母表(简短版本:忽略它/将其视为 ASCII)和 S 寄存器的引用可能看起来很神秘,但不要放弃,它会很快让你感兴趣。
The only correct way to process modem output is to divide the output into complete lines and iterate one full line at the time. This is universal and applies to absolutely all AT commands (with only one single exception that I can think of1).
让我强调一下:您应该只在严格的行尾边界上分割调制解调器响应文本("\r\n"
),并在一次操作中处理生成的行字符串。所以你真的应该去实施read_line_from_modem
我在我的建议中建议的功能之前的回答 https://stackoverflow.com/a/36873777/23118.
This means that whenever you want to check for the OK
result code you should only use strcmp(line, "OK\r\n")
and not strstr or similar because you know you are processing a complete, full line which should start with the final result code at the very beginning and it will always be followed by "\r\n"
2.
Now there are more final result codes than just OK
and ERROR
, and instead of trying to figure out everything by yourself3
I suggest looking at is_final_result_code or isFinalResponseSuccess as listed in this answer https://stackoverflow.com/a/31688517/23118.
Summary
- 始终逐行读取调制解调器输出,并使用单独的函数进行读取,返回/修改字符串/缓冲区以包含该行。
- 读完一行后要做的第一件事是检查它是否是最终结果代码。编写一个单独的函数,将行作为参数并检查它是否是。
- 如果该行不是最终结果代码,请执行适合正在执行的 AT 命令的任何操作。
- 阅读下一行并返回2。
1
The "\r\n> "
prefix for AT+CMGS
is the only place you do something a bit different, i.e. start processing modem response on something other than a strict line boundary.
2
Unless you have misconfigured S3
and S4
which you never should do.
3
The list in V.250 is not complete, there exist a couple more defined in 27.005 and 27.007.