refactor(modbus): switch to native Modbus TCP protocol
Switch from Modbus RTU over TCP to native Modbus TCP based on hardware testing. Uses standard MBAP header (no CRC16), port 502, and TCP-only tokio-modbus feature for simpler implementation. Updated: Cargo.toml, plan.md, research.md, tasks.md
This commit is contained in:
@@ -13,25 +13,25 @@
|
||||
**Purpose**: Initialize project dependencies and directory structure
|
||||
|
||||
- [x] **T001** [Setup] [TDD] Add Rust dependencies to Cargo.toml
|
||||
- Add: tokio-modbus = "0.17.0", sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite"] }, mockall = "0.13", async-trait = "0.1"
|
||||
- Add: tokio-modbus = { version = "0.17.0", default-features = false, features = ["tcp"] }, sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite"] }, mockall = "0.13", async-trait = "0.1"
|
||||
- **Test**: cargo check passes
|
||||
- **Complexity**: Low | **Uncertainty**: Low
|
||||
|
||||
- [ ] **T002** [P] [Setup] [TDD] Create module structure in src/
|
||||
- [x] **T002** [P] [Setup] [TDD] Create module structure in src/
|
||||
- Create: src/domain/, src/application/, src/infrastructure/, src/presentation/
|
||||
- **Test**: Module declarations compile without errors
|
||||
- **Complexity**: Low | **Uncertainty**: Low
|
||||
|
||||
- [ ] **T003** [P] [Setup] [TDD] Update settings.rs with Modbus configuration
|
||||
- Add ModbusSettings struct with host, port, slave_id, timeout_secs fields
|
||||
- Add RelaySettings struct with label_max_length field
|
||||
- Add ModbusSettings struct with `host`, `port`, `slave_id`, `timeout_secs` fields
|
||||
- Add RelaySettings struct with `label_max_length` field
|
||||
- Update Settings struct to include modbus and relay fields
|
||||
- **Test**: Settings loads from settings/base.yaml with test Modbus config
|
||||
- **Complexity**: Low | **Uncertainty**: Low
|
||||
|
||||
- [ ] **T004** [P] [Setup] [TDD] Create settings/base.yaml with Modbus defaults
|
||||
- Add modbus section: host: "192.168.1.100", port: 502, slave_id: 1, timeout_secs: 3
|
||||
- Add relay section: label_max_length: 50
|
||||
- Add modbus section: host: "192.168.0.200", port: 502, slave_id: 0, timeout_secs: 5
|
||||
- Add relay section: label_max_length: 8
|
||||
- **Test**: Settings::new() loads config without errors
|
||||
- **Complexity**: Low | **Uncertainty**: Low
|
||||
|
||||
@@ -184,9 +184,9 @@
|
||||
|
||||
- [ ] **T024** [US1] [TDD] Write tests for ModbusRelayController
|
||||
- **REQUIRES HARDWARE/MOCK**: Integration test with tokio_modbus::test utilities
|
||||
- Test: Connection succeeds with valid config
|
||||
- Test: Connection succeeds with valid config (Modbus TCP on port 502)
|
||||
- Test: read_state() returns correct coil value
|
||||
- Test: write_state() sends correct Modbus command
|
||||
- Test: write_state() sends correct Modbus TCP command (no CRC needed)
|
||||
- **File**: src/infrastructure/modbus/modbus_controller.rs
|
||||
- **Complexity**: High → DECOMPOSED below
|
||||
- **Uncertainty**: High
|
||||
@@ -198,6 +198,7 @@
|
||||
**Complexity**: High → Broken into 6 sub-tasks
|
||||
**Uncertainty**: High
|
||||
**Rationale**: Nested Result handling, Arc<Mutex> synchronization, timeout wrapping
|
||||
**Protocol**: Native Modbus TCP (MBAP header, no CRC16 validation)
|
||||
|
||||
- [ ] **T025a** [US1] [TDD] Implement ModbusRelayController connection setup
|
||||
- Struct: ModbusRelayController { ctx: Arc<Mutex<Context>>, timeout_duration: Duration }
|
||||
@@ -219,6 +220,7 @@
|
||||
{
|
||||
use tokio_modbus::prelude::*;
|
||||
|
||||
// Connect using native Modbus TCP protocol (port 502)
|
||||
let socket_addr = format!("{}:{}", host, port)
|
||||
.parse()
|
||||
.map_err(|e| ControllerError::ConnectionError(format!("Invalid address: {}", e)))?;
|
||||
@@ -244,6 +246,7 @@
|
||||
- Private method: read_coils_with_timeout(addr: u16, count: u16) → Result<Vec<bool>, ControllerError>
|
||||
- Wrap ctx.read_coils() with tokio::time::timeout()
|
||||
- Handle nested Result: timeout → io::Error → Modbus Exception
|
||||
- **Note**: Modbus TCP uses MBAP header (no CRC validation needed)
|
||||
- **File**: src/infrastructure/modbus/modbus_controller.rs
|
||||
- **Complexity**: Medium | **Uncertainty**: Medium
|
||||
|
||||
|
||||
Reference in New Issue
Block a user