TcSocketHelper.lib examples
The examples presented here are based on the functionality offered by TcSocketHelper.Lib.
System requirements:
- TwinCAT version 2.10 Build 1331 (or higher)
- TwinCAT Connection Server v1.0.0.47 or higher installed on the client and server PC;
Communication settings used in the examples:
- 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 address and the IP address accordingly (use the PING command in the prompt to check the connection).
You can test the client and server with the default values on one PC by loading the client application into the first PLC runtime system (801) and the server application into the second PLC runtime system (811).
The behaviour of the PLC project example is determined by the following 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 examples 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.). |
PLC project |
Description |
---|---|
Implementation of an "echo" client/server. The client cyclically sends a test string (sToServer) to the remote server. The server returns the same string to the client unchanged (sFromServer). | |
TcSocketHelper_MultiClientString.zip (Client) TcSocketHelper_MultiServerString.zip (Server) |
As above, with the difference that the server can establish up to 5 connections. The client application has 5 client instances. Each instance establishes a connection to the server. |
A client-server application for the exchanging of binary data. A simple sample protocol is used. 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 headers 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 determined 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 functionality, you have to modify the function blocks FB_ServerApplication and FB_ClientApplication accordingly. | |
TcSocketHelper_MultiClientBinary.zip (Client) TcSocketHelper_MultiServerBinary.zip (Server) |
As above, with the difference that the server can establish up to 5 connections. The client application has 5 client instances. Each instance establishes a connection to the server. |
The client and server applications (FB_ServerApplication, FB_ClientApplication) were implemented as function blocks. The application and the connection can thus be instanced repeatedly.
For troubleshooting purposes you can set the input variable bDbg to TRUE, thereby activating the debugging output for the sent data in the TwinCAT System Manager Log View: