tcp_server.c
4.79 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
153
154
/**
******************************************************************************
* @file tcp_server.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_server.h"
#include "socket.h"
#include "w5100s_conf.h"
#include "wizchip_conf.h"
#include "bsp.h"
//unsigned char ServerRecvBuff[BYTELENGTH] = {0};
//unsigned char ServerSendBuff[BYTELENGTH] = {0};
//extern u16 DataFromPLC[WORDLENGTH]; //PLC发送给控制器
//extern u16 DataToPLC[WORDLENGTH]; //控制器发送给PLC的数据数组
//extern void UpdateDataFromHolding(agvHandlePlcData *HandlePlcData);
//extern void UpdateDataToHolding(agvHandlePlcData *HandlePlcData);
void CopyShort(u16 *Des,u16 *Ptr, u16 Length)
{
u16 i = 0;
while(i < Length)
{
Des[i] = Ptr[i];
i++;
}
}
void do_tcp_server(void)
{
unsigned short len=0;
switch(getSn_SR(SOCK_TCPS)) // 获取socket的状态
{
case SOCK_CLOSED: // socket处于关闭状态
Uart_Printf(COM1,"Creat\r\n");
socket(SOCK_TCPS ,Sn_MR_TCP,local_port,Sn_MR_ND); // 打开socket
break;
case SOCK_INIT: // socket已初始化状态
Uart_Printf(COM1,"LISTEN\r\n");
listen(SOCK_TCPS); // socket建立监听
break;
case SOCK_ESTABLISHED: // socket处于连接建立状态
//Uart_Printf(COM1,"plc已连接\r\n");
if(getSn_IR(SOCK_TCPS) & Sn_IR_CON)
{
setSn_IR(SOCK_TCPS, Sn_IR_CON); // 清除接收中断标志位
}
len=getSn_RX_RSR(SOCK_TCPS); // 定义len为已接收数据的长度
if(len>0)
{
// recv(SOCK_TCPS,ServerRecvBuff,len); // 接收来自Client的数据
// ServerRecvBuff[len]=0x00; // 添加字符串结束符
// CombineTwoByteToOneShort(DataFromPLC, WORDLENGTH, (char *)ServerRecvBuff);//因为PLC发送short型数据高低位有问题,因此在这里将char按照PLC高低位组成short型
//
// if(pSetup[SETUP_SIEMENS_SCREEN_OPEN])
// {
// CopyShort(&DataToPLC[60],&usRegHoldingBuf[1],100);
// CopyShort(&usRegHoldingBuf[101],&DataFromPLC[60],100);
// }
//
// UpdateDataFromHolding(&HandlePlcData);//将PLC发送来的数据更新到专门结构体中去
//
// UpdateDataToHolding(&HandlePlcData);//将PLC需要的数据更新到要发送给PLC的数组中去
//
// BreakShortToTwoByte(DataToPLC, WORDLENGTH, (char *)ServerSendBuff);//将要发送给PLC的short型数组拆分成char型数组
//
// send(SOCK_TCPS,ServerSendBuff,BYTELENGTH); // 向Client发送数据
// ServerSendBuff[0] = 1;
// ServerSendBuff[1] = 2;
// ServerSendBuff[2] = 3;
// send(SOCK_TCPS,ServerSendBuff,3);
}
break;
case SOCK_CLOSE_WAIT: // socket处于等待关闭状态
//Uart_Printf(COM1,"CLOSE\r\n");
close(SOCK_TCPS);
break;
}
}
//unsigned char ServerRecvBuffQT[MonitorBYTELENGTH] = {0};
//unsigned char ServerSendBuffQT[MonitorBYTELENGTH] = {0};
//extern unsigned short DataToQt[MonitorWORDLENGTH]; //PLC发送给控制器
void do_tcp_serverQT(void)
{
unsigned short len=0;
switch(getSn_SR(SOCK_DHCP)) // 获取socket的状态
{
case SOCK_CLOSED: // socket处于关闭状态
Uart_Printf(COM1,"Creat\r\n");
socket(SOCK_DHCP ,Sn_MR_TCP,local_port3,Sn_MR_ND); // 打开socket
break;
case SOCK_INIT: // socket已初始化状态
Uart_Printf(COM1,"Listen\r\n");
listen(SOCK_DHCP); // socket建立监听
break;
case SOCK_ESTABLISHED: // socket处于连接建立状态
if(getSn_IR(SOCK_DHCP) & Sn_IR_CON)
{
setSn_IR(SOCK_DHCP, Sn_IR_CON); // 清除接收中断标志位
}
len=getSn_RX_RSR(SOCK_DHCP); // 定义len为已接收数据的长度
Uart_Printf(COM1,"len=%d\r\n",len);
if(len>0)
{
// recv(SOCK_DHCP,ServerRecvBuffQT,len);
//
// agvPlcToControl();
// agvControlToPlc();
// BreakShortToTwoByte(DataToQt, MonitorWORDLENGTH, (char *)ServerSendBuffQT);
// send(SOCK_DHCP,ServerSendBuffQT,500); // 向Client发送数据
// setSn_IR(SOCK_DHCP, Sn_IR_CON);
// len = 0;
}
break;
case SOCK_CLOSE_WAIT: // socket处于等待关闭状态
close(SOCK_DHCP);
break;
}
}