自定义实时数据读取&读标定信息
This commit is contained in:
81
client.go
81
client.go
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user