-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.go
190 lines (174 loc) · 3.62 KB
/
tools.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
package isp
import (
"fmt"
"time"
"errors"
"strconv"
)
func (t *ISP) checkSum(data []byte) byte {
result := byte(0)
for index := 0; index < len(data); index++ {
result ^= data[index]
}
return result
}
func (t *ISP) commandBytes(command Command) []byte {
return []byte{byte(command), 0xFF ^ byte(command)}
}
func (t *ISP) bcd2Int(bcd byte) uint8 {
return ((bcd / 0x10) * 10) + (bcd % 0x10)
}
func (t *ISP) int2BCD(num uint8) byte {
return ((num % 100) / 10 << 4) + num%10
}
/*
* @Description: 接收数据
* @receiver t
*/
func (t *ISP) receivePack(command Command, setLen int) ([]byte, error) {
timeout := time.After(5 * time.Second)
buff := make([]byte, 0)
ack := false
for {
select {
case <-timeout:
return nil, errors.New("receive timeout")
default:
temp := make([]byte, 100)
n, err := t.Port.Read(temp)
if err != nil {
return nil, err
}
if n <= 0 {
continue
}
if n >= 1 {
buff = append(buff, temp[:n]...)
for index := 0; index < len(buff); index++ {
// 接收到包开始
if buff[index] == 0x79 && index+1 < len(buff) {
ack = true
packLen := int(buff[index+1]) + index + 4
if setLen != 0 {
packLen = setLen + 2
}
if command == CommandGetVersion {
packLen = 2 + index + 3
}
// 包长度符合要求
if len(buff) >= packLen {
pack := buff[index:packLen]
if command == CommandReadMemory {
if pack[0] == 0x79 {
return pack, nil
}
}
if pack[0] == 0x79 && pack[len(pack)-1] == 0x79 {
return pack, nil
}
}
}
if buff[index] == 0x1F && ack == false {
buff = buff[index:]
return nil, NACKError
}
}
}
}
}
}
/*
* @Description: 等待确认帧
* @return error
*/
func (t *ISP) ack(data []byte, retry bool, after time.Duration) error {
timeout := time.After(after)
flag := 0
for {
select {
case <-timeout:
return fmt.Errorf("0x%02X timeout", data)
default:
if flag == 0 {
if _, err := t.Port.Write(data); err != nil {
return err
}
}
if retry == false {
flag = 1
}
if err := t.waitACK(300 * time.Millisecond); err != nil {
if err.Error() == "timeout" {
continue
}
return err
}
return nil
}
}
}
func (t *ISP) waitACK(after time.Duration) error {
timeout := time.After(after)
for {
select {
case <-timeout:
return fmt.Errorf("timeout")
default:
buff := make([]byte, 8)
n, err := t.Port.Read(buff)
if err != nil {
return err
}
if n > 0 {
for index := 0; index < n; index++ {
if buff[index] == 0x79 {
return nil
}
if buff[index] == 0x1F {
return NACKError
}
}
}
}
}
}
/*
* @Description: 执行指令
* @param command
* @return []byte
* @return error
*/
func (t *ISP) command(command Command) ([]byte, error) {
timeout := time.After(5 * time.Second)
for {
select {
case <-timeout:
return nil, fmt.Errorf("command 0x%02X timeout", command)
default:
data := []byte{byte(command), byte(0xFF ^ command)}
if _, err := t.Port.Write(data); err != nil {
return nil, err
}
pack, err := t.receivePack(command, 0)
if err != nil {
time.Sleep(100 * time.Millisecond)
continue
}
return pack, nil
}
}
}
func (t *ISP) hexCharToBytes(hexStr string) ([]byte, error) {
if len(hexStr)%2 != 0 {
return nil, fmt.Errorf("hex string length must be even")
}
bytes := make([]byte, len(hexStr)/2)
for i := 0; i < len(hexStr); i += 2 {
val, err := strconv.ParseUint(hexStr[i:i+2], 16, 8)
if err != nil {
return nil, err
}
bytes[i/2] = byte(val)
}
return bytes, nil
}