Skip to content

Commit 9f543b6

Browse files
robertmassaiolireconbot
authored andcommitted
Adding in port selection to serialport-term. (#1448)
* Adding in port selection to serialport-term. This makes the terminal command much more useable when you need to open it multiple times in a row; especially when the names of the serialports are rapidly changing. * Fix the command line arguments. * Fixing eslint mistakes. * Make all of the negative errors positive in serialport-terminal.
1 parent 672c198 commit 9f543b6

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

bin/terminal.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
const SerialPort = require('../lib/');
55
const version = require('../package.json').version;
66
const args = require('commander');
7+
const List = require('prompt-list');
78

89
function makeNumber(input) {
910
return Number(input);
1011
}
1112

1213
args
1314
.version(version)
14-
.usage('-p <port> [options]')
15+
.usage('[options]')
1516
.description('A basic terminal interface for communicating over a serial port. Pressing ctrl+c exits.')
1617
.option('-l --list', 'List available ports then exit')
17-
// TODO make the port not a flag as it's always required
1818
.option('-p, --port <port>', 'Path or Name of serial port')
1919
.option('-b, --baud <baudrate>', 'Baud rate default: 9600', makeNumber, 9600)
2020
.option('--databits <databits>', 'Data bits default: 8', makeNumber, 8)
@@ -36,21 +36,53 @@ function listPorts() {
3636
});
3737
};
3838

39-
function createPort() {
40-
if (!args.port) {
41-
args.outputHelp();
42-
args.missingArgument('port');
43-
process.exit(-1);
39+
function setupPort() {
40+
if (args.port) {
41+
createPort(args.port);
4442
}
4543

44+
SerialPort.list((err, ports) => {
45+
if (err) {
46+
console.error('Error listing ports, and missing port argument.', err);
47+
args.outputHelp();
48+
args.missingArgument('port');
49+
process.exit(4);
50+
} else {
51+
if (ports.length > 0) {
52+
const portSelection = new List({
53+
name: 'serial-port-selection',
54+
message: 'Select a serial port to open',
55+
choices: ports.map((port, i) => `[${i + 1}]\t${port.comName}\t${port.pnpId || ''}\t${port.manufacturer || ''}`)
56+
});
57+
58+
portSelection.run()
59+
.then(answer => {
60+
const choice = answer.split('\t')[1];
61+
console.log(`Opening serial port: ${choice}`);
62+
createPort(choice);
63+
})
64+
.catch(error => {
65+
console.log(`Could not select a port: ${error}`);
66+
process.exit(2);
67+
});
68+
} else {
69+
args.outputHelp();
70+
args.missingArgument('port');
71+
process.exit(3);
72+
}
73+
}
74+
});
75+
}
76+
77+
function createPort(selectedPort) {
4678
const openOptions = {
4779
baudRate: args.baud,
4880
dataBits: args.databits,
4981
parity: args.parity,
5082
stopBits: args.stopbits
5183
};
5284

53-
const port = new SerialPort(args.port, openOptions);
85+
const port = new SerialPort(selectedPort, openOptions);
5486

5587
process.stdin.resume();
5688
process.stdin.setRawMode(true);
@@ -82,5 +114,5 @@ function createPort() {
82114
if (args.list) {
83115
listPorts();
84116
} else {
85-
createPort();
117+
setupPort();
86118
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"nan": "^2.6.2",
6363
"prebuild-install": "^2.4.1",
6464
"promirepl": "^1.0.1",
65+
"prompt-list": "^3.1.2",
6566
"safe-buffer": "^5.0.1"
6667
},
6768
"devDependencies": {
@@ -99,6 +100,7 @@
99100
"lint": "eslint lib test bin examples",
100101
"rebuild-all": "npm rebuild && node-gyp rebuild",
101102
"repl": "node bin/repl.js",
103+
"terminal": "node bin/terminal.js",
102104
"stress": "mocha --no-timeouts test/arduinoTest/stress.js",
103105
"test": "istanbul cover ./node_modules/mocha/bin/_mocha",
104106
"test:watch": "mocha -w",

0 commit comments

Comments
 (0)