# Modbus Word to Float Conversion (Big-Endian Byte Swap)

When transferring floating-point values via Modbus TCP/IP, devices often split a 32-bit floating-point number into two 16-bit Modbus registers.

However, many industrial devices send the bytes in a different order than the receiving PLC expects.\
This is why we often need to apply a **Big-Endian Byte Swap** before converting the registers to a floating-point value.

This guide explains:

* Why byte swapping is needed
* What **Big-Endian Byte Swap (BADC)** means
* How to convert registers into a **REAL / float**
* Multiple real industrial examples

## 1. Why Byte Swapping is Needed in Modbus

The Modbus protocol was originally designed to transfer **16-bit register values**.

A register contains:

```
16 bits = 2 bytes
```

But a floating-point number requires:

```
32 bits = 4 bytes
```

So a device must split the float across **two registers**.

Example float:

```
0.1251
```

IEEE-754 representation:

```
0x3E001ADB
```

Split into bytes:

```
A   B   C   D
3E  00  1A  DB
```

### Stored in Modbus registers

```
Register 1 = A B = 3E00
Register 2 = C D = 1ADB
```

This layout is called:

```
ABCD
```

***

## 2. The Industry Problem

The Modbus specification **does not define how 32-bit values should be arranged across registers**. Different vendors, therefore, implemented different byte orders.

Common layouts used in industry:

| Layout | Byte Order   | Description                    |
| ------ | ------------ | ------------------------------ |
| ABCD   | Big Endian   | Standard order                 |
| CDAB   | Word Swap    | Registers swapped              |
| BADC   | Byte Swap    | Bytes swapped within registers |
| DCBA   | Full Reverse | Completely reversed            |

Because of this, PLCs often must **rearrange the bytes before converting to a float**.

***

## 3. What Big-Endian Byte Swap Means

Big-Endian Byte Swap rearranges the bytes from:

```
A B C D
```

to:

```
B A D C
```

This means:

* swap bytes **inside each register**
* keep register order the same

Visual representation:

```
Original Registers

Register1      Register2
+----+----+    +----+----+
| A  | B  |    | C  | D  |
+----+----+    +----+----+

After Byte Swap

+----+----+----+----+
| B  | A  | D  | C  |
+----+----+----+----+
```

***

## 4. Example - Real Industrial Data

Modbus registers from a vibration sensor:

```
HighWord = 62
LowWord  = -9446
```

Convert to HEX:

```
62    = 0x003E
-9446 = 0xDB1A
```

Bytes:

```
00 3E | DB 1A
 A  B   C  D
```

Apply **Byte Swap (BADC)**:

```
3E 00 | 1A DB
 B  A   D  C
```

Combine into 32-bit value:

```
0x3E001ADB
```

Convert to float:

```
0.1251
```

***

## 5. How PLCs Perform This Conversion

Typical steps used in PLC or C/C++ code:

#### Step 1 - Extract bytes

```
A = HighWord >> 8
B = HighWord & 0xFF
C = LowWord  >> 8
D = LowWord  & 0xFF
```

#### Step 2 - Rearrange bytes

```
B A D C
```

#### Step 3 - Combine into a 32-bit value

```
(B << 24) | (A << 16) | (D << 8) | C
```

#### Step 4 - Interpret as float

```
REAL value
```

## 6. Why Industry Uses Byte Swapping

Byte swapping is common in industrial automation because devices may have:

#### Different CPU architectures

Examples:

* ARM processors
* Intel processors
* DSP controllers
* Embedded microcontrollers

Each architecture may represent multi-byte values differently.

***

### Legacy Modbus implementations

Many industrial devices were designed decades ago, when **memory layout decisions differed between manufacturers**.

Therefore:

```
Vendor A → ABCD
Vendor B → BADC
Vendor C → CDAB
```

PLCs must adapt to the device format.

***

### Sensor manufacturers

Common devices requiring byte swapping:

* Vibration sensors
* Power meters
* Drives
* Temperature transmitters
* PLC-to-PLC communication
* SCADA systems

***

## 9. How Engineers Detect the Correct Byte Order

A common debugging trick:

1. Read the two registers
2. Try all four byte layouts
3. See which one produces a realistic value

Example:

Expected vibration value:

```
0 – 10 g
```

Possible interpretations:

```
ABCD → 3.2e-38
CDAB → -1.4e12
BADC → 0.125
DCBA → 6.3e-20
```

Correct format is clearly:

```
BADC
```

***

## 10. Summary

Key points:

* Modbus registers are **16-bit**
* Floats require **32-bit**
* Devices split floats across **two registers**
* Modbus does **not define 32-bit ordering**
* Different vendors use different layouts
* **Big-Endian Byte Swap (BADC)** rearranges bytes from:

```
A B C D → B A D C
```

This ensures the PLC reconstructs the **correct IEEE-754 floating-point value**.

***

## 11. Practical Tip

Whenever a Modbus float looks incorrect:

```
1. Check register order
2. Check byte order
3. Try ABCD / BADC / CDAB / DCBA
```

In many industrial sensors, the correct format is:

```
BADC (Big-Endian Byte Swap)
```

{% hint style="info" %}
You can verify the conversion using this free tool: <https://g2384.github.io/collection/Hex_Calc_IEEE754_conversion.html>
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.codeandcompile.com/factory-automation/plc/articles/modbus-word-to-float-conversion-big-endian-byte-swap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
