FB_SocketCloseAll

If TwinCAT is restarted or stopped, the TwinCAT TCP/IP Connection Server is also stopped. Any open sockets (TCP/IP and UDP connection handles) are closed automatically. The PLC program is reset after a "PLC reset", a "Rebuild all..." or a new "Download", and the information about already opened sockets (connection handles) is no longer available in the PLC. Any open connections can then no longer be closed properly.
The function block FB_SocketCloseAll can be used to close all connection handles (TCP/IP and UDP sockets) that were opened by a PLC runtime system. This means that, if FB_SocketCloseAll is called in one of the tasks of the first runtime systems (port 801), all sockets that were opened in the first runtime system are closed. In each PLC runtime system that uses the socket function blocks, an instance of FB_SocketCloseAll should be called during the PLC start.
Inputs
VAR_INPUT
sSrvNetId : T_AmsNetId := '';
bExecute : BOOL;
tTimeout : TIME := T#5s;
END_VAR
Name | Type | Description |
---|---|---|
sSrvNetId | T_AmsNetId | String containing the network address of the TwinCAT TCP/IP Connection Server. For the local computer (default) an empty string may be specified. |
bExecute | BOOL | The function block is enabled by a positive edge at this input. |
tTimeout | TIME | Maximum time allowed for the execution of the function block. |
Outputs
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
nErrId : UDINT;
END_VAR
Name | Type | Description |
---|---|---|
bBusy | BOOL | This output is active if the function block is activated. It remains active until acknowledgement. |
bError | BOOL | If an error should occur during the transfer of the command, then this output is set once the bBusy output was reset. |
nErrId | UDINT | If an bError output is set, this parameter returns the TwinCAT TCP/IP Connection Server error number. |
Sample of an implementation in ST
The following program code is used to properly close the connection handles (sockets) that were open before a "PLC reset" or "Download" before a PLC restart.
PROGRAM MAIN
VAR
fbSocketCloseAll : FB_SocketCloseAll;
bCloseAll : BOOL := TRUE;
END_VAR
IF bCloseAll THEN(*On PLC reset or program download close all old connections *)
bCloseAll := FALSE;
fbSocketCloseAll( sSrvNetId:= '', bExecute:= TRUE, tTimeout:= T#10s );
ELSE
fbSocketCloseAll( bExecute:= FALSE );
END_IF
IF NOT fbSocketCloseAll.bBusy THEN
(*...
... continue program execution...
...*)
END_IF
Requirements
Development environment |
Target system type |
PLC libraries to include (category group) |
---|---|---|
TwinCAT v3.1.0 |
PC, or CX (x86, X64, ARM) |
Tc2_TcpIp (communication) |