Skip to content

feat(emulator): implement TCP transport#252

Merged
groupsky merged 3 commits into
mainfrom
feature/tcp-transport
Feb 7, 2026
Merged

feat(emulator): implement TCP transport#252
groupsky merged 3 commits into
mainfrom
feature/tcp-transport

Conversation

@groupsky

@groupsky groupsky commented Feb 7, 2026

Copy link
Copy Markdown
Owner

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

  • TcpTransport class extending BaseTransport
  • Uses modbus-serial's ServerTCP for protocol handling
  • Service vector bridge pattern for request/response mapping
  • Support for all Modbus operations (read/write registers and coils)

Integration

  • Integrated with ModbusEmulator configuration system
  • Host and port validation for TCP transport
  • Configuration interface already existed in EmulatorConfig

Testing

  • 30 unit tests with 94.05% statement coverage, 81.57% branch coverage
  • 11 integration tests verifying end-to-end operations
  • Mocked ServerTCP for isolated testing
  • Tests mirror RTU transport structure for consistency

Documentation

  • Quick Start examples for TCP transport
  • CLI usage documentation
  • Configuration examples (YAML format)
  • Updated feature list

Test Coverage

emulator/src/transports
  tcp.ts                       |   94.05 |    81.57 |     100 |   93.93

All tests pass (226 tests total in emulator package).

Implementation Details

Service Vector Bridge Pattern:

  • Maps modbus-serial callbacks to internal request handler
  • Converts between library's async API and buffer-based protocol
  • Handles 9 Modbus operations: read/write holding registers, input registers, coils, and discrete inputs

Configuration:

  • Required: host (string), port (number)
  • Default: unitID: 255 (listen to all slave IDs)
  • Validation in emulator constructor

Error Handling:

  • Lifecycle errors (double start, stop failures)
  • Invalid responses with proper validation
  • Non-existent device routing

Architecture Consistency

Follows identical patterns to RTU transport:

  • Same BaseTransport interface implementation
  • Same service vector bridge architecture
  • Same handler method structure
  • Equivalent test coverage and organization

Related Issues

Closes #244

Follow-up Issues

The implementation is production-ready but has identified areas for improvement:

  • Event listener memory leak (will be tracked separately)
  • Code duplication with RTU transport (refactoring opportunity)
  • Documentation gaps (missing example file)

These will be addressed in follow-up issues created after this PR is merged.

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.
@pkg-pr-new

pkg-pr-new Bot commented Feb 7, 2026

Copy link
Copy Markdown

Open in StackBlitz

@ya-modbus/cli

npm i https://pkg.pr.new/@ya-modbus/cli@252

@ya-modbus/device-profiler

npm i https://pkg.pr.new/@ya-modbus/device-profiler@252

@ya-modbus/driver-ex9em

npm i https://pkg.pr.new/@ya-modbus/driver-ex9em@252

@ya-modbus/driver-loader

npm i https://pkg.pr.new/@ya-modbus/driver-loader@252

@ya-modbus/driver-or-we-516

npm i https://pkg.pr.new/@ya-modbus/driver-or-we-516@252

@ya-modbus/driver-sdk

npm i https://pkg.pr.new/@ya-modbus/driver-sdk@252

@ya-modbus/driver-types

npm i https://pkg.pr.new/@ya-modbus/driver-types@252

@ya-modbus/driver-xymd1

npm i https://pkg.pr.new/@ya-modbus/driver-xymd1@252

@ya-modbus/emulator

npm i https://pkg.pr.new/@ya-modbus/emulator@252

@ya-modbus/mqtt-bridge

npm i https://pkg.pr.new/@ya-modbus/mqtt-bridge@252

@ya-modbus/transport

npm i https://pkg.pr.new/@ya-modbus/transport@252

commit: bd16d2d

@groupsky
groupsky merged commit 3df8fda into main Feb 7, 2026
14 checks passed
@groupsky
groupsky deleted the feature/tcp-transport branch February 7, 2026 07:16
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(emulator): implement TCP transport

1 participant