CASE nServerStep OF
ENUM_tcp_server.nInitial://复位
TCP_Server.xEnable:=FALSE;
TCP_Connection.xEnable:=FALSE;
TCP_Read.xEnable:=FALSE;
TCP_Write.xExecute:=FALSE;
TcpServerStatus.bListenDone:=FALSE;
TcpServerStatus.bAcceptAccepted:=FALSE;
TcpServerStatus.bReceiveDone:=FALSE;
TcpServerStatus.bSendDone:=FALSE;
TcpServerStatus.bCloseAllDone:=FALSE;
IF TcpServerControl.bListen THEN
nServerStep:=ENUM_tcp_server.nListen;
END_IF
ENUM_tcp_server.nListen://打开监听
TCP_Server.xEnable:=TRUE;
nServerStep:=ENUM_tcp_server.nWhetherListen;
ENUM_tcp_server.nWhetherListen://判断是否监听成功
IF TCP_Server.xError THEN
nServerStep:=ENUM_tcp_server.nInitial;
END_IF
IF TCP_Server.xBusy AND NOT TCP_Server.xError THEN
TcpServerStatus.bListenDone:=TRUE;
nServerStep:=ENUM_tcp_server.nAccept;
END_IF
ENUM_tcp_server.nAccept://接收客户端连接
TCP_Connection.xEnable:=TRUE;
nServerStep:=ENUM_tcp_server.nWhetherAccept;
ENUM_tcp_server.nWhetherAccept://判断客户端是否连接
IF TcpServerControl.bCloseAll THEN
nServerStep:=ENUM_tcp_server.nInitial;
END_IF
IF TCP_Connection.xError THEN
nServerStep:=ENUM_tcp_server.nInitial;
END_IF
IF TCP_Connection.xActive AND NOT TCP_Connection.xError THEN
TcpServerStatus.bAcceptAccepted:=TRUE;
nServerStep:=ENUM_tcp_server.nDetermineAction;
END_IF
ENUM_tcp_server.nDetermineAction://判断动作
TcpServerStatus.bSendDone:=FALSE;
TcpServerStatus.bReceiveDone:=FALSE;
IF TcpServerControl.bCloseAll THEN
nServerStep:=ENUM_tcp_server.nInitial;
ELSIF TcpServerControl.bSend THEN
nServerStep:=ENUM_tcp_server.nSend;
ELSE
nServerStep:=ENUM_tcp_server.nReceive;
END_IF
ENUM_tcp_server.nReceive://接收数据
TCP_Read.xEnable:=TRUE;
nServerStep:=ENUM_tcp_server.nWhetherReceive;
ENUM_tcp_server.nWhetherReceive://判断是否接收成功
TCP_Read.xEnable:=FALSE;
IF TCP_Read.xError THEN
nServerStep:=ENUM_tcp_server.nInitial;
END_IF
IF NOT TCP_Read.xBusy AND NOT TCP_Read.xError THEN
IF nReceiveBytes>0 THEN
TcpServerStatus.bReceiveDone:=TRUE;
nServerStep:=ENUM_tcp_server.nDetermineAction;
ELSE
nServerStep:=ENUM_tcp_server.nDetermineAction;
END_IF
END_IF
ENUM_tcp_server.nSend://发送接收数据
TCP_Write.xExecute:=TRUE;
nServerStep:=ENUM_tcp_server.nWhetherSend;
ENUM_tcp_server.nWhetherSend://判断是否发送成功
TCP_Write.xExecute:=FALSE;
IF TCP_Write.xError THEN
nServerStep:=ENUM_tcp_server.nInitial;
END_IF
IF NOT TCP_Write.xBusy AND NOT TCP_Write.xError THEN
TcpServerStatus.bSendDone:=TRUE;
nServerStep:=ENUM_tcp_server.nDetermineAction;
END_IF
END_CASE
//功能块TCP_Server(
xEnable:=,
xDone=>,
xBusy=>,
xError=>,
ipAddr:=TcpServerParameter.sIp ,
uiPort:=TcpServerParameter.nPort ,
eError=>,
hServer=>hListener );TCP_Connection(
xEnable:=,
xDone=>,
xBusy=>,
xError=>,
hServer:=hListener ,
eError=>,
xActive=>,
hConnection=>hSocket );TCP_Read(
xEnable:=,
xDone=>,
xBusy=>,
xError=>,
hConnection:=hSocket ,
szSize:=TcpServerParameter.nReceiveDataLength ,
pData:=ADR(TcpServerParameter.ReceiveData),
eError=>,
xReady=>,
szCount=>nReceiveBytes );
IF nReceiveBytes>0 THEN
TcpServerParameter.ReceiveData.sString:=LEFT(TcpServerParameter.ReceiveData.sString,ULINT_TO_INT(nReceiveBytes));
FOR i:=INT_TO_UINT(LEN(TcpServerParameter.ReceiveData.sString)) TO 255 BY 1 DO
TcpServerParameter.ReceiveData.nByte[i]:=0;
END_FOR
END_IF
IF TCP_Write.xExecute THEN
FOR i:=TcpServerParameter.nSendDataLength TO 255 BY 1 DO
TcpServerParameter.SendData.nByte[i]:=0;
END_FOR
END_IF
TCP_Write(
xExecute:=,
udiTimeOut:=,
xDone=>,
xBusy=>,
xError=>,
hConnection:=hSocket ,
szSize:=TcpServerParameter.nSendDataLength ,
pData:=ADR(TcpServerParameter.SendData),
eError=>);
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
//本地IP和端口号
IF tcpServer.parameter.sIp.sAddr='' THEN
tcpServer.parameter.sIp.sAddr:='192.168.150.204';
tcpServer.parameter.nPort:=66;
END_IF
tcpServer.parameter.nSendDataLength:=INT_TO_UINT(LEN(tcpServer.parameter.SendData.sString));//功能块
tcpServer.FB(
TcpServerControl:=tcpServer.control ,
TcpServerParameter:=tcpServer.parameter ,
TcpServerStatus=>tcpServer.status );//复位
IF tcpServer.status.bSendDone THEN
tcpServer.control.bSend:=FALSE;
END_IF