Skip to content

Commit fbebc96

Browse files
committed
FoxESS (Modbus): ensureReg: read-compare-write instead of write-then-verify
Some registers reject redundant writes with Modbus exception 3 (illegal data value). Switch from write-then-read-back-on-error to read-first, skip write if value already matches.
1 parent 5906a53 commit fbebc96

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

charger/foxess-modbus.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,18 @@ func (wb *FoxESSEVC) writeReg(reg, val uint16) error {
151151
return err
152152
}
153153

154-
// ensureReg writes a value to a read/write register and verifies the result.
155-
// If the write is rejected (e.g. because the register already holds the value),
156-
// it reads back the register and succeeds if the value matches.
154+
// ensureReg writes a value to a read/write register only if it differs from
155+
// the current value, avoiding spurious write errors on registers that reject
156+
// redundant writes (e.g. Modbus exception 3 when value is unchanged).
157157
func (wb *FoxESSEVC) ensureReg(reg, val uint16) error {
158-
if err := wb.writeReg(reg, val); err != nil {
159-
b, readErr := wb.conn.ReadHoldingRegisters(reg, 1)
160-
if readErr != nil {
161-
return err
162-
}
163-
if binary.BigEndian.Uint16(b) != val {
164-
return err
165-
}
158+
b, err := wb.conn.ReadHoldingRegisters(reg, 1)
159+
if err != nil {
160+
return err
166161
}
167-
168-
return nil
162+
if binary.BigEndian.Uint16(b) == val {
163+
return nil
164+
}
165+
return wb.writeReg(reg, val)
169166
}
170167

171168
// detectPbox reads the system alarm register and inspects the PBOX alarm bit (bit 1

0 commit comments

Comments
 (0)