Integration in TwinCAT and Test

The following section describes how to prepare and start the PLC server and client. The PLC samples are provided as TwinCAT 3 PLC project files. To import a PLC project into TwinCAT XAE, first create a new TwinCAT 3 Solution. Then select the Add Existing Item command in the context menu of the PLC node and select the downloaded sample file (Plc 3.x Project archive (*.tpzip)) as the file type in the dialog that opens. After confirming the dialog, the PLC project is added to the Solution.

Integration in TwinCAT and Test 1:

Sample PLC server

Create a new TwinCAT 3 solution in TwinCAT XAE and import the TwinCAT TCP/IP server project. Select a target system. The function must also be installed on the target system and licenses for TF6310 must have been generated. Keep the TwinCAT 3 Solution open.

PROGRAM MAIN
VAR
    fbServer         : FB_LocalServer := ( sLocalHost := '127.0.0.1' (*own IP address!*), nLocalPort := 200 );
    bEnableServer    : BOOL := TRUE;
    fbSocketCloseAll : FB_SocketCloseAll := ( sSrvNetID := '', tTimeout := DEFAULT_ADS_TIMEOUT );
    bCloseAll        : BOOL := TRUE;
END_VAR
 

IF bCloseAll THEN (*On PLC reset or program download close all old connections *)
    bCloseAll := FALSE;
    fbSocketCloseAll( bExecute:= TRUE );
ELSE
    fbSocketCloseAll( bExecute:= FALSE );
END_IF

IF NOT fbSocketCloseAll.bBusy THEN
    fbServer( bEnable := bEnableServer );
END_IF

Sample PLC client

Import the TCP/IP client project into the TwinCAT 3 Solution as a second PLC project. Link this PLC project to a different task than the server sample. The IP address of the server must be adapted to your system (initialization values of the sRemoteHost variable). In this case, the server is on the same PC, so enter 127.0.0.1. Enable the configuration, log in and start the server and then the client PLC project.

PROGRAM MAIN
VAR
    fbClient1     : FB_LocalClient := ( sRemoteHost:= '127.0.0.1' (* IP address of remote server! *), nRemotePort:= 200 );
    fbClient2     : FB_LocalClient := ( sRemoteHost:= '127.0.0.1', nRemotePort:= 200 );
    fbClient3     : FB_LocalClient := ( sRemoteHost:= '127.0.0.1', nRemotePort:= 200 );
    fbClient4     : FB_LocalClient := ( sRemoteHost:= '127.0.0.1', nRemotePort:= 200 );
    fbClient5     : FB_LocalClient := ( sRemoteHost:= '127.0.0.1', nRemotePort:= 200 );

    bEnableClient1 : BOOL := TRUE;
    bEnableClient2 : BOOL := FALSE;
    bEnableClient3 : BOOL := FALSE;
    bEnableClient4 : BOOL := FALSE;
    bEnableClient5 : BOOL := FALSE;

    fbSocketCloseAll    : FB_SocketCloseAll := ( sSrvNetID := '', tTimeout := DEFAULT_ADS_TIMEOUT );
    bCloseAll     : BOOL := TRUE;

    nCount     : UDINT;
END_VAR
IF bCloseAll THEN (*On PLC reset or program download close all old connections *)
    bCloseAll := FALSE;
    fbSocketCloseAll( bExecute:= TRUE );
ELSE
    fbSocketCloseAll( bExecute:= FALSE );
END_IF

IF NOT fbSocketCloseAll.bBusy THEN
    nCount := nCount + 1;
    fbClient1( bEnable := bEnableClient1, sToServer := CONCAT( 'CLIENT1-', UDINT_TO_STRING( nCount ) ) );
    fbClient2( bEnable := bEnableClient2, sToServer := CONCAT( 'CLIENT2-', UDINT_TO_STRING( nCount ) ) );
    fbClient3( bEnable := bEnableClient3, sToServer := CONCAT( 'CLIENT3-', UDINT_TO_STRING( nCount ) ) );
    fbClient4( bEnable := bEnableClient4 );
    fbClient5( bEnable := bEnableClient5 );
END_IF

When setting one of the bEnableCientX variables, up to five client instances can be enabled. Each client sends one string per second to the server (default: 'TEST'). The server returns the same string to the client (echo server). For the test, a string with a counter value is generated automatically for the first three instances. The first client is enabled automatically when the program is started. Set the bEnableCilent4 variable in the client project to TRUE. The new client then attempts to establish a connection to the server. If successful, the 'TEST' string is sent cyclically. Now open the fbClient4 instance of the FB_LocalClient function block. Double-click to open the dialog for writing the sToString variable and change the value of the string variable to 'Hello', for example.

Integration in TwinCAT and Test 2:

Close the dialog with OK. Force the new value into the PLC. Shortly afterwards, the value returned by the server can be viewed online.

Integration in TwinCAT and Test 3:

Now open the fbServer instance of the FB_LocalServer function block in the server project. The string: 'Hello' can be seen in the online data of the server.

Integration in TwinCAT and Test 4: