Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0377201fc9 | ||
|
|
a77c072cb5 | ||
| 8c260d4061 | |||
|
|
f79cf0243b | ||
| 4a82e6f652 | |||
| 31af890159 | |||
| e09b96fab0 |
114
client.go
114
client.go
@@ -773,6 +773,62 @@ func (mc *ModbusClient) WriteRegister(addr uint16, value uint16) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Writes a single 16-bit register (function code 06).
|
||||||
|
func (mc *ModbusClient) WriteRegisterWithRes(addr uint16, value uint16) (bytes []byte, err error) {
|
||||||
|
var req *pdu
|
||||||
|
var res *pdu
|
||||||
|
|
||||||
|
mc.lock.Lock()
|
||||||
|
defer mc.lock.Unlock()
|
||||||
|
|
||||||
|
// create and fill in the request object
|
||||||
|
req = &pdu{
|
||||||
|
unitId: mc.unitId,
|
||||||
|
functionCode: fcWriteSingleRegister,
|
||||||
|
}
|
||||||
|
|
||||||
|
// register address
|
||||||
|
req.payload = uint16ToBytes(BIG_ENDIAN, addr)
|
||||||
|
// register value
|
||||||
|
req.payload = append(req.payload, uint16ToBytes(mc.endianness, value)...)
|
||||||
|
|
||||||
|
// run the request across the transport and wait for a response
|
||||||
|
res, err = mc.executeRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 ||
|
||||||
|
// bytes 3-4 should be the value
|
||||||
|
bytesToUint16(mc.endianness, res.payload[2:4]) != value {
|
||||||
|
err = ErrProtocolError
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes = req.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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Writes multiple 16-bit registers (function code 16).
|
// Writes multiple 16-bit registers (function code 16).
|
||||||
func (mc *ModbusClient) WriteRegisters(addr uint16, values []uint16) (err error) {
|
func (mc *ModbusClient) WriteRegisters(addr uint16, values []uint16) (err error) {
|
||||||
var payload []byte
|
var payload []byte
|
||||||
@@ -1132,11 +1188,11 @@ func (mc *ModbusClient) readRegistersWithFunctionCode(addr uint16, quantity uint
|
|||||||
unitId: mc.unitId,
|
unitId: mc.unitId,
|
||||||
}
|
}
|
||||||
|
|
||||||
if functionCode != fcCustomize {
|
// if functionCode != fcCustomize {
|
||||||
err = ErrUnexpectedParameters
|
// err = ErrUnexpectedParameters
|
||||||
mc.logger.Errorf("unexpected function code (%d)", functionCode)
|
// mc.logger.Errorf("unexpected function code (%d)", functionCode)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
if functionCode == 0 {
|
if functionCode == 0 {
|
||||||
err = ErrUnexpectedParameters
|
err = ErrUnexpectedParameters
|
||||||
@@ -1176,21 +1232,53 @@ func (mc *ModbusClient) readRegistersWithFunctionCode(addr uint16, quantity uint
|
|||||||
}
|
}
|
||||||
|
|
||||||
// validate the response code
|
// 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 {
|
switch {
|
||||||
case res.functionCode == req.functionCode:
|
case res.functionCode == req.functionCode:
|
||||||
// make sure the payload length is what we expect
|
// make sure the payload length is what we expect
|
||||||
// (1 byte of length + 2 bytes per register)
|
// (1 byte of length + 2 bytes per register)
|
||||||
if len(res.payload) != 1+2*int(quantity) {
|
// if len(res.payload) != 1+2*int(quantity) {
|
||||||
err = ErrProtocolError
|
// err = ErrProtocolError
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
// validate the byte count field
|
// validate the byte count field
|
||||||
// (2 bytes per register * number of registers)
|
// (2 bytes per register * number of registers)
|
||||||
if uint(res.payload[0]) != 2*uint(quantity) {
|
// if uint(res.payload[0]) != 2*uint(quantity) {
|
||||||
err = ErrProtocolError
|
// err = ErrProtocolError
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
// remove the byte count field from the returned slice
|
// remove the byte count field from the returned slice
|
||||||
bytes = res.payload[1:]
|
bytes = res.payload[1:]
|
||||||
|
|||||||
Reference in New Issue
Block a user