FB_SocketUdpReceiveFrom
Available in the product version: TwinCAT TCP/IP Connection Server v1,0,0,31 or higher
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 must be called cyclically (polling) within the PLC task. During this process, a rising edge is generated at the bExecute input, e.g. every 100ms. 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.
VAR_INPUT
VAR_INPUT
sSrvNetId : T_AmsNetId := '';
hSocket : T_HSOCKET;
cbLen : UDINT;
pDest : DWORD;
bExecute : BOOL;
tTimeout : TIME := T#5s;
END_VAR
sSrvNetId: string containing the network address of the TwinCAT TCP/IP Connection Server. For the local computer (default) an empty string may be specified.
hSocket: the handle of an opened UDP socket whose data are to be received.
cbLen: the 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 by default (by declaring the TCPADS_MAXUDP_BUFFSIZE constant in the library to conserve memory resources).
pDest: the address (pointer) of the receive buffer.
bExecute: the function block is enabled via a positive edge at this input.
tTimeout: maximum time that may not be exceeded when the function block is executed.
From product version: TwinCAT TCP/IP Connection Server v1.0.50 and higher, the maximum number of data bytes to be sent can be increased (if absolutely necessary). |
1) In the PLC project, redefine the global constant (in our sample we want to increase the maximum number of data bytes to 32000 bytes):
VAR_GLOBAL CONSTANT
TCPADS_MAXUDP_BUFFSIZE : UDINT :=32000;
END_VAR
2) Then activate the option "Replace constants" in TwinCAT PLC Control->"Project->Options...->Build" dialog box.
3) Compile the project.
VAR_OUTPUT
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
nErrId : UDINT;
sRemoteHost : STRING(15);
nRemotePort : UDINT;
nRecBytes : UDINT;
END_VAR
bBusy: When the function block is activated this output is set. It remains set until and acknowledgement is received.
bError: If an error should occur during the transfer of the command, then this output is set once the bBusy output was reset.
nErrId : If the bError output is set, this parameter returns the TwinCAT TCP/IP Connection Server error number.
sRemoteHost: If successful, IP address (Ipv4) of the remote device whose data were received.
nRemotePort: If successful, IP port number of the remote device whose data were received (e.g. 200).
nRecBytes: Number of the last successfully receive data bytes.
Requirements
Development environment | Target system type | PLC libraries to be linked |
---|---|---|
TwinCAT version 2.8.0 or higher | PC or CX (x86) | TcpIp.Lib (v1.0.4 or higher) ( Standard.Lib; TcBase.Lib; TcSystem.Lib are included automatically ) |
TwinCAT v2.10.0 Build >= 1301 | CX (ARM) |