新手上路
- 积分
- 45
- 金钱
- 45
- 注册时间
- 2017-3-8
- 在线时间
- 4 小时
|
用SiM900A在ucosii系统中发送数据到服务器,在ucosii系统中新建一个gsm任务,然后进行发送数据,但是SIM900A一直连接不成功,实现不了实时通信。为什么sim900a_send_cmd("AT+CPIN?","OK",200)一直返回值是1(发送指令失败)void gsm_task(void *pdata)
{
u8 *hua = mymalloc(100);
char a = 'a' ;
sprintf((char*)hua,"%c",a);
while(1)
{
sim900a_send_data(hua) ;
}
}
gsm.c文件中:
#include "gsm.h"
#include "usart.h"
#include "delay.h"
#include "malloc.h"
#include "string.h"
#include "usart2.h"
#include "led.h"
#include "dma.h"
//sim900a·¢ËíÃüáîoó,¼ì2a½óêÕμ½μÄó|′e
//str:Æú′yμÄó|′e½á1û
//·μ»ØÖμ:0,ûóDμÃμ½Æú′yμÄó|′e½á1û
// ÆäËû,Æú′yó|′e½á1ûμÄλÖÃ(strμÄλÖÃ)
u8* sim900a_check_cmd(u8 *str)
{
char *strx=0;
if(USART2_RX_STA&0X8000) //½óêÕμ½ò»′Îêy¾YáË
{
USART2_RX_BUF[USART2_RX_STA&0X7FFF]=0;//ìí¼ó½áêø·û
strx=strstr((const char*)USART2_RX_BUF,(const char*)str);
}
return (u8*)strx;
}
//Ïòsim900a·¢ËíÃüáî
//cmd:·¢ËíμÄÃüáî×Ö·û′®(2»Dèòaìí¼ó»Ø3μáË),μ±cmd<0XFFμÄê±oò,·¢Ëíêy×Ö(±èèç·¢Ëí0X1A),′óóúμÄê±oò·¢Ëí×Ö·û′®.
//ack:Æú′yμÄó|′e½á1û,èç1ûÎa¿Õ,Ôò±íê¾2»Dèòaμè′yó|′e
//waittime:μè′yê±¼ä(μ¥λ:10ms)
//·μ»ØÖμ:0,·¢Ëí3é1|(μÃμ½áËÆú′yμÄó|′e½á1û)
// 1,·¢Ëí꧰ü
u8 sim900a_send_cmd(u8 *cmd,u8 *ack,u16 waittime)
{
u8 res=0;
USART2_RX_STA=0;
if((u32)cmd<=0XFF)
{
while(DMA1_Channel7->CNDTR!=0); //μè′yí¨μà7′«êäíê3é
USART2->DR=(u32)cmd;
}
else u2_printf("%s\r\n",cmd);//·¢ËíÃüáî
if(ack&&waittime) //Dèòaμè′yó|′e
{
while(--waittime) //μè′yμ1¼Æê±
{
delay_ms(10);
if(USART2_RX_STA&0X8000)//½óêÕμ½Æú′yμÄó|′e½á1û
{
if(sim900a_check_cmd(ack))break;//μÃμ½óDD§êy¾Y
USART2_RX_STA=0;
}
}
if(waittime==0)res=1;
}
return res;
}
u8 sim900a_gsminfo(u16 x,u16 y)
{
u8 *p,*p1,*p2;
u8 res=0;
p=mymalloc(50);//éêÇë50¸ö×Ö½úμÄÄú′æ
USART2_RX_STA=0;
if(sim900a_send_cmd("AT+CPIN?","OK",200)){
RED_LED=0;
delay_ms(100);
RED_LED=1;
delay_ms(50);
RED_LED=0;
delay_ms(100);
RED_LED=1;
delay_ms(1500);
res|=1<<0; }//2éÑˉSIM¿¨êÇ·ñÔúλ
USART2_RX_STA=0;
if(sim900a_send_cmd("AT+COPS?","OK",200)==0) //2éÑˉÔËóaéìÃû×Ö
{
USART2_RX_STA=0;
}
else res|=1<<1;
if(sim900a_send_cmd("AT+CSQ","+CSQ:",200)==0) //2éÑˉDÅoÅÖêá¿
{
USART2_RX_STA=0;
}else res|=1<<2;
if(sim900a_send_cmd("AT+CBC","+CBC:",200)==0) //2éÑˉμç3Øμçá¿
{
USART2_RX_STA=0;
}else res|=1<<3;
if(sim900a_send_cmd("AT+CCLK?","+CCLK:",200)==0) //éèÖÃê±Öó +CCLK:
{
p1=(u8*)strstr((const char*)(USART2_RX_BUF),"\"");
p2=(u8*)strstr((const char*)(p1+1),":");
p2[3]=0;//¼óèë½áêø·û
sprintf((char*)p,"èÕÆúê±¼ä:%s",p1+1);
USART2_RX_STA=0;
}else res|=1<<4;
myfree(p);
return res;
}
const u8* kbd_tbl2[13]={"1","2","3","4","5","6","7","8","9",".","0","#","DEL"};
const u8 *modetbl[2]={"TCP","UDP"};//ᬽóÄ£ê½
//sim900a·¢Æeᬽó·tÎñÆ÷
//·μ»ØÖμ:1,ᬽó3é1|
// 0,ᬽó꧰ü
u8 sim900a_gprs_connect(void)
{
u8 connectsta=0; //0,ᬽó꧰ü;1,ᬽó3é1|
u8 mode=0; //0,TCPᬽó;1,UDPᬽó
const u8 *port="8086"; //¶Ë¿ú1춨Îa8086,μ±ÄãμÄμçÄÔ8086¶Ë¿ú±»ÆäËû3ìDòÕ¼óÃμÄê±oò,ÇëDT¸ÄÎaÆäËû¿ÕÏD¶Ë¿ú
u8 ipbuf[16]; //ip»o′æ
u8 *p=mymalloc(100); //éêÇë100×Ö½úÄú′æ;
u8 sim_ready=0;
while(sim_ready==0)
{
delay_ms(10);
if(sim900a_gsminfo(40,225)==0) sim_ready=1;//SIMÕy3£
else sim_ready=0;//SIM2»Õy3£
}
sim900a_send_cmd("AT+CIPCLOSE=1","CLOSE OK",100); //1رÕᬽó
sim900a_send_cmd("AT+CIPSHUT","SHUT OK",100); //1رÕòƶˉ3¡¾°
if(sim900a_send_cmd("AT+CGCLASS=\"B\"","OK",1000))return 0; //éèÖÃGPRSòƶˉì¨àà±eÎaB,Ö§3Ö°ü½»»»oíêy¾Y½»»»
if(sim900a_send_cmd("AT+CGDCONT=1,\"IP\",\"CMNET\"","OK",1000))return 0;//éèÖÃDPéÏÏÂÎÄ,»¥áaíø½óD-òé,½óèëμãμèDÅÏ¢
if(sim900a_send_cmd("AT+CGATT=1","OK",500))return 0; //¸½×ÅGPRSòμÎñ
if(sim900a_send_cmd("AT+CIPCSGP=1,\"CMNET\"","OK",500))return 0; //éèÖÃÎaGPRSᬽóÄ£ê½
if(sim900a_send_cmd("AT+CIPHEAD=1","OK",500))return 0; //éèÖýóêÕêy¾YÏÔê¾IPí·(·½±ãÅD¶Ïêy¾Yà′Ô′)
//ipμØÖ·
ipbuf[0] = kbd_tbl2[0][0];//1
ipbuf[1] = kbd_tbl2[1][0];//2
ipbuf[2] = kbd_tbl2[10][0];//0
ipbuf[3] = kbd_tbl2[9][0];//.
ipbuf[4] = kbd_tbl2[6][0]; //7
ipbuf[5] = kbd_tbl2[6][0];//7
ipbuf[6] = kbd_tbl2[9][0];//.
ipbuf[7] = kbd_tbl2[3][0];//4
ipbuf[8] = kbd_tbl2[1][0];//2
ipbuf[9] = kbd_tbl2[9][0]; //.
ipbuf[10] = kbd_tbl2[4][0];//5
ipbuf[11] = kbd_tbl2[5][0];//6
USART2_RX_STA=0;
sprintf((char*)p,"AT+CIPSTART=\"%s\",\"%s\",\"%s\"",modetbl[mode],ipbuf,port);
while(connectsta==0)
{
if(sim900a_send_cmd(p,"OK",500)) //·¢Æeᬽó
connectsta=0; //ᬽó꧰ü
else connectsta=1; //ᬽó3é1|
delay_ms(10);
}
return connectsta;
}
//sim900a·¢Ëíêy¾Yμ½·tÎñÆ÷
//USART1_RX_BUF:òa·¢ËíμÄêy¾Y
void sim900a_send_data(u8* USART1_RX_BUF)
{
u8 data_sta=0; //0£o·¢Ëíêy¾Y꧰ü;1£o·¢Ëíêy¾Y3é1|
u8 flag=0; //0£osim900aᬽó꧰ü;1£osim900aᬽó3é1|
u8 *p1=mymalloc(100); //éêÇë100×Ö½úÄú′æ;
flag=sim900a_gprs_connect();
USART2_RX_STA=0;
if(flag) //sim900aᬽó3é1|
{
while(data_sta==0)
{
if(sim900a_send_cmd("AT+CIPSEND",">",500)==0) //·¢Ëíêy¾Y
{
sprintf((char*)p1,"%s",USART1_RX_BUF);
u2_printf("%s",p1); //·¢Ëíêy¾Y
delay_ms(10);
if(sim900a_send_cmd((u8*)0X1A,"SEND OK",1000)==0)//×î3¤μè′y10s
data_sta=1; //·¢Ëí3é1|
else data_sta=0; //·¢Ëí꧰ü
delay_ms(500);
}
else
{
sim900a_send_cmd((u8*)0X1B,0,0); //ESC,è¡Ïû·¢Ëí
data_sta=0;
}
}
}
myfree(p1);
}
|
|