liblightmodbus 3.0
A lightweight, header-only, hardware-agnostic Modbus RTU/TCP library
|
In order to use slave side functions, make sure to define LIGHTMODBUS_MASTER
macro and enable necessary functions (e.g. LIGHTMODBUS_F03M
) before including the library. Please also see Building and integrating liblightmodbus for more information.
Master device state is represented by ModbusMaster structure. It must be initialized with modbusMasterInit() before it is used:
Similarly to slave, master has two callbacks - data callback and exception callback. The first is used for processing all register data incoming to master, the latter for handling exceptions reported by the slave. These callbacks are described in more detail in Data callback and Exception callback.
Master also relies on a memory allocator - please refer to Custom allocators for more details.
The 5th argument determines the set of functions supported by the master device. It should be a pointer to an array of ModbusMasterFunctionHandler
structures which associate function codes with callbacks. The lifetime of this array must not be shorter than the lifetime of the ModbusMaster
structure. The last argument must contain that array's length.
The data callback is a function matching ModbusDataCallback, used to provide data read from the slaves to user.
This function is called from modbusParseResponse*()
for every received register/coil/input value. The address of the slave, index of the register, its value and function used to acquire it are provided as an argument of type ModbusDataCallbackArgs
.
If you're sure that none of the used parsing functions use the data callback, you can pass NULL
as the argument to modbusMasterInit()
. Importantly, all default parsing functions in the library require the data callback to be provided.
The data callback must return a ModbusError
. It should always return MODBUS_OK
.
An example of a simple data callback printing out all received data:
Master exception callback is a function matching ModbusMasterExceptionCallback called when an exception response frame is parsed by one of the modbusParseResponse*()
functions.
When called, the callback is provided with address of the slave that reported the exception, function code of the function that caused it and a Modbus exception code (ModbusExceptionCode). The callback must return a ModbusError
value, which should be MODBUS_OK
.
An example of a simple exception callback printing out all received exceptions:
Master side of the library provides a set of functions for building requests. They are named according to the pattern modbusMasterBuildRequest*()
. Where *
is code of the function followed by the Modbus request format to be generated (PDU, RTU or TCP).
The functions without PDU, RTU or TCP at the end of their name are generic functions responsible for building the request frame and must only be used in between calls to modbusBeginRequest*()
and modbusEndRequest*()
.
Please see master_func.impl.h for more details.
For instance, a request to read the holding register 17 from slave with address 1 can be built with following code:
If the request has been built successfully, it can be accessed via modbusMasterGetRequest()
, has length of modbusMasterGetRequestLength()
bytes and can be sent to the slave.
After obtaining the response from the slave, it's time to process it. In order to do that, one of the modbusParseResponse*()
functions can be used:
When you're done using an instance of ModbusMaster
, you can destroy it using modbusMasterDestroy()
.