libmodbus - C++ Modbus Protocol Implementation๏ƒ

CMake Build Status Documentation Status License: MIT

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๏ƒ

๐Ÿš€ 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

Indices and tables๏ƒ