feat(emulator): implement TCP transport#252
Merged
Merged
Conversation
Implement TcpTransport class extending BaseTransport: - Uses modbus-serial ServerTCP for protocol handling - Service vector bridge pattern for request/response handling - Support for all Modbus operations (read/write registers and coils) - Comprehensive unit tests with mocked ServerTCP - Follows RTU transport architecture and patterns Includes complete test coverage for lifecycle, service vector callbacks, configuration, and error handling.
Add TCP transport support to ModbusEmulator: - Import and instantiate TcpTransport when transport is 'tcp' - Validate required host and port configuration - Add comprehensive integration tests verifying: - Read/write operations via TCP service vector - Multiple device routing by unit ID - Error handling for non-existent devices - Configuration validation Integration tests mirror RTU transport test structure.
Update README.md with TCP transport: - Add TCP transport to Quick Start section with examples - Include TCP configuration examples in CLI usage - Add basic-tcp.yaml example - Update feature list to include TCP transport - Remove obsolete test for unsupported TCP transport Documentation shows how to use TCP transport for network-based device emulation, following same pattern as RTU examples.
@ya-modbus/cli
@ya-modbus/device-profiler
@ya-modbus/driver-ex9em
@ya-modbus/driver-loader
@ya-modbus/driver-or-we-516
@ya-modbus/driver-sdk
@ya-modbus/driver-types
@ya-modbus/driver-xymd1
@ya-modbus/emulator
@ya-modbus/mqtt-bridge
@ya-modbus/transport
commit: |
This was referenced Feb 7, 2026
This was referenced Feb 7, 2026
groupsky
added a commit
that referenced
this pull request
Feb 7, 2026
…266) Extracts ~200 lines of duplicated buffer building and parsing logic from TCP and RTU transports into reusable pure functions. This addresses the DRY violation identified in the code review of #252. ## Changes - Created `modbus-protocol-helpers.ts` with 6 pure functions for protocol handling - Added comprehensive test suite with 20 tests (98% coverage) - Refactored TCP transport handler methods to use helpers (264 → 197 lines) - Refactored RTU transport handler methods to use helpers (276 → 209 lines) - Moved helpers to utils folder for better organization ## Benefits - Eliminates code duplication between transports (~200 lines) - Protocol logic is now independently testable - Easier to add new transport types (USB, Bluetooth, etc.) - Clear separation: protocol handling vs transport lifecycle - All 246 existing tests pass, verifying behavior unchanged Addresses code quality improvement identified in review of #252.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements TCP transport for the Modbus emulator, enabling testing of Modbus TCP devices without physical hardware. Follows the same architecture patterns as the RTU transport implementation.
Changes
Core Implementation
Integration
Testing
Documentation
Test Coverage
All tests pass (226 tests total in emulator package).
Implementation Details
Service Vector Bridge Pattern:
Configuration:
host(string),port(number)unitID: 255(listen to all slave IDs)Error Handling:
Architecture Consistency
Follows identical patterns to RTU transport:
Related Issues
Closes #244
Follow-up Issues
The implementation is production-ready but has identified areas for improvement:
These will be addressed in follow-up issues created after this PR is merged.