Modbus Comms: How to Set Floating Point Values
In industrial settings, Modbus is one of the oldest protocols for devices to communicate their data digitally to a central controller. It allows transmitters to send integer data to controllers, and controllers to send integer data to SCADAs used in large, fully-monitored mission critical facilities such as data centers. Here we will give a pithy explanation of successfully encoding floating point values via Modbus.
Floating Point Values Via Modbus: Recap of Modbus Data Model
Modbus encodes analog values either as Holding Registers or Input Registers.
Object Type | Object Size | Address Range | Access |
Input Register | 16 Bits | 30001-39999 | Read-Only |
Holding Register | 16 Bits | 40001-49999 | Read-Write |
Modbus Encoding for Floating Point Values
IEEE 754 Single-Precision Floating Point Format is the most popular method for representing floating point numbers but requires 32-Bits. Manufacturers have settled on using 2 consecutive Modbus register for encoding floating point values. This sounds easy… but, not so fast. Although it was agreed to use 2 consecutive register to encode floating point values, there is no standard method for the order in which the data is sent.
Example: for the floating-point value 50.123 the IEE 754 single precision floating point representation is as shown below.
Decimal | 50.1230049133 |
Binary | 01000010010010000111110111110100 |
Hexidecimal | 0x42487df4 |
Split into two, 16 Bit Words (Registers): High 0x4248 Low 0x7df4. Encode for Modbus Holding Register 40001 & 40002. Some manufacturers send the High Word first, and then the Low Word.
Modbus Reg | Contents Hex | Contents Decimal |
400001 | 0x7df4 | 32244 |
400002 | 0x4248 | 16968 |
Other manufacturers send the Low Word First, and then the High Word.
Modbus Reg | Contents Hex | Contents Decimal |
400001 | 0x4248 | 16968 |
400002 | 0x7df4 | 32244 |
How to read Modbus floating point values
Most Modbus Masters provide a means to swap the word order for floating point values. Consult the communications point list for the device in question to determine the word (register) order used to encode floating point values. Configure the Master device based on that setting.
If no information is provided on how the data is sent, it becomes a trial and error process. Pick a format and configure the Master device to read the value in that format. If the value read back is very inaccurate, then swap the words, and often that will correct the value read back.