FB_ClientServerConnection
The function block FB_ClientServerConnection can be used to manage (establish or remove) a client connection. FB_ClientServerConnection simplifies the implementation of a client application by encapsulating the functionality of the two function blocks FB_SocketConnect and FB_SocketClose internally. The integrated debugging output of the connection status facilitates troubleshooting in the event of configuration or communication errors. In addition, a minimum client application only requires an instance of the FB_SocketSend function block and/or an instance of the FB_SocketReceive function block.
In the first step, a typical client application establishes the connection with the server via the FB_ClientServerConnection function block. In the next step instances of FB_SocketSend and/or FB_SocketReceive can be used to exchange data with the server. When a connection is closed depends on the requirements of the application.
VAR_INPUT
VAR_INPUT
sSrvNetID : T_AmsNetID := '';
nMode : DWORD := 0;
sRemoteHost : STRING(15) := '';
nRemotePort : UDINT;
bEnable : BOOL;
tReconnect : TIME := T#45s;
END_VAR
sSrvNetID: string containing the Ams network address of the TwinCAT TCP/IP Connection Server. For the local computer (default) an empty string may be specified.
nMode: parameter flags (modes). The permissible parameters are listed in the table and can be combined via an OR operation:
Flag | Description |
---|---|
CONNECT_MODE_ENABLEDBG | Activates logging of debug messages in the application log. In order to view the debugging messages open the TwinCAT System Manager and activate log view. |
sRemoteHost: IP address (Ipv4) of the remote server as a string (e.g. '172.33.5.1'). An empty string can be entered on the local computer for a server.
nRemotePort: IP port number of the remote server (e.g. 200).
bEnable: as long as this input is TRUE, the system attempts to establish a connection at regular intervals until a connection was established successfully. Once established, a connection can be closed again with FALSE.
tReconnect: cycle time with which the function block attempts to establish the connection. After this time at the latest, the attempt is aborted and a new one is started.
The tReconnect value should not be set too low, since timeout periods of > 30 s may occur in the event of a network interruption. If the value is too low, command execution would be interrupted prematurely, and ADS error code 1861 (timeout elapsed) would be returned instead of the Winsocket error WSAETIMEDOUT. |
VAR_OUTPUT
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
nErrId : UDINT;
hSocket : T_HSOCKET;
eState : E_SocketConnectionState := eSOCKET_DISCONNECTED;
END_VAR
bBusy: This output is TRUE as long as the function block is active.
bError: Becomes TRUE as soon as an error has occurred.
nErrID: If the bError output is set, this parameter returns the TwinCAT TCP/IP Connection Server error number.
hSocket: Connection handle for the newly opened local client socket. If successful, this variable is transferred to the instances of the function blocks FB_SocketSend and/or FB_SocketReceive.
eState: Returns the current connection status.
Example of a call in FBD:
PROGRAM MAIN
VAR
fbClientConnection1 : FB_ClientServerConnection;
bConnect1 : BOOL;
bBusy1 : BOOL;
bError1 : BOOL;
nErrID1 : UDINT;
hSocket1 : T_HSOCKET;
eState1 : E_SocketConnectionState;
END_VAR
Further application examples (including source code) can be found here: Examples
Requirements
Development Environment | Target System | PLC libraries to include |
---|---|---|
TwinCAT v2.9.0 Build >= 1030 | PC or CX (x86) | TcSocketHelper.Lib ( Standard.Lib; TcBase.Lib; TcSystem.Lib; TcUtilities.Lib; TcpIp.Lib are included automatically ) |
TwinCAT v2.10.0 Build >= 1301 | CX (ARM) |