目的:stm32(mini板子)的LED1亮了,则发送一条短信,短信发送成功则LED1灭,结束。不用键盘按键输入电话号码,直接固定电话。
问题:改了原子哥的程序以后,LED1一直亮,短信也就一直不停的发。
//测试短信发送内容(70个字[UCS2的时候,1个字符/数字都算1个字])
const u8* sim900a_test_msg="这是一条发送测试短信";
//SIM900A发短信测试
void sim900a_sms_send_test(void)
{
u8 *p,*p1,*p2;
//u8 phonebuf[20]; //号码缓存
//u8 pohnenumlen=0; //号码长度,最大15个数
u8* phonenum="15150518392"; //固定电话15150518392
u8 smssendsta=0; //短信发送状态,0,等待发送;1,发送失败;2,发送成功
p=mymalloc(SRAMIN,100); //申请100个字节的内存,用于存放电话号码的unicode字符串
p1=mymalloc(SRAMIN,300);//申请300个字节的内存,用于存放短信的unicode字符串
p2=mymalloc(SRAMIN,100);//申请100个字节的内存 存放:AT+CMGS=p1
LCD_Clear(WHITE);
POINT_COLOR=RED;
Show_Str_Mid(0,30,"ATK-SIM900A 发短信测试",16,240);
Show_Str(30,50,200,16,"发送给:",16,0);
Show_Str(30,70,200,16,"状态:",16,0);
Show_Str(30,90,200,16,"内容:",16,0);
POINT_COLOR=BLUE;
Show_Str(30+40,70,170,90,"等待发送",16,0);//显示状态
Show_Str(30+40,90,170,90,(u8*)sim900a_test_msg,16,0);//显示短信内容
while(1)
{
if(smssendsta)
{
smssendsta=0;
Show_Str(30+40,70,170,90,"等待发送",16,0);//显示状态
}
Show_Str(30+40,70,170,90,"正在发送",16,0);//显示正在发送
//smssendsta=1;
sim900a_unigbk_exchange((u8*)phonenum,p,1);//将电话号码转换为unicode字符串
sim900a_unigbk_exchange((u8*)sim900a_test_msg,p1,1);//将短信内容转换为unicode字符串.
sprintf((char*)p2,"AT+CMGS="%s"",p);
if(sim900a_send_cmd(p2,">",200)==0)//发送短信命令+电话号码
{
u2_printf("%s",p1); //发送短信内容到GSM模块
if(sim900a_send_cmd((u8*)0X1A,"+CMGS:",1000)==0)
smssendsta=2;//发送结束符,等待发送完成(最长等待10秒钟,因为短信长了的话,等待时间会长一些)
else
smssendsta=1;
}
if(smssendsta==1)
Show_Str(30+40,70,170,90,"发送失败",16,0); //显示状态
else
Show_Str(30+40,70,170,90,"发送成功",16,0); //显示状态
USART2_RX_STA=0;
LCD_Fill(30+54,50,239,50+16,WHITE);
Show_Str(30+54,50,156,16,(u8*)phonenum,16,0);
delay_ms(10);
if(USART2_RX_STA&0X8000)
sim_at_response(1);//检查从GSM模块接收到的数据
}
myfree(SRAMIN,p);
myfree(SRAMIN,p1);
myfree(SRAMIN,p2);
}
//sim900a短信测试
//用于读短信或者发短信
//返回值:0,正常
// 其他,错误代码
u8 sim900a_sms_test(void)
{
if(sim900a_send_cmd("AT+CMGF=1","OK",200))return 1; //设置文本模式
if(sim900a_send_cmd("AT+CSCS="UCS2"","OK",200))return 2; //设置TE字符集为UCS2
if(sim900a_send_cmd("AT+CSMP=17,0,2,25","OK",200))return 3; //设置短消息文本模式参数
while(1)
{
if(LED1==0)
{
sim900a_sms_send_test(); //发短信
LED1=1;
//break;
}
delay_ms(10);
sim_at_response(1); //检查GSM模块发送过来的数据,及时上传给电脑
}
sim900a_send_cmd("AT+CSCS="GSM"","OK",200); //设置默认的GSM 7位缺省字符集
return 0;
}
//sim900a主测试程序
void sim900a_test(void)
{
u8 key=0;
// u8 timex=0;
u8 sim_ready=0;
POINT_COLOR=RED;
Show_Str_Mid(0,30,"ATK-SIM900A 测试程序",16,240);
while(sim900a_send_cmd("AT","OK",100))//检测是否应答AT指令
{
Show_Str(40,55,200,16,"未检测到模块!!!",16,0);
delay_ms(800);
LCD_Fill(40,55,200,55+16,WHITE);
Show_Str(40,55,200,16,"尝试连接模块...",16,0);
delay_ms(400);
}
LCD_Fill(40,55,200,55+16,WHITE);
key+=sim900a_send_cmd("ATE0","OK",200);//不回显
while(1)
{
delay_ms(10);
sim_at_response(1);//检查GSM模块发送过来的数据,及时上传给电脑
if(sim900a_gsminfo_show(40,225)==0)
sim_ready=1;
else
sim_ready=0;
if(sim_ready)//SIM卡就绪.
{
sim900a_sms_test();
break;
}
//if((timex%20)==0)LED0=!LED0;//200ms闪烁
//timex++;
}
}
一周热门 更多>