Modbus RTU Over USB: Five Lessons From a Year of Polling Solar Inverters
USB-RS485 adapter quirks, pymodbus CPU bugs, silent data corruption, and the wiring trap that cost me a full day. Hard-won lessons from running Modbus RTU on a Raspberry Pi 24/7.
- Filed
- Updated
- Reading
- 8 min
- Section
- C / Embedded & IoT
- Tags
Updated 09.10.2026FC16-only writes, registers never to write, a second vendor with different bus limits, and treating silence as a fault.Jump to the update ↓
One of the FutureGrid edge devices talks to solar inverters over Modbus RTU - a protocol from 1979 that still powers most industrial equipment. RS-485 wiring, 9600 baud, holding registers. It sounds simple until you actually have to make it reliable on a Raspberry Pi running 24/7 in someone's garage.
Here are five things I learned the hard way.
011. Your USB-RS485 Adapter Matters More Than You Think¶
Not all USB-to-RS485 adapters are equal. I tested two common chips:
- FTDI FT232R (vendor
0403:6001) - stable, deterministic timing - CH340 (vendor
1a86:7523) - cheaper, but needs special handling
The CH340 would occasionally corrupt responses when reading multiple register blocks in sequence. The fix was counterintuitive: close and reopen the serial port between every block read. I call it "reconnect-per-read."
# Pseudocode - the reconnect-per-read pattern
for block in register_blocks:
client = open_serial(port, baudrate=9600)
data = client.read_holding_registers(block.start, block.count)
client.close()
await asyncio.sleep(0.5) # give the bus breathing roomThat 0.5-second inter-block delay is not optional. Without it, you get fragmented responses - especially if there's a CAN bus on the same device (some hybrid inverters multiplex CAN and Modbus internally).
022. pymodbus AsyncModbusSerialClient Will Burn Your CPU¶
I initially used pymodbus's async serial client because - why wouldn't you in an async application? Turns out AsyncModbusSerialClient.connect() busy-spins when the device is unresponsive:
# top output at 3 AM when the inverter was in standby
PID %CPU COMMAND
1234 98.7 python3 collectorThe async transport layer polls the serial port in a tight loop with no backoff when there's nothing to read. This is fine over TCP, but over serial with a sleeping inverter it pegs the CPU at 100% and starves every other coroutine in the event loop.
Fix: Use the synchronous ModbusSerialClient wrapped in asyncio.to_thread():
def _sync_read(self, address: int, count: int) -> list[int]:
"""Runs in a thread pool - can block without killing the event loop."""
result = self._client.read_holding_registers(address, count)
if result.isError():
raise ModbusException(str(result))
return list(result.registers)
async def read_holding_registers(self, address: int, count: int) -> list[int]:
return await asyncio.to_thread(self._sync_read, address, count)Boring, but it works. The thread blocks, the event loop stays free, CPU sits at 2%.
033. Not All Function Codes Work on All Devices¶
The Modbus spec defines FC3 (read holding registers) and FC4 (read input registers) as two separate address spaces. Many devices implement both. Some don't.
I spent a full day debugging why FC4 returned IllegalFunction on a three-phase hybrid inverter. The answer was in a forum post from 2019: this particular inverter family only implements FC3. Everything - configuration, telemetry, identification - lives in holding registers.
Lesson: Don't assume FC4 works. Start with FC3 and test FC4 explicitly. Your driver abstraction should let each device declare which function codes it supports.
044. Register Block Size Has a Hidden Limit¶
The Modbus RTU spec allows reading up to 125 registers in one request. In practice, many devices choke well before that. I found a sweet spot around 12 registers per block for the devices I work with.
Larger blocks (20+) would return valid CRC but scrambled data - the device's internal bus couldn't marshal that many registers in one response frame. There's no error, no exception code. Just wrong numbers.
The only way to find the limit is empirical: start small, increase until data gets weird, back off by 20%.
Block 500-511: ✓ (12 registers, clean)
Block 500-519: ✓ (20 registers, looks ok)
Block 500-529: ✗ (30 registers, register 520+ is garbage)055. Inverters Behave Differently at Night¶
Solar inverters in standby mode (no PV production, battery idle) are a different beast:
- Response times go from ~200ms to 19 seconds per read
- Serial buffers fill with
0xFFpadding bytes - Some registers return stale values, others return zero
This means your polling loop needs to handle a cycle that takes 2 minutes instead of 10 seconds - without assuming the device is dead. I use a simple heuristic: if the battery voltage register reads exactly 0.0V, the BMS is disconnected, and all battery metrics get dropped rather than stored as misleading zeros.
# A 48V LiFePO4 battery always reads >40V when connected
if metrics.get("battery_voltage", -1) == 0:
for key in BATTERY_METRICS:
metrics.pop(key, None)The dashboard shows "--" instead of "0 kW" - which is what the operator actually expects to see.
06Bonus: The Wiring Trap¶
Some hybrid inverters have two RJ45 ports that look identical. One is for the battery management system (CAN bus), the other is for monitoring (Modbus RS-485). If you plug your USB adapter into the wrong port, everything appears to work - you get a serial connection, you can send requests - but every response comes back as IllegalFunction (0x01).
The reason: when a CAN battery is connected to the BMS port, the device disables Modbus on that port entirely. The Modbus-only port continues to work fine. There is nothing in the documentation that tells you this. I found it by unplugging cables one at a time.
07What I'd Do Differently¶
If I started over, I'd build a register discovery tool that probes FC3/FC4 across the full address space (0-65535) in small blocks, maps out which addresses respond, and generates a device profile automatically. Would have saved me weeks of reading PDFs and forum posts.
I'd also invest earlier in a device identification protocol - reading serial numbers, firmware versions, and rated capacities from the device on first connection, rather than requiring manual configuration. Most Modbus devices expose this information somewhere in their register map; you just have to find it.
Modbus is old, cranky, and underspecified. But it's everywhere in energy infrastructure, and once you learn its quirks, it's remarkably reliable. The protocol isn't the problem - it's the implementations.
08Update, October 2026¶
Six more months of polling, a second inverter vendor and an ESP32 port later, a few things to add.
Writes are their own lesson. The day this post went out I switched every write to the Deye to FC16 (write multiple registers). Its protocol lists only FC3 for reads and FC16 for writes, and FC6 (write single register) simply never answers. The CH340 also drops write responses now and then, so each write retries up to three times with the same 0.5 s settle as the reads. Then the second vendor arrived, the Volt Sinus Pro Ultra, and it is the other way round: FC6 for writes. Let each driver declare its function codes, for writes as much as for reads.
Know which registers you must never write. On the Deye, registers 108 and 109 hold the battery's maximum charge and discharge current, which the installer sets during commissioning. An early ESP32 build wrote them to 0 A on every mode change, which quietly stopped the battery from charging or discharging until someone fixed it on the inverter's own screen. That firmware now rejects any write that touches those two registers at the lowest layer of the Modbus code, and logs the attempt. A similar side effect in the same batch write switched off the installer's time-of-use schedule, so the battery sat at 100% all evening. That register is now left alone too.
The block-size limit really is per device. The 12-register sweet spot from lesson 4 still holds for the Deye. The Volt takes the full 125-register block without complaint but needs a 20 ms gap between frames (10 ms drops frames); a complete poll is about one second across five blocks.
One-by-one reads have a cost you only see from the cloud. Reading 38 control registers individually, with a reconnect each time, took about six minutes - longer than the five-minute window Central uses to decide a device is online, so a working device showed as offline. Grouping neighbouring addresses into three block reads brought it down to about 30 seconds.
A new kind of silent corruption. The Volt's energy counters are register pairs where the high word counts whole 1000 kWh and the low word 0.1 kWh. Combine them as a normal 32-bit number and everything looks right until the high word leaves zero, then the total jumps by orders of magnitude. A couple of its registers also use whole watts and amps where every neighbour uses tenths, so the tests now check that current times voltage agrees with power.
Battery without a BMS. Lesson 5 dropped battery metrics when the BMS was missing. On the Volt with a voltage-controlled pack the SOC register reads 0 forever, so the edge now estimates SOC itself by integrating battery power. Deciding whether a pack is really empty has to use the open-circuit voltage: terminal voltage sags under load, and at 2.4 kW it came close enough to the floor to make a healthy pack look empty.
Treat silence as a fault. In August the poll loop on one device died without raising a single error, and it reported healthy for fourteen hours while reading nothing, because "healthy" was an error rate and there were no errors. Health now also asks when the inverter last answered, with a deliberately wide 15-minute window, because a Deye at night can spend four minutes on one cycle.
From "what I'd do differently" to done. The device identification part is built: on the first successful connection the edge reads the Deye's identification registers once and records serial number, rated power, phase count, firmware version and battery capacity, instead of asking for them in settings.
Not every inverter needs a cable. Inverters that ship with a Solarman WiFi logger can now be read through that logger over its local protocol, with no vendor cloud in the path. The client behind it exposes the same interface as the Modbus client, so all the register decoders are reused.
For the bigger picture of what changed across the platform, see the update at the end of the FutureGrid post.
This is part of an ongoing series about building FutureGrid, a smart energy monitoring platform. Next up: MQTT over mTLS through nginx stream proxies - and the reconnect race condition that took three days to find.
@misc{oljasz2026modbuslessons,
author = {Oljasz, Pawe{\l}},
title = {Modbus {RTU} Over {USB}: Five Lessons From a Year of Polling Solar Inverters},
howpublished = {Lab notebook, entry 008},
year = {2026},
month = mar,
url = {https://oljasz.com/blog/modbus-lessons}
}Entry 008 / filed 24.03.2026 / stable URL, no tracking parameters.
