-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (69 loc) · 1.95 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
const { SerialPort, Readline } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const Table = require("cli-table3");
const port = new SerialPort({
path: '/dev/ttyUSB0',
baudRate: 9600,
autoOpen: false,
})
let tables = {};
port.open(function (err) {
if (err) {
return console.log('Error opening port: ', err.message)
}
// Because there's no callback to write, write errors will be emitted on the port:
port.write('main screen turn on')
})
// The open event is always emitted
port.on('open', function () {
// open logic
console.log("Opened");
})
// Read data that is available but keep the stream in "paused mode"
port.on('readable', function () {
port.read();
// console.log('Data:', port.read())
})
// Switches the port into "flowing mode"
port.on('data', function (data) {
// console.log('Data:', data)
})
// Pipe the data into another stream (like a parser or standard out)
const lineStream = port.pipe(new ReadlineParser({
delimiter: "\r\n"
})
)
function createTable(header, data) {
let table = new Table({
head: header,
style: {
head: ["blue"]
}
});
table.push(data);
return table.toString();
}
function draw() {
console.clear();
// console.log(tables);
let keys = Object.keys(tables);
keys.forEach((element) => {
if(tables[element].hasOwnProperty("data"))
console.log(createTable(tables[element].header, tables[element].data));
})
}
lineStream.on("data", (data) => {
data = data.replace(/[^\w\s,.#]/gi, "").replace("\n","");
let incommingArray = data.split(",");
// console.log(data);
if (!tables.hasOwnProperty(incommingArray[0]) && incommingArray[0].length >= 1) {
tables[incommingArray[0]] = {};
tables[incommingArray[0]].header = incommingArray;
// tables[incommingArray[0]].header[0] = "ID";
}
else if (tables.hasOwnProperty(incommingArray[0])) {
tables[incommingArray[0]].data = incommingArray;
draw();
}
// console.log(data)
});