Hey, friends! Have you ever encountered the awkward situation of wanting to implement the Modbus protocol on a microcontroller, only to find that the library is too large, has too many dependencies, and fills up the Flash memory? Today, I want to recommend a “mini” level Modbus C library—nanoMODBUS, lightweight and easy to use!
What is nanoMODBUS? In simple terms, nanoMODBUS is a compact Modbus RTU/TCP implementation with only 2000 lines of C code.
- • Written in pure C99, can run on any platform;
- • Client/server code can be toggled on or off as needed;
- • Completely avoids malloc and free, stack-friendly;
- • Supports both RTU and TCP transmission;
- • Supports common function codes (01, 02, 03, 04, 05, 06, 0F, 10, 14, 15, 17, 2B/0E)…
Let’s take a look at the features in a table:
| Feature | Description |
| Code Size | ~2000 lines (including comments) |
| Dependencies | Only the C99 standard library, zero external dependencies |
| Transmission Method | RTU, TCP |
| Roles | Client and server selectable |
| Dynamic Memory | None |
| Function Code Support | 01, 02, 03, 04, 05, 06, 0F, 10, 14, 15, 17, 2B/0E |
What pain points does it solve?
- • Insufficient Flash: Small size, won’t occupy too much space;
- • Resource constraints: No dynamic allocation, suitable for microcontrollers with limited stack;
- • Customization needs: Platform read/write functions and CRC can be user-defined;
- • Quick to get started: Simple API, intuitive and clear example code.
Installation & Usage Guide
- 1. Manual Integration
- • Simply drop
<span>nanomodbus.c</span>and<span>nanomodbus.h</span>into your project, and you’re done.
FetchContent_Declare(
nanomodbus
GIT_REPOSITORY https://github.com/debevv/nanoMODBUS
GIT_TAG master
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(nanomodbus)
target_link_libraries(your_program nanomodbus)
- • Users need to implement two functions:
int32_t read(uint8_t* buf, uint16_t cnt, int32_t timeout_ms, void* arg); int32_t write(const uint8_t* buf, uint16_t cnt, int32_t timeout_ms, void* arg); - • Place these functions and the timeout into
<span>nmbs_platform_conf</span>, then:nmbs_client_create(&nmbs, &platform_conf); nmbs_read_holding_registers(&nmbs, addr, len, buf);
Pros and Cons Overview
| Pros | Cons |
| Small size, few dependencies | Full coverage of function codes, but advanced features (like security, encryption) need to be extended by the user |
| Completely avoids dynamic memory allocation, strong real-time performance | Microcontroller debugging requires writing low-level read/write; slightly higher learning curve |
| Client/server modules can be toggled on or off as needed | Documentation is mainly comments + examples, lacks a complete tutorial in Chinese |
| Platform-independent, C99 standard, good compatibility | Default CRC function performance is average; complex scenarios suggest user-defined optimization |
Conclusion In summary, nanoMODBUS is a “small but complete” Modbus protocol stack, especially suitable for microcontroller projects with tight Flash and RAM constraints. If you just want a “working” Modbus implementation and want to quickly establish communication, it is definitely worth a try! Project address: https://github.com/debevv/nanoMODBUS