-
Notifications
You must be signed in to change notification settings - Fork 1
/
readinputregistersrequest.go
186 lines (169 loc) · 6.34 KB
/
readinputregistersrequest.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
package packet
import (
"encoding/binary"
"fmt"
"math/rand"
)
// ReadInputRegistersRequestTCP is TCP Request for Read Input Registers (FC=04)
//
// Example packet: 0x00 0x01 0x00 0x00 0x00 0x06 0x01 0x04 0x00 0x6B 0x00 0x01
// 0x00 0x01 - transaction id (0,1)
// 0x00 0x00 - protocol id (2,3)
// 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5)
// 0x01 - unit id (6)
// 0x04 - function code (7)
// 0x00 0x6B - start address (8,9)
// 0x00 0x01 - input registers quantity to return (10,11)
type ReadInputRegistersRequestTCP struct {
MBAPHeader
ReadInputRegistersRequest
}
// ReadInputRegistersRequestRTU is RTU Request for Read Input Registers (FC=04)
//
// Example packet: 0x01 0x04 0x00 0x6B 0x00 0x01 0x40 0x16
// 0x01 - unit id (0)
// 0x04 - function code (1)
// 0x00 0x6B - start address (2,3)
// 0x00 0x01 - input registers quantity to return (4,5)
// 0x40 0x16 - CRC16 (6,7)
type ReadInputRegistersRequestRTU struct {
ReadInputRegistersRequest
}
// ReadInputRegistersRequest is Request for Read Input Registers (FC=04)
type ReadInputRegistersRequest struct {
UnitID uint8
StartAddress uint16
Quantity uint16
}
// NewReadInputRegistersRequestTCP creates new instance of Read Input Registers TCP request
func NewReadInputRegistersRequestTCP(unitID uint8, startAddress uint16, quantity uint16) (*ReadInputRegistersRequestTCP, error) {
if quantity == 0 || quantity > MaxRegistersInReadResponse {
return nil, fmt.Errorf("quantity is out of range (1-125): %v", quantity)
}
return &ReadInputRegistersRequestTCP{
MBAPHeader: MBAPHeader{
TransactionID: uint16(1 + rand.Intn(65534)),
ProtocolID: 0,
},
ReadInputRegistersRequest: ReadInputRegistersRequest{
UnitID: unitID,
// function code is added by Bytes()
StartAddress: startAddress,
Quantity: quantity,
},
}, nil
}
// Bytes returns ReadInputRegistersRequestTCP packet as bytes form
func (r ReadInputRegistersRequestTCP) Bytes() []byte {
length := uint16(6)
result := make([]byte, tcpMBAPHeaderLen+length)
r.MBAPHeader.bytes(result[0:6], length)
r.ReadInputRegistersRequest.bytes(result[6 : 6+length])
return result
}
// ExpectedResponseLength returns length of bytes that valid response to this request would be
func (r ReadInputRegistersRequestTCP) ExpectedResponseLength() int {
// response = 6 header len + 1 unitid + 1 fc + 1 register byte count + N data len (2-256)
return 6 + 3 + 2*int(r.Quantity)
}
// ParseReadInputRegistersRequestTCP parses given bytes into ReadInputRegistersRequestTCP
func ParseReadInputRegistersRequestTCP(data []byte) (*ReadInputRegistersRequestTCP, error) {
header, err := ParseMBAPHeader(data)
if err != nil {
return nil, err
}
unitID := data[6]
if data[7] != FunctionReadInputRegisters {
tmpErr := NewErrorParseTCP(ErrIllegalFunction, "received function code in packet is not 0x04")
tmpErr.Packet.TransactionID = header.TransactionID
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionReadInputRegisters
return nil, tmpErr
}
quantity := binary.BigEndian.Uint16(data[10:12])
if !(quantity >= 1 && quantity <= 125) { // 0x0001 to 0x007D
tmpErr := NewErrorParseTCP(ErrIllegalDataValue, "invalid quantity. valid range 1..125")
tmpErr.Packet.TransactionID = header.TransactionID
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionReadInputRegisters
return nil, tmpErr
}
return &ReadInputRegistersRequestTCP{
MBAPHeader: header,
ReadInputRegistersRequest: ReadInputRegistersRequest{
UnitID: unitID,
// function code = data[7]
StartAddress: binary.BigEndian.Uint16(data[8:10]),
Quantity: quantity,
},
}, nil
}
// NewReadInputRegistersRequestRTU creates new instance of Read Input Registers RTU request
func NewReadInputRegistersRequestRTU(unitID uint8, startAddress uint16, quantity uint16) (*ReadInputRegistersRequestRTU, error) {
if quantity == 0 || quantity > MaxRegistersInReadResponse {
return nil, fmt.Errorf("quantity is out of range (1-125): %v", quantity)
}
return &ReadInputRegistersRequestRTU{
ReadInputRegistersRequest: ReadInputRegistersRequest{
UnitID: unitID,
// function code is added by Bytes()
StartAddress: startAddress,
Quantity: quantity,
},
}, nil
}
// Bytes returns ReadInputRegistersRequestRTU packet as bytes form
func (r ReadInputRegistersRequestRTU) Bytes() []byte {
result := make([]byte, 6+2)
bytes := r.ReadInputRegistersRequest.bytes(result)
crc := CRC16(bytes[:6])
result[6] = uint8(crc)
result[7] = uint8(crc >> 8)
return result
}
// ParseReadInputRegistersRequestRTU parses given bytes into ReadInputRegistersRequestRTU
func ParseReadInputRegistersRequestRTU(data []byte) (*ReadInputRegistersRequestRTU, error) {
dLen := len(data)
if dLen != 8 && dLen != 6 { // with or without CRC bytes
return nil, NewErrorParseRTU(ErrServerFailure, "invalid data length to be valid packet")
}
unitID := data[0]
if data[1] != FunctionReadInputRegisters {
tmpErr := NewErrorParseRTU(ErrIllegalFunction, "received function code in packet is not 0x04")
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionReadInputRegisters
return nil, tmpErr
}
quantity := binary.BigEndian.Uint16(data[4:6])
if !(quantity >= 1 && quantity <= 125) { // 0x0001 to 0x007D
tmpErr := NewErrorParseRTU(ErrIllegalDataValue, "invalid quantity. valid range 1..125")
tmpErr.Packet.UnitID = unitID
tmpErr.Packet.Function = FunctionReadInputRegisters
return nil, tmpErr
}
return &ReadInputRegistersRequestRTU{
ReadInputRegistersRequest: ReadInputRegistersRequest{
UnitID: unitID,
// function code = data[1]
StartAddress: binary.BigEndian.Uint16(data[2:4]),
Quantity: quantity,
},
}, nil
}
// ExpectedResponseLength returns length of bytes that valid response to this request would be
func (r ReadInputRegistersRequest) ExpectedResponseLength() int {
// response = 1 UnitID + 1 functionCode + 2 register byte count + N register data
return 4 + 2*int(r.Quantity)
}
// FunctionCode returns function code of this request
func (r ReadInputRegistersRequest) FunctionCode() uint8 {
return FunctionReadInputRegisters
}
// Bytes returns ReadInputRegistersRequest packet as bytes form
func (r ReadInputRegistersRequest) Bytes() []byte {
return r.bytes(make([]byte, 6))
}
func (r ReadInputRegistersRequest) bytes(bytes []byte) []byte {
putReadRequestBytes(bytes, r.UnitID, FunctionReadInputRegisters, r.StartAddress, r.Quantity)
return bytes
}