S02: UDP Demo (PLC)
This example describes a UDP server that is implemented in a PLC project.
It receives UDP packets and returns them to the sender ("echo server“).
Download
Download the sample: https://github.com/Beckhoff/TF6311_Samples/tree/main/S02-UdpDemoPlc
- 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 sample is also available in more detail as Quick Start.
The interface ITcIoUdpProtocolRecv is implemented and a pointer to a ITcIoUdpProtocol is used analogous to the Quick Start 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. This procedure is described in more detail in Sample 11 of the C++ documentation.
The implementing function block (in sample UdpReceiver) calls the method “CheckReceived”. In this way the IP stack is enabled to process incoming packets and transmit callbacks on the “ReceiveData” method of the function block.
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":
- "FB_init": This is executed automatically when the PLC is started
- "FB_exit": This is executed automatically when the PLC is stopped
This initialization phase can largely be taken from the sample code.
Two methods are responsible for the actual UDP functionality in the PLC code:
- The "ReceiveData" method in the implemented function block receives the data.
- The "SendData" method in the ITcIoUdpProtocol interface sends data.
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
ipUdpRecv : ITcIoUdpProtocolRecv;
ipUnknown : ITcUnknown;
END_VAR
IF GuidsEqual(ADR(iid), ADR(TC_GLOBAL_IID_LIST.IID_ITcIoUdpProtocolRecv)) THEN
ipUdpRecv := 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
- TcAddRef / TcRelease
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).
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). |