From c3e98c83d8e9b5080bf07985dbc7be72312462da Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 10 Mar 2023 21:41:50 +0100 Subject: [PATCH] Proxy: match function codes when writing holding registers or coils (#6765) --- go.mod | 2 +- go.sum | 4 ++-- server/modbus/handler.go | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index fdf61fd28..b88533c13 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/Masterminds/sprig/v3 v3.2.3 github.com/PuerkitoBio/goquery v1.8.1 github.com/andig/gosunspec v0.0.0-20211108155140-af2e73b86e71 - github.com/andig/mbserver v0.0.0-20221101171752-364e71b0089c + github.com/andig/mbserver v0.0.0-20230310203303-a5f054e2e6e7 github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef github.com/avast/retry-go/v3 v3.1.1 github.com/aws/aws-sdk-go v1.44.212 diff --git a/go.sum b/go.sum index 0bdbad676..986bd17cd 100644 --- a/go.sum +++ b/go.sum @@ -225,8 +225,8 @@ github.com/andig/go-powerwall v0.2.1-0.20230102102528-4d59ac6910c6 h1:DxdKrb4O3B github.com/andig/go-powerwall v0.2.1-0.20230102102528-4d59ac6910c6/go.mod h1:MR0vuGPmz9sLXrIAsdkXulW1j6t6svVr2D1nJKaJI78= github.com/andig/gosunspec v0.0.0-20211108155140-af2e73b86e71 h1:tnjVNZjuz+CK6fdc7ohJpMHjcEGFI5APp0l5T5Ocr/Y= github.com/andig/gosunspec v0.0.0-20211108155140-af2e73b86e71/go.mod h1:c6P6szcR+ROkqZruOR4f6qbDKFjZX6OitPpj+yJ/r8k= -github.com/andig/mbserver v0.0.0-20221101171752-364e71b0089c h1:H8xwM6qZbn2aE3B+HffaeZQYHGhf3PY34IH3jlmqBIw= -github.com/andig/mbserver v0.0.0-20221101171752-364e71b0089c/go.mod h1:4VtYzTm//oUipwvO3yh0g/udTE7pYJM+U/kyAuFDsgM= +github.com/andig/mbserver v0.0.0-20230310203303-a5f054e2e6e7 h1:INf2QLw+J0ArrvuZusnhIb4msWWk6KkP3+V6ZDkRj+s= +github.com/andig/mbserver v0.0.0-20230310203303-a5f054e2e6e7/go.mod h1:4VtYzTm//oUipwvO3yh0g/udTE7pYJM+U/kyAuFDsgM= github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= diff --git a/server/modbus/handler.go b/server/modbus/handler.go index 5492ac10c..eb3afe106 100644 --- a/server/modbus/handler.go +++ b/server/modbus/handler.go @@ -103,7 +103,7 @@ func (h *handler) HandleCoils(req *mbserver.CoilsRequest) ([]bool, error) { return nil, mbserver.ErrIllegalFunction } - if req.Quantity == 1 { + if req.WriteFuncCode == gridx.FuncCodeWriteSingleCoil { h.log.TRACE.Printf("write coil: id %d addr %d val %t", req.UnitId, req.Addr, req.Args[0]) var u uint16 if req.Args[0] { @@ -137,8 +137,8 @@ func (h *handler) HandleHoldingRegisters(req *mbserver.HoldingRegistersRequest) return nil, mbserver.ErrIllegalFunction } - if req.Quantity == 1 { - h.log.TRACE.Printf("write holding: id %d addr %d val %0x", req.UnitId, req.Addr, req.Args[0]) + if req.WriteFuncCode == gridx.FuncCodeWriteSingleRegister { + h.log.TRACE.Printf("write single holding: id %d addr %d val %04x", req.UnitId, req.Addr, req.Args[0]) b, err := h.conn.WriteSingleRegisterWithSlave(req.UnitId, req.Addr, req.Args[0]) return h.exceptionToUint16AndError("write holding", b, err) }