MB_HoldingRegWrFP()

MB_HoldingRegWrFP() is called when a Modbus master sends a Function Code 6 and Function Code 16 command. MB_HoldingRegWrFP() writes a single floating?point holding register value. Floating-point holding registers are numbered from MODBUS_CFG_FP_START_IX to 65535. In other words, MODBUS_CFG_FP_START_IX allows you to specify the start of ‘floating-point’ holding register addresses. MB_HoldingRegWrFP() should only be called by µC/Modbus.

Prototype

void MB_HoldingRegWrFP (CPU_INT16U reg,
                        CPU_FP32 reg_val,
                        CPU_INT16U *perr)

Arguments

reg

Is the desired holding 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 floating-point holding register number. Note that if your product doesn’t have any floating-point registers but a large number of integer holding registers, you can set MODBUS_CFG_FP_START_IX to 65535.

reg_val

Is the desired value for the specified holding register and can be any IEEE-754 floating point value.

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 floating-point holding register number you specified is a valid floating-point holding register and you are able to have code access the value of this floating-point holding register.

MODBUS_ERR_RANGE

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

Returned Value

None

Notes / Warnings

Code is enabled when either MODBUS_CFG_FC06_EN is set to DEF_ENABLED or MODBUS_CFG_FC16_EN is set to DEF_ENABLED in your product’s mb_cfg.h file.

Called By:

MBS_FC06_HoldingRegWr() and MBS_FC16_HoldingRegWr() in mbs_core.c

Example

In this example, our product has 2 floating-point integer variables that we want to assign to floating-point holding registers. Your systems Engineer decided to assign MODBUS floating-point holding register numbers MODBUS_CFG_FP_START_IX+0 and MODBUS_CFG_FP_START_IX+1 to the two floating-point variables. You will notice that we disable interrupts to access the variables. This is done in case your CPU does not perform floating-point data accesses atomically.


CPU_FP32  AppDiameter;         /* Modbus Holding Register # MODBUS_CFG_FP_START_IX + 0        */
CPU_FP32  AppCircumference;
CPU_FP32  AppTempDegC;         /* Modbus Holding Register # MODBUS_CFG_FP_START_IX + 1        */
CPU_FP32  AppTempDegF;            


void  MB_HoldingRegWrFP (CPU_INT16U reg, CPU_FP32 reg_val, CPU_INT16U *perr)
{
    CPU_FP32  temp_val;


    *perr = MODBUS_ERR_NONE;
    switch (reg) {
        case MODBUS_CFG_FP_START_IX + 0:
            temp_val         = reg_val * (CPU_FP32)3.141592654; /* Compute circumference      */
            CPU_CRITICAL_ENTER();
            AppDiameter      = reg_val;
            AppCircumference = temp_val;
            CPU_CRITICAL_EXIT();
            Break;

        case MODBUS_CFG_FP_START_IX + 1:
            temp_val    = reg_val * (CPU_FP32)1.8 + (CPU_FP32)32.0; /* C -> F Conversion      */ 
            CPU_CRITICAL_ENTER();
            AppTempDegC = reg_val;
            AppTempDegF = temp_val;
            CPU_CRITICAL_EXIT();
            break;
 
        default:
            *perr = MODBUS_ERR_RANGE;
            break;
    }
}



As shown in the example above, computations are performed when a value is changed.