-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (38 loc) · 1.08 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
'use strict';
var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var inquirer = require('inquirer');
var listofports = [];
SerialPort.list(function (e, ports) {
// prepare list for user interaction
let serialPortChoices = {};
serialPortChoices.type = 'list';
serialPortChoices.message = 'Choose serial port you would like to connect:'
serialPortChoices.name = "serialport";
let listOfdevices = [];
ports.forEach(element => {
listOfdevices.push(element.comName);
});
serialPortChoices.choices = listOfdevices
listofports.push(serialPortChoices);
// generate interaction
inquirer.prompt(listofports)
.then(answer => {
openPortAndOutputData(answer.serialport);
})
})
function openPortAndOutputData(serialport){
var port = new SerialPort(serialport, {
baudRate: 9600
}, function (err) {
if (err) {
return console.log('Error: ', err.message)
}
});
var parser = new Readline()
port.pipe(parser)
parser.on('data', function (data) {
console.log('data received: ' + data)
// data handling
})
};