Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

arduino - Get AT command response

I am using an Arduino Uno with a sim900 gps/gprs module and I'm using at commands, how can I get the response of at command (i.e OK, ERROR) so that I can do something if response == "OK" or response == "ERROR"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you have not yet read all of chapter 5 in V.250 specification stop reading here and do so immediately, it is an important basis for the rest of this answer, I'll wait until you'll come back. Some of the references to alphabets (short version: ignore this/treat it as ASCII) and S-registers might seem cryptic but don't give up, it will grow on you quickly.


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).

Let me emphasis that: you should only split modem response text on strict end of line boundaries (" "), and process the resulting line string in one single operation. So you really should go and implement the read_line_from_modem function I suggested in my previous answer.

This means that whenever you want to check for the OK result code you should only use strcmp(line, "OK ") 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 " "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.

Summary

  1. Always read modem output line by line, and use a separate function do the reading, returning/modifying a string/buffer to contain the line.
  2. The first thing you do after having read a line is to check if it is a final result code. Write a separate function that takes the line as a parameter and checks if it is.
  3. If the line was not a final result code, do whatever is appropriate for the AT command(s) that is (are) executing.
  4. Read the next line and go back to 2.

1 The " > " 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...