From 7e1856bcc46299f8ff38b07e4695224d3a4e9030 Mon Sep 17 00:00:00 2001 From: Robert Massaioli Date: Sun, 4 Feb 2018 01:17:12 +1100 Subject: [PATCH] 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. --- bin/terminal.js | 50 ++++++++++++++++++++++++++++++++++++++++--------- package.json | 2 ++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/bin/terminal.js b/bin/terminal.js index dd95e953a..dbf49c71c 100755 --- a/bin/terminal.js +++ b/bin/terminal.js @@ -4,6 +4,7 @@ const SerialPort = require('../lib/'); const version = require('../package.json').version; const args = require('commander'); +const List = require('prompt-list'); function makeNumber(input) { return Number(input); @@ -11,10 +12,9 @@ function makeNumber(input) { args .version(version) - .usage('-p [options]') + .usage('[options]') .description('A basic terminal interface for communicating over a serial port. Pressing ctrl+c exits.') .option('-l --list', 'List available ports then exit') - // TODO make the port not a flag as it's always required .option('-p, --port ', 'Path or Name of serial port') .option('-b, --baud ', 'Baud rate default: 9600', makeNumber, 9600) .option('--databits ', 'Data bits default: 8', makeNumber, 8) @@ -36,13 +36,45 @@ function listPorts() { }); }; -function createPort() { - if (!args.port) { - args.outputHelp(); - args.missingArgument('port'); - process.exit(-1); +function setupPort() { + if (args.port) { + createPort(args.port); } + SerialPort.list((err, ports) => { + if (err) { + console.error('Error listing ports, and missing port argument.', err); + args.outputHelp(); + args.missingArgument('port'); + process.exit(4); + } else { + if (ports.length > 0) { + const portSelection = new List({ + name: 'serial-port-selection', + message: 'Select a serial port to open', + choices: ports.map((port, i) => `[${i + 1}]\t${port.comName}\t${port.pnpId || ''}\t${port.manufacturer || ''}`) + }); + + portSelection.run() + .then(answer => { + const choice = answer.split('\t')[1]; + console.log(`Opening serial port: ${choice}`); + createPort(choice); + }) + .catch(error => { + console.log(`Could not select a port: ${error}`); + process.exit(2); + }); + } else { + args.outputHelp(); + args.missingArgument('port'); + process.exit(3); + } + } + }); +} + +function createPort(selectedPort) { const openOptions = { baudRate: args.baud, dataBits: args.databits, @@ -50,7 +82,7 @@ function createPort() { stopBits: args.stopbits }; - const port = new SerialPort(args.port, openOptions); + const port = new SerialPort(selectedPort, openOptions); process.stdin.resume(); process.stdin.setRawMode(true); @@ -82,5 +114,5 @@ function createPort() { if (args.list) { listPorts(); } else { - createPort(); + setupPort(); } diff --git a/package.json b/package.json index cb7a89abe..2187f5208 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "nan": "^2.6.2", "prebuild-install": "^2.4.1", "promirepl": "^1.0.1", + "prompt-list": "^3.1.2", "safe-buffer": "^5.0.1" }, "devDependencies": { @@ -99,6 +100,7 @@ "lint": "eslint lib test bin examples", "rebuild-all": "npm rebuild && node-gyp rebuild", "repl": "node bin/repl.js", + "terminal": "node bin/terminal.js", "stress": "mocha --no-timeouts test/arduinoTest/stress.js", "test": "istanbul cover ./node_modules/mocha/bin/_mocha", "test:watch": "mocha -w",