Skip to content

fix(modbus): reconnect on transport errors (Sungrow silent-zeros bug) - #125

Merged
frahlg merged 1 commit into
masterfrom
fix/modbus-reconnect-on-error
Apr 19, 2026
Merged

frahlg merged 1 commit into
masterfrom
fix/modbus-reconnect-on-error

Conversation

@frahlg

@frahlg frahlg commented Apr 19, 2026

Copy link
Copy Markdown
Member

Summary

On 2026-04-19 Fredrik's Sungrow SH10RT silently stopped reporting telemetry — pv/bat/meter/SoC all stuck at zero for hours, SoC slowly draining below the configured 10 % floor with no visibility. Root cause: after our watchdog-triggered write at startup, the inverter's TCP Modbus server dropped the session. The simonvetter/modbus client does not auto-reopen — subsequent ReadRegisters calls return transport errors until Open() is called again. Our driver's pcall around host.modbus_read caught those errors silently and emitted zeros. A manual driver-restart fixed it, confirming the theory.

Fix

Capability.Read / WriteSingle / WriteMulti now classify errors:

  • Transport (EOF, ECONNRESET, EPIPE, i/o timeout, net.Error.Timeout=true, plus a narrow allowlist of simonvetter's wrapped string-errors) → reconnect + retry once
  • Protocol (illegal address / illegal function / slave busy) → bubble up — peer is alive, connection is fine

Test plan

  • isTransportError truth table (10 cases)
  • Toy TCP server responds once then closes the socket; verifies Read reconnects and returns the next queued value
  • Full go test ./... incl. e2e stays green
  • Deploy to Fredrik's Pi; Sungrow telemetry should now survive any mid-session disconnect automatically

🤖 Generated with Claude Code

simonvetter/modbus doesn't self-heal from a closed TCP socket:
subsequent reads return errors forever until Open() is called again.
Observed on 2026-04-19 on Fredrik's Sungrow SH10RT — after the
driver's watchdog-triggered write at startup, the inverter silently
dropped the TCP connection and every driver_poll's 23 modbus_reads
returned errors (caught by Lua pcall silently). The driver kept
emitting zeros for PV, battery, meter, and SoC for hours until a
manual driver restart. SoC reporting for the Sungrow got stuck at
its pre-disconnect value while the inverter autonomously discharged
down below the configured 10 % floor.

Capability.Read / WriteSingle / WriteMulti now classify errors:
- Transport errors (EOF, ECONNRESET, EPIPE, i/o timeout, net.Error
  with Timeout=true, plus a narrow string-match allowlist for
  messages simonvetter wraps as plain errors) → reconnect + retry once.
- Modbus protocol errors (illegal address / function / slave busy)
  → bubble up; the peer is alive, connection is fine.

Test stands up a toy TCP server that responds once then closes;
verifies the second Read succeeds via reconnect. isTransportError
classifier has its own truth table.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@frahlg
frahlg merged commit 7358887 into master Apr 19, 2026
1 check passed
@frahlg
frahlg deleted the fix/modbus-reconnect-on-error branch April 19, 2026 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant