Adjustment of the IP Adress through TAG`s

IP Change via Application

Controller`s IP address, sub-net mask and gateway changes can be performed at runtime by the PLC application using function blocks available in the SysSocket library of CODESYS(more information may be consulted on Online Help).

The first step is to add the SysSocket Implementation library to the project as follows:

The SysTypes2 Interfaces library will also need to be added to the project:

Three functions of this library will be used:

  •          SysSockSetIpAddress
  •          SysSockSetSubnetMask
  •          SysSockSetDefaultGateway

The function blocks may be used on UserPrg, StartPrg or in any other POU as the user prefers to apply. Example of application of function blocks in the ST language for an XP3xx controller below:

For this example, when bChangeIp is set to TRUE, the `eth0` interface (which represents Xpress NET1 interface) will receive the IP address, sub-net mask and gateway address settings from the respective variables. The FBs are called in sequence in condition to their returns (Result, Result2 and Result3), ‘0’ indicate that the functions executed successfully.

The Ethernet interface identification string is specific to each controller model, as shown below:

NOTE 1: when applying the new IP configuration via application there will be a disconnection with BCS Tools(time-out). To Login back into the controller it is necessary to rescan the network on the Gateway and select the device. It is not necessary to change the NET1 configuration in the project.

NOTE 2: this configuration change is not retentive, so when rebooting the controller it will return to the IP address configured in the NET1 interface of the project.

    PROGRAM ChangeIPsett


    bChangeIp   : BOOL;


    szInterface             : STRING    := `eth0`;

    szInterfaceGateway      : WSTRING   := "eth0";

    szNewIp                 : STRING    := ``;

    szNewMask               : STRING    := ``;

    NewGatewayAddr          : SysSocket_Implementation.SysSocket_Interfaces.INADDR;

    Result      : RTS_IEC_RESULT;

    Result2     : RTS_IEC_RESULT;

    Result3     : RTS_IEC_RESULT;



    IF bChangeIp THEN

    Result := SysSockSetIPAddress(szInterface, szNewIp);


    IF Result = 0 THEN

         Result2 := SysSockSetSubnetMask(szNewIp, szNewMask);


         IF Result2 = 0 THEN






               Result3 := SysSockSetDefaultGateway(szInterfaceGateway, NewGatewayAddr);




    bChangeIp := FALSE;


Do you have any questions? Then talk to us!


Was this publication relevant to you? Evaluate the material so we can continue to improve.

Click to save rating
Liked? Then share

Subscribe to our newsletter and learn all about automation!

Receive news about the automation market, our solutions and the latest actions involving Altus directly in your e-mail.