This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
client_rtu.go
241 lines (214 loc) · 6.7 KB
/
client_rtu.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
package modbus
import (
"encoding/binary"
"fmt"
"io"
"time"
)
const (
rtuExceptionSize = 5
)
// RTUClientProvider implements ClientProvider interface.
type RTUClientProvider struct {
serialPort
logger
*pool
}
// check RTUClientProvider implements the interface ClientProvider underlying method
var _ ClientProvider = (*RTUClientProvider)(nil)
// request pool, all RTU client use this pool
var rtuPool = newPool(rtuAduMaxSize)
// NewRTUClientProvider allocates and initializes a RTUClientProvider.
// it will use default /dev/ttyS0 19200 8 1 N and timeout 1000
func NewRTUClientProvider(opts ...ClientProviderOption) *RTUClientProvider {
p := &RTUClientProvider{
logger: newLogger("modbusRTUMaster => "),
pool: rtuPool,
}
for _, opt := range opts {
opt(p)
}
return p
}
func (sf *protocolFrame) encodeRTUFrame(slaveID byte, pdu ProtocolDataUnit) ([]byte, error) {
length := len(pdu.Data) + 4
if length > rtuAduMaxSize {
return nil, fmt.Errorf("modbus: length of data '%v' must not be bigger than '%v'", length, rtuAduMaxSize)
}
requestAdu := sf.adu[:0:length]
requestAdu = append(requestAdu, slaveID, pdu.FuncCode)
requestAdu = append(requestAdu, pdu.Data...)
checksum := CRC16(requestAdu)
requestAdu = append(requestAdu, byte(checksum), byte(checksum>>8))
return requestAdu, nil
}
// decode extracts slaveID and PDU from RTU frame and verify CRC.
func decodeRTUFrame(adu []byte) (uint8, []byte, error) {
if len(adu) < rtuAduMinSize { // Minimum size (including address, funcCode and CRC)
return 0, nil, fmt.Errorf("modbus: response length '%v' does not meet minimum '%v'", len(adu), rtuAduMinSize)
}
// Calculate checksum
crc, expect := CRC16(adu[:len(adu)-2]), binary.LittleEndian.Uint16(adu[len(adu)-2:])
if crc != expect {
return 0, nil, fmt.Errorf("modbus: response crc '%x' does not match expected '%x'", expect, crc)
}
// slaveID & PDU but pass crc
return adu[0], adu[1 : len(adu)-2], nil
}
// Send request to the remote server, it implements on SendRawFrame
func (sf *RTUClientProvider) Send(slaveID byte, request ProtocolDataUnit) (ProtocolDataUnit, error) {
var response ProtocolDataUnit
frame := sf.pool.get()
defer sf.pool.put(frame)
aduRequest, err := frame.encodeRTUFrame(slaveID, request)
if err != nil {
return response, err
}
aduResponse, err := sf.SendRawFrame(aduRequest)
if err != nil {
return response, err
}
rspSlaveID, pdu, err := decodeRTUFrame(aduResponse)
if err != nil {
return response, err
}
response = ProtocolDataUnit{pdu[0], pdu[1:]}
err = verify(slaveID, rspSlaveID, request, response)
return response, err
}
// SendPdu send pdu request to the remote server
func (sf *RTUClientProvider) SendPdu(slaveID byte, pduRequest []byte) ([]byte, error) {
if len(pduRequest) < pduMinSize || len(pduRequest) > pduMaxSize {
return nil, fmt.Errorf("modbus: pdu size '%v' must not be between '%v' and '%v'",
len(pduRequest), pduMinSize, pduMaxSize)
}
frame := sf.pool.get()
defer sf.pool.put(frame)
request := ProtocolDataUnit{pduRequest[0], pduRequest[1:]}
requestAdu, err := frame.encodeRTUFrame(slaveID, request)
if err != nil {
return nil, err
}
aduResponse, err := sf.SendRawFrame(requestAdu)
if err != nil {
return nil, err
}
rspSlaveID, pdu, err := decodeRTUFrame(aduResponse)
if err != nil {
return nil, err
}
response := ProtocolDataUnit{pdu[0], pdu[1:]}
if err = verify(slaveID, rspSlaveID, request, response); err != nil {
return nil, err
}
// PDU pass slaveID & crc
return pdu, nil
}
// SendRawFrame send Adu frame
func (sf *RTUClientProvider) SendRawFrame(aduRequest []byte) (aduResponse []byte, err error) {
sf.mu.Lock()
defer sf.mu.Unlock()
if err = sf.connect(); err != nil {
return
}
// Send the request
sf.Debugf("sending [% x]", aduRequest)
_, err = sf.port.Write(aduRequest)
if err != nil {
sf.close()
return
}
function, functionFail := aduRequest[1], aduRequest[1]|0x80
bytesToRead := calculateResponseLength(aduRequest)
time.Sleep(sf.calculateDelay(len(aduRequest) + bytesToRead))
var n int
var n1 int
var data [rtuAduMaxSize]byte
// We first read the minimum length and then read either the full package
// or the error package, depending on the error status (byte 2 of the response)
n, err = io.ReadAtLeast(sf.port, data[:], rtuAduMinSize)
if err != nil {
return
}
switch {
case data[1] == function:
// if the function is correct
// we read the rest of the bytes
if n < bytesToRead {
if bytesToRead > rtuAduMinSize && bytesToRead <= rtuAduMaxSize {
if bytesToRead > n {
n1, err = io.ReadFull(sf.port, data[n:bytesToRead])
n += n1
}
}
}
case data[1] == functionFail:
// for error we need to read 5 bytes
if n < rtuExceptionSize {
n1, err = io.ReadFull(sf.port, data[n:rtuExceptionSize])
}
n += n1
default:
err = fmt.Errorf("modbus: unknown function code % x", data[1])
}
if err != nil {
return
}
aduResponse = data[:n]
sf.Debugf("received [% x]", aduResponse)
return aduResponse, nil
}
// calculateDelay roughly calculates time needed for the next frame.
// See MODBUS over Serial Line - Specification and Implementation Guide (page 13).
func (sf *RTUClientProvider) calculateDelay(chars int) time.Duration {
var characterDelay, frameDelay int // us
if sf.BaudRate <= 0 || sf.BaudRate > 19200 {
characterDelay = 750
frameDelay = 1750
} else {
characterDelay = 15000000 / sf.BaudRate
frameDelay = 35000000 / sf.BaudRate
}
return time.Duration(characterDelay*chars+frameDelay) * time.Microsecond
}
func calculateResponseLength(adu []byte) int {
length := rtuAduMinSize
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
}
// helper
// verify confirms valid data(including slaveID,funcCode,response data)
func verify(reqSlaveID, rspSlaveID uint8, reqPDU, rspPDU ProtocolDataUnit) error {
switch {
case reqSlaveID != rspSlaveID: // Check slaveID same
return fmt.Errorf("modbus: response slave id '%v' does not match request '%v'", rspSlaveID, reqSlaveID)
case rspPDU.FuncCode != reqPDU.FuncCode: // Check correct function code returned (exception)
return responseError(rspPDU)
case rspPDU.Data == nil || len(rspPDU.Data) == 0: // check Empty response
return fmt.Errorf("modbus: response data is empty")
}
return nil
}