From d795f205f9384cacf6bfc73712d5ddc897da2f2e Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Sun, 6 Feb 2022 00:55:37 -0500 Subject: [PATCH] feat: have terminal ask for baudRate (#2423) --- UPGRADE_GUIDE.md | 1 + packages/terminal/lib/index.ts | 38 +++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/UPGRADE_GUIDE.md b/UPGRADE_GUIDE.md index 6870a8685..68b8c5f6b 100644 --- a/UPGRADE_GUIDE.md +++ b/UPGRADE_GUIDE.md @@ -13,6 +13,7 @@ Upgading from 9x to 10x - `SerialPort` class from the `serialport` package no longer has a static `Bindings` property as it provides the OS specific bindings from `@serialport/bindings-cpp` if you need control of the bindings use `SerialPortStream`. - `SerialPortStream` methods (and by extension `SerialPort` methods) no longer throw when called with invalid input, their callbacks or the error event will convey input errors. This is because the binding layer now handles input validation as it's different on each platform. - `SerialPort` is now delivering a few more parsers +- `@serialport/terminal` no longer has a default baudRate TODO - stop bits are incorrect diff --git a/packages/terminal/lib/index.ts b/packages/terminal/lib/index.ts index 7dba606db..37cfe9b34 100755 --- a/packages/terminal/lib/index.ts +++ b/packages/terminal/lib/index.ts @@ -5,22 +5,22 @@ import { program } from 'commander' import { SerialPortStream, OpenOptions } from '@serialport/stream' import { OutputTranslator } from './output-translator' import { autoDetect, AutoDetectTypes } from '@serialport/bindings-cpp' -const { version } = require('../package.json') +const { version } = require('../package.json') const binding = autoDetect() const makeNumber = (input: string) => Number(input) program .version(version) - .usage('[options]') + .usage('--list OR -p -b [options...]') .description('A basic terminal interface for communicating over a serial port. Pressing ctrl+c exits.') .option('-l --list', 'List available ports then exit') .option('-p, --path ', 'Path of the serial port') - .option('-b, --baud ', 'Baud rate default: 9600', makeNumber, 9600) - .option('--databits ', 'Data bits default: 8', makeNumber, 8) - .option('--parity ', 'Parity default: none', 'none') - .option('--stopbits ', 'Stop bits default: 1', makeNumber, 1) + .option('-b, --baud ', 'Baud rate', makeNumber) + .option('--databits ', 'Data bits', makeNumber, 8) + .option('--parity ', 'Parity', 'none') + .option('--stopbits ', 'Stop bits', makeNumber, 1) .option('--no-echo', "Don't print characters as you type them.") .option('--flow-ctl ', 'Enable flow control {XONOFF | RTSCTS}.') .parse(process.argv) @@ -56,7 +56,7 @@ const askForPort = async () => { name: 'serial-port-selection', message: 'Select a serial port to open', choices: ports.map((port, i) => ({ - value: `[${i + 1}]\t${port.path}\t${port.pnpId || ''}\t${port.manufacturer || ''}`, + message: `[${i + 1}] ${port.path}${port.pnpId ? ` - ${port.pnpId}` : ''}${port.manufacturer ? ` - ${port.manufacturer}` : ''}`, name: port.path, })), required: true, @@ -64,13 +64,30 @@ const askForPort = async () => { return answer as string } -const createPort = (path: string) => { +const askForBaudRate = async () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const baud = await new (Enquirer as any).NumberPrompt({ + name: 'baudRate-selection', + message: 'Enter a baud rate', + required: true, + float: false, + validate(input: number) { + if (Number(input) <= 0) { + return 'BaudRate must be a number greater than 0' + } + return true + }, + }).run() + return baud +} + +const createPort = ({ path, baudRate }: { path: string; baudRate: number }) => { console.log(`Opening serial port: ${path} echo: ${args.echo}`) const openOptions: OpenOptions = { path, binding, - baudRate: args.baud, + baudRate, dataBits: args.databits, parity: args.parity, stopBits: args.stopbits, @@ -121,7 +138,8 @@ const run = async () => { return } const path = args.path || (await askForPort()) - await createPort(path) + const baudRate = Number(args.baud || (await askForBaudRate())) + await createPort({ path, baudRate }) } run().catch(error => {