Set_Ethernet_Config

Overview

The Set_Ethernet_Config function block configures the PLC Ethernet interface, including:

  • Network operation mode (Modbus TCP Server / MQTT Client / MQTTS Client)
  • IP allocation method (Static / DHCP)
  • IP Address
  • Subnet Mask
  • Gateway
  • DNS Server
  • Port
💡 When are settings applied?

The Ethernet configuration is applied immediately upon successful execution of the function block. Changes to the network settings will take effect without requiring a PLC restart. The settings become visible on the OLED as soon as connection to the network is established.

⚠️ Should the function only be called once?

No special care is required, like a first scan call to apply settings.
The function block is called whenever changes in the settings are detected.

Network_Operation_Mode      := (USINT)
IP_Allocation_Method        := (USINT)
IP_Address[4]               := (USINT)
Subnet_Mask[4]              := (USINT)
Gateway[4]                  := (USINT)
DNS[4]                      := (USINT)
Port                        := (INT)
STS_Done                    => (BOOL)
STS_Failed                  => (BOOL)
STS_Fault_Code              => (USINT)

Interface

Inputs

NameTypeRange / UnitsDescription
Network_Operation_ModeUSINT1-3Selects the Ethernet application mode: 1 = Modbus TCP Server, 2 = MQTT Client, 3 = MQTTS Client.
IP_Allocation_MethodUSINT0-1IP assignment: 0 = Static, 1 = DHCP.
IP_AddressARRAY[0..3] OF USINT0-255Static IP address (e.g. [192,168,11,2]).
Subnet_MaskARRAY[0..3] OF USINT0-255Subnet mask (e.g. [255,255,255,0]).
GatewayARRAY[0..3] OF USINT0-255Default gateway (e.g. [192,168,11,1]).
DNSARRAY[0..3] OF USINT0-255DNS server (e.g. [8,8,8,8]).
PortINT0-65535TCP port used by the selected mode (e.g. 5000, 1883, 8883, etc.).

Outputs

NameTypeRange / UnitsDescription
STS_DoneBOOL0 / 1TRUE when all configuration data has been successfully sent to the module.
STS_FailedBOOL0 / 1TRUE if the configuration data write operation failed.
STS_Fault_CodeUSINT#Diagnostic code indicating the reason for failure.

Status Codes

CodeMeaningDescription
90Invalid modeNetwork_Operation_Mode is outside the supported PLC range (expected 1..3).
91Invalid IP allocation methodIP_Allocation_Method is invalid (expected 0 = Static, 1 = DHCP).
92Invalid portPort is outside the valid range (1..65535).
93Invalid IP formatOne or more IPv4 parameters are invalid in Static mode (e.g. IP_Address, Gateway, or DNS is 0.0.0.0 / 255.255.255.255).
94Invalid subnet maskSubnet_Mask is not a valid contiguous IPv4 subnet mask (or is 0.0.0.0 / 255.255.255.255).
95Gateway not in same subnetIn Static mode, Gateway is not in the same subnet as IP_Address when applying Subnet_Mask.
96Unknown errorUnknown error occurred during operation.
97Module FaultedThe COMMS module reported an internal fault.
98Module mismatchSlot 1 is populated but not a COMMS module.
99Module not foundCOMMS module is not part of the current hardware configuration.

Example

PROGRAM PROG_Set_Ethernet_Config
    VAR
        EthCfg : SET_ETHERNET_CONFIG;
        _IP_Address : ARRAY [0..3] OF USINT := [192, 168, 11, 2];
        _Subnet_Mask : ARRAY [0..3] OF USINT := [255, 255, 255, 0];
        _Gateway : ARRAY [0..3] OF USINT := [192, 168, 11, 1];
        _DNS : ARRAY [0..3] OF USINT := [8, 8, 8, 8];
    END_VAR

    EthCfg(
        Network_Operation_Mode := 1,                
        IP_Allocation_Method   := 0,                
        IP_Address             := _IP_Address,
        Subnet_Mask            := _Subnet_Mask,
        Gateway                := _Gateway,
        DNS                    := _DNS,
        Port                   := 5000
    );
END_PROGRAM

CONFIGURATION Config0
    RESOURCE Res0 ON PLC
        TASK task1_20ms(INTERVAL := T#20ms, PRIORITY := 1);
        PROGRAM Prog_Instance_SET_ETHERNET_CONFIG WITH task1_20ms : PROG_Set_Ethernet_Config;
    END_RESOURCE
END_CONFIGURATION