liblightmodbus 3.0
A lightweight, header-only, hardware-agnostic Modbus RTU/TCP library
|
Since the version v3.0, liblightmodbus is a header-only library. This decision was made due to large number of questions and problems regarding compilation on different platforms. As it turns out, CMake doesn't go very well with embedded projects.
In order to integrate liblightmodbus with your project you need to add liblightmodbus/include
to you include paths. If you're using CMake you might do that automatically by including lightmodbus
package.
In your source code you should be using #include <lightmodbus/lightmodbus.h>
to include the library (or lightmodbus.hpp
if you wish to try the experimental C++ API). The library can be configured by defining certain macros before including that file:
Macro | Description |
---|---|
LIGHTMODBUS_IMPL | Include implementation |
LIGHTMODBUS_SLAVE | Includes slave part of the library |
LIGHTMODBUS_FxxS | Adds function xx to modbusSlaveDefaultFunctions |
LIGHTMODBUS_SLAVE_FULL | Includes slave part of the library and adds all functions to modbusSlaveDefaultFunctions |
LIGHTMODBUS_MASTER | Includes master part of the library |
LIGHTMODBUS_FxxM | Adds function xx to modbusMasterDefaultFunctions |
LIGHTMODBUS_MASTER_FULL | Includes master part of the library and adds all functions to modbusMasterDefaultFunctions |
LIGHTMODBUS_FULL | Equivalent of both LIGHTMODBUS_SLAVE_FULL and LIGHTMODBUS_MASTER_FULL |
LIGHTMODBUS_DEBUG | Includes some debugging utilities |
LIGHTMODBUS_MASTER_OMIT_REQUEST_CRC | Omits request CRC calculation for request on master side |
LIGHTMODBUS_WARN_UNUSED | Compiler attribute to warn about unused return value. __attribute__((warn_unused_result)) by default |
LIGHTMODBUS_ALWAYS_INLINE | Compiler attribute to always inline a function. __attribute__((always_inline)) by default |
Each time lightmodbus.h
is included, the set of defined configuration macros must be the same (except from LIGHTMODBUS_IMPL
which should only be used once).
Moreover, it's very important that you include implementation of the library functions (LIGHTMODBUS_IMPL
) in exactly one place in your codebase. It may be a good idea to create a source file dedicated specifically for including implementation of liblightmodbus. For instance, you may want to create lightmodbus-impl.c
containing:
See example integration of liblightmodbus with a simple demo project here.
Since v3.0 building liblightmodbus on embedded platforms is no different from building it for your PC. You're only required to add the header files to your project.
-flto
) when building your project to further reduce the size of your binary. If your compiler doesn't support it, you can try -ffunction-sections -fdata-sections -Wl,--gc-sections
to delete unused functions.Liblightmodbus can be used as an ESP-IDF component. The relevant configuration is kept on esp-idf
branch and in liblightmodbus-esp
repository. It can be cloned directly to your components directory with:
When using liblightmodbus with ESP-IDF, the implementation of the library functions is automatically included for you - you shouldn't use LIGHTMODBUS_IMPL
anywhere in your code. You shouldn't define any config options when including lightmodbus.h
either. Library configuration is read from esp.config.h
and sdkconfig.h
and should be edited with idf.py menuconfig
.