|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this |
| 4 | +repository. |
| 5 | + |
| 6 | +## Project Overview |
| 7 | + |
| 8 | +A modern, performant Modbus client and server library for Java 17+, supporting: |
| 9 | + |
| 10 | +- Modbus TCP (with optional TLS/SSL) |
| 11 | +- Modbus RTU over Serial |
| 12 | +- Modbus RTU over TCP |
| 13 | + |
| 14 | +Published to Maven Central as `com.digitalpetri.modbus`. |
| 15 | + |
| 16 | +## Module Structure |
| 17 | + |
| 18 | +This is a multi-module Maven project with the following modules: |
| 19 | + |
| 20 | +- **modbus** (core): Protocol-agnostic implementation containing: |
| 21 | + - PDU definitions for all supported function codes (in `pdu/` package) |
| 22 | + - `ModbusClient` and `ModbusServer` abstractions |
| 23 | + - PDU serialization/deserialization (`ModbusPduSerializer`) |
| 24 | + - Exception handling and function code enums |
| 25 | + - Transport abstraction interfaces (`ModbusClientTransport`, `ModbusServerTransport`) |
| 26 | + |
| 27 | +- **modbus-tcp**: Netty-based TCP transport implementations: |
| 28 | + - `NettyTcpClientTransport` and `NettyRtuClientTransport` for clients |
| 29 | + - `NettyTcpServerTransport` and `NettyRtuServerTransport` for servers |
| 30 | + - TLS/SSL support via Netty's SSL handlers |
| 31 | + - Uses `netty-channel-fsm` for connection state management |
| 32 | + |
| 33 | +- **modbus-serial**: Serial port transport using jSerialComm library: |
| 34 | + - `SerialPortClientTransport` and `SerialPortServerTransport` |
| 35 | + - Configuration for baud rate, parity, stop bits, data bits |
| 36 | + |
| 37 | +- **modbus-tests**: Integration tests exercising all transport types |
| 38 | + |
| 39 | +## Build Commands |
| 40 | + |
| 41 | +```bash |
| 42 | +# Clean and build all modules |
| 43 | +mvn clean install |
| 44 | + |
| 45 | +# Run all tests (including integration tests) |
| 46 | +mvn verify |
| 47 | + |
| 48 | +# Run tests for a specific module |
| 49 | +mvn test -pl modbus-tcp |
| 50 | + |
| 51 | +# Run a specific test class |
| 52 | +mvn test -Dtest=ClientServerIT |
| 53 | + |
| 54 | +# Skip tests during build |
| 55 | +mvn clean install -DskipTests |
| 56 | + |
| 57 | +# Check code style |
| 58 | +mvn checkstyle:check |
| 59 | + |
| 60 | +# Update dependency/plugin versions (check for updates) |
| 61 | +mvn versions:display-dependency-updates |
| 62 | +mvn versions:display-plugin-updates |
| 63 | +``` |
| 64 | + |
| 65 | +## Testing |
| 66 | + |
| 67 | +- Unit tests are in each module's `src/test/java` |
| 68 | +- Integration tests (ending with `IT.java`) are in the `modbus-tests` module |
| 69 | +- Tests use JUnit 5 (Jupiter) |
| 70 | +- Integration tests start real servers and clients to test end-to-end functionality |
| 71 | + |
| 72 | +## Architecture Notes |
| 73 | + |
| 74 | +### Transport Abstraction |
| 75 | + |
| 76 | +The core `modbus` module defines transport interfaces but doesn't implement them. Implementations |
| 77 | +are in `modbus-tcp` and `modbus-serial`: |
| 78 | + |
| 79 | +- `ModbusClientTransport<T>` and `ModbusServerTransport<T>` are generic interfaces |
| 80 | +- Transport implementations handle framing (MBAP for TCP, RTU for serial) |
| 81 | +- Transports are pluggable - new transports can be implemented without modifying core |
| 82 | + |
| 83 | +### Client Architecture |
| 84 | + |
| 85 | +- `ModbusClient` is an abstract base providing synchronous and asynchronous methods for all standard |
| 86 | + function codes |
| 87 | +- Concrete implementations: `ModbusTcpClient`, `ModbusRtuClient` (TCP module), `ModbusRtuClient` ( |
| 88 | + serial module) |
| 89 | +- Each client wraps a transport and delegates frame sending/receiving |
| 90 | +- Both blocking (throws exceptions) and async (returns `CompletionStage`) APIs available |
| 91 | + |
| 92 | +### Server Architecture |
| 93 | + |
| 94 | +- `ModbusServer` interface with `start()`, `stop()`, and `setModbusServices()` |
| 95 | +- `ModbusServices` handles incoming requests via service handlers for each function code |
| 96 | +- Authorization support via `ModbusServerAuthz` interface in `server.authz` package |
| 97 | +- Servers are transport-agnostic and work with any `ModbusServerTransport` |
| 98 | + |
| 99 | +### PDU Layer |
| 100 | + |
| 101 | +- All request/response PDUs in `com.digitalpetri.modbus.pdu` package |
| 102 | +- `ModbusPduSerializer` handles encoding/decoding PDUs to/from byte buffers |
| 103 | +- PDUs are simple records/classes with validation |
| 104 | +- Supports both standard function codes (0x01-0x17) and exception responses |
| 105 | + |
| 106 | +### Netty Integration (TCP) |
| 107 | + |
| 108 | +- Uses Netty 4.x for non-blocking I/O |
| 109 | +- `netty-channel-fsm` library manages connection lifecycle as a state machine |
| 110 | +- `ModbusTcpCodec` handles MBAP header encoding/decoding |
| 111 | +- TLS support via Netty's `SslContext` and `SslHandler` |
| 112 | + |
| 113 | +## Code Style |
| 114 | + |
| 115 | +- Uses Google Java Style Guide |
| 116 | +- Enforced via Checkstyle during `verify` phase |
| 117 | +- Configuration: `config/checkstyle/checkstyle.xml` |
| 118 | +- Build fails on checkstyle violations |
| 119 | + |
| 120 | +## Commit Messages |
| 121 | + |
| 122 | +Keep the title of the commit message to ~72 characters. |
| 123 | + |
| 124 | +Avoid overly verbose descriptions or unnecessary details. |
| 125 | + |
| 126 | +If the commit contains a number of disparate changes, try to generate a brief one-line subject, |
| 127 | +then summarize it using bullet points. Avoid subjective justification for changes; state the |
| 128 | +change on its own. |
| 129 | + |
| 130 | +## Release Process |
| 131 | + |
| 132 | +Two Maven profiles for publishing to Maven Central: |
| 133 | + |
| 134 | +- `release`: Full release with GPG signing |
| 135 | +- `deploy-snapshot`: Snapshot deployment without signing |
| 136 | + |
| 137 | +Releases are tagged as `v{version}` (e.g., `v2.1.3`). |
0 commit comments