Ask one device, wait for the reply, ask the next one – while the microcontroller still has other work to do. That is why I developed mod(ern)bus: a C++ library for building asynchronous Modbus clients and servers.

My goal was a shared interface for different connections, without having to piece together a different library and API for every use case. Whether you are polling measurements or providing data to other devices, modernbus handles the communication flow so your program can focus on its actual job.

Modbus without waiting around

Communication is handled in steps using TaskScheduler. A client can send individual requests or poll values periodically. When a response arrives, a handler processes it. On the server, handlers can be registered for a function code and address, or data can be mapped directly to a range. Parsing is handled by mbparser.

One API, different interfaces

Client and server use a provider to access the data stream. The repository includes a serial provider and an RS485 provider with transmit-pin control. You can develop your own providers for other streams, so the application is not tied to a single serial interface.

Get started and explore

The GitHub repository contains the source code, client and server examples, and a crosslink approach for connecting both on one system. The README describes usage and installation via PlatformIO. The library requires Arduino headers and at least C++11; further dependencies are listed there.

modernbus is available under the MIT licence. You can explore the code, use it for your own projects and adapt it under the licence terms.

Project status: This article refers to the repository snapshot from 10 July 2023. The API may change, and the README advises against use for critical tasks. Polling intervals are not guaranteed. Please test hardware, timing and error conditions for your own application.