自定义读取接口&自定义写入接口 #5
13
client.go
13
client.go
@@ -1188,15 +1188,9 @@ func (mc *ModbusClient) readRegistersWithFunctionCode(addr uint16, quantity uint
|
|||||||
unitId: mc.unitId,
|
unitId: mc.unitId,
|
||||||
}
|
}
|
||||||
|
|
||||||
// if functionCode != fcCustomize {
|
if functionCode != fcCustomize {
|
||||||
// err = ErrUnexpectedParameters
|
|
||||||
// mc.logger.Errorf("unexpected function code (%d)", functionCode)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
if functionCode == 0 {
|
|
||||||
err = ErrUnexpectedParameters
|
err = ErrUnexpectedParameters
|
||||||
mc.logger.Errorf("unexpected register type (%v)", functionCode)
|
mc.logger.Errorf("unexpected function code (%d)", functionCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1208,7 +1202,8 @@ func (mc *ModbusClient) readRegistersWithFunctionCode(addr uint16, quantity uint
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if quantity > 1024 {
|
// 16 * 16 * 40
|
||||||
|
if quantity > 10240 {
|
||||||
err = ErrUnexpectedParameters
|
err = ErrUnexpectedParameters
|
||||||
mc.logger.Error("quantity of registers exceeds 1024")
|
mc.logger.Error("quantity of registers exceeds 1024")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ const (
|
|||||||
fcWriteFileRecord uint8 = 0x15
|
fcWriteFileRecord uint8 = 0x15
|
||||||
|
|
||||||
// customize
|
// customize
|
||||||
fcCustomize uint8 = 0x29
|
fcCustomize uint8 = 0x41
|
||||||
|
|
||||||
// exception codes
|
// exception codes
|
||||||
exIllegalFunction uint8 = 0x01
|
exIllegalFunction uint8 = 0x01
|
||||||
|
|||||||
103
rtu_transport.go
103
rtu_transport.go
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
maxRTUFrameLength int = 256
|
maxRTUFrameLength int = 256
|
||||||
)
|
)
|
||||||
|
|
||||||
type rtuTransport struct {
|
type rtuTransport struct {
|
||||||
@@ -21,10 +21,10 @@ type rtuTransport struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type rtuLink interface {
|
type rtuLink interface {
|
||||||
Close() (error)
|
Close() error
|
||||||
Read([]byte) (int, error)
|
Read([]byte) (int, error)
|
||||||
Write([]byte) (int, error)
|
Write([]byte) (int, error)
|
||||||
SetDeadline(time.Time) (error)
|
SetDeadline(time.Time) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a new RTU transport.
|
// Returns a new RTU transport.
|
||||||
@@ -58,11 +58,11 @@ func (rt *rtuTransport) Close() (err error) {
|
|||||||
// Runs a request across the rtu link and returns a response.
|
// Runs a request across the rtu link and returns a response.
|
||||||
func (rt *rtuTransport) ExecuteRequest(req *pdu) (res *pdu, err error) {
|
func (rt *rtuTransport) ExecuteRequest(req *pdu) (res *pdu, err error) {
|
||||||
var ts time.Time
|
var ts time.Time
|
||||||
var t time.Duration
|
var t time.Duration
|
||||||
var n int
|
var n int
|
||||||
|
|
||||||
// set an i/o deadline on the link
|
// set an i/o deadline on the link
|
||||||
err = rt.link.SetDeadline(time.Now().Add(rt.timeout))
|
err = rt.link.SetDeadline(time.Now().Add(rt.timeout))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -78,7 +78,7 @@ func (rt *rtuTransport) ExecuteRequest(req *pdu) (res *pdu, err error) {
|
|||||||
|
|
||||||
// build an RTU ADU out of the request object and
|
// build an RTU ADU out of the request object and
|
||||||
// send the final ADU+CRC on the wire
|
// send the final ADU+CRC on the wire
|
||||||
n, err = rt.link.Write(rt.assembleRTUFrame(req))
|
n, err = rt.link.Write(rt.assembleRTUFrame(req))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -112,7 +112,7 @@ func (rt *rtuTransport) ExecuteRequest(req *pdu) (res *pdu, err error) {
|
|||||||
// Reads a request from the rtu link.
|
// Reads a request from the rtu link.
|
||||||
func (rt *rtuTransport) ReadRequest() (req *pdu, err error) {
|
func (rt *rtuTransport) ReadRequest() (req *pdu, err error) {
|
||||||
// reading requests from RTU links is currently unsupported
|
// reading requests from RTU links is currently unsupported
|
||||||
err = fmt.Errorf("unimplemented")
|
err = fmt.Errorf("unimplemented")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -123,7 +123,7 @@ func (rt *rtuTransport) WriteResponse(res *pdu) (err error) {
|
|||||||
|
|
||||||
// build an RTU ADU out of the request object and
|
// build an RTU ADU out of the request object and
|
||||||
// send the final ADU+CRC on the wire
|
// send the final ADU+CRC on the wire
|
||||||
n, err = rt.link.Write(rt.assembleRTUFrame(res))
|
n, err = rt.link.Write(rt.assembleRTUFrame(res))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -135,16 +135,16 @@ func (rt *rtuTransport) WriteResponse(res *pdu) (err error) {
|
|||||||
|
|
||||||
// Waits for, reads and decodes a frame from the rtu link.
|
// Waits for, reads and decodes a frame from the rtu link.
|
||||||
func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
||||||
var rxbuf []byte
|
var rxbuf []byte
|
||||||
var byteCount int
|
var byteCount int
|
||||||
var bytesNeeded int
|
var bytesNeeded int
|
||||||
var crc crc
|
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
|
// read the serial ADU header: unit id (1 byte), function code (1 byte) and
|
||||||
// PDU length/exception code (1 byte)
|
// PDU length/exception code (1 byte)
|
||||||
byteCount, err = io.ReadFull(rt.link, rxbuf[0:3])
|
byteCount, err = io.ReadFull(rt.link, rxbuf[0:3])
|
||||||
if (byteCount > 0 || err == nil) && byteCount != 3 {
|
if (byteCount > 0 || err == nil) && byteCount != 3 {
|
||||||
err = ErrShortFrame
|
err = ErrShortFrame
|
||||||
return
|
return
|
||||||
@@ -160,15 +160,15 @@ func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we need to read 2 additional bytes of CRC after the payload
|
// we need to read 2 additional bytes of CRC after the payload
|
||||||
bytesNeeded += 2
|
bytesNeeded += 2
|
||||||
|
|
||||||
// never read more than the max allowed frame length
|
// never read more than the max allowed frame length
|
||||||
if byteCount + bytesNeeded > maxRTUFrameLength {
|
if byteCount+bytesNeeded > maxRTUFrameLength {
|
||||||
err = ErrProtocolError
|
err = ErrProtocolError
|
||||||
return
|
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 {
|
if err != nil && err != io.ErrUnexpectedEOF {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -180,19 +180,19 @@ func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
|||||||
|
|
||||||
// compute the CRC on the entire frame, excluding the CRC
|
// compute the CRC on the entire frame, excluding the CRC
|
||||||
crc.init()
|
crc.init()
|
||||||
crc.add(rxbuf[0:3 + bytesNeeded - 2])
|
crc.add(rxbuf[0 : 3+bytesNeeded-2])
|
||||||
|
|
||||||
// compare CRC values
|
// 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
|
err = ErrBadCRC
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res = &pdu{
|
res = &pdu{
|
||||||
unitId: rxbuf[0],
|
unitId: rxbuf[0],
|
||||||
functionCode: rxbuf[1],
|
functionCode: rxbuf[1],
|
||||||
// pass the byte count + trailing data as payload, withtout the CRC
|
// pass the byte count + trailing data as payload, withtout the CRC
|
||||||
payload: rxbuf[2:3 + bytesNeeded - 2],
|
payload: rxbuf[2 : 3+bytesNeeded-2],
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -200,18 +200,18 @@ func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
|||||||
|
|
||||||
// Turns a PDU object into bytes.
|
// Turns a PDU object into bytes.
|
||||||
func (rt *rtuTransport) assembleRTUFrame(p *pdu) (adu []byte) {
|
func (rt *rtuTransport) assembleRTUFrame(p *pdu) (adu []byte) {
|
||||||
var crc crc
|
var crc crc
|
||||||
|
|
||||||
adu = append(adu, p.unitId)
|
adu = append(adu, p.unitId)
|
||||||
adu = append(adu, p.functionCode)
|
adu = append(adu, p.functionCode)
|
||||||
adu = append(adu, p.payload...)
|
adu = append(adu, p.payload...)
|
||||||
|
|
||||||
// run the ADU through the CRC generator
|
// run the ADU through the CRC generator
|
||||||
crc.init()
|
crc.init()
|
||||||
crc.add(adu)
|
crc.add(adu)
|
||||||
|
|
||||||
// append the CRC to the ADU
|
// append the CRC to the ADU
|
||||||
adu = append(adu, crc.value()...)
|
adu = append(adu, crc.value()...)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -220,24 +220,31 @@ func (rt *rtuTransport) assembleRTUFrame(p *pdu) (adu []byte) {
|
|||||||
func expectedResponseLenth(responseCode uint8, responseLength uint8) (byteCount int, err error) {
|
func expectedResponseLenth(responseCode uint8, responseLength uint8) (byteCount int, err error) {
|
||||||
switch responseCode {
|
switch responseCode {
|
||||||
case fcReadHoldingRegisters,
|
case fcReadHoldingRegisters,
|
||||||
fcReadInputRegisters,
|
fcReadInputRegisters,
|
||||||
fcReadCoils,
|
fcReadCoils,
|
||||||
fcReadDiscreteInputs: byteCount = int(responseLength)
|
fcReadDiscreteInputs,
|
||||||
|
0x41:
|
||||||
|
byteCount = int(responseLength)
|
||||||
case fcWriteSingleRegister,
|
case fcWriteSingleRegister,
|
||||||
fcWriteMultipleRegisters,
|
fcWriteMultipleRegisters,
|
||||||
fcWriteSingleCoil,
|
fcWriteSingleCoil,
|
||||||
fcWriteMultipleCoils: byteCount = 3
|
fcWriteMultipleCoils:
|
||||||
case fcMaskWriteRegister: byteCount = 5
|
byteCount = 3
|
||||||
|
case fcMaskWriteRegister:
|
||||||
|
byteCount = 5
|
||||||
case fcReadHoldingRegisters | 0x80,
|
case fcReadHoldingRegisters | 0x80,
|
||||||
fcReadInputRegisters | 0x80,
|
fcReadInputRegisters | 0x80,
|
||||||
fcReadCoils | 0x80,
|
fcReadCoils | 0x80,
|
||||||
fcReadDiscreteInputs | 0x80,
|
fcReadDiscreteInputs | 0x80,
|
||||||
fcWriteSingleRegister | 0x80,
|
fcWriteSingleRegister | 0x80,
|
||||||
fcWriteMultipleRegisters | 0x80,
|
fcWriteMultipleRegisters | 0x80,
|
||||||
fcWriteSingleCoil | 0x80,
|
fcWriteSingleCoil | 0x80,
|
||||||
fcWriteMultipleCoils | 0x80,
|
fcWriteMultipleCoils | 0x80,
|
||||||
fcMaskWriteRegister | 0x80: byteCount = 0
|
fcMaskWriteRegister | 0x80,
|
||||||
default: err = ErrProtocolError
|
0x41 | 0x80:
|
||||||
|
byteCount = 0
|
||||||
|
default:
|
||||||
|
err = ErrProtocolError
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user