-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
274 lines (264 loc) · 8.48 KB
/
client.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
package modbus
import (
"encoding/binary"
"fmt"
)
// Client modbus客户端功能接口
type Client interface {
//Bit 操作
// ReadCoils 读线圈
//地址:0~65535
//范围:1~2000
//功能码:1
ReadCoils(address, quantity uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error)
// ReadDiscreteInputs 读离散量输入
//地址:0~65535
//范围:1~2000
//功能码:2
ReadDiscreteInputs(address, quantity uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error)
// WriteSingleCoil 写单个线圈
//地址:0~65535
//值:0或1
//功能码:5
WriteSingleCoil(address uint16, value bool) (request ApplicationDataUnit, results ApplicationDataUnit, err error)
// WriteMultipleCoils 写多个线圈
//地址:0~65535
//范围:1~1968
//值:0或1
//功能码:15
WriteMultipleCoils(address, quantity uint16, value []bool) (request ApplicationDataUnit, results ApplicationDataUnit, err error)
//16-bit 操作
// ReadInputRegisters 读输入寄存器
//地址:0~65535
//范围:1~125
//功能码:4
ReadInputRegisters(address, quantity uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error)
// ReadHoldingRegisters 读保持寄存器
//地址:0~65535
//范围:1~125
//功能码:3
ReadHoldingRegisters(address, quantity uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error)
// WriteSingleRegister 写单个寄存器
//地址:0~65535
//值:0~65535
//功能码:6
WriteSingleRegister(address, value uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error)
// WriteMultipleRegisters 写多个寄存器
//地址:0~65535
//范围:1~123
//功能码:16
WriteMultipleRegisters(address, quantity uint16, value []byte) (request ApplicationDataUnit, results ApplicationDataUnit, err error)
// ReadWriteMultipleRegisters 读/写多个寄存器
//地址:0~65535
//读范围:1~125
//写范围:1~121
//功能码:23
ReadWriteMultipleRegisters(readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (request ApplicationDataUnit, results ApplicationDataUnit, err error)
Send(request ApplicationDataUnit) (results ApplicationDataUnit, err error)
}
// 客户端 结构体
type client struct {
packager Packager
transporter Transporter
}
func (c *client) ReadCoils(address, quantity uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error) {
if quantity < 1 || quantity > 2000 {
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v'", quantity, 1, 2000)
return
}
data := dataBlock(address, quantity)
pdu := protocolDataUnit{
functionCode: FuncCodeReadCoils,
data: data,
length: len(data),
}
request, err = c.packager.Encode(pdu)
results, err = c.Send(request)
return
}
func (c *client) ReadDiscreteInputs(address, quantity uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error) {
if quantity < 1 || quantity > 2000 {
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 2000)
return
}
data := dataBlock(address, quantity)
pdu := protocolDataUnit{
functionCode: FuncCodeReadDiscreteInputs,
data: data,
length: len(data),
}
request, err = c.packager.Encode(pdu)
results, err = c.Send(request)
return
}
func (c *client) WriteSingleCoil(address uint16, value bool) (request ApplicationDataUnit, results ApplicationDataUnit, err error) {
if address > 65535 {
err = fmt.Errorf("modbus: address '%v' must be between '%v' and '%v'", address, 0, 65535)
return
}
// The requested ON/OFF state can only be 0xFF00 and 0x0000
var coil uint16 = 0x0000
if value {
coil = 0xFF00
}
data := dataBlock(address, coil)
pdu := protocolDataUnit{
functionCode: FuncCodeWriteSingleCoil,
data: data,
length: len(data),
}
request, err = c.packager.Encode(pdu)
results, err = c.Send(request)
return
}
func (c *client) WriteMultipleCoils(address, quantity uint16, value []bool) (request ApplicationDataUnit, results ApplicationDataUnit, err error) {
if quantity < 1 || quantity > 1968 {
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v'", quantity, 1, 1968)
return
}
length := len(value)
if int(quantity) != length {
err = fmt.Errorf("modbus: quantity '%v' and value length '%v' are the same", quantity, length)
return
}
coils := toBit(value)
data := dataBlockSuffix(coils, address, quantity)
pdu := protocolDataUnit{
functionCode: FuncCodeWriteMultipleCoils,
data: data,
length: len(data),
}
request, err = c.packager.Encode(pdu)
results, err = c.Send(request)
return
}
func (c *client) ReadInputRegisters(address, quantity uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error) {
if quantity < 1 || quantity > 125 {
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 125)
return
}
data := dataBlock(address, quantity)
pdu := protocolDataUnit{
functionCode: FuncCodeReadInputRegisters,
data: data,
length: len(data),
}
request, err = c.packager.Encode(pdu)
results, err = c.Send(request)
return
}
func (c *client) ReadHoldingRegisters(address, quantity uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error) {
if quantity < 1 || quantity > 125 {
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 125)
return
}
data := dataBlock(address, quantity)
pdu := protocolDataUnit{
functionCode: FuncCodeReadHoldingRegisters,
data: data,
length: len(data),
}
request, err = c.packager.Encode(pdu)
results, err = c.Send(request)
return
}
func (c *client) WriteSingleRegister(address, value uint16) (request ApplicationDataUnit, results ApplicationDataUnit, err error) {
data := dataBlock(address, value)
pdu := protocolDataUnit{
functionCode: FuncCodeWriteSingleRegister,
data: data,
length: len(data),
}
request, err = c.packager.Encode(pdu)
results, err = c.Send(request)
return
}
func (c *client) WriteMultipleRegisters(address, quantity uint16, value []byte) (request ApplicationDataUnit, results ApplicationDataUnit, err error) {
if quantity < 1 || quantity > 123 {
err = fmt.Errorf("modbus: quantity '%v' must be between '%v' and '%v',", quantity, 1, 123)
return
}
data := dataBlockSuffix(value, address, quantity)
pdu := protocolDataUnit{
functionCode: FuncCodeWriteMultipleRegisters,
data: data,
length: len(data),
}
request, err = c.packager.Encode(pdu)
results, err = c.Send(request)
return
}
func (c *client) ReadWriteMultipleRegisters(readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (request ApplicationDataUnit, results ApplicationDataUnit, err error) {
if readQuantity < 1 || readQuantity > 125 {
err = fmt.Errorf("modbus: quantity to read '%v' must be between '%v' and '%v',", readQuantity, 1, 125)
return
}
if writeQuantity < 1 || writeQuantity > 121 {
err = fmt.Errorf("modbus: quantity to write '%v' must be between '%v' and '%v',", writeQuantity, 1, 121)
return
}
data := dataBlockSuffix(value, readAddress, readQuantity, writeAddress, writeQuantity)
pdu := protocolDataUnit{
functionCode: FuncCodeWriteMultipleRegisters,
data: data,
length: len(data),
}
request, err = c.packager.Encode(pdu)
results, err = c.Send(request)
return
}
var complementary = []byte{0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80}
func toBit(value []bool) []byte {
l := len(value)
length := l / 8
if l%8 > 0 {
length++
}
bi := make([]byte, length)
for i := 0; i < l; i++ {
if value[i] {
index := i / 8
b := bi[index]
s := i % 8
c := complementary[s]
bi[index] = b | c
}
}
return bi
}
func dataBlock(value ...uint16) []byte {
data := make([]byte, 2*len(value))
for i, v := range value {
binary.BigEndian.PutUint16(data[i*2:], v)
}
return data
}
func dataBlockSuffix(suffix []byte, value ...uint16) []byte {
length := 2 * len(value)
data := make([]byte, length+1+len(suffix))
for i, v := range value {
binary.BigEndian.PutUint16(data[i*2:], v)
}
data[length] = uint8(len(suffix))
copy(data[length+1:], suffix)
return data
}
func (c *client) Send(request ApplicationDataUnit) (results ApplicationDataUnit, err error) {
bys, err := c.transporter.Send(request)
if err != nil {
return
}
results, err = c.packager.Decode(bys)
if err != nil {
return
}
err = c.packager.Verify(request, results)
return
}
func NewClient(packager Packager, transporter Transporter) (c Client) {
c = &client{
packager: packager,
transporter: transporter,
}
return
}