How to Swap Registers for Modbus Floating Point Values

Updated Dec 20, 2017

Reported In

Software

  • LabVIEW Full
  • LabVIEW Datalogging and Supervisory Control Module

Issue Details

My LabVIEW Modbus Slave application requires that the least significant bytes of a single floating point number be placed in the first register. Currently the most significant bytes are placed in the first register, how can I swap the bytes for my application?

Solution

Since Modbus uses 16-bit registers to hold values, 32-bit floating point numbers must split between two registers. Modbus does not declare a standard how to represent floating point numbers, so it is possible that a device handles floating point numbers differently than LabVIEW. The following article discusses the different forms that floating point numbers can be represented with two registers: How Real (Floating Point) and 32-bit Data is Encoded in Modbus RTU Messages

Figure 1 below shows how floating point numbers are defined using IEEE 754 standard. The least significant bits are stored in the first register.
Figure 1: Least significant values first

In this form, if you attempt to store the number 123456.00 in register F400001 then bytes "A B" are stored in the first register, F40001. Bytes "C D" are stored in the second register, F400002.  With LabVIEW 2011 and earlier this is NOT how LabVIEW represents floating point numbers in Modbus registers when using a Modbus Slave I/O Server. If your device expects the least significant bits first then your data will appear like the decimal value in figure 2.

Figure 2: Most significant values first
 
Figure 2 shows how LabVIEW places the most significant bytes, "C D", in the first register. Then the least significant bytes, "A B", are placed in the second register. This form is generally called "word swapped".

If your device follows the form of Figure 1, you can perform a "word swap" on the single floating point value in LabVIEW before writing the value to the device. The image below shows how to reverse the order, and the steps explain how to set it up.

Figure 3: Word swap floating point value
 
  1. Place a Type Cast function on the block diagram. Wire a numeric array constant into the typeinput. Wire single floating point number into x input.
  2. Right-click Numeric Constant in the array and select Representation»I16
  3. Place Reverse 1D Array function wire the first Type Cast to the array input.
  4. Place a second Type Cast function. Wire a single floating point number into type input. Wire reversed array output into x input.
  5. The swapped single floating point value can now be written to the Modbus register. This example uses a shared variable bound to a Modbus register.

 

WAS THIS ARTICLE HELPFUL?

Not Helpful