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 will first have to 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 has to be called cyclically (polling) within the PLC task, until all required data have been received. 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.

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

FB_SocketReceive 2:

Set timeout value

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 wait forever for data in this case. It is recommended to implement timeout monitoring in the PLC application. If not all data were received after a certain period, e.g. 10 seconds, the connection has to be closed and reinitialized.

FB_SocketReceive 3: 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

Connection handle of the communication partner from which data are to be received.

cbLen

UDINT

Maximum available buffer size (in bytes) for the data to be read.

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.

FB_SocketReceive 4: Outputs

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

nRecBytes

UDINT

Number of the last successfully received data bytes.

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)