tcp_client.c
2.68 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
/**
******************************************************************************
* @file tcp_client.c
* @version V1.0
* @date 2018-06-18
* @brief tcp客户端数据回环测试
*
* @company 深圳炜世科技有限公司
* @information WIZnet W5100S官方代理商,全程技术支持,价格优势大!
* @website www.wisioe.com
* @forum www.w5100s.com
* @qqGroup 579842114
******************************************************************************
*/
#include <stdio.h>
#include "tcp_client.h"
#include "socket.h"
#include "wizchip_conf.h"
#include "w5100s_conf.h"
#include "bsp.h"
uint8_t ClientRecvBuff[DATA_BUF_SIZE] = {0};
uint8_t ClientSendBuff[DATA_BUF_SIZE] = {0};
void analysInfo()
{
}
void do_tcp_client(void)
{
uint16 len=0,i = 0;
switch(getSn_SR(SOCK_TCPC)) // 获取socket的状态
{
case SOCK_CLOSED: // socket处于关闭状态
socket(SOCK_TCPC,Sn_MR_TCP,local_port2,Sn_MR_ND);
break;
case SOCK_INIT: // socket处于初始化状态
connect(SOCK_TCPC,remote_ip,remote_port); // socket连接服务器
break;
case SOCK_ESTABLISHED: // socket处于连接建立状态
if(getSn_IR(SOCK_TCPC) & Sn_IR_CON)
{
setSn_IR(SOCK_TCPC, Sn_IR_CON); // 清除接收中断标志位
}
len=getSn_RX_RSR(SOCK_TCPC); // 定义len为已接收数据的长度
if(len>0)
{
recv(SOCK_TCPC,ClientRecvBuff,len); // 接收来自Server的数据
for(i = 0; i < len; i++)
{
/**解碼接收的數據**/
if (CenterCommand.NewReceive == false)
{
if (CenterDecode(ClientRecvBuff[i], &CenterCommand.CmdList) == teDecodSuccess)
{
CenterCommand.NewReceive = true;
}
}
/**處理接收的數據**/
if (CenterCommand.NewReceive)
{
if (CommandAnalysis() == 0) //解码命令字
{
SendOrReplyTypeHandle(); // 命令字码处理
}
CenterCommand.NewReceive = false;
//如果接收回来的数据是中控应答的,不再做处理
if (CenterCommand.SendOrReply == sort_Receive)//判断是否是接受中控协议
{
Recive_check(); //取消重新发送
}
}
}
ClientRecvBuff[len]=0x00; // 添加字符串结束符
Uart_Printf(COM1,"来自服务器数据%s\r\n",ClientRecvBuff);
memcpy(ClientSendBuff,"abcdefge\r\n",10);
send(SOCK_TCPC,ClientSendBuff,10); // 向Server发送数据
}
break;
case SOCK_CLOSE_WAIT: // socket处于等待关闭状态
close(SOCK_TCPC);
break;
}
}