-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
206 lines (177 loc) · 6.65 KB
/
main.js
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
'use strict';
/**
* 适合烧录的文件后缀名称:
* .hex 文本格式烧写flash
* .bin 二进制格式烧写flash
* .eep 文本格式烧写eeprom
* .eepb 二进制格式烧写eeprom
*/
const { SerialPort } = require("serialport");
const fs = require('fs');
const baudRate = 250000;/*caution: BAUD 250k*/
const comName = "com3";
// console.log(process.argv);
const hexFilePath = process.argv[2];
if (!fs.existsSync(hexFilePath)) {
console.error("file no exists: ", hexFilePath);
process.exit();
}
const fmt = hexFilePath.substr(hexFilePath.lastIndexOf("."), hexFilePath.length).toLowerCase();
const fileBuf = fs.readFileSync(hexFilePath);
// console.log(fileBuf);
let fileBufInd = 0;
const binBuf = Buffer.alloc(514);
let isBinFileSizeSended = false;
let rowCount = getRowCount(fileBuf);
// console.log(rowCount);
let rs = rowByRow(fileBuf);
const com = new SerialPort({ path: comName, baudRate });
com.on("error", (err) => console.error("Serial Error: ", err));
com.on("pause", () => console.log("com pause!"));
com.on("end", () => console.log("com end!"));
com.on("close", () => console.log("com closed!"));
com.on("resume", () => console.log("com resume"));
com.addListener("drain", () => {
console.log("drain");
});
const pps = { ".hex": "/p\r\n", ".bin": "/pb\r\n", ".eep": "/pe\r\n", ".eepb": "/pbe\r\n" };
if (!pps[fmt]) {
console.error("File format must be .hex|.bin|.eep !");
process.exit();
}
const isBin = fmt === ".bin" || fmt === ".eepb";
const cmdList = [
// "/h\r\n",
"/d\r\n",/*死活烧不了的时候可以用串口调试助手执行/d /ch大多数情况就可以烧了 */
// "/ch\r\n",
"/gi\r\n",
pps[fmt],
];
let cmdInd = 0;
let recvBuf = [];
let rowInd = 0;
/*com 打开时会触发uno重置 */
com.on("data", (data) => {
for (let i = 0; i < data.length; i++) {
const b = data[i];
if (b !== 0) {
recvBuf.push(String.fromCharCode(b));
} else {
const s = recvBuf.join('');
if (s === '') {
continue;
}
recvBuf = [];
if (cmdInd < cmdList.length) {
console.log(">>", s);
com.write(cmdList[cmdInd]);
console.log("<<", cmdList[cmdInd]);
cmdInd++;
} else {
if (cmdInd === cmdList.length) {
console.log(">>");
console.log(s);
cmdInd++;
} else if (!s.startsWith("ok")) {
com.write("/r\r\n");
console.log("");
console.log(">>");
console.log(s);
console.log("program failed!");
setTimeout(() => {
process.exit();
}, 20);
return;
}
if (!isBin) {
const r = rs.next();
if (!r.done) {
com.write(r.value);
rowInd++;
renderProgressBar(rowInd, rowCount, "programing: ");
} else {
com.write("/r\r\n");
cmdInd = cmdList.length
console.log("");
console.log("program finish!");
setTimeout(() => {
process.exit();
}, 20);
}
} else {
if (!isBinFileSizeSended) {
const size = Math.ceil(fileBuf.length / 512) * 512;
const bufSize = Buffer.from([size & 0xFF, (size >> 8) & 0xFF, (size >> 16) & 0xFF, (size >> 24) & 0xFF]);
com.write(bufSize);
isBinFileSizeSended = true;
} else {/*辣鸡SerialPort模块,在一次data回调里面如果多次调用write会卡好几秒,辣鸡。。。 */
if (fileBufInd < fileBuf.length) {
let checkSum = 0;
if (fileBufInd + 512 <= fileBuf.length) {
for (let i = 0; i < 512; i++) {
binBuf[i] = fileBuf[fileBufInd];
checkSum += fileBuf[fileBufInd];
fileBufInd++;
}
} else {
let remain = fileBuf.length - fileBufInd;
for (let i = 0; i < remain; i++) {
binBuf[i] = fileBuf[fileBufInd];
checkSum += fileBuf[fileBufInd];
fileBufInd++;
}
for (let i = remain; i < 512; i++) {
binBuf[i] = 0xFF;
checkSum += 0xFF;
}
}
binBuf[512] = checkSum & 0xFF;
binBuf[513] = (checkSum >> 8) & 0xFF;
com.write(binBuf);
renderProgressBar(fileBufInd, fileBuf.length, "programing: ");
} else {
com.write("/r\r\n");
cmdInd = cmdList.length
console.log("");
console.log("program finish!");
setTimeout(() => {
process.exit();
}, 20);
}
}
}
}
}
}
});
function getRowCount(buf) {
const charCode = '\n'.charCodeAt(0);
let row = 0;
for (let i = 0; i < buf.length; i++) {
if (buf[i] === charCode) row++;
}
return row;
}
function* rowByRow(buf) {
const charCode = '\n'.charCodeAt(0);
let row = [];
for (let i = 0; i < buf.length; i++) {
const b = buf[i];
row.push(b);
if (b === charCode) {
yield Buffer.from(row);
row = [];
}
}
}
function renderProgressBar(count, totalCount, title) {
process.stdout.clearLine();
process.stdout.moveCursor(-200, 0);
const percent = parseInt((count / totalCount) * 100);
const c = parseInt(percent / 2);
process.stdout.write(title);
process.stdout.write("▮".repeat(c));
process.stdout.write("▯".repeat(50 - c));
process.stdout.write(" " + percent.toString() + "% ");
process.stdout.write(count + "/" + totalCount);
}