Testing the client and server function blocks

1. Open the TCP/IP client project on the local PC. The IP address of the server has to be adapted to your remote system (initialisation values of the sRemoteHost variables). Load the project into the PLC runtime system on the local PC. Start the PLC.

PROGRAM MAIN
VAR
    fbClient1           : FB_LocalClient := ( sRemoteHost:= '172.16.11.83' (* IP address of remote server! *), nRemotePort:= 200 );
    fbClient2           : FB_LocalClient := ( sRemoteHost:= '172.16.11.83', nRemotePort:= 200 );
    fbClient3                    : FB_LocalClient := ( sRemoteHost:= '172.16.11.83', nRemotePort:= 200 );
    fbClient4                    : FB_LocalClient := ( sRemoteHost:= '172.16.11.83', nRemotePort:= 200 );
    fbClient5                    : FB_LocalClient := ( sRemoteHost:= '172.16.11.83', 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

Up to 5 client instances can be activated by setting the bEnableCientX variable. Each client sends a string (default: 'TEST') to the server approximately every second. 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 activated automatically when the program is started.

2. Open the TCP/IP server project also on the local PC. Select a PLC runtime system on a remote PC as the target system. Load the PLC program into the runtime system. Start the PLC.

PROGRAM MAIN
VAR
    fbServer            : FB_LocalServer := ( sLocalHost := '172.16.11.83' (*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

3. Set the bEnableCilent4 variable in the client project to TRUE. The second client will then attempt to establish a connection with the server. If successful, the 'TEST' string is sent cyclically. Now open the fbClient4 instance of the FB_LocalClient function block.

Testing the client and server function blocks 1:

Double-click to open the dialog for writing the sToString variable. Change the value of the string variable, for example to 'Hello'. Close the dialog with OK. Write the new value into the PLC (CTRL+F7). Shortly afterwards, the value read back by the server can also be seen online.

Testing the client and server function blocks 2:

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

Testing the client and server function blocks 3:

5. In the server and client examples, messages are written into the log book of the operating system during establishment/closing of the connection and in the event of an error. This facilitates troubleshooting. These messages can be displayed in the logger output of the TwinCAT System Manager. Start the TwinCAT System Manager on the local system and activate the logger output. Now deactivate the two clients (set bEnableClient1 and bEnableClient4 to FALSE ).

Testing the client and server function blocks 4:

The server messages can also be displayed on the local PC. To this end, you have to open a second instance of the TwinCAT System Manager on the local PC and select the remote PC as the target system in the TwinCAT System Manager.