CASE nClientStep OF
ENUM_tcp_client.nInitial://复位信号
TCP_Client.xEnable:=FALSE;
TCP_Read.xEnable:=FALSE;
TCP_Write.xExecute:=FALSE;
TcpClientStatus.bConnectDone:=FALSE;
TcpClientStatus.bReceiveDone:=FALSE;
TcpClientStatus.bSendDone:=FALSE;
TcpClientStatus.bCloseDone:=FALSE;
IF TcpClientControl.bConnect THEN
nClientStep:=ENUM_tcp_client.nConnect;
END_IF
ENUM_tcp_client.nConnect://连接服务器
TCP_Client.xEnable:=TRUE;
nClientStep:=ENUM_tcp_client.nWhetherConnect;
ENUM_tcp_client.nWhetherConnect://判断是否连接成功
IF TCP_Client.xError THEN
nClientStep:=ENUM_tcp_client.nInitial;
END_IF
IF TCP_Client.xActive AND NOT TCP_Client.xError THEN
TcpClientStatus.bConnectDone:=TRUE;
nClientStep:=ENUM_tcp_client.nDetermineAction;
END_IF
ENUM_tcp_client.nDetermineAction://判断动作
TcpClientStatus.bSendDone:=FALSE;
TcpClientStatus.bReceiveDone:=FALSE;
IF TcpClientControl.bClose THEN
nClientStep:=ENUM_tcp_client.nInitial;
ELSIF TcpClientControl.bSend THEN
nClientStep:=ENUM_tcp_client.nSend;
ELSE
nClientStep:=ENUM_tcp_client.nReceive;
END_IF
ENUM_tcp_client.nReceive://接收数据
TCP_Read.xEnable:=TRUE;
nClientStep:=ENUM_tcp_client.nWhetherReceive;
ENUM_tcp_client.nWhetherReceive://判断是否接收成功
TCP_Read.xEnable:=FALSE;
IF TCP_Read.xError THEN
nClientStep:=ENUM_tcp_client.nInitial;
END_IF
IF NOT TCP_Read.xBusy AND NOT TCP_Read.xError THEN
IF nReceiveBytes>0 THEN
TcpClientStatus.bReceiveDone:=TRUE;
nClientStep:=ENUM_tcp_client.nDetermineAction;
ELSE
nClientStep:=ENUM_tcp_client.nDetermineAction;
END_IF
END_IF
ENUM_tcp_client.nSend://发送数据
TCP_Write.xExecute:=TRUE;
nClientStep:=ENUM_tcp_client.nWhetherSend;
ENUM_tcp_client.nWhetherSend://判断是否发送成功
TCP_Write.xExecute:=FALSE;
IF TCP_Write.xError THEN
nClientStep:=ENUM_tcp_client.nClose;
END_IF
IF NOT TCP_Write.xBusy AND NOT TCP_Write.xError THEN
TcpClientStatus.bSendDone:=TRUE;
nClientStep:=ENUM_tcp_client.nDetermineAction;
END_IF
END_CASE
//功能块TCP_Client(
xEnable:=,
xDone=>,
xBusy=>,
xError=>,
udiTimeOut:=,
ipAddr:=TcpClientParameter.sIp ,
uiPort:=TcpClientParameter.nPort ,
eError=>,
xActive=>,
hConnection=>hSocket );TCP_Read(
xEnable:=,
xDone=>,
xBusy=>,
xError=>,
hConnection:=hSocket ,
szSize:=TcpClientParameter.nReceiveDataLength ,
pData:=ADR(TcpClientParameter.ReceiveData),
eError=>,
xReady=>,
szCount=>nReceiveBytes );
IF nReceiveBytes>0 THEN
TcpClientParameter.ReceiveData.sString:=LEFT(TcpClientParameter.ReceiveData.sString,ULINT_TO_INT(nReceiveBytes));
FOR i:=INT_TO_UINT(LEN(TcpClientParameter.ReceiveData.sString)) TO 255 BY 1 DO
TcpClientParameter.ReceiveData.nByte[i]:=0;
END_FOR
END_IF
IF TCP_Write.xExecute THEN
FOR i:=TcpClientParameter.nSendDataLength TO 255 BY 1 DO
TcpClientParameter.SendData.nByte[i]:=0;
END_FOR
END_IF
TCP_Write(
xExecute:=,
udiTimeOut:=,
xDone=>,
xBusy=>,
xError=>,
hConnection:=hSocket ,
szSize:=TcpClientParameter.nSendDataLength ,
pData:=ADR(TcpClientParameter.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
//远程IP和端口号
IF tcpClient.parameter.sIp.sAddr='' THEN
tcpClient.parameter.sIp.sAddr:='192.168.150.204';
tcpClient.parameter.nPort:=55;
END_IF
tcpClient.parameter.nSendDataLength:=INT_TO_UINT(LEN(tcpClient.parameter.SendData.sString));//功能块
tcpClient.FB(
TcpClientControl:=tcpClient.control ,
TcpClientParameter:=tcpClient.parameter ,
TcpClientStatus=>tcpClient.status );//复位
IF tcpClient.status.bSendDone THEN
tcpClient.control.bSend:=FALSE;
END_IF