Sample04: Binary data exchange
This sample uses the functions of the former TcSocketHelper.Lib, which is now integrated into the Tc2_TcpIp library. It shows a client/server PLC application based on the functions of the former SocketHelper library.
This sample provides a client-server application for the exchange of binary data. A simple sample protocol has been implemented for this purpose. The length of the binary data and a frame counter for the sent and received telegrams are transferred in the protocol header.
The structure of the binary data is defined by the PLC structure ST_ApplicationBinaryData. The binary data are appended to the header and transferred. The instances of the binary structure are called toServer, fromServer on the client side and toClient, fromClient on the server side.
The structure declaration on the client and server sides can be adapted as required. The structure declaration must be identical on both sides.
The maximum size of the structure must not exceed the maximum buffer size of the send/receive Fifos. The maximum buffer size is defined by a constant.
The server functionality is implemented in the function block FB_ServerApplication and the client functionality in the function block FB_ClientApplication.
In the standard implementation the client cyclically sends the data of the binary structure to the server and waits for a response from the server. The server modifies some data and returns them to the client.
If you require a specific function, you must modify the function blocks FB_ServerApplication and FB_ClientApplication accordingly.

System requirements
- TwinCAT 3 Build 3093 or higher
- TwinCAT 3 Function TF6310 TCP/IP
- If two computers are used to execute the sample (one client and one server), the Function TF6310 needs to be installed on both computers
- If one computer is used to execute the sample, e.g. client and server running in two separate PLC runtimes, both PLC runtimes need to run in separate tasks.
Project information
The default communication settings used in the above samples are as follows:
- PLC client application: Port and IP address of the remote server: 200, '127.0.0.1'
- PLC server application: Port and IP address of the local server: 200, '127.0.0.1'
To test the client and server application on two different PCs, you have to adjust the port and the IP address accordingly.
However, you can also test the client and server samples with the default values on a single computer by loading the client application into the first PLC runtime system and the server application into the second PLC runtime system.
The behavior of the PLC project sample is determined by the following global variables/constants.
Constant | Value | Description |
---|---|---|
PLCPRJ_MAX_CONNECTIONS | 5 | Max. number of server->client connections. A server can establish connections to more than one client. A client can establish a connection to only one server at a time. |
PLCPRJ_SERVER_RESPONSE_TIMEOUT | T#10s | Max. delay time (timeout time) after which a server should send a response to the client. |
PLCPRJ_CLIENT_SEND_CYCLE_TIME | T#1s | Cycle time based on which a client sends send data (TX) to the server. |
PLCPRJ_RECEIVER_POLLING_CYCLE_TIME | T#200ms | Cycle time based on which a client or server polls for receive data (RX). |
PLCPRJ_BUFFER_SIZE | 10000 | Max. internal buffer size for RX/TX data. |
The PLC samples define and use the following internal error codes:
Error code | Value | Description |
---|---|---|
PLCPRJ_ERROR_RECEIVE_BUFFER_OVERFLOW | 16#8101 | The internal receive buffer reports an overflow. |
PLCPRJ_ERROR_SEND_BUFFER_OVERFLOW | 16#8102 | The internal send buffer reports an overflow. |
PLCPRJ_ERROR_RESPONSE_TIMEOUT | 16#8103 | The server has not sent the response within the specified timeout time. |
PLCPRJ_ERROR_INVALID_FRAME_FORMAT | 16#8104 | The telegram formatting is incorrect (size, faulty data bytes etc.). |
The client and server applications (FB_ServerApplication, FB_ClientApplication) were implemented as function blocks. The application and the connection can thus be instanced repeatedly.