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 DATA array
💡 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

NameTypeRange / UnitsDescription
MDB_Start_RegisterUINT0 - 999Start index in the PLC Modbus holding register table.
MDB_Number_of_registersUINT1 - 99Number of consecutive holding registers to copy.
DATA_Start_ElementUINT0 - 99Start index inside the destination DATA array.

Outputs

NameTypeDescription
DATAARRAY[0..99] OF UINTDestination buffer containing copied holding register values.
STS_DoneBOOLTRUE when the copy completed successfully.
STS_FailedBOOLTRUE when the operation failed.
STS_Fault_CodeUSINTDiagnostic code indicating the reason for failure.

Status Codes

CodeMeaningDescription
90Index outside of array boundsOne or more limits were exceeded (max read count, holding register range, or DATA range).
91Unknown errorUnknown 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