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
| Name | Type | Range / Units | Description |
|---|---|---|---|
| Network_Operation_Mode | USINT | 1-3 | Selects the Ethernet application mode: 1 = Modbus TCP Server, 2 = MQTT Client, 3 = MQTTS Client. |
| IP_Allocation_Method | USINT | 0-1 | IP assignment: 0 = Static, 1 = DHCP. |
| IP_Address | ARRAY[0..3] OF USINT | 0-255 | Static IP address (e.g. [192,168,11,2]). |
| Subnet_Mask | ARRAY[0..3] OF USINT | 0-255 | Subnet mask (e.g. [255,255,255,0]). |
| Gateway | ARRAY[0..3] OF USINT | 0-255 | Default gateway (e.g. [192,168,11,1]). |
| DNS | ARRAY[0..3] OF USINT | 0-255 | DNS server (e.g. [8,8,8,8]). |
| Port | INT | 0-65535 | TCP port used by the selected mode (e.g. 5000, 1883, 8883, etc.). |
Outputs
| Name | Type | Range / Units | Description |
|---|---|---|---|
| STS_Done | BOOL | 0 / 1 | TRUE when all configuration data has been successfully sent to the module. |
| STS_Failed | BOOL | 0 / 1 | TRUE if the configuration data write operation failed. |
| STS_Fault_Code | USINT | # | Diagnostic code indicating the reason for failure. |
Status Codes
| Code | Meaning | Description |
|---|---|---|
| 90 | Invalid mode | Network_Operation_Mode is outside the supported PLC range (expected 1..3). |
| 91 | Invalid IP allocation method | IP_Allocation_Method is invalid (expected 0 = Static, 1 = DHCP). |
| 92 | Invalid port | Port is outside the valid range (1..65535). |
| 93 | Invalid IP format | One 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). |
| 94 | Invalid subnet mask | Subnet_Mask is not a valid contiguous IPv4 subnet mask (or is 0.0.0.0 / 255.255.255.255). |
| 95 | Gateway not in same subnet | In Static mode, Gateway is not in the same subnet as IP_Address when applying Subnet_Mask. |
| 96 | Unknown error | Unknown error occurred during operation. |
| 97 | Module Faulted | The COMMS module reported an internal fault. |
| 98 | Module mismatch | Slot 1 is populated but not a COMMS module. |
| 99 | Module not found | COMMS 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