forked from grid-x/modbus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtuclient.go
342 lines (294 loc) · 8.79 KB
/
rtuclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2014 Quoc-Viet Nguyen. All rights reserved.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
package modbus
import (
"encoding/binary"
"errors"
"fmt"
"io"
"time"
)
const (
rtuMinSize = 4
rtuMaxSize = 256
rtuExceptionSize = 5
)
const (
stateSlaveID = 1 << iota
stateFunctionCode
stateReadLength
stateReadPayload
stateCRC
)
const (
readCoilsFunctionCode = 0x01
readDiscreteInputsFunctionCode = 0x02
readHoldingRegisterFunctionCode = 0x03
readInputRegisterFunctionCode = 0x04
writeSingleCoilFunctionCode = 0x05
writeSingleRegisterFunctionCode = 0x06
writeMultipleRegisterFunctionCode = 0x10
writeMultipleCoilsFunctionCode = 0x0F
maskWriteRegisterFunctionCode = 0x16
readWriteMultipleRegisterFunctionCode = 0x17
readFifoQueueFunctionCode = 0x18
)
// RTUClientHandler implements Packager and Transporter interface.
type RTUClientHandler struct {
rtuPackager
*rtuSerialTransporter
}
// NewRTUClientHandler allocates and initializes a RTUClientHandler.
func NewRTUClientHandler(address string) *RTUClientHandler {
return &RTUClientHandler{
rtuSerialTransporter: &rtuSerialTransporter{
serialPort: defaultSerialPort(address),
},
}
}
// RTUClient creates RTU client with default handler and given connect string.
func RTUClient(address string) Client {
handler := NewRTUClientHandler(address)
return NewClient(handler)
}
// Clone creates a new client handler with the same underlying shared transport.
func (mb *RTUClientHandler) Clone() *RTUClientHandler {
return &RTUClientHandler{
rtuSerialTransporter: mb.rtuSerialTransporter,
}
}
// rtuPackager implements Packager interface.
type rtuPackager struct {
SlaveID byte
}
// SetSlave sets modbus slave id for the next client operations
func (mb *rtuPackager) SetSlave(slaveID byte) {
mb.SlaveID = slaveID
}
// Encode encodes PDU in an RTU frame:
//
// Slave Address : 1 byte
// Function : 1 byte
// Data : 0 up to 252 bytes
// CRC : 2 byte
func (mb *rtuPackager) Encode(pdu *ProtocolDataUnit) (adu []byte, err error) {
length := len(pdu.Data) + 4
if length > rtuMaxSize {
err = fmt.Errorf("modbus: length of data '%v' must not be bigger than '%v'", length, rtuMaxSize)
return
}
adu = make([]byte, length)
adu[0] = mb.SlaveID
adu[1] = pdu.FunctionCode
copy(adu[2:], pdu.Data)
// Append crc
var crc crc
crc.reset().pushBytes(adu[0 : length-2])
checksum := crc.value()
adu[length-1] = byte(checksum >> 8)
adu[length-2] = byte(checksum)
return
}
// Verify verifies response length and slave id.
func (mb *rtuPackager) Verify(aduRequest []byte, aduResponse []byte) (err error) {
length := len(aduResponse)
// Minimum size (including address, function and CRC)
if length < rtuMinSize {
err = fmt.Errorf("modbus: response length '%v' does not meet minimum '%v'", length, rtuMinSize)
return
}
// Slave address must match
if aduResponse[0] != aduRequest[0] {
err = fmt.Errorf("modbus: response slave id '%v' does not match request '%v'", aduResponse[0], aduRequest[0])
return
}
return
}
// Decode extracts PDU from RTU frame and verify CRC.
func (mb *rtuPackager) Decode(adu []byte) (pdu *ProtocolDataUnit, err error) {
length := len(adu)
// Calculate checksum
var crc crc
crc.reset().pushBytes(adu[0 : length-2])
checksum := uint16(adu[length-1])<<8 | uint16(adu[length-2])
if checksum != crc.value() {
err = fmt.Errorf("modbus: response crc '%v' does not match expected '%v'", checksum, crc.value())
return
}
// Function code & data
pdu = &ProtocolDataUnit{}
pdu.FunctionCode = adu[1]
pdu.Data = adu[2 : length-2]
return
}
// rtuSerialTransporter implements Transporter interface.
type rtuSerialTransporter struct {
serialPort
}
// InvalidLengthError is returned by readIncrementally when the modbus response would overflow buffer
// implemented to simplify testing
type InvalidLengthError struct {
length byte // length received which triggered the error
}
// Error implements the error interface
func (e *InvalidLengthError) Error() string {
return fmt.Sprintf("invalid length received: %d", e.length)
}
// readIncrementally reads incrementally
func readIncrementally(slaveID, functionCode byte, r io.Reader, deadline time.Time) ([]byte, error) {
if r == nil {
return nil, fmt.Errorf("reader is nil")
}
buf := make([]byte, 1)
data := make([]byte, 0, rtuMaxSize)
state := stateSlaveID
var length, toRead byte
var crcCount int
for {
if time.Now().After(deadline) { // Possible that serialport may spew data
return nil, errors.New("failed to read from serial port within deadline")
}
if _, err := io.ReadAtLeast(r, buf, 1); err != nil {
return nil, err
}
switch state {
// expecting slaveID
case stateSlaveID:
// read slaveID
if buf[0] == slaveID {
state = stateFunctionCode
data = append(data, buf[0])
}
case stateFunctionCode:
// read function code
if buf[0] == functionCode {
switch functionCode {
case readDiscreteInputsFunctionCode,
readCoilsFunctionCode,
readHoldingRegisterFunctionCode,
readInputRegisterFunctionCode,
readWriteMultipleRegisterFunctionCode,
readFifoQueueFunctionCode:
state = stateReadLength
case writeSingleCoilFunctionCode,
writeSingleRegisterFunctionCode,
writeMultipleRegisterFunctionCode,
writeMultipleCoilsFunctionCode:
state = stateReadPayload
toRead = 4
case maskWriteRegisterFunctionCode:
state = stateReadPayload
toRead = 6
default:
return nil, fmt.Errorf("functioncode not handled: %d", functionCode)
}
data = append(data, buf[0])
} else if buf[0] == functionCode+0x80 {
state = stateReadPayload
toRead = 1 // only exception code left to read
data = append(data, buf[0])
}
case stateReadLength:
// read length byte
length = buf[0]
// max length = rtuMaxSize - SlaveID(1) - FunctionCode(1) - length(1) - CRC(2)
if length > rtuMaxSize-5 || length == 0 {
return nil, &InvalidLengthError{length: length}
}
state = stateReadPayload
toRead = length
data = append(data, length)
case stateReadPayload:
// read payload
data = append(data, buf[0])
toRead--
if toRead == 0 {
state = stateCRC
}
case stateCRC:
// read crc
data = append(data, buf[0])
crcCount++
if crcCount == 2 {
return data, nil
}
}
}
}
func (mb *rtuSerialTransporter) Send(aduRequest []byte) (aduResponse []byte, err error) {
mb.mu.Lock()
defer mb.mu.Unlock()
// Make sure port is connected
if err = mb.connect(); err != nil {
return
}
// Wait for previous frame delay to elapse
time.Sleep(time.Until(mb.lastActivity.Add(mb.frameDelay())))
defer func() { mb.lastActivity = time.Now() }()
// Start the timer to close when idle
mb.lastActivity = time.Now()
mb.startCloseTimer()
// Send the request
mb.logf("modbus: send % x\n", aduRequest)
if _, err = mb.port.Write(aduRequest); err != nil {
return
}
bytesToRead := calculateResponseLength(aduRequest)
time.Sleep(mb.calculateDelay(len(aduRequest) + bytesToRead))
data, err := readIncrementally(aduRequest[0], aduRequest[1], mb.port, time.Now().Add(mb.Config.Timeout))
mb.logf("modbus: recv % x\n", data[:])
aduResponse = data
return
}
// calculateDelay roughly calculates time needed for the next frame.
// See MODBUS over Serial Line - Specification and Implementation Guide (page 13).
func (mb *rtuSerialTransporter) calculateDelay(chars int) time.Duration {
characterDuration := 11000000 / mb.BaudRate
var characterDelay int // us
if mb.BaudRate <= 0 || mb.BaudRate > 19200 {
characterDelay = 750
} else {
characterDelay = 16500000 / mb.BaudRate
}
return time.Duration((characterDuration+characterDelay)*chars)*time.Microsecond + mb.frameDelay()
}
// frameDelay returns the required minimum delay at the start and and the end of each frame.
// See MODBUS over Serial Line - Specification and Implementation Guide (page 13).
func (mb *rtuSerialTransporter) frameDelay() time.Duration {
var fd int // µs
if mb.BaudRate <= 0 || mb.BaudRate > 19200 {
fd = 1750
} else {
fd = 38500000 / mb.BaudRate
}
return time.Duration(fd) * time.Microsecond
}
func calculateResponseLength(adu []byte) int {
length := rtuMinSize
switch adu[1] {
case FuncCodeReadDiscreteInputs,
FuncCodeReadCoils:
count := int(binary.BigEndian.Uint16(adu[4:]))
length += 1 + count/8
if count%8 != 0 {
length++
}
case FuncCodeReadInputRegisters,
FuncCodeReadHoldingRegisters,
FuncCodeReadWriteMultipleRegisters:
count := int(binary.BigEndian.Uint16(adu[4:]))
length += 1 + count*2
case FuncCodeWriteSingleCoil,
FuncCodeWriteMultipleCoils,
FuncCodeWriteSingleRegister,
FuncCodeWriteMultipleRegisters:
length += 4
case FuncCodeMaskWriteRegister:
length += 6
case FuncCodeReadFIFOQueue:
// undetermined
default:
}
return length
}