FB_IpReceive
The function block FB_IpReceice enables receiving of UDP or TCP telegrams. Which of the two connections is used is specified in FB_IpStartSession.
Received data must be stored in a PLC variable. To this end a pointer to pBuffAddr is required, and the size of the variable has to be entered in cbBuffLen. A positive edge of bValid indicates that the memory contains data or the data of the associated variable are now valid. A positive edge of bClear indicates that the function block is ready to receive data again or, if data are still in the buffer they are copied to the variable next.
With TCP in particular you should monitor how many data were received or are still in the buffer. With TCP/IP data are stored as a "stream", i.e. there is no start or end. With UDP, on the other hand, the content of a UDP frame is always stored in its own buffer.
Four UDP telegrams can be cached; additional UDP telegrams are lost. cbReceive indicates the number of data in bytes copied into the variable. If the buffer contains more data than were read, the number of remaining data is in cbBytesInStream.
sReceiveIPAddr indicates the IP address of the device that has sent data to the Beckhoff controller, along with the corresponding port number iReceivePortNo. Both variables are cleared with bClear. sReceiveIPAddr and iReceivePortNo can be used for sending data back to the device via function block FB_IpSend.

INPUT
VAR_INPUT
bClear : BOOL;
iSession : WORD;
sRemoteIPAddr : STRING(15);
iPortNo : WORD;
pBuffAddr : DWORD;
cbBuffLen : WORD;
END_VAR
bClear: A rising edge clears the memory. The function block is then ready to receive data again.
iSession: Is linked with iSession from function block FB_StartSession.
sRemoteIPAddr: Can be used as filter for approving only a specific IP address.
iPortNo: Can be used as filter for approving only a specific port.
pBuffAddr: With ADR the pointer to the variable is transferred where the data that were received are to be copied.
cbBuffLen: Size of the variable, can be determined with SIZEOF.
OUTPUT
VAR_OUTPUT
bValid : BOOL;
bError : BOOL;
iErrorId : WORD;
cbReceive : WORD;
cbBytesInStream : WORD;
sReceiveIPAddr : STRING(15)
iReceivePortNo : WORD;
END_VAR
bValid: A positive edge change indicates that new data were received. These data are now available in the variable available that was linked via the pointer pBuffAddr.
bError: This output is switched to TRUE as soon as an error occurs during the execution of a command. The command-specific error code is contained in iErrorId.
iErrorId: Contains the command-specific error code of the most recently executed command (see table).
cbReceive: Indicates the number bytes that were copied.
cbBytesInStream: Indicates the data remaining in the memory. This should always be zero. If the value is >0, the variable linked to pBuffAddr is too small. A further read operation is required for obtaining the remaining data.
sReceiveIPAddr: Indicates the IP address of the device that has sent the data. Cleared with positive edge of bClear.
iReceivePortNo: Indicates the port number of the device that has sent the data. Cleared with positive edge of bClear.