MB_InRegRd()

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

Prototype

CPU_INT16U MB_InRegRd (CPU_INT16U reg,

CPU_INT16U * perr)

Arguments

reg

Is the desired input 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 input register number. Note that if your product doesn’t have any floating-point registers but a large number of input 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 input register number you specified is a valid input register and you are able to have code access the value of this input register.

MODBUS_ERR_RANGE

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

Returned Value

MB_InRegRd() returns the current value of the specified input 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 input register number is specified, you should return 0.

Notes / Warnings

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

Called By:

MBS_FC04_InRegRd() in mbs_core.c

Example

In this example, our product has 4 integer variables that we want to assign to input registers. Your systems Engineer decided to assign Modbus input register numbers 1000, 1001, 1002 and 1003 to the four integer values. You will notice that we disable interrupts to access the variables. This is done in case your CPU is an 8-bit CPU and data accesses to 16-bit values are not atomic.


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


CPU_INT16U  MB_InRegRd (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);
    }
}