Skip to content

Commit

Permalink
feat: have terminal ask for baudRate
Browse files Browse the repository at this point in the history
  • Loading branch information
reconbot committed Feb 6, 2022
1 parent e8be807 commit 1694c4d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions UPGRADE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 27 additions & 10 deletions packages/terminal/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <port> -b <baud rate> [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>', 'Path of the serial port')
.option('-b, --baud <baudrate>', 'Baud rate default: 9600', makeNumber, 9600)
.option('--databits <databits>', 'Data bits default: 8', makeNumber, 8)
.option('--parity <parity>', 'Parity default: none', 'none')
.option('--stopbits <bits>', 'Stop bits default: 1', makeNumber, 1)
.option('-b, --baud <baudrate>', 'Baud rate', makeNumber)
.option('--databits <databits>', 'Data bits', makeNumber, 8)
.option('--parity <parity>', 'Parity', 'none')
.option('--stopbits <bits>', 'Stop bits', makeNumber, 1)
.option('--no-echo', "Don't print characters as you type them.")
.option('--flow-ctl <mode>', 'Enable flow control {XONOFF | RTSCTS}.')
.parse(process.argv)
Expand Down Expand Up @@ -56,21 +56,37 @@ 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,
}).run()
return answer as string
}

const createPort = (path: string) => {
const askForBaudRate = async () => {
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<AutoDetectTypes> = {
path,
binding,
baudRate: args.baud,
baudRate,
dataBits: args.databits,
parity: args.parity,
stopBits: args.stopbits,
Expand Down Expand Up @@ -121,7 +137,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 => {
Expand Down

0 comments on commit 1694c4d

Please sign in to comment.