Skip to content

Commit 2f1e405

Browse files
committed
feat: have terminal ask for baudRate
1 parent e8be807 commit 2f1e405

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

UPGRADE_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Upgading from 9x to 10x
1313
- `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`.
1414
- `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.
1515
- `SerialPort` is now delivering a few more parsers
16+
- `@serialport/terminal` no longer has a default baudRate
1617

1718
TODO
1819
- stop bits are incorrect

packages/terminal/lib/index.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import { program } from 'commander'
55
import { SerialPortStream, OpenOptions } from '@serialport/stream'
66
import { OutputTranslator } from './output-translator'
77
import { autoDetect, AutoDetectTypes } from '@serialport/bindings-cpp'
8-
const { version } = require('../package.json')
98

9+
const { version } = require('../package.json')
1010
const binding = autoDetect()
1111

1212
const makeNumber = (input: string) => Number(input)
1313

1414
program
1515
.version(version)
16-
.usage('[options]')
16+
.usage('--list OR -p <port> -b <baud rate> [options...]')
1717
.description('A basic terminal interface for communicating over a serial port. Pressing ctrl+c exits.')
1818
.option('-l --list', 'List available ports then exit')
1919
.option('-p, --path <path>', 'Path of the serial port')
20-
.option('-b, --baud <baudrate>', 'Baud rate default: 9600', makeNumber, 9600)
21-
.option('--databits <databits>', 'Data bits default: 8', makeNumber, 8)
22-
.option('--parity <parity>', 'Parity default: none', 'none')
23-
.option('--stopbits <bits>', 'Stop bits default: 1', makeNumber, 1)
20+
.option('-b, --baud <baudrate>', 'Baud rate', makeNumber)
21+
.option('--databits <databits>', 'Data bits', makeNumber, 8)
22+
.option('--parity <parity>', 'Parity', 'none')
23+
.option('--stopbits <bits>', 'Stop bits', makeNumber, 1)
2424
.option('--no-echo', "Don't print characters as you type them.")
2525
.option('--flow-ctl <mode>', 'Enable flow control {XONOFF | RTSCTS}.')
2626
.parse(process.argv)
@@ -56,21 +56,38 @@ const askForPort = async () => {
5656
name: 'serial-port-selection',
5757
message: 'Select a serial port to open',
5858
choices: ports.map((port, i) => ({
59-
value: `[${i + 1}]\t${port.path}\t${port.pnpId || ''}\t${port.manufacturer || ''}`,
59+
message: `[${i + 1}] ${port.path}${port.pnpId ? ` - ${port.pnpId}` : ''}${port.manufacturer ? ` - ${port.manufacturer}` : ''}`,
6060
name: port.path,
6161
})),
6262
required: true,
6363
}).run()
6464
return answer as string
6565
}
6666

67-
const createPort = (path: string) => {
67+
const askForBaudRate = async () => {
68+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
69+
const baud = await new (Enquirer as any).NumberPrompt({
70+
name: 'baudRate-selection',
71+
message: 'Enter a baud rate',
72+
required: true,
73+
float: false,
74+
validate(input: number) {
75+
if (Number(input) <= 0) {
76+
return 'BaudRate must be a number greater than 0'
77+
}
78+
return true
79+
},
80+
}).run()
81+
return baud
82+
}
83+
84+
const createPort = ({ path, baudRate }: { path: string; baudRate: number }) => {
6885
console.log(`Opening serial port: ${path} echo: ${args.echo}`)
6986

7087
const openOptions: OpenOptions<AutoDetectTypes> = {
7188
path,
7289
binding,
73-
baudRate: args.baud,
90+
baudRate,
7491
dataBits: args.databits,
7592
parity: args.parity,
7693
stopBits: args.stopbits,
@@ -121,7 +138,8 @@ const run = async () => {
121138
return
122139
}
123140
const path = args.path || (await askForPort())
124-
await createPort(path)
141+
const baudRate = Number(args.baud || (await askForBaudRate()))
142+
await createPort({ path, baudRate })
125143
}
126144

127145
run().catch(error => {

0 commit comments

Comments
 (0)