An energy report says a compressor used 16 million kilowatt-hours in seven months, and the finance office asks who will pay. The meter is correct, the cable is correct and every frame has a good CRC. The fault is two register words joined in the wrong order. Modbus carries 16-bit words and says nothing about what they mean, so the meaning comes from the maker’s register map and from you. This lesson shows how to read that map, decode each data type, and test the result before anyone trusts it. #Modbus #RegisterMap #DataTypes
Learning Objectives
By the end of this lesson, you will be able to:
Convert a register number in a manual into the address that is sent on the wire
Decode signed and unsigned 16-bit registers with their scaling factors
Join two registers into a 32-bit integer or an IEEE 754 float32 in the stated word order
Read single bits out of a status word
Test a decoded value for plausibility before it enters a record
Real-World System Problem: An energy meter that reports the wrong total
A three-phase energy meter sits on the feeder of a compressor. It is a Modbus RTU server at address 5. A gateway reads it over RS-485 and passes the readings upward. Its numbers divide the energy bill between departments, and later they are the proof that a saving is real.
The response to a read is a row of bytes. Nothing in the frame says “this is a voltage” or “this is half of a 32-bit counter”, and the CRC proves only that the bytes arrived as sent. Which register holds which quantity, whether it is signed, how two registers join, and what one count is worth are in the maker’s register table. Read that table wrongly when you configure the gateway, and the bus works without one error while the record is false.
Some false values are easy to see, for example a voltage of 6,553.5 V. Others look reasonable: a power of 8.40 kW where the true value is 24.15 kW.
From raw words to an engineering value
Fundamental Theory: From a register word to a value
What a register is
A holding register is one 16-bit word inside the server. On the wire it travels as two bytes, high byte first. As a plain number it lies between 0 and 65,535, or in hexadecimal between 0x0000 and 0xFFFF. This plain number is called the raw value, or the count.
Five decisions stand between the raw value and the quantity:
which address to ask for,
whether the word is signed or unsigned,
whether it stands alone or is part of a larger value, and in which order,
what one count is worth (the scaling factor),
whether the word is a number at all, or a set of separate bits.
Register numbers and addresses
Manuals name holding registers with numbers that begin at 40001; the frame carries an address that begins at 0, like a building whose wall signs start at floor 1 and whose lift buttons start at 0. The leading 4 is not sent at all: it only says “holding register”, and the function code already carries that.
Register number to address
Holding register 40001 is address 0x0000. Holding register 40007 is address 6, which is 0x0006.
With six-digit numbers, subtract 400001. Input registers are numbered from 30001, are read with function 04, and give address = register number − 30001.
Many energy meters keep their readings in input registers, not holding registers. A read of 30007 with function 03 does not reach it: the server answers exception 02, or returns holding register 40007, which may hold something else. Match the function code to the leading digit in the table.
The number in the manual and the address on the wire
The register map of meter A, used through this course, is:
Register
Address
Quantity
Type
Scaling
40001
0x0000
Voltage L1-N
uint16
0.1 V per count
40002
0x0001
Voltage L2-N
uint16
0.1 V per count
40003
0x0002
Voltage L3-N
uint16
0.1 V per count
40004
0x0003
Current L1
uint16
0.01 A per count
40005
0x0004
Current L2
uint16
0.01 A per count
40006
0x0005
Current L3
uint16
0.01 A per count
40007
0x0006
Total active power
int16
0.01 kW per count
40008
0x0007
Power factor
int16
0.001 per count
40009
0x0008
Frequency
uint16
0.01 Hz per count
40011 to 40012
0x000A
Active energy import
uint32, high word first
0.01 kWh per count
40013 to 40014
0x000C
Active power, precise
float32, high word first
kW
Register 40010 is not in the table. Maps often have such gaps. Some servers answer an exception when a read includes an address they do not define, so read 40001 to 40009 as one block and 40011 to 40014 as another.
Scaling
A register holds whole numbers only. To carry 240.3 V, the meter stores 2,403 and the table says that one count is 0.1 V.
Scaling
The first form decodes a reading. The second form prepares a value for a write.
The scaling factor also fixes the largest value the register can hold. An unsigned register at 0.1 V per count ends at . A signed register at 0.01 kW per count ends at . A meter for a larger load must use a coarser scale or a wider type.
Tables write the same scaling in several ways: “0.1 V per count”, “multiplier 0.1”, “divide by 10” or “one decimal place”. They mean the same thing.
Signed and unsigned 16-bit
An unsigned 16-bit word (uint16) covers 0 to 65,535. A signed word (int16) uses the same 16 bits in two’s complement, the usual way computers store negative whole numbers: raw values from 32,768 to 65,535 stand for −32,768 to −1. It covers −32,768 to 32,767. The bits on the wire are identical. Only the reading differs.
Signed 16-bit from a raw word
For raw values up to 32,767 the two readings agree, so a wrong choice stays hidden while the quantity is positive. It shows on the day the quantity becomes negative. Take a power factor register that holds 0xFCB8:
read as unsigned: , which is impossible for a power factor,
read as signed: , and .
The signed reading is right. The maker’s manual says what the sign means.
A 32-bit integer across two registers
An energy counter at 0.01 kWh per count does not fit in 16 bits. An unsigned word would reach its end at 655.35 kWh, which this compressor uses in about 27 hours. The meter therefore uses a 32-bit unsigned integer (uint32) spread over two registers that follow each other.
Let be the word that carries the upper 16 bits and the word that carries the lower 16 bits.
Joining two words
Modbus fixes the byte order inside one register: high byte first. It does not fix which register carries the upper half. Both orders are in use:
high word first, often written ABCD or “big-endian”: the first register is ,
low word first, often written CDAB or “word-swapped”: the first register is .
One 32-bit value, two possible layouts
Meter A uses high word first, and its table says so. Many tables do not say, and then you find the order by test, as Application 1 shows. Read a 32-bit value in one request: if the two halves come from two requests, the counter can step between them and the halves no longer belong together.
A float32 across two registers
A float32 is the 32-bit floating point format of IEEE 754. Its 32 bits are a sign bit , an 8-bit exponent and a 23-bit fraction .
IEEE 754 float32
For 24.15 the four bytes are 41 C1 33 33. The sign bit is 0, the exponent is , so the factor is , and the fraction gives 1.509375. The product is .
A float32 needs no scaling factor, because it carries its own exponent. It keeps about seven significant digits, so the stored number is 24.1499996 and not exactly 24.15. It has the same word order question as the 32-bit integer.
Some devices, and some gateway settings, also swap the two bytes inside each word (written BADC, or DCBA when both swaps are made). Most of the wrong orders give absurd values, but not all: the bytes of 24.15 kW read as BADC give −12.07 kW, wrong yet not impossible. So test all four orders, and keep the one that agrees with a second source.
Bit fields in status words
Some registers are not numbers. Each bit is a separate yes or no. Bit 0 is the lowest bit, with a weight of 1. Bit has a weight of .
Reading and setting one bit
In code this is (word >> n) & 1. To set bit in a word that you will write, use word | (1 << n).
Status word 0x0009 of drive B
Some manuals count bits from 1 and not from 0, so “bit 1” in one manual is bit 0 in another. And never scale a status word: 0x0009 is not “9 of something”, it is two separate facts.
The checks that catch a wrong map
A CRC cannot find a wrong map. A plausibility check can find most of them. Before a new point enters the record, ask these questions.
Is the value inside the range that the equipment can physically produce? A phase voltage of 6,553.5 V on a 240 V system is the raw value 0xFFFF. A power factor of 32.768 is the raw value 0x8000. Values at the exact end of a type are almost never measurements. They are a wrong type, a wrong address, or a marker the maker uses for “no value”.
Does the value agree with a second source: the meter’s own display, or another register that should give the same quantity?
Do related quantities agree with each other? Power should be close to the sum of voltage times current times power factor.
Does it change sensibly from one poll to the next? An energy counter must rise slowly and never fall.
Application 1: Decode the energy and the precise power of meter A
Step 1: Take the register words out of the response
Click to reveal the four words
Check the header. The first byte is the address, 05. The second is the function code, 03. The third is the byte count, 08, which is four registers of two bytes each. ✅
Split the data into words. Each register is two bytes, high byte first.
Register
Bytes
Word
40011
00 BC
0x00BC
40012
61 4E
0x614E
40013
41 C1
0x41C1
40014
33 33
0x3333
The last two bytes, 8C 76, are the CRC and are not data. ✅
Step 2: Decode the energy in both word orders
Click to reveal the two energy values
High word first. The first register is the upper half: and .
The energy is 123,456.78 kWh. ✅
Low word first. Now the first register is taken as the lower half: and .
The same four bytes now read as 16.3 million kWh. ✅
Test against the load. At 24.15 kW without a stop, the first value needs
which fits a meter that is seven months old. The second value needs 676,000 hours, which is 77 years. Only high word first is possible. ✅
Step 3: Decode the precise power in both word orders
Click to reveal the two power values
High word first. The four bytes in order are 41 C1 33 33. The sign bit is 0. The exponent is , so the factor is . The fraction is .
The power is 24.15 kW. ✅
Low word first. The four bytes become 33 33 41 C1. Python gives
which is 42 millionths of a watt. No compressor feeder carries that. ✅
Cross-check with the 16-bit register. Register 40007 holds the same power as an int16 at 0.01 kW per count. In the example readings it holds 2,415 counts, which is 24.15 kW. The float32 with high word first agrees with it. ✅
Step 4: Confirm with a second reading
A single reading can mislead when both halves of a value are small. Two readings a short time apart settle the question.
Click to reveal the second reading and the script check
Expected change in one minute. At 24.15 kW, one minute adds
The counter should rise by 0.40 kWh. ✅
High word first. The new words are 0x00BC and 0x6176. The lower word rose from 0x614E to 0x6176, which is 40 counts. The energy is 123,457.18 kWh, a rise of 0.40 kWh. ✅
Low word first. The word that changed is now treated as the upper half, so each of its counts is worth 65,536.
That is a power of 1.57 million kW through a compressor feeder. The wrong order shows itself as a counter that jumps. ✅
Check with the script. The Python script below builds the response with struct, decodes it both ways and prints 123,456.78 kWh and 16,325,019.48 kWh, then 24.15 kW and 4.17365e-08 kW, and the two steps of 0.40 kWh and 26,214.40 kWh. All values agree with the hand calculation. ✅
Application 2: Read the status of drive B and write a speed reference
Step 1: Decode the status block
Click to reveal the decoded block
Output frequency. Register 40201 holds 0x0DAC, which is 3,500 counts.
The drive output is 35.00 Hz. ✅
Output current. Register 40202 holds 0x00A2, which is 162 counts, so the current is . ✅
Status word. Register 40203 holds 0x0009. In binary this is 0000 0000 0000 1001. Since , bit 3 and bit 0 are set and every other bit is clear. ✅
Meaning. Bit 0 set: the drive is running. Bit 3 set: the drive reports a fault. Both are true at the same moment. ✅
Fault code. Register 40204 holds 0x0004, fault code 4. The number alone means nothing. Its meaning is in the fault table of the maker’s manual for this drive. ✅
Step 2: Build the write request
Click to reveal the request, byte by byte
Convert the value to counts.
The value fits in 16 bits. ✅
Convert the register number to an address.. ✅
Assemble the frame. Function 06 writes one holding register. The frame is the address, the function code, the register address and the value, each 16-bit field with its high byte first.
Field
Bytes
Server address (10)
0A
Function code
06
Register address
00 64
Value
10 9A
CRC, low byte first
44 C5
The request is 0A 06 00 64 10 9A 44 C5. ✅
The response. For function 06 a server that accepts the write returns the request unchanged: 0A 06 00 64 10 9A 44 C5. An exception response, with function code 0x86, means the drive refused the write. ✅
Step 3: Decide, then check the frame
Click to reveal the decision and the check
Do not send it yet. The status word shows a fault. A drive that reports a fault needs a person at the machine before anyone changes its speed from a distance. The correct action for the gateway is to record the status and the fault code and to raise an alert. ✅
After the fault is cleared and the permit is given, send the write, then read register 40101 again. It must return 0x109A. Then read 40201 over the next seconds: the output frequency should move from 35.00 Hz toward 42.50 Hz along the drive’s own ramp. ✅
Check with the script. The Python script builds the same frame with struct.pack('>BBHH', 10, 6, 0x0064, 4250), adds the CRC and prints 0A 06 00 64 10 9A 44 C5. It also decodes the status block and prints 35.00 Hz, 16.2 A, status 0x0009 and fault code 4. ✅
Verify It in Python
The script builds each response with the struct module, checks its CRC and decodes it: the energy and the precise power in both word orders, 0xFCB8 signed and unsigned, the off-by-one with the readings of meter A, the status block of drive B, and the write request. The plot shows ten polls of the energy registers, one minute apart, decoded in both word orders.
register_decode.py
"""Decode Modbus register words: word order, float32, signed values and bit fields."""
import struct
import matplotlib.pyplot as plt
defcrc16(data):
"""CRC-16/MODBUS: polynomial 0xA001, initial value 0xFFFF, low byte first."""
crc =0xFFFF
for byte in data:
crc ^= byte
for _ inrange(8):
crc = (crc >>1) ^0xA001if crc &1else crc >>1
return struct.pack("<H", crc)
defhexs(data):
return"".join(f"{b:02X}"for b in data)
defwords_from_response(frame):
"""Check the CRC of a function 03 response and return its register words."""
status block: 0A 03 08 0D AC 00 A2 00 09 00 04 35 78
output 35.00 Hz, 16.2 A, status 0x0009 = 0000000000001001, fault code 4
bit 0 running: 1, bit 3 fault: 1
42.50 Hz = 4250 counts = 0x109A
write request : 0A 06 00 64 10 9A 44 C5
write response: 0A 06 00 64 10 9A 44 C5 (echo)
One minute later
high word first: 123,457.18 kWh (step 0.40 kWh)
low word first : 16,351,233.88 kWh (step 26,214.40 kWh)
Saved register_decode.png
Look at the “Off-by-one” lines. A client that wants register 40007 and sends address 7 receives 840 counts, the power factor. Scaled as power, that is 8.40 kW. The value is inside the physical range, so a range check passes it. The cross-check does not: the three voltages, the three currents and the power factor give 24.29 kW, close to the true 24.15 kW and far from 8.40 kW.
Common Mistakes
Sending the register number without converting it. Register 40007 is address 6. If the result looks reasonable but disagrees with the display, read one address lower and one higher and compare.
Reading a signed register as unsigned. It works for months while the value is positive, then shows 64.696 or 655.35 on the day the value becomes negative. Take the type from the table, not from the first reading.
Assuming the word order of a 32-bit value. The standard does not fix it. Take it from the table, or watch which word changes between two polls.
Reading the two halves of a 32-bit value in two separate requests. Read both registers in one request so that they belong to the same instant.
Treating the echo of a write as proof that the machine obeyed. Read the measured result.
Summary
A register is a 16-bit word with no meaning of its own. The register map gives the address, the type, the word order and the scaling.
The address on the wire is the register number minus 40001. A wrong address returns a neighbour without any error.
Signed and unsigned readings differ only for raw values of 32,768 and above. A signed value is the raw value minus 65,536 in that range.
A 32-bit integer or a float32 uses two registers. The word order is the maker’s choice, so state it and test it.
A status word is a set of bits. Read each bit by its position and never scale the word.
A good CRC does not prove a good value. Range, cross-check and change between polls do.
Exercises
1. Give the address, in decimal and in hexadecimal, that is sent for holding registers 40013 and 40204. Then give the register number for address 0x0064.
Answer
and . Address 0x0064 is 100, so the register is .
2. On a site with solar panels, the total active power register of a meter (int16, 0.01 kW per count) returns 0xFB1E. Give the power. Then give the value that a client shows if it reads the register as unsigned.
Answer
0xFB1E is 64,286, which is above 32,767, so the signed value is counts, or (power flows the other way). Read as unsigned it shows 642.86 kW.
3. A second meter returns 0x0096 and 0xB43F for its energy registers (uint32, 0.01 kWh per count). Decode the energy with high word first and with low word first. The meter serves a 30 kW load and is one year old. Which order is right?
Answer
High word first: counts, which is 98,765.43 kWh. Low word first: counts, which is 30,240,277.98 kWh. A 30 kW load can use at most kWh in a year, so high word first is right.
4. Two registers hold 0x4366 and 0x8000. The table says float32, high word first, in volts. Decode the value by hand. Then say what the bytes give with the words swapped.
Answer
The bytes are 43 66 80 00. The sign is 0, the exponent is 134, so the factor is . The fraction is 0x668000, which is 6,717,440, and . The value is . With the words swapped the bytes are 80 00 43 66, which struct.unpack('>f', ...) decodes as about , plainly not a voltage.
5. Drive B (address 10) later returns a status word of 0x0008. State what the drive is doing. Then build the complete function 06 request, with CRC, that sets its speed reference (register 40101) to 30.00 Hz, and say what must be true before it is sent.
Answer
0x0008 has only bit 3 set: a fault is present and the drive is not running. The value is and the address is 0x0064, so the frame is 0A 06 00 64 0B B8 plus its CRC, CE 2C: 0A 06 00 64 0B B8 CE 2C. It is sent only after the fault has been examined and cleared at the machine, and under the site’s permit and lock-out rules.
Modbus Organization, MODBUS over Serial Line Specification and Implementation Guide V1.02, 2006.
IEEE, IEEE Standard for Floating-Point Arithmetic, IEEE Std 754-2019.
From raw words to an engineering value
The number in the manual and the address on the wire
One 32-bit value, two possible layouts
Status word 0x0009 of drive B
Predict first
A meter sends its energy counter in two registers, 0x00BC and then 0x614E, at 0.01 kWh per count. The true value is 123,456.78 kWh. A gateway joins the two words in the wrong order. What does its record show?
See it: two words, read four ways
The four bytes on the wire
First word alone, 16 bits
Second word alone, 16 bits
Unsigned 32-bit, high word first (ABCD)
Unsigned 32-bit, low word first (CDAB)
Float32, high word first (ABCD)
Float32, low word first (CDAB)
The two words are the same in every card. Only the reading changes. A whole number is multiplied by the scaling. A float32 carries its own exponent, so no scaling is applied to it. The three buttons load words from the lesson, and then each card says whether its value is possible for the lesson's compressor, which takes about 24 kW and has a meter about seven months old. For your own words the page cannot know the machine, so the cards give the general test. A float32 is decoded by the browser from the four bytes, in the order shown in each card.
Check yourself
Three questions. Each wrong choice is a mistake people really make, so read the note after you answer.
1. On a site with solar panels, the total active power register of a meter (int16, 0.01 kW per count) returns 0xFF38, which is 65,336 as a plain number. What is the power?
2. A client wants the total active power of the lesson's meter, register 40007, and sends address 7. The meter holds 2,415 counts in register 40007 and 840 counts in register 40008. Both are scaled by 0.01 kW in the client. What does the client show?
3. A table gives an energy counter as uint32 in two registers and does not state the word order. One poll returns 0x00BC and 0x614E. A minute later the poll returns 0x00BC and 0x6176. Which register is the low word?
Watch it build: four bytes become 24.15
A float32 is 32 bits: 1 sign bit, an 8-bit exponent E and a 23-bit fraction F.
41 C1 33 33: sign 0, E = 131 so the factor is 16, fraction 1.509375. The value is 24.15.
No scaling factor is needed. About seven digits are kept: the stored number is 24.1499996.
The same bytes with each word's two bytes swapped (BADC) give −12.07 kW: wrong, yet not impossible.
Found this useful?
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
Text size
←→
Previous and next card. On a turned card: Not yet, I knew it
SpaceEnter
Turn the card, show the answer
123
Overview, Recap, Test me
HomeEnd
First and last card
Esc
Close
← → or scroll · N notes · F full screen · B blank0:00
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.