-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (68 loc) · 1.67 KB
/
index.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
const _ = require('lodash');
const SerialPort = require('serialport');
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('ascii');
const SERIAL_PORT_SETTINGS = {
baudRate: 115200
};
const getPortList = (callback) => {
console.log('getPortList');
SerialPort.list((err, ports) => {
if (!err) {
let nPorts = [];
if (ports.length) {
console.log('ports', ports);
ports.map((port) => {
nPorts.push({
key: port.comName,
value: port.comName,
text: port.comName
});
});
}
callback(null, nPorts);
} else {
callback();
}
});
};
getPortList((err, ports) => {
if (!err) {
if (ports && ports.length > 0) {
// try connecting to each port
let index = 0;
tryConnect(index, ports);
}
}
});
const tryConnect = (index, ports, callback) => {
const port = ports[index];
if (port && port.value) {
console.log('try connecting to port: ' + port.value);
let serialport = new SerialPort(port.value, SERIAL_PORT_SETTINGS);
serialport.write('V\r');
serialport.on('data', (data) => {
//console.log('onData:', data);
let textChunk = decoder.write(data);
// check if textChunk starts with V1
if (textChunk.substring(0, 2) === 'V1') {
// disconnect
serialport.close();
// log port connected to
console.log('verified connection to port: ' + port.value);
}
});
// wait for data to be received
_.delay(() => {
// try next port
tryConnect(index + 1, ports, callback);
}, 2500);
// receive error
serialport.on('error', (error) => {
//console.log('onError: ', error);
});
} else {
console.log('ENDING CHECK');
process.exit();
}
};