This repository has been archived by the owner on May 26, 2021. It is now read-only.
forked from things-go/go-modbus
-
Notifications
You must be signed in to change notification settings - Fork 7
/
register.go
404 lines (372 loc) · 12 KB
/
register.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package modbus
// 本文件提供了寄存器的底层封装,并且是线程安全的,丰富的api满足基本需求
import (
"bytes"
"encoding/binary"
"sync"
)
// NodeRegister 节点寄存器
type NodeRegister struct {
rw sync.RWMutex // 读写锁
slaveID byte
coilsAddrStart, coilsQuantity uint16
coils []uint8
discreteAddrStart, discreteQuantity uint16
discrete []uint8
inputAddrStart uint16
input []uint16
holdingAddrStart uint16
holding []uint16
}
// NewNodeRegister 创建一个modbus子节点寄存器列表
func NewNodeRegister(slaveID byte,
coilsAddrStart, coilsQuantity,
discreteAddrStart, discreteQuantity,
inputAddrStart, inputQuantity,
holdingAddrStart, holdingQuantity uint16) *NodeRegister {
coilsBytes := (int(coilsQuantity) + 7) / 8
discreteBytes := (int(discreteQuantity) + 7) / 8
b := make([]byte, coilsBytes+discreteBytes)
w := make([]uint16, int(inputQuantity)+int(holdingQuantity))
return &NodeRegister{
slaveID: slaveID,
coilsAddrStart: coilsAddrStart,
coilsQuantity: coilsQuantity,
coils: b[:coilsBytes],
discreteAddrStart: discreteAddrStart,
discreteQuantity: discreteQuantity,
discrete: b[coilsBytes:],
inputAddrStart: inputAddrStart,
input: w[:inputQuantity],
holdingAddrStart: holdingAddrStart,
holding: w[inputQuantity:],
}
}
// SlaveID 获取从站地址
func (sf *NodeRegister) SlaveID() byte {
sf.rw.RLock()
id := sf.slaveID
sf.rw.RUnlock()
return id
}
// SetSlaveID 更改从站地址
func (sf *NodeRegister) SetSlaveID(id byte) *NodeRegister {
sf.rw.Lock()
sf.slaveID = id
sf.rw.Unlock()
return sf
}
// CoilsAddrParam 读coil起始地址与数量
func (sf *NodeRegister) CoilsAddrParam() (start, quantity uint16) {
return sf.coilsAddrStart, sf.coilsQuantity
}
// DiscreteParam 读discrete起始地址与数量
func (sf *NodeRegister) DiscreteParam() (start, quantity uint16) {
return sf.discreteAddrStart, sf.discreteQuantity
}
// InputAddrParam 读input起始地址与数量
func (sf *NodeRegister) InputAddrParam() (start, quantity uint16) {
return sf.inputAddrStart, uint16(len(sf.input))
}
// HoldingAddrParam 读holding起始地址与数量
func (sf *NodeRegister) HoldingAddrParam() (start, quantity uint16) {
return sf.holdingAddrStart, uint16(len(sf.holding))
}
// getBits 读取切片的位的值, nBits <= 8, nBits + start <= len(buf)*8
func getBits(buf []byte, start, nBits uint16) uint8 {
byteOffset := start / 8 // 计算字节偏移量
preBits := start - byteOffset*8 // 有多少个位需要设置
mask := (uint16(1) << nBits) - 1 // 准备一个掩码来设置新的位
word := uint16(buf[byteOffset]) // 复制到临时存储
if preBits+nBits > 8 {
word |= uint16(buf[byteOffset+1]) << 8
}
word >>= preBits // 抛弃不用的位
word &= mask
return uint8(word)
}
// setBits 设置切片的位的值, nBits <= 8, nBits + start <= len(buf)*8
func setBits(buf []byte, start, nBits uint16, value byte) {
byteOffset := start / 8 // 计算字节偏移量
preBits := start - byteOffset*8 // 有多少个位需要设置
newValue := uint16(value) << preBits // 移到要设置的位的位置
mask := uint16((1 << nBits) - 1) // 准备一个掩码来设置新的位
mask <<= preBits
newValue &= mask
word := uint16(buf[byteOffset]) // 复制到临时存储
if (preBits + nBits) > 8 {
word |= uint16(buf[byteOffset+1]) << 8
}
word = (word & (^mask)) | newValue // 要写的位置清零
buf[byteOffset] = uint8(word & 0xFF) // 写回到存储中
if (preBits + nBits) > 8 {
buf[byteOffset+1] = uint8(word >> 8)
}
}
// WriteCoils 写线圈
func (sf *NodeRegister) WriteCoils(address, quality uint16, valBuf []byte) error {
sf.rw.Lock()
if len(valBuf)*8 >= int(quality) && (address >= sf.coilsAddrStart) &&
((address + quality) <= (sf.coilsAddrStart + sf.coilsQuantity)) {
start := address - sf.coilsAddrStart
nCoils := int16(quality)
for idx := 0; nCoils > 0; idx++ {
num := nCoils
if nCoils > 8 {
num = 8
}
setBits(sf.coils, start, uint16(num), valBuf[idx])
start += 8
nCoils -= 8
}
sf.rw.Unlock()
return nil
}
sf.rw.Unlock()
return &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// WriteSingleCoil 写单个线圈
func (sf *NodeRegister) WriteSingleCoil(address uint16, val bool) error {
newVal := byte(0)
if val {
newVal = 1
}
return sf.WriteCoils(address, 1, []byte{newVal})
}
// ReadCoils 读线圈,返回值
func (sf *NodeRegister) ReadCoils(address, quality uint16) ([]byte, error) {
sf.rw.RLock()
if (address >= sf.coilsAddrStart) &&
((address + quality) <= (sf.coilsAddrStart + sf.coilsQuantity)) {
start := address - sf.coilsAddrStart
nCoils := int16(quality)
result := make([]byte, 0, (quality+7)/8)
for ; nCoils > 0; nCoils -= 8 {
num := nCoils
if nCoils > 8 {
num = 8
}
result = append(result, getBits(sf.coils, start, uint16(num)))
start += 8
}
sf.rw.RUnlock()
return result, nil
}
sf.rw.RUnlock()
return nil, &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// ReadSingleCoil 读单个线圈
func (sf *NodeRegister) ReadSingleCoil(address uint16) (bool, error) {
v, err := sf.ReadCoils(address, 1)
if err != nil {
return false, err
}
return v[0] > 0, nil
}
// WriteDiscretes 写离散量
func (sf *NodeRegister) WriteDiscretes(address, quality uint16, valBuf []byte) error {
sf.rw.Lock()
if len(valBuf)*8 >= int(quality) && (address >= sf.discreteAddrStart) &&
((address + quality) <= (sf.discreteAddrStart + sf.discreteQuantity)) {
start := address - sf.discreteAddrStart
nCoils := int16(quality)
for idx := 0; nCoils > 0; idx++ {
num := nCoils
if nCoils > 8 {
num = 8
}
setBits(sf.discrete, start, uint16(num), valBuf[idx])
start += 8
nCoils -= 8
}
sf.rw.Unlock()
return nil
}
sf.rw.Unlock()
return &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// WriteSingleDiscrete 写单个离散量
func (sf *NodeRegister) WriteSingleDiscrete(address uint16, val bool) error {
newVal := byte(0)
if val {
newVal = 1
}
return sf.WriteDiscretes(address, 1, []byte{newVal})
}
// ReadDiscretes 读离散量
func (sf *NodeRegister) ReadDiscretes(address, quality uint16) ([]byte, error) {
sf.rw.RLock()
if (address >= sf.discreteAddrStart) &&
((address + quality) <= (sf.discreteAddrStart + sf.discreteQuantity)) {
start := address - sf.discreteAddrStart
nCoils := int16(quality)
result := make([]byte, 0, (quality+7)/8)
for ; nCoils > 0; nCoils -= 8 {
num := nCoils
if nCoils > 8 {
num = 8
}
result = append(result, getBits(sf.discrete, start, uint16(num)))
start += 8
}
sf.rw.RUnlock()
return result, nil
}
sf.rw.RUnlock()
return nil, &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// ReadSingleDiscrete 读单个离散量
func (sf *NodeRegister) ReadSingleDiscrete(address uint16) (bool, error) {
v, err := sf.ReadDiscretes(address, 1)
if err != nil {
return false, err
}
return v[0] > 0, nil
}
// WriteHoldingsBytes 写保持寄存器
func (sf *NodeRegister) WriteHoldingsBytes(address, quality uint16, valBuf []byte) error {
sf.rw.Lock()
if len(valBuf) == int(quality*2) &&
(address >= sf.holdingAddrStart) &&
((address + quality) <= (sf.holdingAddrStart + uint16(len(sf.holding)))) {
start := address - sf.holdingAddrStart
end := start + quality
buf := bytes.NewBuffer(valBuf)
err := binary.Read(buf, binary.BigEndian, sf.holding[start:end])
sf.rw.Unlock()
if err != nil {
return &ExceptionError{ExceptionCodeServerDeviceFailure}
}
return nil
}
sf.rw.Unlock()
return &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// WriteHoldings 写保持寄存器
func (sf *NodeRegister) WriteHoldings(address uint16, valBuf []uint16) error {
quality := uint16(len(valBuf))
sf.rw.Lock()
if (address >= sf.holdingAddrStart) &&
((address + quality) <= (sf.holdingAddrStart + uint16(len(sf.holding)))) {
start := address - sf.holdingAddrStart
end := start + quality
copy(sf.holding[start:end], valBuf)
sf.rw.Unlock()
return nil
}
sf.rw.Unlock()
return &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// ReadHoldingsBytes 读保持寄存器,仅返回寄存器值
func (sf *NodeRegister) ReadHoldingsBytes(address, quality uint16) ([]byte, error) {
sf.rw.RLock()
if (address >= sf.holdingAddrStart) &&
((address + quality) <= (sf.holdingAddrStart + uint16(len(sf.holding)))) {
start := address - sf.holdingAddrStart
end := start + quality
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, sf.holding[start:end])
sf.rw.RUnlock()
if err != nil {
return nil, &ExceptionError{ExceptionCodeServerDeviceFailure}
}
return buf.Bytes(), nil
}
sf.rw.RUnlock()
return nil, &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// ReadHoldings 读保持寄存器,仅返回寄存器值
func (sf *NodeRegister) ReadHoldings(address, quality uint16) ([]uint16, error) {
sf.rw.RLock()
if (address >= sf.holdingAddrStart) &&
((address + quality) <= (sf.holdingAddrStart + uint16(len(sf.holding)))) {
start := address - sf.holdingAddrStart
end := start + quality
result := make([]uint16, quality)
copy(result, sf.holding[start:end])
sf.rw.RUnlock()
return result, nil
}
sf.rw.RUnlock()
return nil, &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// WriteInputsBytes 写输入寄存器
func (sf *NodeRegister) WriteInputsBytes(address, quality uint16, regBuf []byte) error {
sf.rw.Lock()
if len(regBuf) == int(quality*2) &&
(address >= sf.inputAddrStart) &&
((address + quality) <= (sf.inputAddrStart + uint16(len(sf.input)))) {
start := address - sf.inputAddrStart
end := start + quality
buf := bytes.NewBuffer(regBuf)
err := binary.Read(buf, binary.BigEndian, sf.input[start:end])
sf.rw.Unlock()
if err != nil {
return &ExceptionError{ExceptionCodeServerDeviceFailure}
}
return nil
}
sf.rw.Unlock()
return &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// WriteInputs 写输入寄存器
func (sf *NodeRegister) WriteInputs(address uint16, valBuf []uint16) error {
quality := uint16(len(valBuf))
sf.rw.Lock()
if (address >= sf.inputAddrStart) &&
((address + quality) <= (sf.inputAddrStart + uint16(len(sf.input)))) {
start := address - sf.inputAddrStart
end := start + quality
copy(sf.input[start:end], valBuf)
sf.rw.Unlock()
return nil
}
sf.rw.Unlock()
return &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// ReadInputsBytes 读输入寄存器
func (sf *NodeRegister) ReadInputsBytes(address, quality uint16) ([]byte, error) {
sf.rw.RLock()
if (address >= sf.inputAddrStart) &&
((address + quality) <= (sf.inputAddrStart + uint16(len(sf.input)))) {
start := address - sf.inputAddrStart
end := start + quality
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, sf.input[start:end])
sf.rw.RUnlock()
if err != nil {
return nil, &ExceptionError{ExceptionCodeServerDeviceFailure}
}
return buf.Bytes(), nil
}
sf.rw.RUnlock()
return nil, &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// ReadInputs 读输入寄存器
func (sf *NodeRegister) ReadInputs(address, quality uint16) ([]uint16, error) {
sf.rw.RLock()
if (address >= sf.inputAddrStart) &&
((address + quality) <= (sf.inputAddrStart + uint16(len(sf.input)))) {
start := address - sf.inputAddrStart
end := start + quality
result := make([]uint16, quality)
copy(result, sf.input[start:end])
sf.rw.RUnlock()
return result, nil
}
sf.rw.RUnlock()
return nil, &ExceptionError{ExceptionCodeIllegalDataAddress}
}
// MaskWriteHolding 屏蔽写保持寄存器 (val & andMask) | (orMask & ^andMask)
func (sf *NodeRegister) MaskWriteHolding(address, andMask, orMask uint16) error {
sf.rw.Lock()
if (address >= sf.holdingAddrStart) &&
((address + 1) <= (sf.holdingAddrStart + uint16(len(sf.holding)))) {
sf.holding[address] &= andMask
sf.holding[address] |= orMask & ^andMask
sf.rw.Unlock()
return nil
}
sf.rw.Unlock()
return &ExceptionError{ExceptionCodeIllegalDataAddress}
}