FB_SocketReceive

FB_SocketReceive 1:

Using the function block FB_SocketReceive, data from a remote client or remote server can be received via the TwinCAT TCP/IP Connection Server. A remote client connection must first be established via the function block FB_SocketAccept, and a remote server connection via the function block FB_SocketConnect. The data can be received or sent in fragmented form (i.e. in several packets) within a TCP/IP network. It is therefore possible that not all data may be received with a single call of the FB_SocketReceive instance. For this reason, the instance must be called cyclically (polling) within the PLC task, until all required data have been received. During this process, a 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 == null.

In a simple protocol for receiving, for example, a null-terminated string on a remote server, the function block FB_SocketReceive, for example, must be called repeatedly until the null-termination is detected in the data received.

FB_SocketReceive 2:

If the remote device was disconnected from the TCP/IP network (on the remote side only) while the local device is still connected to the TCP/IP network, the function block FB_SocketReceive returns no error and no data. The open socket still exists, but no data are received. The application may then wait endlessly for the remaining frame data bytes. It is recommended to implement timeout monitoring in the application. If not all frame data bytes were received after a certain period, e.g. 10 seconds, the connection must be closed and reinitialized.

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: Connection handle of the communication partner from which data are to be received.

cbLen: Maximum available buffer size in bytes for the data to be read.

pDest: Address (pointer) of the receive buffer.

bExecute: The block is activated by a rising edge at this input.

tTimeout: Maximum time allowed for the execution of the function block.

VAR_OUTPUT

VAR_OUTPUT
    bBusy       : BOOL;
    bError      : BOOL;
    nErrId      : 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.

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

( Standard.Lib; TcBase.Lib; TcSystem.Lib are included automatically )

TwinCAT v2.10.0 Build >= 1301

CX (ARM)