FB_ServerClientConnection
The function block FB_ServerClientConnection can be used to manage (establish or remove) a server connection. FB_ServerClientConnection simplifies the implementation of a server application by encapsulating the functionality of the three function blocks FB_SocketListen, FB_SocketAccept 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 server 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 server application establishes the connection with the client via the FB_ServerClientConnection function block (more precisely, the server application accepts the incoming connection request). 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_IN_OUT
VAR_IN_OUT
hServer : T_HSERVER;
END_VAR
hServer: server handle. This input variable must be initialized via the F_CreateServerHnd function.
VAR_INPUT
VAR_INPUT
eMode : E_SocketAcceptMode := eACCEPT_ALL;
sRemoteHost : STRING(15) := '';
nRemotePort : UDINT := 0;
bEnable : BOOL;
tReconnect : TIME := T#1s;
END_VAR
eMode: Determines whether all or only certain connections should be accepted.
sRemoteHost: IP address (Ipv4) of the remote client whose connection is to be accepted as a string (e.g.: '172.33.5.1'). For a client on the local computer an empty string may be specified.
nRemotePort: IP port number of the remote client whose connection is to be accepted (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 used by the function block to try and establish a connection.
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 remote 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 in FBD:
The following example illustrates initialisation of a server handle variable. The server handle is then transferred to three instances of the FB_ServerClientConnection function block.
PROGRAM MAIN
VAR
hServer : T_HSERVER;
bListen : BOOL;
fbServerConnection1 : FB_ServerClientConnection;
bConnect1 : BOOL;
bBusy1 : BOOL;
bError1 : BOOL;
nErrID1 : UDINT;
hSocket1 : T_HSOCKET;
eState1 : E_SocketConnectionState;
fbServerConnection2 : FB_ServerClientConnection;
bConnect2 : BOOL;
bBusy2 : BOOL;
bError2 : BOOL;
nErrID2 : UDINT;
hSocket2 : T_HSOCKET;
eState2 : E_SocketConnectionState;
fbServerConnection3 : FB_ServerClientConnection;
bConnect3 : BOOL;
bBusy3 : BOOL;
bError3 : BOOL;
nErrID3 : UDINT;
hSocket3 : T_HSOCKET;
eState3 : E_SocketConnectionState;
END_VAR
Online View:
The first connection is activated (bConnect1=TRUE), although the connection has not yet been established (passive open).
The second connection has not yet been activated (bConnect2=FALSE) (closed).
The third connection was activated (bConnect3=TRUE), and a connection to the remote client has been established.
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) |