Quick Start

The following chapter provides a quick introduction to the TwinCAT TCP/IP product. The instructions are based on the corresponding download in our samples and can be downloaded as a finished TwinCAT project. The individual components of the application are explained in more detail below.

The TwinCAT project implements a TCP/IP Client/Server application, which sends a message to the server as a client and receives the same message back accordingly. The TwinCAT project can be activated directly and starts its program execution immediately. Communication between client and server takes place via localhost.

MAIN

In the MAIN program, the corresponding variable declarations are first made for the client and the server. Client and server are encapsulated by two function blocks, which use the corresponding function blocks from the Tc2_TcpIp library to call the socket functions (Send, Receive, Listen, Connect, etc.).

The following variables are declared for the client:

fbTcpClient1: FB_TCPClient;
sClient1SendData : STRING(255);
nServer1Port : UDINT := 12000;
sServer1Host : T_IPv4Addr := '';
bStartClient1Communication : BOOL := TRUE;
tClient1CycleTime : TIME := T#0.5S;
bClient1SendTrigger : BOOL;
bClient1Connected : BOOL;
sClient1ReceivedData : STRING(255);
bClient1Busy : BOOL;
bClientError1 : BOOL;
nClient1ErrorID1 : UDINT;

The following variables are declared for the server:

fbTcpEchoServer1 : FB_TCPServer;
sServerReceivedData : STRING(255);
bStartServer1Communication : BOOL := TRUE;
bServer1Connected : BOOL;
sServerData : STRING(255);
bServer1Busy : BOOL;
bServer1Error : BOOL;
nServer1ErrorID : UDINT;

The message to be sent is saved in the variable sClient1SendData. Later in the program, the value of this variable is changed cyclically. Specifically, a counter value is appended to the variable using string concatenation.

IF bAutogenerateData AND bClient1Connected THEN
  fbTimer1(IN:= NOT fbTimer1.Q, PT:= tClient1CycleTime, Q=> , ET=> );
  IF fbTimer1.Q THEN
    sClient1SendData := Concat('TestString No.', UDINT_TO_STRING(nCnt));
    fbTcpClient1.bSendTrigger := TRUE;
    nCnt := nCnt + 1;
  ELSE
    fbTcpClient1.bSendTrigger := FALSE;
  END_IF
END_IF

The cycle in which the variable value is set and sent is defined by the timer fbTimer1. The send process is started by setting the variable fbTcpClient1.bSendTrigger.

The server application is represented by the function block instance fbTcpEchoServer1. This is activated cyclically in the program flow so that messages can be received via the server's state machine.

fbTcpEchoServer1(
  sLocalHost := '',
  nLocalPort := nServer1Port,
  bStartCommunication := bStartServer1Communication,
  bConnected => bServer1Connected,
  sSendData => sServerData,
  bBusy=> bServer1Busy,
  bError=> bServer1Error,
  nErrorID=> nServer1ErrorID);

The incoming TCP port of the server is defined via the nLocalPort input. The client connects to this port to exchange data with the server. If you modify this sample so that the communication between client and server is to be carried out via the network, make sure that the server port in your system's firewall is open.

Quick Start 1:

Closing the sockets

When the TwinCAT project is restarted, the variable bInit causes all active socket connections to be closed. This is done at the beginning of the program execution.

FB_TCPServer

This function block encapsulates the server application and uses the function blocks from the Tc2_TcpIp library to set up a socket connection for the server, listen for incoming messages and send back a corresponding response. Specifically, the function blocks FB_SocketAccept, FB_SocketListen, FB_SocketReceive, FB_SocketSend, FB_SocketClose and FB_SocketCloseAll are used for this purpose.

The internal state machine of the function block is based on the following steps:

State

Description

0

Initial state. The process is started via the variable bStartCommunication.

10

In this state, the socket listener is started, i.e. the server application connects to the defined TCP port.

20

In this state, an incoming socket connection is accepted.

30

In this state, a message is received from the connected client.

35

In this state, a message is sent back to the connected client.

40-42

The socket connections are closed in these states.

FB_TCPClient

This function block encapsulates the client application and uses the function blocks from the Tc2_TcpIp library to establish a connection to the server, send a message to the server and receive a corresponding response. Specifically, the function blocks FB_SocketConnect, FB_SocketSend, FB_SocketReceive and FB_SocketClose are used for this purpose.

The internal state machine of the function block is based on the following steps:

State

Description

0

Initial state. The process is started via the variable bStartCommunication.

10

In this state, a connection is established with the server.

15

In this state, a message is sent to the server and the response is processed.

20

In this state, the connection to the server is closed.