-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream
executable file
·58 lines (48 loc) · 1.43 KB
/
stream
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
#!/usr/bin/env node
var program = require('commander');
var serialport = require("serialport");
var SerialPort = serialport.SerialPort; // localize object constructor
// set up CLI flags
// Later, maybe add: "Detect the path by running the detect command."
program
.version('0.0.1')
.option('-p, --path <path>', "Set a path to the Dylos's USB/serial interface, default /dev/ttyUSB0", '/dev/ttyUSB0')
.option('-b, --baudrate <rate>', "Set a baudrate for the serial interface, default 9600", 9600)
.option('-v, --verbose', 'Turn on verbose mode')
.option('-j, --json', 'Return JSON output')
.parse(process.argv);
var log = function(msg) {
if (program.verbose) {
console.log(msg);
}
}
var serialPort = new SerialPort(program.path, {
baudrate: program.baudrate,
parser: serialport.parsers.readline()
});
if (program.verbose) {
serialport.list(function (err, ports) {
ports.forEach(function(port) {
log(port.comName,port.manufacturer,port.pnpId);
});
});
}
serialPort.on("open", function () {
log('open');
serialPort.on('data', function(data) {
var output = ""+data;
if (program.json) {
output = output.split(',');
output = {
'output1': parseInt(output[0]),
'output2': parseInt(output[1])
}
console.log(JSON.stringify(output));
} else {
console.log(output);
}
});
serialPort.on("error", function(string) {
console.warn(string);
});
});