From a77c072cb56de00ca2fa2970925a0597c2a1f8fd Mon Sep 17 00:00:00 2001 From: PopCorn Date: Thu, 13 Nov 2025 15:49:29 +0800 Subject: [PATCH] =?UTF-8?q?modbus=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index a22e2a5..5e5ebc8 100644 --- a/client.go +++ b/client.go @@ -1232,21 +1232,53 @@ func (mc *ModbusClient) readRegistersWithFunctionCode(addr uint16, quantity uint } // validate the response code + // switch { + // case res.functionCode == req.functionCode: + // // make sure the payload length is what we expect + // // (1 byte of length + 2 bytes per register) + // if len(res.payload) != 1+2*int(quantity) { + // err = ErrProtocolError + // return + // } + + // // validate the byte count field + // // (2 bytes per register * number of registers) + // if uint(res.payload[0]) != 2*uint(quantity) { + // err = ErrProtocolError + // return + // } + + // // remove the byte count field from the returned slice + // bytes = res.payload[1:] + + // case res.functionCode == (req.functionCode | 0x80): + // if len(res.payload) != 1 { + // err = ErrProtocolError + // return + // } + + // err = mapExceptionCodeToError(res.payload[0]) + + // default: + // err = ErrProtocolError + // mc.logger.Warningf("unexpected response code (%v)", res.functionCode) + // } + switch { case res.functionCode == req.functionCode: // make sure the payload length is what we expect // (1 byte of length + 2 bytes per register) - if len(res.payload) != 1+2*int(quantity) { - err = ErrProtocolError - return - } + // if len(res.payload) != 1+2*int(quantity) { + // err = ErrProtocolError + // return + // } // validate the byte count field // (2 bytes per register * number of registers) - if uint(res.payload[0]) != 2*uint(quantity) { - err = ErrProtocolError - return - } + // if uint(res.payload[0]) != 2*uint(quantity) { + // err = ErrProtocolError + // return + // } // remove the byte count field from the returned slice bytes = res.payload[1:]