使用的硬件为单片机STC32G12K128,晶振24MHZ,波特率9600
通信模块SIM900a。
发送英文短信的指令为以下流程
AT
(应答)
OK
--------------分界线------------------
AT+CSQ
(应答,会根据实际信号变化如下)
+CSQ: 22,0
OK
--------------分界线------------------
AT+CPIN?
(应答)
+CPIN:READY
OK
--------------分界线------------------
AT+COPS?
(应答,最好使用移动(正常电话卡),尽管说联通可以,但是我试了不大行,没有移动好用)
+COPS:0,0,"CHINA MOBILE")
OK
-------开始要发送短信-------分界线------------------
AT+CSCS="GSM"
(应答)
OK
AT+CMGF=1
(应答)
OK
AT+CMGS=”132359*****”
(应答)
>
-------然后就可以输入发送短信的内容------
hello
-------结束短信发送---------
0x1A
-------等待回复-------------
+CMGS:107(数字是不确定的,所以验证"+CMGS"即可)
OK
在我测试发送短信时,指令按照上面的流程走,在电脑测试时可以,但是在单片机上就不可以。
排除硬件问题,例如查看D6 3s一闪,拨打电话给模块中的手机号D5熄灭,D6 3s一闪.
故就剩下代码问题。
在编写的时候由于使用的keil251中的string.h不知道是不是有问题,没有strstr()函数,所以自己写了一个:
char* my_strstr(const char* str, const char* substr)
{
while (*str)
{
const char* s1 = str;
const char* s2 = substr;
while (*s1 && *s2 && (*s1 == *s2))
{
s1++;
s2++;
}
if (!*s2)
{
return (char*)str;
}
str++;
}
return NULL;
}
出现问题的地方在使用的hand()函数上,以下为订正过的:
u8 hand(char* str)
{
if(my_strstr( RX3_Buffer,str) != NULL)// RX3_Buffer,str位置千万不要反,debug好久才发现
{
return 1;
}
else
return 0;
}
再记录一下发送短信的函数:
void MSG(char *msg_content, char *phone_num){
u8 i = 0;
u8 timeout = 0;
char phone_num_cmd[50] = {0};
char msg_content_cmd[200] = {0};
delay_ms(5000);
// 握手
printf("AT\r\n");
PrintString3("AT\r\n");
while (!hand("OK") && timeout < 5){
clear_rec_data();
PrintString3("AT\r\n");
delay_ms(1000);
timeout++;
}
if (timeout >= 5){
printf("Handshake failed.\r\n");
return;
}
// 获取信号质量
timeout = 0;
printf("AT+CSQ\r\n");
PrintString3("AT+CSQ\r\n");
while (!hand("OK") && timeout < 5){
clear_rec_data();
PrintString3("AT+CSQ\r\n");
delay_ms(1000);
timeout++;
}
if (timeout >= 5){
printf("Get signal quality failed.\r\n");
return;
}
// 检查是否读到手机卡
timeout = 0;
printf("AT+CPIN?\r\n");
PrintString3("AT+CPIN?\r\n");
while (!hand("READY") && timeout < 5){
clear_rec_data();
PrintString3("AT+CPIN?\r\n");
delay_ms(1000);
timeout++;
}
if (timeout >= 5){
printf("No SIM card detected.\r\n");
return;
}
// 检查是否注册到网络
timeout = 0;
printf("AT+COPS?\r\n");
PrintString3("AT+COPS?\r\n");
while (!hand("CHINA") && timeout < 5){
clear_rec_data();
PrintString3("AT+COPS?\r\n");
delay_ms(1000);
timeout++;
}
if (timeout >= 5){
printf("Failed to register to network.\r\n");
return;
}
// 设置编码方式和短信中心号码
timeout = 0;
printf("AT+CSCS=\"GSM\"\r\n");
// PrintString3("AT+CSCS=\"UCS2\"\r\n");
PrintString3("AT+CSCS=\"GSM\"\r\n");
while (!hand("OK") && timeout < 5){
clear_rec_data();
PrintString3("AT+CSCS=\"GSM\"\r\n");
// PrintString3("AT+CSCS=\"UCS2\"\r\n");
delay_ms(1000);
timeout++;
}
if (timeout >= 5){
printf("Failed to set encoding.\r\n");
return;
}
timeout = 0;
printf("AT+CMGF=1\r\n");
PrintString3("AT+CMGF=1\r\n");
while (!hand("OK") && timeout < 5){
clear_rec_data();
PrintString3("AT+CMGF=1\r\n");
delay_ms(1000);
timeout++;
}
if (timeout >= 5){
printf("Failed to set text mode.\r\n");
return;
}
timeout = 0;
PrintString3("AT+CSCA?\r\n"); //短信中心号码
while (!hand("OK") && timeout < 5){
clear_rec_data();
PrintString3("AT+CSCA?\r\n"); //短信中心号码
delay_ms(1000);
timeout++;
}
if (timeout >= 5){
printf("Failed to set massage center.\r\n");
return;
}
// // 设置短信内容
// sprintf(phone_num_cmd, "AT+CMGS=\"%s\"\r\n", phone_num);
// sprintf(msg_content_cmd, "%s\r\n", msg_content);
// // 发送短信
// timeout = 0;
// printf("%s",phone_num_cmd);
// PrintString3(phone_num_cmd);
// while (!hand(">") && timeout < 5){
// clear_rec_data();
// PrintString3(phone_num_cmd);
// delay_ms(1000);
// timeout++;
// }
// if (timeout >= 5){
// printf("Failed to send message.\r\n");
// return;
// }
// printf("%s",msg_content_cmd);
// PrintString3(msg_content_cmd);
// delay_ms(1000);
// timeout=0;
// PrintString3("AT+CSMP=17,167,0,8\r\n"); //
// while (!hand("OK") && timeout < 5){
// clear_rec_data();
// PrintString3("AT+CSMP=17,167,0,8\r\n");//
// delay_ms(1000);
// timeout++;
// }
// if (timeout >= 5){
// printf("Failed .\r\n");
// return;
// }
timeout = 0;
PrintString3("AT+CMGS=\"1323507****\"\r");
// PrintString3("AT+CMGS=\"003100330032003300350030003700**00**00**00**\"\r\n");
while (!hand(">") && timeout < 5){
clear_rec_data();
PrintString3("AT+CMGS=\"1323507****\"\r");
// PrintString3("AT+CMGS=\"003100330032003300350030003700**00**00**00**\"\r\n");
delay_ms(1000);
timeout++;
}
if (timeout >= 5){
printf("Failed to send message.\r\n");
return;
}
// clear_rec_data();
PrintString3("hello,leila");
// delay_ms(1000);
TX3_write2buff(0x1A);
timeout = 0;
printf("+CMGS:\r\n");
while (!hand("+CMGS") && timeout < 5){
clear_rec_data();
delay_ms(2000);
timeout++;
}
if (timeout >= 5){
send_success=0;
printf("Failed to send message1.\r\n");
return;
}
// while(!hand("+CMGS"));
send_success = 1;
// 删除短信
// timeout = 0;
// printf("AT+CMGD=1,4\r\n");
// PrintString3("AT+CMGD=1,4\r\n");
// while (!hand("OK") && timeout < 5){
// clear_rec_data();
// PrintString3("AT+CMGD=1,4\r\n");
// delay_ms(1000);
// timeout++;
// }
// if (timeout >= 5){
// printf("Failed to delete message.\r\n");
// return;
// }
// clear_rec_data();
if (send_success){
printf("Message sent successfully.\r\n");
}
}