liblightmodbus
2.0
A lightweight, cross-platform Modbus RTU library
|
If you want to get started with liblightmodbus, you should probably start here. This page shows basic usage of the library and assumes you have a working copy of it.
If you don't, see Building liblightmodbus or download a development package from the PPA.
The library consists of two independent parts. One contains all functions related with slave's tasks and the other functions related with master's tasks.
The most important structure type on the slave side is ModbusSlave which represents slave device's status and configuration. A pointer to such structure is passed to all the slave-related functions.
In order to get started, you need to allocate some space for your slave's registers and coils. Two arrays will do just fine:
Next, the slave structure has to be initialized:
Note the modbusSlaveInit call. This is the function that actually initializes the structure for use. It shall be called before any other library function is used on the structure
Processing incoming requests is done using modbusParseRequest function. However, before calling it, the request needs to be loaded to ModbusSlave::request. Please see the example below.
This code causes the library to process the incoming request. Afterwards, the response from ModbusSlave::response needs to be send back to master device.
After you're done using the library, don't forget to clean up. You should call modbusSlaveEnd on your slave structure.
The most important structure type on the master side is ModbusMaster which represents master device's status and configuration (like ModbusSlave on the slave side). A pointer to such structure is passed to all the master-related functions.
Unlike on save side, there's nothing more to be done apart from initializing the master structure with modbusMasterInit.
Request frames can be generated using modbusBuildRequest**
functions. After each successful call, the new request frame will be written to ModbusMaster::request.
The modbusParseResponse function does the response parsing job on the master side. It needs to have the response frame provided in ModbusMaster::response. After successful parsing, the incoming data will be available in ModbusMaster::data structure. If the slave had returned an exception, the exception details will be available in the ModbusMaster::exception structure.
After you're done using the library, don't forget to clean up. You should call modbusMasterEnd on your master structure.
Please see the contents of the examples
directory.