Arduino,GSM屏蔽,AT + CMGR和+ CMGL不响应SMS文本数据

时间:2017-11-26 11:11:43

标签: arduino sms gsm at-command sim900

我是Arduino和GSM盾牌的新手。我一直在使用SMS,我有一个问题是从调制解调器读取SMS的文本内容。

以下是Arduino和GSM的设置。

#include <SoftwareSerial.h>
SoftwareSerial sim900(7, 8);

#define BUFF_SIZE 400

char buffer[BUFF_SIZE];
bool GSMconnected = false;

//functions
bool copyResponse(int* size);
void printBuff(int size);
bool readlnbuffer(int* start, int* nextline, int* size);
bool isResponseOK(int start, int endofline);
bool checkConnection();
bool checkMessage(int index);
bool checkString(int start, int end, String command);
bool checkSMS(int start, int nextline);
bool newMessage();

void setup() {
  sim900.begin(9600);
  Serial.begin(9600);
  delay(100);
  pinMode(relayIN, OUTPUT);
  delay(100);
  while (!GSMconnected) {
    checkConnection();
    delay(10000);
  }
  int rsize;
  //text mode
  sim900.print("AT+CMGF=1\r");
  delay(1000);

  //store message in SIM card
  sim900.print("AT+CPMS=\"SM\"\r");
  delay(1000);

  sim900.print("AT+CMGD=0,4\r\n");
  delay(1000);
  copyResponse(&rsize);
  printBuff(rsize);
  /*
     0: do not show header values
     1: show the values in result codes
  */
  sim900.print("AT+CSDH=1\r");
  delay(1000);
  //receive mode +CMTI:"SM", 1
  sim900.print("AT+CNMI=2,2,0,0,0 \r");
  delay(1000);
  copyResponse(&rsize);
  printBuff(rsize);

  sim900.print("AT+CSMP=17,167,0,0\r\n");
  delay(10000);
  copyResponse(&rsize);
  printBuff(rsize);
  Serial.println("GSM is ready");

我想在有新短信时查看短信。所以我只需在GSM可用时调用该功能。

void loop() {
  if(sim900.available()) {
    newMessage();
  }
}

newMessage()检查存储在SIM卡中的新短信的索引。

bool newMessage() {
  int responseSize;
  if (!copyResponse(&responseSize)) {
    return false;
  }
  //+CMTI: “SM”,1
  //read a line and check if there is a SMS
  int start = 0;
  int nextline = 0;
  int index;
  while (readlnbuffer(&start, &nextline, &responseSize)) {
    String command = "+CMTI: \"SM\",";
    if (checkString(start, start + 12, command)) {
      Serial.println("SMS found");
      index = int(buffer[start + 12]);
      break;
    }
    start = nextline;
  }

  //debug
  printBuff(responseSize);
  delay(10000);
  if (checkMessage(index)) {
    //delete message
    sim900.print("AT+CMGD=");
    sim900.print(char(index));
    sim900.println("\r");
    delay(100);
    return true;
  } else {
    return false;
  }

checkMessage()读取短信,并应打印短信文本数据。

bool checkMessage(int index) {
  //listing
  //sim900.print("AT+CMGL=\"REC UNREAD\"\r\n");
  //reading
  sim900.print("AT+CMGR=");
  sim900.print(char(index));
  sim900.print("\r\n");
  delay(5000);
  int responseSize;
  if (!copyResponse(&responseSize)) {
    return false;
  }
  delay(100);

  //debug, to check what is in the buffer
  printBuff(responseSize);
  //read a line and check if there is a SMS
  int start = 0;
  int nextline = 0;
  bool isSMS = false;
  while (!isSMS) {
    start = nextline;
    if (!readlnbuffer(&start, &nextline, &responseSize)) {
      Serial.println("no SMS found");
      return false;
    }
    String command = "+CMGR:";
    if (checkString(nextline, nextline + 6, command)) {
      Serial.println("SMS checking");
      isSMS = true;
    }
  }

  //debug
  if (!readlnbuffer(&start, &nextline, &responseSize)) {
    Serial.println("cannot read the sms info");
    return false;
  }
  Serial.println("SMS message: ");
  for (int i = start; i < nextline; i++) {
    //debug
    Serial.print(buffer[i]);
  }
  Serial.println("");
}

当我运行代码时,看起来AT + CMGR的响应无法完全复制。以下是串行监视器显示的内容。

  

检查网络

     

AT + CREG?

     

+ CREG:0,1

     

     

AT + CMGF = 1

     

     

AT + CPMS =&#34; SM&#34;

     

+ CPMS:1,30,1,30,1,30

     

     

AT +

     

AT + CSDH = 1

     

     

AT + CNMI = 2,1,0,0,0

     

     

AT + CSMP = 17,167,0,0

     

     

GSM准备就绪

     

发现短信

     

+ CMTI:&#34; SM&#34;,1

     

AT + CMGR = 1

     

&GT; + CMGR:&#34; REC UNREAD&#34;,&#34; + 123456789&#34;,&#34;&#34;,&#34; 17/11 / 26,11

     

短信检查

     

短信:

     

AT + CMGD = 1

     

我认为我在缓冲区中复制响应时出错了。我也是编写代码的初学者,我需要一些帮助。任何人都可以告诉我为什么我会遇到这个问题?

谢谢!

0 个答案:

没有答案