libmodbus - C++ Modbus Protocol Implementation๏
A comprehensive C++ implementation of the Modbus protocol supporting RTU, ASCII, and TCP variants. The library provides a clean, modern C++ interface for building Modbus servers and clients with full compliance to Modbus specifications.
๐ Key Features๏
Complete Function Code Support: All standard Modbus function codes (FC 01-08, 15-16)
Multiple Transport Protocols: RTU (complete), ASCII and TCP (planned)
Standards Compliance: Full compliance with Modbus Application Protocol V1.1b3
Modern C++ Design: C++17 compatible with RAII principles and type safety
Configurable Data Model: Supports standard and custom Modbus limits
Comprehensive Testing: Full unit test coverage with Google Test
Header-Only Design: Easy integration into existing projects
Cross-Platform: Works on Windows, Linux, and macOS
๐ Supported Function Codes๏
Code |
Function |
Status |
---|---|---|
01 |
Read Coils |
โ Complete |
02 |
Read Discrete Inputs |
โ Complete |
03 |
Read Holding Registers |
โ Complete |
04 |
Read Input Registers |
โ Complete |
05 |
Write Single Coil |
โ Complete |
06 |
Write Single Register |
โ Complete |
08 |
Diagnostics |
โ Complete |
15 |
Write Multiple Coils |
โ Complete |
16 |
Write Multiple Registers |
โ Complete |
๐๏ธ Architecture๏
The library is built around several key components:
ModbusDataModel: Stores all four types of Modbus data (coils, inputs, registers)
ModbusCommand: Command pattern implementation for function codes
ModbusFrame: Protocol Data Unit and transport-specific frames
ModbusBaseServer: Abstract server base with protocol-specific implementations
Checksum: Utilities for CRC-16 and LRC calculations
๐ Documentation Sections๏
API Reference
๐ Quick Start๏
Building the Library
mkdir build && cd build
cmake ..
make
Basic Server Example
#include "libmodbus.hpp"
int main() {
// Create RTU server
ModbusRtuServer server;
// Initialize some data
server.data.writeCoil(0, true);
server.data.writeHoldingRegister(0, 0x1234);
// Process a request (normally from serial port)
std::vector<uint8_t> request = {0x01, 0x01, 0x00, 0x00, 0x00, 0x08, 0x3D, 0xCC};
auto response = server.process(request);
return 0;
}
๐ฆ Installation๏
Prerequisites
C++17 compatible compiler
CMake 3.10 or higher
Google Test (included as submodule)
Clone and Build
git clone --recursive https://github.com/shishir-dey/libmodbus.git
cd libmodbus
mkdir build && cd build
cmake ..
make
Running Tests
make test
๐ License๏
This project is licensed under the MIT License - see the LICENSE file for details.
๐ค Contributing๏
Contributions are welcome! Please feel free to submit a Pull Request.
๐ Support๏
For questions, issues, or suggestions, please open an issue on the GitHub repository.
Version: 1.0
Last Updated: Jun 20, 2025