FB_ServerClientConnection

FB_ServerClientConnection 1:

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

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.

FB_ServerClientConnection 2: Inputs

VAR_INPUT
    eMode       : E_SocketAcceptMode := eACCEPT_ALL;
    sRemoteHost : T_IPv4Addr := '';
    nRemotePort : UDINT := 0;
    bEnable     : BOOL;
    tReconnect  : TIME := T#1s;
END_VAR

Name

Type

Description

eMode

E_SocketAcceptMode

Defines whether all or only certain connections are to be accepted.

sRemoteHost

T_IPv4Addr

IP address (Ipv4) in string form (e.g. '172.33.5.1') of the remote client whose connection is to be accepted. For a client on the local computer an empty string may be specified.

nRemotePort

UDINT

IP port number (e.g. 200) of the remote client whose connection is to be accepted.

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 to establish a connection.

FB_ServerClientConnection 3: FB_ServerClientConnection 4: Inputs/outputs

VAR_IN_OUT
    hServer     : T_HSERVER;
END_VAR

Name

Type

Description

hServer

hServer

Server handle. This input variable has to be initialized via the F_CreateServerHnd function.

FB_ServerClientConnection 5: 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 remote 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 in FBD

The following sample illustrates initialization 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:

FB_ServerClientConnection 6:

The first connection is activated (bConnect1 = TRUE), but the connection has not yet been established (passive open).

The second connection has not yet been activated (bConnect2 = FALSE) (closed).

The third connection has been activated (bConnect3 = TRUE) and a connection to the remote client has been established.

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)