Compare commits
3 Commits
v0.0.3
...
64d006461f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
64d006461f | ||
|
|
d5e9ce78d9 | ||
| 1c8ea53e87 |
13
client.go
13
client.go
@@ -1188,15 +1188,9 @@ func (mc *ModbusClient) readRegistersWithFunctionCode(addr uint16, quantity uint
|
||||
unitId: mc.unitId,
|
||||
}
|
||||
|
||||
// if functionCode != fcCustomize {
|
||||
// err = ErrUnexpectedParameters
|
||||
// mc.logger.Errorf("unexpected function code (%d)", functionCode)
|
||||
// return
|
||||
// }
|
||||
|
||||
if functionCode == 0 {
|
||||
if functionCode != fcCustomize {
|
||||
err = ErrUnexpectedParameters
|
||||
mc.logger.Errorf("unexpected register type (%v)", functionCode)
|
||||
mc.logger.Errorf("unexpected function code (%d)", functionCode)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1208,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
|
||||
|
||||
@@ -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)
|
||||
@@ -163,12 +163,12 @@ func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
||||
bytesNeeded += 2
|
||||
|
||||
// never read more than the max allowed frame length
|
||||
if byteCount + bytesNeeded > maxRTUFrameLength {
|
||||
if byteCount+bytesNeeded > maxRTUFrameLength {
|
||||
err = ErrProtocolError
|
||||
return
|
||||
}
|
||||
|
||||
byteCount, err = io.ReadFull(rt.link, rxbuf[3:3 + bytesNeeded])
|
||||
byteCount, err = io.ReadFull(rt.link, rxbuf[3:3+bytesNeeded])
|
||||
if err != nil && err != io.ErrUnexpectedEOF {
|
||||
return
|
||||
}
|
||||
@@ -180,10 +180,10 @@ func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
||||
|
||||
// compute the CRC on the entire frame, excluding the CRC
|
||||
crc.init()
|
||||
crc.add(rxbuf[0:3 + bytesNeeded - 2])
|
||||
crc.add(rxbuf[0 : 3+bytesNeeded-2])
|
||||
|
||||
// compare CRC values
|
||||
if !crc.isEqual(rxbuf[3 + bytesNeeded - 2], rxbuf[3 + bytesNeeded - 1]) {
|
||||
if !crc.isEqual(rxbuf[3+bytesNeeded-2], rxbuf[3+bytesNeeded-1]) {
|
||||
err = ErrBadCRC
|
||||
return
|
||||
}
|
||||
@@ -192,7 +192,7 @@ func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
||||
unitId: rxbuf[0],
|
||||
functionCode: rxbuf[1],
|
||||
// pass the byte count + trailing data as payload, withtout the CRC
|
||||
payload: rxbuf[2:3 + bytesNeeded - 2],
|
||||
payload: rxbuf[2 : 3+bytesNeeded-2],
|
||||
}
|
||||
|
||||
return
|
||||
@@ -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