TF6311 TwinCAT 3 TCP/UDP Realtime implementation.
Build in TwinCAT 4026.20 and tested with a C6015 with TC/BSD 14.2.3.5.
This library can be used for the TF6311 when you want real-time performance of your TCP/UDP. I made this library because the TF6310 could not keep with the performance when running 1ms task cycles and the UDP performance was bad.
The TF6311 lets you have direct access to the hardware, but is hard to implement and there was some error in Beckhoff documentation and the example code was outdated. The PLC code have a interface pointer to the hardware where the Ethernet RT module can interrupt the code when it receives a new message and the ReceiveData method can handle the message. There is no buffer from the hardware so you have to handle data in the interrupt. I have implemented a ring buffer for MEMCPY the data to a 32kB buffer where you can call the Receive method to get data from the ring buffer. Ring buffer size is adjustable from parameter list TcTcpUdpRT_Param.
The ReceiveData method can't push back on the TCP stack if the ring buffer is full and get the sender to retransmit the data, so the data will be lost breaking the concept of TCP. Beckhoff have agreed to add that feature. You will get an error from the FB if the buffer is full or the received message can't fit in the ring buffer.
Beckhoff documentation for Quick Start
-
Start by adding a Real-Time Ethernet Adapter to your I/O. Right click on the
Devicesunder theI/Oand clickAdd New Item.... -
Select the Ethernet adapter for the target in the
Adaptertab.- If you are the same code to multiple IPC, you can check the
Virtual Device Namesso it will ignore the MAC address of the adapter and only use the name to find the adapter. - Beckhoff documentation on Ethernet adapter
- If you are the same code to multiple IPC, you can check the
-
Now you need to add the TCP/UDP RT module to the Ethernet adapter. Right click in the Ethernet adater adn click
Add Object(s). -
Set the interface pointer for the TCP/UDP module to the point to the Ethernet adapter in the
Interface Pointertab. -
Assign the TCP/UDP RT module to a task in the
Contexttab.
Beckhoff documentation for Multitask access to network card
You can have multiple TCP/UDP RT modules on one Ethernet adapter, but only one of module should be fetching data from the network card. The module with the fastest task with a low priority should be the one to fetch data from the network card. The other modules should be set to passive mode under the Parameter (Init) tab.
Beckhoff documentation on TCP/UDP RT module parameters
Beckhoff documentation on TCP/UDP RT module diagnostics
There are 3 FB's with matching interfaces:
- TcTcpServer and ITcpServer
- TcTcpClient and ITcpClient
- TcUdpSendReceive and IUdpSendReceive
They each have a Run method that needs to be called cyclic to maintain the communication between the PLC code and IO!
The instance of the TcTcpServer, TcTcpClient and TcUdpSendReceive needs to be assigned to the TCP/UDP RT module.
-
Build the project so the symbols are available.
-
Go to the PLC instance and find the
Symbol Initializationtab. -
Set the value to the Object ID of the TCP/UDP RT module.
- You can have multiple instance assigned to the same module as long as the are called by the same task.






