Skip to content

feat(device-profiler): add slave ID range validation (1-247) #374

Description

@groupsky

Description

The CLI currently accepts any slave ID value, but Modbus spec requires slave IDs to be in range 1-247.

Current Behavior

ya-modbus-profile --port /dev/ttyUSB0 --slave-id 0   # Accepted but invalid
ya-modbus-profile --port /dev/ttyUSB0 --slave-id 300 # Accepted but invalid

Expected Behavior

Invalid slave IDs should be rejected with a clear error message:

error: Invalid slave ID: 0. Must be between 1 and 247 (Modbus specification)

Required Changes

Update packages/device-profiler/src/program.ts:

.requiredOption('--slave-id <id>', 'Modbus slave ID (1-247)', parseNumber)
.action(async (options: CliOptions) => {
  try {
    // Add validation
    if (options.slaveId < 1 || options.slaveId > 247) {
      throw new Error(`Invalid slave ID: ${options.slaveId}. Must be between 1 and 247`)
    }
    // ... rest of code

Add test in packages/device-profiler/src/program.test.ts:

it('should validate slave ID range', async () => {
  await expect(
    program.parseAsync(['node', 'ya-modbus-profile', '--port', '/dev/ttyUSB0', '--slave-id', '0'])
  ).rejects.toThrow()
  
  await expect(
    program.parseAsync(['node', 'ya-modbus-profile', '--port', '/dev/ttyUSB0', '--slave-id', '248'])
  ).rejects.toThrow()
})

Reference

  • Modbus spec defines valid slave IDs as 1-247
  • 0 is reserved for broadcast (not supported by ya-modbus)
  • 248-255 are reserved

Related

Identified in aspected review of #<PR_NUMBER>

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestvalidationInput validation and configuration checks

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions