自定义实时数据读取&读标定信息

This commit is contained in:
2025-11-17 11:01:51 +08:00
parent 1c4d54df71
commit 72429c08a3
2 changed files with 196 additions and 23 deletions

View File

@@ -801,17 +801,27 @@ func (mc *ModbusClient) WriteRegisterWithRes(addr uint16, value uint16) (bytes [
// validate the response code
switch {
case res.functionCode == req.functionCode:
// expect 4 bytes (2 byte of address + 2 bytes of value)
if len(res.payload) != 4 ||
// bytes 1-2 should be the register address
bytesToUint16(BIG_ENDIAN, res.payload[0:2]) != addr ||
// expect at least 4 bytes (2 byte of address + 2 bytes of value)
// 后面可能还有自定义数据
if len(res.payload) < 4 {
err = ErrProtocolError
return
}
// bytes 1-2 should be the register address
if bytesToUint16(BIG_ENDIAN, res.payload[0:2]) != addr ||
// bytes 3-4 should be the value
bytesToUint16(mc.endianness, res.payload[2:4]) != value {
err = ErrProtocolError
return
}
bytes = req.payload[1:]
// 返回自定义数据第4字节之后的数据
if len(res.payload) > 4 {
bytes = res.payload[4:]
} else {
bytes = []byte{} // 没有自定义数据,返回空切片
}
case res.functionCode == (req.functionCode | 0x80):
if len(res.payload) != 1 {
@@ -1261,22 +1271,55 @@ func (mc *ModbusClient) readRegistersWithFunctionCode(addr uint16, quantity uint
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
// }
// For custom function code 0x41, the payload format is:
// [起始地址(2字节)] [字节数(2字节)] [数据...]
if functionCode == fcCustomize {
// validate minimum payload length (start address 2 bytes + byte count 2 bytes)
if len(res.payload) < 4 {
err = ErrProtocolError
mc.logger.Errorf("payload too short for custom function code: %d bytes", len(res.payload))
return
}
// validate the byte count field
// (2 bytes per register * number of registers)
// if uint(res.payload[0]) != 2*uint(quantity) {
// err = ErrProtocolError
// return
// }
// extract byte count from payload (bytes 2-3, big endian)
byteCount := bytesToUint16(BIG_ENDIAN, res.payload[2:4])
// remove the byte count field from the returned slice
bytes = res.payload[1:]
// validate payload length matches expected data length
expectedLength := 4 + int(byteCount) // start address (2) + byte count (2) + data
if len(res.payload) != expectedLength {
err = ErrProtocolError
mc.logger.Errorf("payload length mismatch: expected %d, got %d", expectedLength, len(res.payload))
return
}
// validate byte count matches requested quantity
if byteCount != 2*quantity {
err = ErrProtocolError
mc.logger.Errorf("byte count mismatch: expected %d, got %d", 2*quantity, byteCount)
return
}
// extract only the data part (skip start address and byte count)
bytes = res.payload[4:]
} else {
// standard modbus protocol handling
// 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 {