【问题标题】:Why I could not read the other received sms's except the first SMS?为什么除了第一条短信外,我看不到其他收到的短信?
【发布时间】:2015-08-01 12:52:06
【问题描述】:

我必须在我的 gsm 模块 SIM900(连接到 Arduino)上读取传入的 SMS,并且我想将发件人号码和消息打印到串行监视器上。

我首先用 AT 命令配置 gsm 模块,Response() 函数会给我对 AT 命令的响应。

因为任何短信都将采用以下模式

+CMT: "[手机号码]", "[日期和时间]" [消息正文]

所以,我首先提取+CMT,然后我将获取手机号码,最后我们有消息正文。我使用的代码是

char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;

void Config() // This function is configuring our SIM900 module i.e. sending the initial AT commands
{
delay(1000);
Serial.print("ATE0\r");
Response();
Serial.print("AT\r");
Response();
Serial.print("AT+CMGF=1\r");
Response();
Serial.print("AT+CNMI=1,2,0,0,0\r");
Response();
}


void setup()
{
  Serial.begin(9600);
  Config();
}

void loop()
{
  RecSMS();
}


void Response() // Get the Response of each AT Command
{
int count = 0;
Serial.println();
while(1)
{
if(Serial.available())
{
char data =Serial.read();
if(data == 'K'){Serial.println("OK");break;}
if(data == 'R'){Serial.println("GSM Not Working");break;}
}
count++;
delay(10);
if(count == 1000){Serial.println("GSM not Found");break;}

}
}

void RecSMS() // Receiving the SMS and extracting the Sender Mobile number & Message Text
{
if(Serial.available())
{
char data = Serial.read();
if(data == '+'){RcvdCheck = 1;}
if((data == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
if((data == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
if((data == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
if(RcvdCheck == 4){RcvdConf = 1; RcvdCheck = 0;}

if(RcvdConf == 1)
{
if(data == '\n'){RcvdEnd++;}
if(RcvdEnd == 3){RcvdEnd = 0;}
RcvdMsg[index] = data;

index++;
if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
if(RcvdConf == 0)
{
Serial.print("Mobile Number is: ");
for(int x = 4;x < 17;x++)
{
  MsgMob[x-4] = RcvdMsg[x];
  Serial.print(MsgMob[x-4]);
}
  Serial.println();
  Serial.print("Message Text: ");
for(int x = 46; x < MsgLength; x++)
{
  MsgTxt[x-46] = RcvdMsg[x];
  Serial.print(MsgTxt[x-46]);
}

Serial.println();
Serial.flush();


}
}
}
}

代码的问题是

收到第一条短信后,我会收到我的手机号码和消息正文。之后,我只会将发件人号码打印到我的串行监视器上,而不是邮件正文。

哪里出错了。没看懂。

请帮助我.......提前谢谢。

【问题讨论】:

  • 如果没有适当的缩进,您的代码很难阅读。

标签: arduino sms gsm iot sim900


【解决方案1】:

如果它第一次确实有效,但以后没有,则可能与某些变量未重置有关。您在文件顶部声明所有变量,即使它们仅在 RecSMS() 函数中需要。尝试将声明移至RecSMS() 的顶部。

void RecSMS() {
  char RcvdMsg[200] = "";
  int RcvdCheck = 0;
  int RcvdConf = 0;
  int index = 0;
  int RcvdEnd = 0;
  char MsgMob[15];
  char MsgTxt[50];
  int MsgLength = 0;

  if(Serial.available()) {

  // Rest of the code goes here

【讨论】:

    【解决方案2】:

    谢谢@迈克尔。我认为这也解决了这个问题。

    我在代码中发现的问题是,我们没有重置 RecSMS 函数中的所有变量。所以要解决这个问题,请在 Serial.flush() 语句之前保留以下代码。

    RcvdCheck = 0;
    RcvdConf = 0;
    index = 0;
    RcvdEnd = 0;
    MsgMob[15];
    MsgTxt[50];
    MsgLength = 0;
    

    这样就可以解决问题了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多