Modbus 32-bit Integer Wrong? Word Swap (CDAB) Fix
Your energy meter says it exported 1,234 kWh, but QModMaster shows 80,889,344. Or a counter reads fine until it crosses 32,767 and suddenly goes negative. If the raw registers look plausible but the 32-bit value is exactly 65,536× too big (or the sign flips), you are looking at a word-swap problem: your device sends the two 16-bit words in the opposite order from what your display format expects.
ABCD vs CDAB: what “word order” means
A 32-bit integer occupies two consecutive 16-bit Modbus registers. Call the register at the lower address the high word (AB) and the next one the low word (CD). Two conventions exist:
- Big-endian words (ABCD): high word first, then low word. This is the Modbus “default” most devices and software assume. Value =
high × 65536 + low. - Little-endian words (CDAB): low word first, then high word. Some devices — the Carlo Gavazzi EM24 energy meter is a well-documented example — transmit multi-register values this way.
Worked example. Suppose the true value is 1,234, stored as high word = 0, low word = 1234:
- Read as ABCD (correct): 0 × 65536 + 1234 = 1,234 ✓
- Read as CDAB-interpreted-as-ABCD (wrong): 1234 × 65536 + 0 = 80,889,344 ✗ — exactly 65,536× too large.
That 65,536× signature is the giveaway. (And if the value only goes wrong past 32,767 — flipping negative — you are likely reading an unsigned 32-bit value as signed INT32, which is the signed/unsigned half of the same family of problems.)
Diagnose it in QModMaster (2 minutes)
- Step 1 — read the raw words. Set QModMaster’s View menu to a plain 16-bit format (Hex or INT16) and read the two registers. Write down both words, e.g.
0x0000and0x04D2. - Step 2 — switch to 32-bit. Change the View menu format to INT32 / unsigned 32-bit and read the same pair. If the displayed value is ~65,536× your expected value, the words are swapped relative to what the format expects.
- Step 3 — verify by swapping. Reconstruct it by hand: treat the second word as the high word. In the example: high = 0x0000, low = 0x04D2 → 0 × 65536 + 1234 = 1234. If the hand-swapped value matches your meter’s display, you have confirmed CDAB word order.
- Step 4 — check the manual. Look for “word order,” “byte order,” “little-endian,” or “low word first” in your device’s Modbus register map. EM24 documentation, for instance, explicitly states little-endian word order for multi-register values.
The EM24 counter-example (little-endian in the wild)
The Carlo Gavazzi EM24 is the classic trap: most of the Modbus world defaults to big-endian words, but the EM24 transmits multi-register values low word first. Community integrations (e.g. ESPHome-based EM24 grid-meter projects) document this explicitly — you must swap the words before interpreting. If your EM24 energy totals are exactly 65,536× off in QModMaster, do not touch your wiring: it is the word order, and the device manual agrees with the “wrong” reading.
Word swap vs byte swap vs float problems
- Word swap (this page): 32-bit integers off by exactly 65,536×, or sign flips. Fix = swap the two 16-bit words.
- Byte swap: 16-bit values that look like garbage (e.g. 0x1234 read as 0x3412). Fix = swap bytes within each word. Different problem, different fix.
- Float byte order: 32-bit floats showing astronomical or garbage values. Same family, but floats have four possible byte orders (ABCD, BADC, CDAB, DCBA) — covered in Modbus Float Shows a Wrong Value? Byte-Order Fix.
FAQ
Why is my Modbus 32-bit value exactly 65536 times too big?
Because the device sends the two 16-bit words low-word-first (CDAB) while your software interprets them high-word-first (ABCD). The low word gets multiplied by 65,536 instead of the high word. Swap the words and the value will be correct.
How do I swap words in QModMaster?
Read the two registers in a 16-bit View format (Hex), note both words, then reconstruct the value treating the second word as the high word. Alternatively switch the View format between 32-bit variants and compare against the device’s own display to identify which interpretation matches.
Is the EM24 really little-endian?
Yes — the Carlo Gavazzi EM24 transmits multi-register values low word first, contrary to the Modbus big-endian default. This is documented in the EM24 register documentation and in community integration projects. A 65,536× error on an EM24 is expected behavior, not a fault.
Related Guides
- Modbus Float Shows a Wrong Value? Byte-Order Fix — the floating-point sibling of this problem (four byte orders, not two).
- QModMaster View Menu: Float/Int/Hex/Binary Explained — which display format to use when.
- Modbus Values Off by 10x? Scale Factor & Offset — when the value is wrong by 10× instead of 65,536×.
- Download QModMaster — free, open-source (LGPLv3) Modbus master.
Debugging a meter or inverter? A protocol reference helps: Modbus protocol book covers data encoding in depth.
