S04: TCP Server Demo (PLC)

This sample describes a TCP server that is implemented in a PLC project.

It accepts a TCP connection, receives TCP packets and returns them to the sender (“echo server”).

Download

Download the sample: https://github.com/Beckhoff/TF6311_Samples/tree/main/S04-TCPServerPlc

1. Get the sample from GitHub, unzip the downloaded ZIP file if necessary
2. Open the project with TwinCAT XAE
3. Select your target system
4. Configure the network card (see below) for the target system
5. Build the sample on your local machine (e.g. Build->Build Solution)
6. Activate the configuration

Description

The interface ITcIoTcpProtocolRecv is implemented and a pointer to a ITcIoTcpProtocol is used analogous to the Quick Starts in this sample.

To this end a PLC block is created, which implements the interface ITcIoUdpProtocolRecv (“Add POU” with “Implements”). It is important to realize the connection to the TCP/UDP RT object in the “FB_init” and “FB_exit” methods. In particular, the Quick Starts illustrate how this OnlineChange can be implemented securely. The procedure is described in more detail in Sample 11 of the C++ documentation.

The implementing function block (in sample TCPServer) calls the method “CheckReceived”. In this way the IP stack is enabled to process incoming packets and transmit callbacks relating to the “ReceiveData” and “ReceiveEvent” methods of the function block.

In order to take into account incoming connections, a port is opened in FB_init via “AllocSocket” and “Listen”. “Accept” is called in the “ReceiveEvent”, if an event to establish a connection has occurred.

In this sample the “ReceiveData” method uses the “SendData” method to return the data to the sender (“echo server”).

Understanding

Two methods are used to establish the communication between the function block and the TcCOM object “TCP/UDP RT”:

This initialization phase can largely be taken from the sample code.

Two methods are responsible for the actual TCP functionality in the PLC code:

In the sample, the “SendData” method is used in the “ReceiveData” method to return the received data:
The TcQueryInterface method must be implemented as follows to ensure that TwinCAT detects that the corresponding interface was implemented:

VAR
ipTcpRecv : ITcIoTcpProtocolRecv;
ipUnknown : ITcUnknown;
END_VAR


IF GuidsEqual(ADR(iid), ADR(TC_GLOBAL_IID_LIST.IID_ITcIoTcpProtocolRecv)) THEN
ipTcpRecv := THIS^; // cast to interface pointer
pipItf^ := ITCUNKNOWN_TO_PVOID(ipUdpRecv);
TcAddRef();
TcQueryInterface := S_OK;
ELSIF GuidsEqual(ADR(iid), ADR(TC_GLOBAL_IID_LIST.IID_ITcUnknown)) THEN
ipUnknown := THIS^; // cast to interface pointer
pipItf^ := ITCUNKNOWN_TO_PVOID(ipUnknown);
TcAddRef();
TcQueryInterface := S_OK;
ELSE
TcQueryInterface := E_HRESULTAdsErr.NOINTERFACE ; //Call super if this fb extends some other
END_IF

The additionally created methods

are inherited by the ITcUnknown interface and are not relevant in this context. For background information we suggest reading the chapter on the TcCOM module concept in the C++ domain.

Preparing the network card

For the TCP/UDP RT module, make sure that the RT Ethernet adapter in the TwinCAT solution is connected with the correct network card (with TwinCAT driver).

S04: TCP Server Demo (PLC) 1:

Local configuration only

Installation of the driver on compatible network cards via the button "Compatible Devices" always takes place locally. On a controller with TwinCAT XAR, the program TcRteInstall.exe can be used. It is included in the installation (usually under C:TwinCAT\3.1\System).

S04: TCP Server Demo (PLC) 2: