-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
83 lines (78 loc) · 1.29 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
import delay from "./lib/delay";
import bootloader from './lib/bootloader';
import portWrapper from './lib/port';
import SerialPort from "serialport";
const syncFrame = [
0xc0,
0x00,
0x08,
0x24,
0x00,
0x00,
0x00,
0x00,
0x00,
0x07,
0x07,
0x12,
0x20,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0xc0
];
const createSend = port => async data => {
return new Promise((resolve, reject) => {
port.flush(() => {
port.write([...data], 'chunk', () => port.drain(resolve))
})
});
}
const receive = async port => {
return new Promise((resolve, reject) => {
port.once('readable', () => resolve(port.read()));
port.once('error', reject);
})
}
const port = new SerialPort("COM12", {
baudRate: 115200
});
port.on("open", async () => {
await bootloader(port);
const send = createSend(port);
let receivedInformation = null;
await send(syncFrame);
await send(syncFrame);
receivedInformation = await receive(port);
console.log(receivedInformation);
port.close()
});