-
Notifications
You must be signed in to change notification settings - Fork 2
/
proto.go
234 lines (191 loc) · 5.85 KB
/
proto.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
package inverterlogger
import (
"bytes"
"encoding/binary"
"fmt"
)
type Frame struct {
PayloadLength uint16
ControlCode uint16
SerialNumber uint16
DeviceSN uint32
Payload []byte
}
func NewFrame(deviceSN uint32, payload []byte) *Frame {
return &Frame{
PayloadLength: uint16(len(payload)),
ControlCode: 0x4510,
SerialNumber: 0x0000,
DeviceSN: deviceSN,
Payload: payload,
}
}
func (f *Frame) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte(0xA5) // V5 start marker
binary.Write(&buf, binary.LittleEndian, f.PayloadLength)
binary.Write(&buf, binary.LittleEndian, f.ControlCode)
binary.Write(&buf, binary.BigEndian, f.SerialNumber)
binary.Write(&buf, binary.LittleEndian, f.DeviceSN)
buf.Write(f.Payload)
buf.WriteByte(calcCheckSum8(buf.Bytes()[1:])) // skip start marker for checksum calculation
buf.WriteByte(0x15) // V5 end marker
return buf.Bytes(), nil
}
func (f *Frame) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
b, err := buf.ReadByte()
if err != nil {
return fmt.Errorf("can't read start marker: %w", err)
} else if b != 0xA5 {
return fmt.Errorf("expected 0xA5 as start marker, got: %x", b)
}
err = binary.Read(buf, binary.LittleEndian, &f.PayloadLength)
if err != nil {
return fmt.Errorf("can't read payload length: %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &f.ControlCode)
if err != nil {
return fmt.Errorf("can't read control code: %w", err)
}
err = binary.Read(buf, binary.BigEndian, &f.SerialNumber)
if err != nil {
return fmt.Errorf("can't read serial number: %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &f.DeviceSN)
if err != nil {
return fmt.Errorf("can't read device SN: %w", err)
}
f.Payload = make([]byte, f.PayloadLength)
n, err := buf.Read(f.Payload)
if err != nil {
return fmt.Errorf("can't read payload: %w", err)
} else if n != int(f.PayloadLength) {
return fmt.Errorf("read only %d bytes of payload instead of %d", n, f.PayloadLength)
}
_, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("can't read checksum: %w", err)
}
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("can't read end marker: %w", err)
} else if b != 0x15 {
return fmt.Errorf("expected 0x15 as end marker, got: %x", b)
}
if buf.Len() != 0 {
return fmt.Errorf("%d bytes left in the buffer", buf.Len())
}
return nil
}
type RequestPayload struct {
FrameType uint8
SensorType uint16
DeliveryTime uint32
PowerOnTime uint32
OffsetTime uint32
DeviceAddress uint8
FunctionCode uint8
StartReg uint16
RegCount uint16
}
func NewRequestPayload(startReg, regCount uint16) *RequestPayload {
return &RequestPayload{
FrameType: 0x02,
SensorType: 0x0000,
DeliveryTime: 0x00000000,
PowerOnTime: 0x00000000,
OffsetTime: 0x00000000,
DeviceAddress: 0x01, // must be equal to 1
FunctionCode: 0x03, // 3 - read real time data
StartReg: startReg,
RegCount: regCount,
}
}
func (r *RequestPayload) marshalBusinessData() []byte {
var buf bytes.Buffer
buf.WriteByte(r.DeviceAddress)
buf.WriteByte(r.FunctionCode)
binary.Write(&buf, binary.BigEndian, r.StartReg)
binary.Write(&buf, binary.BigEndian, r.RegCount)
binary.Write(&buf, binary.LittleEndian, calcCRC16Modbus(buf.Bytes()))
return buf.Bytes()
}
func (r *RequestPayload) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
binary.Write(&buf, binary.LittleEndian, r.FrameType)
binary.Write(&buf, binary.LittleEndian, r.SensorType)
binary.Write(&buf, binary.LittleEndian, r.DeliveryTime)
binary.Write(&buf, binary.LittleEndian, r.PowerOnTime)
binary.Write(&buf, binary.LittleEndian, r.OffsetTime)
buf.Write(r.marshalBusinessData())
return buf.Bytes(), nil
}
type ResponsePayload struct {
FrameType uint8
StatusCode uint8
DeliveryTime uint32
PowerOnTime uint32
OffsetTime uint32
DeviceAddress uint8
FunctionCode uint8
ValueLength uint8
Value []byte
}
func (r *ResponsePayload) unmarshalBusinessPayload(data []byte) error {
buf := bytes.NewBuffer(data)
err := binary.Read(buf, binary.LittleEndian, &r.DeviceAddress)
if err != nil {
return fmt.Errorf("can't read slave address: %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &r.FunctionCode)
if err != nil {
return fmt.Errorf("can't read function code: %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &r.ValueLength)
if err != nil {
return fmt.Errorf("can't read value length: %w", err)
}
r.Value = make([]byte, r.ValueLength)
n, err := buf.Read(r.Value)
if err != nil {
return fmt.Errorf("can't read value: %w", err)
} else if n != int(r.ValueLength) {
return fmt.Errorf("read only %d bytes of value instead of %d", n, r.ValueLength)
}
var crc uint16
err = binary.Read(buf, binary.LittleEndian, &crc)
if err != nil {
return fmt.Errorf("can't read value length: %w", err)
}
// There are two zero bytes at the end of the payload -- I couldn't find what they denote.
buf.Next(2)
if buf.Len() != 0 {
return fmt.Errorf("%d bytes left in the buffer", buf.Len())
}
return nil
}
func (r *ResponsePayload) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
err := binary.Read(buf, binary.LittleEndian, &r.FrameType)
if err != nil {
return fmt.Errorf("can't read frame type: %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &r.StatusCode)
if err != nil {
return fmt.Errorf("can't read status code: %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &r.DeliveryTime)
if err != nil {
return fmt.Errorf("can't read delivery time: %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &r.PowerOnTime)
if err != nil {
return fmt.Errorf("can't read power on time: %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &r.OffsetTime)
if err != nil {
return fmt.Errorf("can't read offset time: %w", err)
}
return r.unmarshalBusinessPayload(buf.Bytes())
}