Skip to content

Commit 85beba8

Browse files
authored
feat(cli): add --parity and --baud-rate filters to discover command (#332)
Adds optional `--parity` and `--baud-rate` CLI parameters to the discover command that limit the serial parameters searched during device discovery, significantly reducing scan time when device configuration is known. ## Features - `--parity <spec>`: Filter by parity modes (none, even, odd) - `--baud-rate <spec>`: Filter by baud rates with range support - Both support comma-separated lists and multiple flag invocations - Automatic deduplication and standard ordering - Comprehensive help and error messages ## Implementation **Generic Parser Abstraction:** - Created `parseSpec<T>()` utility eliminating code duplication - Extracted shared `parseInteger()` helper - Reduced parser code by ~40% while maintaining 100% coverage **Generic Merging Utility:** - Created `mergeSpecs<T>()` for consolidating multiple CLI flags - Simplified discover.ts from 42 lines to 5 declarative calls - Reusable pattern for future filters **Architecture:** - Filters override strategy defaults and driver config - Type-safe generic abstractions - Clean separation of concerns ## Testing - 169 new tests (62 in discover command, 107 in parsers/utilities) - 100% branch coverage for all new utilities - 96.41% branch coverage for CLI package (exceeds 95% threshold) - 16 E2E BATS tests for filter combinations and error handling ## Documentation - Comprehensive discover-command.md updates - Example 9 demonstrating performance benefits - Inline code documentation with usage examples ## Performance Example ```bash # Without filters: 2964 combinations (1-247 IDs × 12 rates × 1 parity) ya-modbus discover /dev/ttyUSB0 # With filters: 5 combinations (5 IDs × 1 rate × 1 parity) ya-modbus discover /dev/ttyUSB0 --id 1-5 --parity none --baud-rate 9600 # 99.8% reduction in search space ```
1 parent f0257a2 commit 85beba8

20 files changed

Lines changed: 1824 additions & 106 deletions

packages/cli/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,88 @@ ya-modbus discover \
196196
--delay 50
197197
```
198198

199+
**Filter specific slave IDs:**
200+
201+
```bash
202+
# Test only specific slave IDs (reduces scan time significantly)
203+
ya-modbus discover \
204+
--port /dev/ttyUSB0 \
205+
--id 1,2,3
206+
207+
# Test a range of IDs
208+
ya-modbus discover \
209+
--port /dev/ttyUSB0 \
210+
--id 1-10
211+
212+
# Combine ranges and individual IDs
213+
ya-modbus discover \
214+
--port /dev/ttyUSB0 \
215+
--id "1-5,10,15-20"
216+
217+
# Multiple --id specifications are merged
218+
ya-modbus discover \
219+
--port /dev/ttyUSB0 \
220+
--id 1,2 \
221+
--id 5-10
222+
```
223+
224+
**Filter specific parities:**
225+
226+
```bash
227+
# Test only specific parity modes
228+
ya-modbus discover \
229+
--port /dev/ttyUSB0 \
230+
--parity none
231+
232+
# Test multiple parities
233+
ya-modbus discover \
234+
--port /dev/ttyUSB0 \
235+
--parity "none,even"
236+
237+
# Multiple --parity specifications are merged
238+
ya-modbus discover \
239+
--port /dev/ttyUSB0 \
240+
--parity none \
241+
--parity even
242+
```
243+
244+
**Filter specific baud rates:**
245+
246+
```bash
247+
# Test only specific baud rates
248+
ya-modbus discover \
249+
--port /dev/ttyUSB0 \
250+
--baud-rate 9600
251+
252+
# Test multiple baud rates
253+
ya-modbus discover \
254+
--port /dev/ttyUSB0 \
255+
--baud-rate "9600,19200"
256+
257+
# Test a range of baud rates (includes intermediate standard rates)
258+
ya-modbus discover \
259+
--port /dev/ttyUSB0 \
260+
--baud-rate "9600-38400" # Tests 9600, 14400, 19200, 38400
261+
262+
# Multiple --baud-rate specifications are merged
263+
ya-modbus discover \
264+
--port /dev/ttyUSB0 \
265+
--baud-rate 9600 \
266+
--baud-rate "19200,38400"
267+
```
268+
269+
**Combine filters for fastest discovery:**
270+
271+
```bash
272+
# When you know the device configuration, combine filters dramatically
273+
# reduces scan time. Example: ~30 seconds vs 25 minutes
274+
ya-modbus discover \
275+
--port /dev/ttyUSB0 \
276+
--id 1-5 \
277+
--parity none \
278+
--baud-rate 9600
279+
```
280+
199281
**Discovery Options:**
200282

201283
- `--strategy <type>` - Discovery strategy: `quick` (default) or `thorough`
@@ -205,6 +287,9 @@ ya-modbus discover \
205287
- `--timeout <ms>` - Response timeout in milliseconds (default: 1000)
206288
- `--delay <ms>` - Delay between attempts in milliseconds (default: 100)
207289
- `--max-devices <count>` - Maximum number of devices to find (default: 1, use 0 for unlimited)
290+
- `--id <spec>` - Limit search to specific slave IDs (e.g., "1,2,3-5"). Can be specified multiple times
291+
- `--parity <spec>` - Limit search to specific parities (e.g., "none,even"). Can be specified multiple times
292+
- `--baud-rate <spec>` - Limit search to specific baud rates (e.g., "9600,19200" or "9600-38400"). Can be specified multiple times
208293
- `--verbose` - Show detailed progress with current parameters being tested
209294
- `--silent` - Suppress all output except final result (useful for scripts)
210295
- `--format <type>` - Output format: `table` (default) or `json`

packages/cli/docs/discover-command.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ This will:
6565
- `--id 1,3-5,10` - Search IDs 1, 3, 4, 5, and 10
6666
- `--id 1-3 --id 5-7` - Search IDs 1, 2, 3, 5, 6, and 7
6767

68+
- `--parity <spec>` - Limit search to specific parity modes (can be specified multiple times)
69+
- Format: Comma-separated parity values (e.g., `none,even,odd`)
70+
- Valid values: `none`, `even`, `odd`
71+
- Multiple `--parity` flags are merged and deduplicated
72+
- Reduces scan time when you know the device's parity setting
73+
- Examples:
74+
- `--parity none` - Test only no parity
75+
- `--parity none,even` - Test no parity and even parity
76+
- `--parity none --parity even` - Same as above (flags are merged)
77+
- `--parity "none,even,odd"` - Test all parity modes (quotes optional)
78+
79+
- `--baud-rate <spec>` - Limit search to specific baud rates (can be specified multiple times)
80+
- Format: Single rates, comma-separated rates, or ranges (e.g., `9600`, `9600,19200`, or `9600-38400`)
81+
- Supported rates: `2400`, `4800`, `9600`, `14400`, `19200`, `38400`, `57600`, `115200`
82+
- Multiple `--baud-rate` flags are merged and deduplicated
83+
- Range syntax expands to standard baud rates between min and max
84+
- Significantly reduces scan time when you know the device's baud rate
85+
- Examples:
86+
- `--baud-rate 9600` - Test only 9600 baud
87+
- `--baud-rate 9600,19200` - Test 9600 and 19200 baud
88+
- `--baud-rate 9600-19200` - Test 9600, 14400, and 19200 baud (expands to standard rates)
89+
- `--baud-rate 9600 --baud-rate 19200` - Same as `--baud-rate 9600,19200`
90+
- `--baud-rate "2400-9600,57600"` - Test low and high baud rates
91+
6892
- `--max-devices <count>` - Maximum devices to find (default: `1`, use `0` for unlimited)
6993
- Stops scanning after finding specified number of devices
7094
- Use `0` to find all devices on the bus
@@ -430,6 +454,37 @@ ya-modbus discover --port /dev/ttyUSB0 --id 1,2 --id 3-5
430454

431455
**Performance benefit:** Searching only 5 IDs instead of all 247 reduces combinations by ~98%, completing in seconds instead of minutes.
432456

457+
### Example 9: Filter by Parity and Baud Rate
458+
459+
When you know the device configuration, combine filters for maximum speed:
460+
461+
```bash
462+
# Search only even parity devices at 9600 baud
463+
ya-modbus discover --port /dev/ttyUSB0 --parity even --baud-rate 9600
464+
465+
# Test multiple parities at a specific baud rate
466+
ya-modbus discover --port /dev/ttyUSB0 --parity none,even --baud-rate 9600
467+
468+
# Test a range of baud rates with specific parity
469+
ya-modbus discover --port /dev/ttyUSB0 --baud-rate 9600-38400 --parity none
470+
471+
# Combine all three filters for fastest possible scan
472+
ya-modbus discover \
473+
--port /dev/ttyUSB0 \
474+
--id 1-5 \
475+
--parity none \
476+
--baud-rate 9600
477+
478+
# Search multiple configurations
479+
ya-modbus discover \
480+
--port /dev/ttyUSB0 \
481+
--id 1,2,3 \
482+
--parity none --parity even \
483+
--baud-rate 9600,19200
484+
```
485+
486+
**Performance benefit:** With all three filters, scan time can be reduced from ~25 minutes (testing all combinations) to ~30 seconds (testing only specified combinations). Example: `--id 1-5 --parity none --baud-rate 9600` tests only 5 combinations instead of 1,482.
487+
433488
## Advanced Usage
434489

435490
### Combining with Other Commands

0 commit comments

Comments
 (0)