新手上路
- 积分
- 44
- 金钱
- 44
- 注册时间
- 2018-1-20
- 在线时间
- 12 小时
|
楼主 |
发表于 2018-3-5 09:35:16
|
显示全部楼层
能在帮我看看吗,是不是在这代码里面?
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(SRAMIN,100); //申请100字节内存
p1=mymalloc(SRAMIN,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,"KEY_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;
USART3_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); //发送数据打印到串口
u3_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(1000);
}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*)USART3_RX_BUF,"CLOSED"))connectsta=2;
if(strstr((const char*)USART3_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(USART3_RX_STA&0X8000) //接收到一次数据了
{
USART3_RX_BUF[USART3_RX_STA&0X7FFF]=0; //添加结束符
printf("%s",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);//接收到的字节数
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); //显示接收到的数据
}
USART3_RX_STA=0;
}
if(oldsta!=connectsta)
{
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(SRAMIN,p);
myfree(SRAMIN,p1);
} |
|