FB_ConnectionlessSocket

FB_ConnectionlessSocket 1:

A UDP socket can be managed (opened/generated and closed) with the function block FB_ConnectionlessSocket. FB_ConnectionlessSocket simplifies the implementation of a UDP application by encapsulating the functionality of the two function blocks FB_SocketUdpCreate and FB_SocketClose already internally. The integrated debugging output of the socket status facilitates troubleshooting in the event of configuration or communication errors. In addition, a minimum UDP application only requires an instance of the function block SocketUdpSendTod and/or an instance of the function block FB_SocketUdpReceiveFrom.

In the first step a typical UDP application opens a connection-less UDP socket with the function block FB_ConnectionlessSocket. In the next step instances of FB_SocketUdpSendTo and/or FB_SocketUdpReceiveFrom can be used for exchanging data with another communication device. When a UDP socket is closed depends on the requirements of the application (e.g. in the event of a communication error).

FB_ConnectionlessSocket 2: Inputs

VAR_INPUT
    sSrvNetID    : T_AmsNetID := '';
    nMode        : DWORD := 0;
    sLocalHost   : T_Ipv4Addr := '';
    nLocalPort   : 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:

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

sLocalHost

T_Ipv4Addr

IP address (Ipv4) in string form (e.g. '172.33.5.1') of the local network adapter. An empty string may be specified for the default network adapter.

nLocalPort

UDINT

IP port number (e.g. 200) on the local computer.

bEnable

BOOL

As long as this input is TRUE, attempts are made cyclically to open a UDP socket until a connection has been established. An open UDP socket can be closed again with FALSE.

tReconnect

TIME

Cycle time with which the function block tries to open the UDP socket.

FB_ConnectionlessSocket 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_ConnectionlessSocket 4: Outputs

VAR_OUTPUT
    bBusy   : BOOL;
    bError  : BOOL;
    nErrId  : UDINT;
    hSocket : T_HSOCKET;
    eState  : E_SocketConnectionlessState := eSOCKET_CLOSED;
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 UDP socket. If successful, this variable is transferred to the instances of the function blocks FB_SocketUdpSendTo and/or FB_SocketUdpReceiveFrom.

eState

E_SocketConnectionlessState

Returns the current connection status.

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)