Skip to content

Retaining Precision After A Transformation

When transforming data with a Float scale, such as 0.01, the precision can sometimes be lost after transformation.

For example, the temperature read by a Modbus device could be 26.53. After a regular transformation, this value becomes 26. Therefore, We have lost 0.53 from the value read by the device.

To avoid this loss of precision, you can use different data types for the device resource and the value descriptor.

The value type in the properties section defines the data type to use when sending transformed data to the user based on the rawType attribute. The supported transformations are as follows:

From To
INT16 FLOAT32
INT16 FLOAT64
UINT16 FLOAT32
UINT16 FLOAT64

In the following device profile extract, the rawType attribute, which defines the binary data read from the Modbus device, is set to INT16. The value type under properties, which defines the data type to use when sending transformed data to the user, is set to FLOAT32:

"deviceResources": [
  {
    "name": "humidity",      
    "description": "The response value is the result of the original value multiplied by 100",
    "properties": {
      "valuetype": "Float32",
      "readWrite": "RW",
      "units": "%",
      "defaultValue": "%RH"
     }
    "attributes": {
       "primaryTable": "HOLDING_REGISTERS",
       "startingAddress": 1,
       "rawType": "INT16",
       "scale": 0.01        
     }
  }  
]

When the Device Service runs the read command, the binary reading data is parsed as an INT16 data type and the integer value is cast to a FLOAT32 value.

When the Device Service runs the write command, the requested FLOAT32 value is cast to an INT16 value and the binary data is sent to the Modbus device.