-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translate.js
65 lines (52 loc) · 1.46 KB
/
Translate.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
const SerialPort = require("serialport");
const Readline = require("@serialport/parser-readline");
// const serialPortName = "/dev/tty-usbserial1"
const serialPortName = "COM17";
const port = new SerialPort(serialPortName, function(err) {
if (err) {
return console.log("Error: ", err.message);
}
});
// // Read data that is available but keep the stream in "paused mode"
// port.on("readable", function() {
// 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 Readline());
const parser = port.pipe(new Readline({ delimiter: "\r\n" }));
parser.on("data", console.log);
function sendCommand(command) {
port.write(command, function(err) {
if (err) {
return console.log("Error on write: ", err.message);
}
console.log(`command: '${command}' was sent to ${serialPortName}`);
});
}
async function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
async function sendCommands() {
let data = {
delay: 10,
y: 200,
arr: [1,2,3,4]
}
let x = 0;
for (let i = 0; i < 100; i++) {
data.delay = x;
const jsonStr = JSON.stringify(data);
console.log(`sending ${i}: ${jsonStr}`);
//sendCommand('j');
sendCommand(jsonStr);
await sleep(1000);
x += 10;
}
}
sendCommands();