$ show Apollo3zehn/FluentModbus
FluentModbus is a .NET library that implements the Modbus protocol (TCP and RTU) for client and server communication. The library provides a full set of function codes (class 0, 1, and partial class 2) including read/write holding registers, coils, discrete inputs, input registers, and combined read-write operations. Both synchronous and asynchronous APIs are available. The server supports multiple unit identifiers, frame validation via a user-defined callback, events for register/coil changes, and configurable endianness. The client exposes high-level generic methods as well as low-level byte-oriented access. The codebase is well-structured with abstract base classes (MobusClient, ModbusServer), separate request handlers for TCP and RTU, and clean separation of concerns. Dependencies are minimal (logging abstractions, I/O, memory). The library includes comprehensive unit tests and samples.
The LLM identified 5 files worth highlighting for reuse:
A custom MemoryMananger that casts Memory<TFrom> to Memory<TTo> without allocation. It leverages MemoryMarshal.Cast and is useful in high-performance scenarios where typed memory views are needed. The component is self-contained, depends only on standard .NET types, and can be extracted into any project requiring zero-allocation memory reinterpretation.
Extends BinaryReader with methods to read primitive types in big-endian byte order (Reverse methods). Useful for protocols that require reverse byte order. The class is stand-alone, depending only on System.IO and System.Runtime.. It can be reemployed in any project needing reverse decoding.
Symetric companion to ExtendedBinaryReader, writes primitives in big-endian byte order. Self-contained and broadly reusable.
Static utility methods including CRC-16 calculation (Modbus RTU), generic endianness switching for arbitrary unmanaged types, and IPEndPoint parsing from strings (supports IPv4/6 with optional port). The TryParseEndpoint method demonstrate careful handling of colons and brackets. These functions are isolated from Modbus-specific logic and can be reused in other networking or embeded projects.
An interface that decouples the RTU client and server from the physical SerialPort implementation. Defines read/write async methods. Allows easy mocking for unit tests and injection of custom serial port wrappers. The pattern is reusable for any protocol over serial ports.