liblightmodbus
2.0
A lightweight, cross-platform Modbus RTU library
|
SLAVE_USER_FUNCTIONS
or MASTER_USER_FUNCTIONS
module (see Building liblightmodbus).Liblightmodbus allows user to define his own request/response parsing functions.
In order to provide library hint which functions shall be treated differently, user should set up an array of ModbusSlaveUserFunction structures and provide pointer to it in ModbusSlave::userFunctions. The number of user-defined functions in the array should be written to ModbusSlave::userFunctionCount. See the example below:
This code causes the library to attempt to parse request 77 with some foo()
function and ignore request 16.
The user-defined functions must have following form:
The foo
function could be defined for example as:
This function causes slaves with addresses divisible by 2 to return an exception upon receiving a frame with function code 77. The odd-numbered slaves respond with 16 byte frame filled with zeros. This is just a silly example, so please see the real guidelines for used-defined functions below.
base
structure - that means you know the function code and slave address.If that's not enough information for you, take a look at some real request parsing functions, for example in src/slave/sregs.c
.
Setting up user-defined functions on master side is very similar to setting them up on the slave side. By analogy ModbusMaster::userFunctions and ModbusMaster::userFunctionCount need to be set up.
The parsing function, however, needs to look a bit differently:
The difference is the requestParser
parameter - it gives you access to the request frame to which response you are parsing.
base
structure - that means you know the function code and slave address.Please take a look at src/master/mpregs.c
for example parsing functions.