新手上路
- 积分
- 20
- 金钱
- 20
- 注册时间
- 2018-6-13
- 在线时间
- 2 小时
|
楼主 |
发表于 2018-7-17 15:32:05
|
显示全部楼层
附加程序源码 这是运行在ucos-ii中的一个任务
void gsm_task(void *pdata)
{
u8 sim_ready=0;
u8 *port=; //端口固定为5000
u8 *ipaddr;//IP
const u8 *modetbl[2]={"TCP","UDP"};//连接模式
const u8* cnttbl[3]={"正在连接","连接成功","连接关闭"};
u8 *p,*p1,*p2,*p3;
u8 i=0;
u8 oldsta=0XFF;
u16 timex=0;
u8 length=0; //用来存储485接收信息的长度
float value=0,previous_value=1;
u8 connectsta=0; //0,正在连接;1,连接成功;2,连接关闭;
u8 hbeaterrcnt=0; //心跳错误计数器,连续5次心跳信号无应答,则重新连接
p=mymalloc(SRAMIN,100); //申请100字节内存
p1=mymalloc(SRAMIN,100); //申请100字节内存
pdata=pdata;
while(sim900a_send_cmd("AT","OK",100))//检测是否应答AT指令
{
printf("未检测到模块...\n\n");
printf("尝试连接模块...\n\n");
delay_ms(300);
}
while(1)
{
printf("ATK-SIM900A 测试程序\n\n");
if(sim900a_gsminfo_show(40,225)==0) //判断sim卡是否准备就绪
sim_ready=1;
else
sim_ready=0;
sim_at_response(1); //检查GSM模块发送过来的数据,及时上传给电脑
if(sim_ready) //SIM卡就绪.
{
printf("sim卡准备就绪\n\n");
sim900a_send_cmd("AT+CIPCLOSE=1","CLOSE OK",100); //关闭连接
sim900a_send_cmd("AT+CIPSHUT","SHUT OK",100); //关闭移动场景
sim900a_send_cmd("AT+CGCLASS=\"B\"","OK",1000); //设置GPRS移动台类别为B,支持包交换和数据交换
sim900a_send_cmd("AT+CGDCONT=1,\"IP\",\"CMNET\"","OK",1000);//设置PDP上下文,互联网接协议,接入点等信息
sim900a_send_cmd("AT+CGATT=1","OK",500); //附着GPRS业务
sim900a_send_cmd("AT+CIPCSGP=1,\"CMNET\"","OK",500); //设置为GPRS连接模式
sim900a_send_cmd("AT+CIPHEAD=1","OK",500); //设置接收数据显示IP头(方便判断数据来源)
printf("p:%s\n",p);
printf("开始进行TCP网络连接...\n\n");
sprintf((char*)p,"%s%s",ipaddr,port);
USART3_RX_STA=0;
printf("p:%s\n",p);
sprintf((char*)p,"AT+CIPSTART=\"%s\",\"%s\",\"%s\"",modetbl[0],ipaddr,port);
printf("p:%s\n",p);
if(sim900a_send_cmd(p,"OK",500)) //发起连接
printf("连接失败...\n\n");
else //连接成功
{
//enheng();
while(1) //连接成功后开始运行网络连接测试
{
//printf("连接成功...\n\n");
RS485_Receive_Data(data,&length); //接收485发送来的信息 并判断
if(length)
{
if(data[0]==0X01)
{
value=data[11]*256+data[12];
}
}
if(value!=previous_value) //485接收到新的数据时发送数据
{
printf("数据发送中...\r\n");
// p1 -> 要发送的数据
sprintf((char*)p1,"Y:C3_%.2f",value/100);
printf("要发送的数据:%s\n\n",p1);
if(sim900a_send_cmd("AT+CIPSEND",">",500)==0) //发送数据
{
printf("CIPSEND DATA:%s\r\n",p1); //发送数据打印到串口
u3_printf("%s\r\n",p1);
delay_us(10000);
if(sim900a_send_cmd((u8*)0X1A,"SEND OK",1000)==0)
{
printf("数据发送成功!"); //最长等待10s
previous_value=value;
}
else
printf("数据发送失败!");
delay_us(1000000);
}
else
sim900a_send_cmd((u8*)0X1B,0,0); //ESC,取消发送
oldsta=0XFF;
}
if((timex%20)==0)
{
LED1=!LED1;
if(connectsta==2||hbeaterrcnt>8)//连接中断了,或者连续8次心跳没有正确发送成功,则重新连接
{
printf("正在重新连接中...\n\n");
sim900a_send_cmd("AT+CIPCLOSE=1","CLOSE OK",500); //关闭连接
sim900a_send_cmd("AT+CIPSHUT","SHUT OK",500); //关闭移动场景
sim900a_send_cmd(p,"OK",500); //尝试重新连接
connectsta=0;
hbeaterrcnt=0;
}
}
if((connectsta==0)&&((timex%200)==0))//连接还没建立的时候,每2秒查询一次CIPSTATUS.
{
printf("正在查询状态...\n\n");
sim900a_send_cmd("AT+CIPSTATUS","OK",500); //查询连接状态
printf("查询结果:%s\r\n",USART3_RX_BUF);
if(strstr((const char*)USART3_RX_BUF,"CLOSED"))
connectsta=2; //连接中断状态
if(strstr((const char*)USART3_RX_BUF,"CONNECT OK"))
connectsta=1; //连接成功状态
}
//printf("connectsta:%d\r\n",connectsta);
//printf("hbeaterrcnt:%d\r\n",hbeaterrcnt);
//printf("timex:%d\r\n",timex);
LCD_ShowNum(40,170,connectsta,3,16);
LCD_ShowNum(90,170,hbeaterrcnt,3,16);
LCD_ShowNum(180,170,timex,3,16);
if(connectsta==1&&timex>=600)//连接正常的时候,每6秒发送一次心跳
{
timex=0;
if(sim900a_send_cmd("AT+CIPSEND",">",200)==0)//发送数据
{
sim900a_send_cmd((u8*)0X00,0,0); //发送数据:0X00
delay_ms(20); //必须加延时
sim900a_send_cmd((u8*)0X1A,0,0); //CTRL+Z,结束数据发送,启动一次传输
printf("发送心跳中...\n\n");
}
else
sim900a_send_cmd((u8*)0X1B,0,0); //ESC,取消发送
hbeaterrcnt++;
printf("usart3:%s\r\n",USART3_RX_BUF);
printf("hbeaterrcnt:%d\r\n",hbeaterrcnt);//方便调试代码
}
delay_us(10000);
if(USART3_RX_STA&0X8000) //接收到一次数据了
{
USART3_RX_BUF[USART3_RX_STA&0X7FFF]=0; //添加结束符
printf("USART3_RX_BUF:%s\n\n",USART3_RX_BUF); //发送到串口
if(hbeaterrcnt) //需要检测心跳应答
{
if(strstr((const char*)USART3_RX_BUF,"SEND OK"))
hbeaterrcnt=0;//心跳正常
}
p2=(u8*)strstr((const char*)USART3_RX_BUF,"+IPD");
if(p2)//接收到TCP/UDP数据
{
p3=(u8*)strstr((const char*)p2,",");
p2=(u8*)strstr((const char*)p2,":");
p2[0]=0;//加入结束符
sprintf((char*)p1,"收到%s字节,内容如下",p3+1);//接收到的字节数
printf("%s:%s\n\n",p1,p2+1);
if(strstr((const char*)(p2+1),"SK7_1")) //解析手机app命令
{
KM6=1;
printf("手机指令:1...\n\n");
}
if(strstr((const char*)(p2+1),"SK7_0")) //解析手机app命令
{
KM6=0;
printf("手机指令:2...\n\n");
}
if(strstr((const char*)(p2+1),"SK6_1")) //解析手机app命令
{
KM5=0;
printf("手机指令:3...\n\n");
}
if(strstr((const char*)(p2+1),"SK6_0")) //解析手机app命令
{
KM5=1;
printf("手机指令:4...\n\n");
}
if(strstr((const char*)(p2+1),"SK5_1")) //解析手机app命令
{
KM4=1;
printf("手机指令:5...\n\n");
}
if(strstr((const char*)(p2+1),"SK5_0")) //解析手机app命令
{
KM4=0;
printf("手机指令:6...\n\n");
}
if(strstr((const char*)(p2+1),"SK4_1")) //解析手机app命令
{
KM3=0;
printf("手机指令:7...\n\n");
}
if(strstr((const char*)(p2+1),"SK4_0")) //解析手机app命令
{
KM3=1;
printf("手机指令:8...\n\n");
}
if(strstr((const char*)(p2+1),"SK3_1")) //解析手机app命令
{
KM2=0;
printf("手机指令:9...\n\n");
}
if(strstr((const char*)(p2+1),"SK3_0")) //解析手机app命令
{
KM2=1;
printf("手机指令:10...\n\n");
}
if(strstr((const char*)(p2+1),"SK2_1")) //解析手机app命令
{
KM1=0;
printf("手机指令:11...\n\n");
}
if(strstr((const char*)(p2+1),"SK2_0")) //解析手机app命令
{
KM1=1;
printf("手机指令:12...\n\n");
}
} //end of if(p2)
USART3_RX_STA=0;
} //end of if(USART3_RX_STA&0X8000)
if(oldsta!=connectsta) //更新连接状态
{
oldsta=connectsta;
printf("当前状态:%s\n\n",cnttbl[connectsta]);
}
timex++;
delay_ms(1000/OS_TICKS_PER_SEC); //延时一个时钟节拍
//delay_ms(100);
} //end of while(1)
} //连接成功
} //sim卡就绪
} //end of while(1)
}
|
|