tcp_client_demo.c 4.24 KB
#include "tcp_client_demo.h"
#include "lwip/opt.h"
#include "lwip_comm.h"
#include "lwip/lwip_sys.h"
#include "lwip/api.h"
#include "includes.h"
#include "key.h"
#include "hardware.h"
#include "all_value.h"

struct netconn *tcp_clientconn;					      //
u8 tcp_client_recvbuf[TCP_CLIENT_RX_BUFSIZE];	//TCP客户端接收数据缓冲区
u8 tcp_client_sendbuf[TCP_CLIENT_TX_BUFSIZE]; //TCP客户端发送数据缓冲区

int Tmr1=0;
#define TCPCLIENT_PRIO		6
#define TCPCLIENT_STK_SIZE	200
OS_STK TCPCLIENT_TASK_STK[TCPCLIENT_STK_SIZE];
//OS_TMR *tmr1;
void tmr1_callback(void *p_tmr,void *p_arg)//tmr1 回调函数
{
 Tmr1=1;
}
//tcp客户端任务函数
err_t err,recv_err;
u8 T_tcp_oksign=0;
//u8 R_tcp_oksign=0;
//int countTimeCut=180000;//断网检测时间,初始3分钟
int countTimeOut=0;
//u8 gygygy=0;
	 u8 data_t=0;
	 u8 stopold=0;
static void tcp_client_thread(void *arg)
{
	OS_CPU_SR cpu_sr;
//	INT8U ERR=0;
	u32 data_len = 0;
	struct pbuf *q;
	static ip_addr_t server_ipaddr,loca_ipaddr;
	static u16_t loca_port;
//	static u8 data_t=0;
//	static u8 stopold=0;
//	static u8 datasendmode=0;

	LWIP_UNUSED_ARG(arg);
	IP4_ADDR(&server_ipaddr, F_lwip.ip_Remo[0],F_lwip.ip_Remo[1], F_lwip.ip_Remo[2],F_lwip.ip_Remo[3]);
//	tmr1=OSTmrCreate(10,10,OS_TMR_OPT_PERIODIC,tmr1_callback,0,"OS_TIMER1",&ERR);
//	OSTimeDly(2);
//	OSTmrStart(tmr1,&ERR);//开启软件定时

	while (1)
	{
		/*创建tcp链接*/
		T_tcp_oksign=1;//允许发送标志位置1
		countTimeOut=0;//接收超时计时清零
		tcp_clientconn = netconn_new(NETCONN_TCP);  //创建一个TCP链接,NETCONN_TCP是0x10,是内部TCP的代号.0x20是UDP
		err = netconn_connect(tcp_clientconn,&server_ipaddr,F_lwip.Port_Remo);//连接服务器
		if(err != ERR_OK){netconn_delete(tcp_clientconn);OSTimeDly(100);}//删除tcp_clientconn连接
		else if (err == ERR_OK)//处理新连接的数据
		{		
			struct netbuf *recvbuf;
			tcp_clientconn->recv_timeout= 10;//
			netconn_getaddr(tcp_clientconn,&loca_ipaddr,&loca_port,1); 
 			F_lwip.Port_Local=loca_port;
 			TCP_SX_Data();//发送缓存信息
			while(1)
			{

				if(T_tcp_oksign)
				{ 
					err = netconn_write(tcp_clientconn ,tcp_client_sendbuf,TCP_CLIENT_TX_LIEN,NETCONN_COPY); //发送tcp_server_sentbuf中的数据,长度是TCP_CLIENT_TX_LIEN字节
					T_tcp_oksign=0;
				}

				recv_err = netconn_recv(tcp_clientconn,&recvbuf);
				if(recv_err == ERR_OK)  //接收到数据
				{
					countTimeOut=0;
					OS_ENTER_CRITICAL(); //关中断。
					
					if(data_t>0)
					{
//						memset(tcp_client_recvbuf1,0,TCP_CLIENT_RX_BUFSIZE);  //数据接收缓冲区清零					
						for(q=recvbuf->p;q!=NULL;q=q->next)  //遍历完整个pbuf
						{
							if(q->len > (TCP_CLIENT_RX_BUFSIZE-data_len))
//							memcpy(tcp_client_recvbuf1+data_len,q->payload,(TCP_CLIENT_RX_BUFSIZE-data_len));//拷贝数据
//							else memcpy(tcp_client_recvbuf1+data_len,q->payload,q->len);
							data_len += q->len;  	
							if(data_len > TCP_CLIENT_RX_BUFSIZE) break; //超出则跳出	
						}
					}else
					{
						memset(tcp_client_recvbuf,0,TCP_CLIENT_RX_BUFSIZE);  //数据接收缓冲区清零					
						for(q=recvbuf->p;q!=NULL;q=q->next)  
						{
							if(q->len > (TCP_CLIENT_RX_BUFSIZE-data_len))
							memcpy(tcp_client_recvbuf+data_len,q->payload,(TCP_CLIENT_RX_BUFSIZE-data_len));
							else memcpy(tcp_client_recvbuf+data_len,q->payload,q->len);
							data_len += q->len;  	
							if(data_len > TCP_CLIENT_RX_BUFSIZE) break;
						}
					}
					
					TCP_RX_Data_Analysis();
					
					OS_EXIT_CRITICAL(); //开中断
					data_len=0;         //复制完成后data_len要清零。					
					netbuf_delete(recvbuf);
					
					netconn_close(tcp_clientconn);
					netconn_delete(tcp_clientconn);
					OSTimeDly(100);
					break;//跳出当前WHILE
				}
				else if(recv_err == ERR_CLSD)
				{
					netconn_close(tcp_clientconn);
					netconn_delete(tcp_clientconn);
					break;//跳出当前WHILE
				}
				else      
				{
					countTimeOut++; 
					if(countTimeOut>=300)
					{
						countTimeOut=0;
						netconn_close(tcp_clientconn);
						netconn_delete(tcp_clientconn);
						break;//跳出当前WHILE				
					}
				}
				OSTimeDly(10);
			}
			OSTimeDly(100);	//断开连接之后延时200ms
		}
		OSTimeDly(10);
		
	}
}

INT8U tcp_client_init(void)
{
	INT8U res;
	OS_CPU_SR cpu_sr;
	
	OS_ENTER_CRITICAL();	//关中断
	res = OSTaskCreate(tcp_client_thread,(void*)0,(OS_STK*)&TCPCLIENT_TASK_STK[TCPCLIENT_STK_SIZE-1],TCPCLIENT_PRIO); 
	OS_EXIT_CRITICAL();		//开中断
	
	return res;
}