///////////////////////////////////////////////////////////////////////////////////////////////////////////
//GPRS测试部分代码
const u8 *modetbl[2]={"TCP","UDP"};//连接模式
//tcp/udp测试
//带心跳功能,以维持连接
//mode:0:TCP测试;1,UDP测试)
//ipaddr:ip地址
//port:端口
void sim900a_tcpudp_test(u8 mode,u8* ipaddr,u8* port)
{
u8 *p,*p1,*p2,*p3;
u8 key;
u16 timex=0;
u8 count=0;
const u8* cnttbl[3]={"正在连接","连接成功","连接关闭"};
u8 connectsta=0; //0,正在连接;1,连接成功;2,连接关闭;
u8 hbeaterrcnt=0; //心跳错误计数器,连续5次心跳信号无应答,则重新连接
u8 oldsta=0XFF;
p=mymalloc(100); //申请100字节内存
p1=mymalloc(100); //申请100字节内存
LCD_Clear(WHITE);
POINT_COLOR=RED;
if(mode)Show_Str_Mid(0,30,"ATK-SIM900A UDP连接测试",16,240);
else Show_Str_Mid(0,30,"ATK-SIM900A TCP连接测试",16,240);
Show_Str(30,50,200,16,"WK_UP:退出测试 KEY0:发送数据",12,0);
sprintf((char*)p,"IP地址:%s 端口:%s",ipaddr,port);
Show_Str(30,65,200,12,p,12,0); //显示IP地址和端口
Show_Str(30,80,200,12,"状态:",12,0); //连接状态
Show_Str(30,100,200,12,"发送数据:",12,0); //连接状态
Show_Str(30,115,200,12,"接收数据:",12,0); //端口固定为8086
POINT_COLOR=BLUE;
USART2_RX_STA=0;
sprintf((char*)p,"AT+CIPSTART=\"%s\",\"%s\",\"%s\"",modetbl[mode],ipaddr,port);
if(sim900a_send_cmd(p,"OK",500))return; //发起连接
while(1)
{
key=KEY_Scan(0);
if(key==WKUP_PRES)//退出测试
{
sim900a_send_cmd("AT+CIPCLOSE=1","CLOSE OK",500); //关闭连接
sim900a_send_cmd("AT+CIPSHUT","SHUT OK",500); //关闭移动场景
break;
}else if(key==KEY0_PRES&(hbeaterrcnt==0)) //发送数据(心跳正常时发送)
{
Show_Str(30+30,80,200,12,"数据发送中...",12,0); //提示数据发送中
if(sim900a_send_cmd("AT+CIPSEND",">",500)==0) //发送数据
{
printf("CIPSEND DATA:%s\r\n",p1); //发送数据打印到串口
u2_printf("%s\r\n",p1);
delay_ms(10);
if(sim900a_send_cmd((u8*)0X1A,"SEND OK",1000)==0)Show_Str(30+30,80,200,12,"数据发送成功!",12,0);//最长等待10s
else Show_Str(30+30,80,200,12,"数据发送失败!",12,0);
delay_ms(500);
}else sim900a_send_cmd((u8*)0X1B,0,0); //ESC,取消发送
oldsta=0XFF;
}
if((timex%20)==0)
{
LED0=!LED0;
count++;
if(connectsta==2||hbeaterrcnt>8)//连接中断了,或者连续8次心跳没有正确发送成功,则重新连接
{
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;
}
sprintf((char*)p1,"ATK-SIM900A %s测试 %d ",modetbl[mode],count);
Show_Str(30+54,100,200,12,p1,12,0);
}
if(connectsta==0&&(timex%200)==0)//连接还没建立的时候,每2秒查询一次CIPSTATUS.
{
sim900a_send_cmd("AT+CIPSTATUS","OK",500); //查询连接状态
if(strstr((const char*)USART2_RX_BUF,"CLOSED"))connectsta=2;
if(strstr((const char*)USART2_RX_BUF,"CONNECT OK"))connectsta=1;
}
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,结束数据发送,启动一次传输
}else sim900a_send_cmd((u8*)0X1B,0,0); //ESC,取消发送
hbeaterrcnt++;
printf("hbeaterrcnt:%d\r\n",hbeaterrcnt);//方便调试代码,心跳计数器
}
delay_ms(10);
if(USART2_RX_STA&0X8000) //接收到一次数据了
{
USART2_RX_BUF[USART2_RX_STA&0X7FFF]=0; //添加结束符
printf("%s",USART2_RX_BUF); //发送到串口
if(hbeaterrcnt) //需要检测心跳应答
{
if(strstr((const char*)USART2_RX_BUF,"SEND OK"))hbeaterrcnt=0;//心跳正常
}
p2=(u8*)strstr((const char*)USART2_RX_BUF,"+IPD");
if(p2)//接收到TCP/UDP数据
{
p3=(u8*)strstr((const char*)p2,","); //*****p3该变量里面现在存在的数据是什么??
p2=(u8*)strstr((const char*)p2,":"); //*****p2变量里面又存在的是什么数据
p2[0]=0;//加入结束符
sprintf((char*)p1,"收到%s字节,内容如下",p3+1);//接收到的字节数 //****sprintf是把格式化的数据写入到字符串中
//即此时p1缓存器里面的数据为 “收到的收到%s字节,内容如下+p3+1单元里面存在的数值”
LCD_Fill(30+54,115,239,130,WHITE);
POINT_COLOR=BRED;
Show_Str(30+54,115,156,12,p1,12,0); //显示接收到的数据长度
POINT_COLOR=BLUE;
LCD_Fill(30,130,210,319,WHITE);
Show_Str(30,130,180,190,p2+1,12,0); //显示接收到的数据
}
USART2_RX_STA=0;
}
if(oldsta!=connectsta) //这个oldsta变量定义是做什么用的呢??????????????
{
oldsta=connectsta;
LCD_Fill(30+30,80,239,80+12,WHITE);
Show_Str(30+30,80,200,12,(u8*)cnttbl[connectsta],12,0); //更新状态
}
timex++;
}
myfree(p);
myfree(p1);
}
|