...
In this example, our product has 4 floating-point variables that we want to assign to holding registers. Your systems Engineer decided to assign Modbus holding 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.
Code Block | ||
---|---|---|
| ||
CPU_FP32 AppTempAir;
CPU_FP32 AppTempFuel;
CPU_FP32 AppPresAir;
CPU_FP32 AppPresFuel;
CPU_FP32 MB_HoldingRegRdFP (CPU_INT16U reg, CPU_INT16U *err)
{
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);
}
} |