tcp_client_demo.c
4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#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;
}