从 SIM900 中读取和删除消息的正确方法是什么?
What is proper way to read and delete message from SIM900?
我想循环等待短信到达我的 SIM900,当检测到短信时,阅读该短信并将其从系统中删除。
困扰我的是,如果在我处理此消息时收到另一条消息(未经请求的消息信息)怎么办。
处理短信读取和删除的正确算法是什么?
这就是我完成类似任务的方式
- 开始时从 sim 内存中删除所有短信
- 使用
AT+CNMI
命令启用新消息提醒
- 使用基于中断的串行通信并使用环形缓冲区存储所有传入数据
- 只要您收到新消息,调制解调器就会向控制器发送警报。
- 解析警报消息以了解短信的位置
- 阅读短信后删除短信
下面是另一种方法。
/*
* Description: This example shows hot to read a SMS from SIM memory.
* This example only shows the AT commands (and the answers of the module) used
* to read the SMS For more information about the AT commands, refer to the AT
* command manual.
*
* Copyright (C) 2013 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version 0.2
* Author: Alejandro Gallego
*/
int8_t answer;
int x;
int onModulePin= 2;
char SMS[200];
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
// sets the PIN code
sendATcommand("AT+CPIN=****", "OK", 2000);
delay(3000);
Serial.println("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text
sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000); // selects the memory
answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000); // reads the first SMS
if (answer == 1)
{
answer = 0;
while(Serial.available() == 0);
// this loop reads the data of the SMS
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(Serial.available() > 0){
SMS[x] = Serial.read();
x++;
// check if the desired answer (OK) is in the response of the module
if (strstr(SMS, "OK") != NULL)
{
answer = 1;
}
}
}while(answer == 0); // Waits for the asnwer with time out
SMS[x] = '[=10=]';
Serial.print(SMS);
}
else
{
Serial.print("error ");
Serial.println(answer, DEC);
}
}
void loop(){
}
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
// waits for an answer from the module
while(answer == 0){ // Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '[=10=]', 100); // Initialice the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
我想循环等待短信到达我的 SIM900,当检测到短信时,阅读该短信并将其从系统中删除。 困扰我的是,如果在我处理此消息时收到另一条消息(未经请求的消息信息)怎么办。
处理短信读取和删除的正确算法是什么?
这就是我完成类似任务的方式
- 开始时从 sim 内存中删除所有短信
- 使用
AT+CNMI
命令启用新消息提醒 - 使用基于中断的串行通信并使用环形缓冲区存储所有传入数据
- 只要您收到新消息,调制解调器就会向控制器发送警报。
- 解析警报消息以了解短信的位置
- 阅读短信后删除短信
下面是另一种方法。
/*
* Description: This example shows hot to read a SMS from SIM memory.
* This example only shows the AT commands (and the answers of the module) used
* to read the SMS For more information about the AT commands, refer to the AT
* command manual.
*
* Copyright (C) 2013 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version 0.2
* Author: Alejandro Gallego
*/
int8_t answer;
int x;
int onModulePin= 2;
char SMS[200];
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
// sets the PIN code
sendATcommand("AT+CPIN=****", "OK", 2000);
delay(3000);
Serial.println("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text
sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000); // selects the memory
answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000); // reads the first SMS
if (answer == 1)
{
answer = 0;
while(Serial.available() == 0);
// this loop reads the data of the SMS
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(Serial.available() > 0){
SMS[x] = Serial.read();
x++;
// check if the desired answer (OK) is in the response of the module
if (strstr(SMS, "OK") != NULL)
{
answer = 1;
}
}
}while(answer == 0); // Waits for the asnwer with time out
SMS[x] = '[=10=]';
Serial.print(SMS);
}
else
{
Serial.print("error ");
Serial.println(answer, DEC);
}
}
void loop(){
}
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
// waits for an answer from the module
while(answer == 0){ // Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '[=10=]', 100); // Initialice the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}