Get_Modbus_Holding_Registers
Overview
The Get_Modbus_Holding_Registers function block reads values from the PLC Modbus Holding Register map and copies them into a local data array.
The following steps are required to correctly use the function block:
- Select the starting Modbus register
- Define the number of registers to read
- Select the starting index inside the destination
DATAarray
💡 Array Size Limitation
The DATA output array has a fixed size of 100 elements (0..99). Make sure MDB_Number_of_registers does not exceed this limit to avoid out-of-bounds errors.
⚠️ What will trigger a fault on the function block?
The following conditions need to be true, in order to correctly run the function block:
- MDB_Number_of_registers must be within the range
0..99(inclusive) - MDB_Start_Register + MDB_Number_of_registers ≤
1000 - DATA_Start_Element + MDB_Number_of_registers ≤
100
MDB_Start_Register := (UINT)
MDB_Number_of_registers := (UINT)
DATA_Start_Element := (UINT)
DATA => (ARRAY [0..99] OF UINT)
STS_Done => (BOOL)
STS_Failed => (BOOL)
STS_Fault_Code => (USINT)
Interface
Inputs
| Name | Type | Range / Units | Description |
|---|---|---|---|
| MDB_Start_Register | UINT | 0 - 999 | Start index in the PLC Modbus holding register table. |
| MDB_Number_of_registers | UINT | 1 - 99 | Number of consecutive holding registers to copy. |
| DATA_Start_Element | UINT | 0 - 99 | Start index inside the destination DATA array. |
Outputs
| Name | Type | Description |
|---|---|---|
| DATA | ARRAY[0..99] OF UINT | Destination buffer containing copied holding register values. |
| STS_Done | BOOL | TRUE when the copy completed successfully. |
| STS_Failed | BOOL | TRUE when the operation failed. |
| STS_Fault_Code | USINT | Diagnostic code indicating the reason for failure. |
Status Codes
| Code | Meaning | Description |
|---|---|---|
| 90 | Index outside of array bounds | One or more limits were exceeded (max read count, holding register range, or DATA range). |
| 91 | Unknown error | Unknown error occurred during operation. |
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
PROGRAM PROG_Get_Modbus_HR
VAR
ReadHR : GET_MODBUS_HOLDING_REGISTERS;
DestData : ARRAY [0..99] OF UINT; (* Received Modbus data *)
_Done : BOOL;
_Failed : BOOL;
_Fault : USINT;
END_VAR
ReadHR(
MDB_Start_Register := 0,
MDB_Number_of_registers := 10,
DATA_Start_Element := 0,
DATA => DestData,
STS_Done => _Done,
STS_Failed => _Failed,
STS_Fault_Code => _Fault
);
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;
PROGRAM Prog_Instance_GET_MODBUS_HR WITH task1_20ms : PROG_Get_Modbus_HR;
END_RESOURCE
END_CONFIGURATION