MB_HoldingRegRd()

MB_HoldingRegRd() is called when a Modbus master sends a Function Code 3 command. MB_HoldingRegRd() read the value of a single holding register. Integer holding registers are numbered from 0 through (MODBUS_CFG_FP_START_IX – 1) . MODBUS_FP_START_IX allows you to specify the start of ‘floating-point’ (see section 5.07, MD_HoldingRegRdFP() ). MB_HoldingRegRd() should only be called by µC/Modbus.

Prototype

CPU_INT16U MB_HoldingRegRd (CPU_INT16U reg,

CPU_INT16U * perr)

Arguments

reg

Is the desired holding register to read and can be a number between 0 and MODBUS_CFG_FP_START_IX-1 (depending on your product). It is up to you to decide what application variable is assigned to each holding register number. Note that if your product doesn’t have any floating-point registers but a large number of holding registers, you can set MODBUS_CFG_FP_START_IX to 65535.

perr

Is a pointer to a variable that will contain an error code based on the outcome of the call. Your code thus needs to return one of the following error codes:

MODBUS_ERR_NONE

if the holding register number you specified is a valid holding register and you are able to have code access the value of this holding register.

MODBUS_ERR_RANGE

if the holding register number passed as an argument is not a valid holding register number for your product.

Returned Value

MB_HoldingRegRd() returns the current value of the specified holding register as an unsigned value. Of course, you can also return ‘signed’ values but those need to be cast to CPU_INT16U. You should note that the value will not be changed if you cast a signed variable to CPU_INT16U. The Modbus master will receive the proper value and it’s up to the Modbus master to properly retrieve the signed data . If an invalid holding register number is specified, you should return 0.

Notes / Warnings

Code is enabled when MODBUS_CFG_FC03_EN is set to DEF_ENABLED in your product’s mb_cfg.h file.

Called By:

MBS_FC03_HoldingRegRd() in mbs_core.c

Example


CPU_INT16S   AppTemp;
CPU_INT16U   AppCtr;
CPU_INT16S   AppPres;
CPU_INT16U   AppRxPktCtr;


CPU_INT16U  MB_HoldingRegRd (CPU_INT16U reg, CPU_INT16U *perr)
{
    CPU_INT16U  val;


    *perr = MODBUS_ERR_NONE;
    switch (reg) {
        case 1000:
            CPU_CRITICAL_ENTER();
            val = (CPU_INT16U)AppTemp;
            CPU_CRITICAL_EXIT();
            return (val);

        case 1001:
            CPU_CRITICAL_ENTER();
            val = AppCtr;
            CPU_CRITICAL_EXIT();
            return (val);
 
        case 1002:
            CPU_CRITICAL_ENTER();
            val = (CPU_INT16U)AppPres;
            CPU_CRITICAL_EXIT();
            return (val);

        case 1003:
            CPU_CRITICAL_ENTER();
            val = AppRxPktCtr;
            CPU_CRITICAL_EXIT();
            return (val);

        default:
            *perr = MODBUS_ERR_RANGE;
            return (0);
    }
}