FB_SocketUdpCreate

FB_SocketUdpCreate 1:

The function block FB_SocketUdpCreate can be used to open a client/server socket for the User Datagram Protocol (UDP). If successful, a new socket is opened, and the associated socket handle is returned at the hSocket output. The handle is required by the function blocks FB_SocketUdpSendTo and FB_SocketUdpReceiveFrom, for example, in order to exchange data with a remote device. If a UDP socket is no longer required, it can be closed with the function block FB_SocketClose. The port address nLocalHost is internally reserved by the TCP/IP Connection Server for the UDP protocol (a "bind" is carried out). Several network adapters may exist in a PC. The input parameter sLocalHost determines the network adapter to be used. If the sLocalHost input variable is ignored (empty string), the TCP/IP Connection Server uses the default network adapter. This is usually the first network adapter from the list of the network adapters in the Control Panel.

FB_SocketUdpCreate 2:

Automatically created network connections

If an empty string was specified for sLocalHost when FB_SocketUdpCreate was called and the PC was disconnected from the network, the system will open a new socket under the software loopback IP address: '127.0.0.1'.

FB_SocketUdpCreate 3:

Automatically created network connections with several network adapters

If two or more network adapters are installed in the PC and an empty string was specified as sLocalHost, and the default network adapter was then disconnected from the network, the new socket will be opened under the IP address of the second network adapter.

FB_SocketUdpCreate 4:

Setting a network address

In order to prevent the sockets from being opened under a different IP address, you can specify the sLocalHost address explicitly or check the returned address in the handle variable (hSocket), close the socket and re-open it.

FB_SocketUdpCreate 5: Inputs

VAR_INPUT
    sSrvNetId  : T_AmsNetId := '';
    sLocalHost : T_IPv4Addr := '';
    nLocalPort : UDINT;
    bExecute   : BOOL;
    tTimeout   : TIME:= T#5s;
END_VAR

Name

Type

Description

sSrvNetId

T_AmsNetId

String containing the network address of the TwinCAT TCP/IP Connection Server. For the local computer (default) an empty string may be specified.

sLocalHost

T_IPv4Addr

Local IP address (Ipv4) of the UDP client/server socket as a string (e.g. '172.33.5.1'). An empty string may be specified for the default network adapter.

nLocalPort

UDINT

Local IP port number of the UDP client/server socket (e.g. 200).

bExecute

BOOL

The function block is enabled by a positive edge at this input.

tTimeout

TIME

Maximum time allowed for the execution of the function block.

FB_SocketUdpCreate 6: Outputs

VAR_OUTPUT
    bBusy     : BOOL;
    bError    : BOOL;
    nErrId    : UDINT;
    hSocket   : T_HSOCKET;
END_VAR

Name

Type

Description

bBusy

BOOL

This output is active if the function block is activated. It remains active until acknowledgement.

bError

BOOL

If an error should occur during the transfer of the command, then this output is set once the bBusy output was reset.

nErrId

UDINT

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

hSocket

T_HSOCKET

Handle of the newly opened UDP client/server socket.

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)