FB_SocketUdpReceiveFrom

Using the function block FB_SocketUdpReceiveFrom, data from an open UDP socket can be received via the TwinCAT TCP/IP Connection Server. The UDP socket must first be opened with the function block FB_SocketUdpCreate. The instance of the FB_SocketUdpReceive function block has to be called cyclically (polling) within the PLC task. During this process, an rising edge is generated at the bExecute input, e.g. every 100 ms. If successful, the data received last are copied into the receive buffer. The nRecBytes output returns the number of the last successfully received data bytes. If no new data could be read during the last call, the function block returns no error and nRecBytes == zero.
Inputs
VAR_INPUT
sSrvNetId : T_AmsNetId := '';
hSocket : T_HSOCKET;
cbLen : UDINT;
pDest : POINTER TO BYTE;
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. |
hSocket | T_HSOCKET | Handle of an opened UDP socket, whose data are to be received. |
cbLen | UDINT | Maximum available buffer size (in bytes) for the data to be read. The maximum number of data bytes to be received is limited to 8192 bytes (constant TCPADS_MAXUDP_BUFFSIZE in the library in order to save storage space). |
pDest | POINTER TO BYTE | Address (pointer) of the receive buffer. |
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. |
![]() | Setting the size of the received data bytes Available in product version: TwinCAT TCP/IP Connection Server v1.0.50 or higher: The maximum number of data bytes to be received can be increased (only if absolutely necessary). |
TwinCAT 2
- Redefine global constant in the PLC project (in the sample the maximum number of data bytes to be received is to be increased to 32000):
VAR_GLOBAL CONSTANT
TCPADS_MAXUDP_BUFFSIZE : UDINT := 32000;
END_VAR - Activate option Replace constants in the dialog of the TwinCAT PLC control (Project > Options ... > Build).
- Rebuild Project.
TwinCAT 3
In TwinCAT 3, this value can be edited via a parameter list of the PLC library (from version 3.3.4.0).

Outputs
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
nErrId : UDINT;
sRemoteHost : T_IPv4Addr := '';
nRemotePort : UDINT;
nRecBytes : UDINT;
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. |
sRemoteHost | T_IPv4Addr | If successful, IP address (Ipv4) of the remote device whose data were received. |
nRemotePort | UDINT | If successful, IP port number of the remote device whose data were received (e.g. 200). |
nRecBytes | UDINT | Number of data bytes last successfully received. |
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) |