Set_Modbus_Holding_Registers

Overview

The Set_Modbus_Holding_Registers function block writes values from a local data array into the PLC Modbus Holding Register map. The following steps are required to correctly use the function block:

  • Select the starting Modbus register
  • Define the number of registers to write
  • Select the starting index inside the source DATA array
  • Provide the DATA array containing the values to be written
💡 Array Size Limitation

The DATA input 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 to write to.
MDB_Number_of_registersUINT1 - 99Number of consecutive holding registers to write.
DATA_Start_ElementUINT0 - 99Start index inside the source DATA array.
DATAARRAY[0..99] OF UINTSource buffer containing the values to be written into holding registers.

Outputs

NameTypeDescription
STS_Set_Data_DoneBOOLTRUE when the write completed successfully.
STS_Set_Data_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 write 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_Set_Modbus_HR
    VAR
        WriteHR : SET_MODBUS_HOLDING_REGISTERS;
        SrcData : ARRAY [0..99] OF UINT := [10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
    END_VAR

    SrcData[9] := SrcData[9] + 1;
    WriteHR(
        MDB_Start_Register      := 0,
        MDB_Number_of_registers := 10,
        DATA_Start_Element      := 0,
        DATA                    := SrcData
    );
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_SET_MODBUS_HR WITH task1_20ms : PROG_Set_Modbus_HR;
    END_RESOURCE
END_CONFIGURATION