-
Notifications
You must be signed in to change notification settings - Fork 1
/
writemultiplecoilsrequest.go
256 lines (237 loc) · 8.47 KB
/
writemultiplecoilsrequest.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
package packet
import (
"encoding/binary"
"fmt"
"math/rand"
)
// WriteMultipleCoilsRequestTCP is TCP Request for Write Multiple Coils (FC=15)
//
// Example packet: 0x01 0x38 0x00 0x00 0x00 0x08 0x11 0x0F 0x04 0x10 0x00 0x03 0x01 0x05
// 0x01 0x38 - transaction id (0,1)
// 0x00 0x00 - protocol id (2,3)
// 0x00 0x08 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5)
// 0x11 - unit id (6)
// 0x0F - function code (7)
// 0x04 0x10 - start address (8,9)
// 0x00 0x03 - count of coils to write (10,11)
// 0x01 - coils byte count (12)
// 0x05 - coils data (13, ...)
type WriteMultipleCoilsRequestTCP struct {
MBAPHeader
WriteMultipleCoilsRequest
}
// WriteMultipleCoilsRequestRTU is RTU Request for Write Multiple Coils (FC=15)
//
// Example packet: 0x11 0x0F 0x04 0x10 0x00 0x03 0x01 0x05 0x8e 0x1f
// 0x11 - unit id (0)
// 0x0F - function code (1)
// 0x04 0x10 - start address (2,3)
// 0x00 0x03 - count of coils to write (4,5)
// 0x01 - coils byte count (6)
// 0x05 - coils data (7, ...)
// 0x8e 0x1f - CRC16 (n-2,n-1)
type WriteMultipleCoilsRequestRTU struct {
WriteMultipleCoilsRequest
}
// WriteMultipleCoilsRequest is Request for Write Multiple Coils (FC=15)
type WriteMultipleCoilsRequest struct {
UnitID uint8
StartAddress uint16
CoilCount uint16
Data []byte
}
// NewWriteMultipleCoilsRequestTCP creates new instance of Write Multiple Coils TCP request
func NewWriteMultipleCoilsRequestTCP(unitID uint8, startAddress uint16, coils []bool) (*WriteMultipleCoilsRequestTCP, error) {
coilsCount := len(coils)
if coilsCount == 0 || coilsCount > 1968 {
// 1968 coils is due that coils byte len size field is 1 byte so max 246*8=1968 coils can be sent
return nil, fmt.Errorf("coils count is out of range (1-1968): %v", coilsCount)
}
coilsBytes := CoilsToBytes(coils)
return &WriteMultipleCoilsRequestTCP{
MBAPHeader: MBAPHeader{
TransactionID: uint16(1 + rand.Intn(65534)),
ProtocolID: 0,
},
WriteMultipleCoilsRequest: WriteMultipleCoilsRequest{
UnitID: unitID,
// function code is added by Bytes()
StartAddress: startAddress,
CoilCount: uint16(coilsCount),
Data: coilsBytes,
},
}, nil
}
// Bytes returns WriteMultipleCoilsRequestTCP packet as bytes form
func (r WriteMultipleCoilsRequestTCP) Bytes() []byte {
length := r.len()
result := make([]byte, tcpMBAPHeaderLen+length)
r.MBAPHeader.bytes(result[0:6], length)
r.WriteMultipleCoilsRequest.bytes(result[6 : 6+length])
return result
}
// ExpectedResponseLength returns length of bytes that valid response to this request would be
func (r WriteMultipleCoilsRequestTCP) ExpectedResponseLength() int {
// response = 6 header len + 1 UnitID + 1 functionCode + 2 start addr + 2 count of coils
return 6 + 6
}
// ParseWriteMultipleCoilsRequestTCP parses given bytes into WriteMultipleCoilsRequestTCP
func ParseWriteMultipleCoilsRequestTCP(data []byte) (*WriteMultipleCoilsRequestTCP, error) {
header, err := ParseMBAPHeader(data)
if err != nil {
return nil, err
}
unitID := data[6]
if data[7] != FunctionWriteMultipleCoils {
tmpErr := NewErrorParseTCP(ErrIllegalFunction, "received function code in packet is not 0x0f")
tmpErr.Packet.TransactionID = header.TransactionID
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionWriteMultipleCoils
return nil, tmpErr
}
coilCount := binary.BigEndian.Uint16(data[10:12])
if !(coilCount >= 1 && coilCount <= 1968) { // 0x0001 to 0x07B0
tmpErr := NewErrorParseTCP(ErrIllegalDataValue, "invalid coils count. valid range 1..1968")
tmpErr.Packet.TransactionID = header.TransactionID
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionWriteMultipleCoils
return nil, tmpErr
}
coilsBytesCount := data[12]
if len(data) < 13+int(coilsBytesCount) {
tmpErr := NewErrorParseTCP(ErrIllegalDataValue, "received data coils bytes length does not match write data length")
tmpErr.Packet.TransactionID = header.TransactionID
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionWriteMultipleCoils
return nil, tmpErr
}
var coilsData []byte
if coilsBytesCount > 0 {
coilsData = make([]byte, coilsBytesCount)
copy(coilsData, data[13:13+coilsBytesCount])
}
return &WriteMultipleCoilsRequestTCP{
MBAPHeader: header,
WriteMultipleCoilsRequest: WriteMultipleCoilsRequest{
UnitID: unitID,
// function code = data[7]
StartAddress: binary.BigEndian.Uint16(data[8:10]),
CoilCount: coilCount,
Data: coilsData,
},
}, nil
}
// NewWriteMultipleCoilsRequestRTU creates new instance of Write Multiple Coils RTU request
func NewWriteMultipleCoilsRequestRTU(unitID uint8, startAddress uint16, coils []bool) (*WriteMultipleCoilsRequestRTU, error) {
coilsCount := len(coils)
if coilsCount == 0 || coilsCount > 1968 {
// 1968 coils is due that coils byte len size field is 1 byte so max 246*8=1968 coils can be sent
return nil, fmt.Errorf("coils count is out of range (1-1968): %v", coilsCount)
}
coilsBytes := CoilsToBytes(coils)
return &WriteMultipleCoilsRequestRTU{
WriteMultipleCoilsRequest: WriteMultipleCoilsRequest{
UnitID: unitID,
// function code is added by Bytes()
StartAddress: startAddress,
CoilCount: uint16(coilsCount),
Data: coilsBytes,
},
}, nil
}
// Bytes returns WriteMultipleCoilsRequestRTU packet as bytes form
func (r WriteMultipleCoilsRequestRTU) Bytes() []byte {
pduLen := r.len() + 2
result := make([]byte, pduLen)
bytes := r.WriteMultipleCoilsRequest.bytes(result)
crc := CRC16(bytes[:pduLen-2])
result[pduLen-2] = uint8(crc)
result[pduLen-1] = uint8(crc >> 8)
return result
}
// ExpectedResponseLength returns length of bytes that valid response to this request would be
func (r WriteMultipleCoilsRequestRTU) ExpectedResponseLength() int {
// response = 1 UnitID + 1 functionCode + 2 start addr + 2 count of coils + 2 CRC
return 6 + 2
}
// ParseWriteMultipleCoilsRequestRTU parses given bytes into WriteMultipleCoilsRequestRTU
func ParseWriteMultipleCoilsRequestRTU(data []byte) (*WriteMultipleCoilsRequestRTU, error) {
dLen := len(data)
if dLen < 7 {
return nil, NewErrorParseRTU(ErrServerFailure, "received data length too short to be valid packet")
}
unitID := data[0]
if data[1] != FunctionWriteMultipleCoils {
tmpErr := NewErrorParseRTU(ErrIllegalFunction, "received function code in packet is not 0x0f")
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionWriteMultipleCoils
return nil, tmpErr
}
coilCount := binary.BigEndian.Uint16(data[4:6])
if !(coilCount >= 1 && coilCount <= 1968) { // 0x0001 to 0x07B0
tmpErr := NewErrorParseRTU(ErrIllegalDataValue, "invalid coils count. valid range 1..1968")
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionWriteMultipleCoils
return nil, tmpErr
}
coilsBytesCount := data[6]
expectedLen := 7 + int(coilsBytesCount)
if dLen != expectedLen && dLen != expectedLen+2 { // without crc and with crc
tmpErr := NewErrorParseRTU(ErrIllegalDataValue, "received data coils bytes length does not match write data length")
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionWriteMultipleCoils
return nil, tmpErr
}
var coilsData []byte
if coilsBytesCount > 0 {
coilsData = make([]byte, coilsBytesCount)
copy(coilsData, data[7:7+coilsBytesCount])
}
return &WriteMultipleCoilsRequestRTU{
WriteMultipleCoilsRequest: WriteMultipleCoilsRequest{
UnitID: unitID,
// function code = data[1]
StartAddress: binary.BigEndian.Uint16(data[2:4]),
CoilCount: coilCount,
Data: coilsData,
},
}, nil
}
// FunctionCode returns function code of this request
func (r WriteMultipleCoilsRequest) FunctionCode() uint8 {
return FunctionWriteMultipleCoils
}
func (r WriteMultipleCoilsRequest) len() uint16 {
return 7 + uint16(len(r.Data))
}
// Bytes returns WriteMultipleCoilsRequest packet as bytes form
func (r WriteMultipleCoilsRequest) Bytes() []byte {
return r.bytes(make([]byte, r.len()))
}
func (r WriteMultipleCoilsRequest) bytes(bytes []byte) []byte {
bytes[0] = r.UnitID
bytes[1] = FunctionWriteMultipleCoils
binary.BigEndian.PutUint16(bytes[2:4], r.StartAddress)
binary.BigEndian.PutUint16(bytes[4:6], r.CoilCount)
bytes[6] = uint8(len(r.Data))
copy(bytes[7:], r.Data)
return bytes
}
// CoilsToBytes converts slice of coil states (as bool values) to byte slice.
func CoilsToBytes(coils []bool) []byte {
cLen := len(coils)
cnt := cLen / 8
if cLen%8 != 0 {
cnt++
}
result := make([]byte, cnt)
for i := 0; i < cLen; i++ {
bit := i % 8
nthByte := i / 8
if coils[i] {
v := result[nthByte] | (1 << bit)
result[nthByte] = v
}
}
return result
}