FB_ClientServerConnection

FB_ClientServerConnection 1:

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 function block FB_SocketSend and/or an instance of the function block FB_SocketReceive.

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.

FB_ClientServerConnection 2: Inputs

VAR_INPUT
    sSrvNetID   : T_AmsNetID := '';
    nMode       : DWORD := 0;
    sRemoteHost : T_IPv4Addr := '';
    nRemotePort : UDINT;
    bEnable     : BOOL;
    tReconnect  : TIME := T#45s;(*!!!*)
END_VAR

Name

Type

Description

sSrvNetID

T_AmsNetID

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

DWORD

Parameter flags (modes). The permissible parameters are listed here and can be combined by ORing:

CONNECT_MODE_ENABLEDBG:

Activates logging of debug messages in the application log. In order to view the debug messages open the TwinCAT System Manager and activate log view.

sRemoteHost

T_IPv4Addr

IP address (Ipv4) of the remote server in the form of a string (e.g. '172.33.5.1'). An empty string can be entered on the local computer for a server.

nRemotePort

UDINT

IP port number of the remote server (e.g. 200).

bEnable

BOOL

As long as this input is TRUE, the system attempts to establish a new connection at regular intervals until a connection was established successfully. Once established, a connection can be closed again with FALSE.

tReconnect

TIME

Cycle time used by the function block to try and establish the connection.

FB_ClientServerConnection 3:

Setting the cycle time for the connection

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.

FB_ClientServerConnection 4: Outputs

VAR_OUTPUT
    bBusy      : BOOL;
    bError     : BOOL;
    nErrId     : UDINT;
    hSocket    : T_HSOCKET;
    eState     : E_SocketConnectionState := eSOCKET_DISCONNECTED;
END_VAR

Name

Type

Description

bBusy

BOOL

TRUE, as long as the function block is active.

bError

BOOL

Becomes TRUE if an error code occurs.

nErrID

UDINT

If an bError output is set, this parameter returns the TwinCAT TCP/IP Connection Server error number.

hSocket

T_HSOCKET

Connection handle to 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

E_SocketConnectionState

Returns the current connection status.

Sample 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

FB_ClientServerConnection 5:

Here you can find more application examples (and source code): Samples

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)