MB_InRegRdFP()

MB_InRegRdFP() is called when a Modbus master sends a Function Code 4 command. MB_InRegRdFP() read the value of a single input register but, it assumes that you are trying to access a floating-point variable. Floating-point input registers are numbered from MODBUS_CFG_FP_START_IX to 65535 (or less if you don’t have a lot of floating-point registers). MODBUS_CFG_FP_START_IX allows you to specify the start of ‘floating-point’. MB_InRegRdFP() should only be called by µC/Modbus.

Prototype

CPU_FP32 MB_InRegRdFP (CPU_INT16U reg,

CPU_INT16U *perr)

Arguments

reg Is the desired input register to read and can be a number between MODBUS_CFG_FP_START_IX and 65535 (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_InRegRdFP() returns the current value of the specified floating?point input register as a 32-bit IEEE-754 unsigned value. If an invalid input register number is specified, you should return (CPU_FP32)0.

Notes / Warnings

Code is enabled when both MODBUS_CFG_FC04_EN is set to DEF_ENABLED and MODBUS_CFG_FP_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 floating-point variables that we want to assign to input registers. Your systems Engineer decided to assign MODBUS input register numbers MODBUS_CFG_FP_START_IX+0, MODBUS_CFG_FP_START_IX+1, MODBUS_CFG_FP_START_IX+2 and MODBUS_CFG_FP_START_IX+3 to the four floating?point values. You will notice that we disable interrupts to access the variables. This is done in case your CPU does not allow atomic access to the 32-bit floating-point values.


CPU_FP32   AppTempAir;
CPU_FP32   AppTempFuel;
CPU_FP32   AppPresAir;
CPU_FP32   AppPresFuel;


CPU_FP32  MB_InRegRdFP (CPU_INT16U reg, CPU_INT16U *perr)
{
    CPU_FP32  val;


    *perr = MODBUS_ERR_NONE;
    switch (reg) {
        case MODBUS_CFG_FP_START_IX + 0:
            CPU_CRITICAL_ENTER();
            val = AppTempAir;
            CPU_CRITICAL_EXIT();
            return (val);

    case MODBUS_CFG_FP_START_IX + 1:
        CPU_CRITICAL_ENTER();
        val = AppTempFuel;
        CPU_CRITICAL_EXIT();
        return (val);
 
    case MODBUS_CFG_FP_START_IX + 2:
        CPU_CRITICAL_ENTER();
        val = AppPresAir;
        CPU_CRITICAL_EXIT();
        return (val);

    case MODBUS_CFG_FP_START_IX + 3:
        CPU_CRITICAL_ENTER();
        val = AppPresFuel;
        CPU_CRITICAL_EXIT();
        return (val);

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