Compare commits
10 Commits
v0.0.0
...
64d006461f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
64d006461f | ||
|
|
d5e9ce78d9 | ||
| 1c8ea53e87 | |||
|
|
0377201fc9 | ||
|
|
a77c072cb5 | ||
| 8c260d4061 | |||
|
|
f79cf0243b | ||
| 4a82e6f652 | |||
| 31af890159 | |||
| e09b96fab0 |
113
client.go
113
client.go
@@ -773,6 +773,62 @@ func (mc *ModbusClient) WriteRegister(addr uint16, value uint16) (err error) {
|
||||
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).
|
||||
func (mc *ModbusClient) WriteRegisters(addr uint16, values []uint16) (err error) {
|
||||
var payload []byte
|
||||
@@ -1138,12 +1194,6 @@ func (mc *ModbusClient) readRegistersWithFunctionCode(addr uint16, quantity uint
|
||||
return
|
||||
}
|
||||
|
||||
if functionCode == 0 {
|
||||
err = ErrUnexpectedParameters
|
||||
mc.logger.Errorf("unexpected register type (%v)", functionCode)
|
||||
return
|
||||
}
|
||||
|
||||
req.functionCode = functionCode
|
||||
|
||||
if quantity == 0 {
|
||||
@@ -1152,7 +1202,8 @@ func (mc *ModbusClient) readRegistersWithFunctionCode(addr uint16, quantity uint
|
||||
return
|
||||
}
|
||||
|
||||
if quantity > 1024 {
|
||||
// 16 * 16 * 40
|
||||
if quantity > 10240 {
|
||||
err = ErrUnexpectedParameters
|
||||
mc.logger.Error("quantity of registers exceeds 1024")
|
||||
return
|
||||
@@ -1176,21 +1227,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:]
|
||||
|
||||
@@ -41,7 +41,7 @@ const (
|
||||
fcWriteFileRecord uint8 = 0x15
|
||||
|
||||
// customize
|
||||
fcCustomize uint8 = 0x29
|
||||
fcCustomize uint8 = 0x41
|
||||
|
||||
// exception codes
|
||||
exIllegalFunction uint8 = 0x01
|
||||
|
||||
@@ -21,10 +21,10 @@ type rtuTransport struct {
|
||||
}
|
||||
|
||||
type rtuLink interface {
|
||||
Close() (error)
|
||||
Close() error
|
||||
Read([]byte) (int, error)
|
||||
Write([]byte) (int, error)
|
||||
SetDeadline(time.Time) (error)
|
||||
SetDeadline(time.Time) error
|
||||
}
|
||||
|
||||
// Returns a new RTU transport.
|
||||
@@ -140,7 +140,7 @@ func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
||||
var bytesNeeded int
|
||||
var crc crc
|
||||
|
||||
rxbuf = make([]byte, maxRTUFrameLength)
|
||||
rxbuf = make([]byte, 10243)
|
||||
|
||||
// read the serial ADU header: unit id (1 byte), function code (1 byte) and
|
||||
// PDU length/exception code (1 byte)
|
||||
@@ -222,12 +222,16 @@ func expectedResponseLenth(responseCode uint8, responseLength uint8) (byteCount
|
||||
case fcReadHoldingRegisters,
|
||||
fcReadInputRegisters,
|
||||
fcReadCoils,
|
||||
fcReadDiscreteInputs: byteCount = int(responseLength)
|
||||
fcReadDiscreteInputs,
|
||||
0x41:
|
||||
byteCount = int(responseLength)
|
||||
case fcWriteSingleRegister,
|
||||
fcWriteMultipleRegisters,
|
||||
fcWriteSingleCoil,
|
||||
fcWriteMultipleCoils: byteCount = 3
|
||||
case fcMaskWriteRegister: byteCount = 5
|
||||
fcWriteMultipleCoils:
|
||||
byteCount = 3
|
||||
case fcMaskWriteRegister:
|
||||
byteCount = 5
|
||||
case fcReadHoldingRegisters | 0x80,
|
||||
fcReadInputRegisters | 0x80,
|
||||
fcReadCoils | 0x80,
|
||||
@@ -236,8 +240,11 @@ func expectedResponseLenth(responseCode uint8, responseLength uint8) (byteCount
|
||||
fcWriteMultipleRegisters | 0x80,
|
||||
fcWriteSingleCoil | 0x80,
|
||||
fcWriteMultipleCoils | 0x80,
|
||||
fcMaskWriteRegister | 0x80: byteCount = 0
|
||||
default: err = ErrProtocolError
|
||||
fcMaskWriteRegister | 0x80,
|
||||
0x41 | 0x80:
|
||||
byteCount = 0
|
||||
default:
|
||||
err = ErrProtocolError
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user