liblightmodbus  2.0
A lightweight, cross-platform Modbus RTU library
Functions
slave.c File Reference

Functions

ModbusError modbusBuildException (ModbusSlave *status, uint8_t function, ModbusExceptionCode code)
 Builds an exception frame and stores it in the ModbusSlave structure. More...
 
ModbusError modbusParseRequest (ModbusSlave *status)
 Interprets incoming Modbus request frame located in the slave structure. More...
 
ModbusError modbusSlaveInit (ModbusSlave *status)
 Performs initialization of the ModbusSlave structure. More...
 
ModbusError modbusSlaveEnd (ModbusSlave *status)
 Frees memory used by slave structure, previously initialized with modbusSlaveInit. More...
 

Function Documentation

◆ modbusBuildException()

ModbusError modbusBuildException ( ModbusSlave status,
uint8_t  function,
ModbusExceptionCode  code 
)

Builds an exception frame and stores it in the ModbusSlave structure.

The exception frame is only built if the request was not broadcasted.

Updates ModbusSlave::lastException member in provided ModbusSlave structure.

This function is used by more sophisticated modbusBuildExceptionErr().

Parameters
statusThe slave structure to work on
functionFunction code of function throwing the exception
codeA Modbus exception code
Returns
A ModbusError error code. Unlike other library functions, this one returns MODBUS_ERROR_EXCEPTION on success as a form of information that exception had been thrown. If the request was broadcasted, returns MODBUS_ERROR_OK
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusParseRequest()

ModbusError modbusParseRequest ( ModbusSlave status)

Interprets incoming Modbus request frame located in the slave structure.

Firstly, the function frees memory allocated for the previous response frame (ModbusSlave::response) (if dynamic memory allocation is enabled). Then, integrity of the frame is verified with modbusCRC function.

If the frame CRC is correct, the destination slave address is checked. Frames meant for other slave devices are discarded at this point.

Before attempting to parse the frame with one of the built-in parsing functions, the array of user-defined parsing functions is searched. It is important to mention that functions defined by user override the default ones. That is to say, a NULL pointer in the function array can disable support of specific function.

If matching function is found, it is exected and modbusParseRequest() returns the same error code the parsing function code had returned. Otherwise, an exception informing the master device of illegal function code is constructed, and MODBUS_ERROR_EXCEPTION is returned.

Parameters
statusThe slave structure to work on
Returns
A ModbusError error code.
+ Here is the call graph for this function:

◆ modbusSlaveEnd()

ModbusError modbusSlaveEnd ( ModbusSlave status)

Frees memory used by slave structure, previously initialized with modbusSlaveInit.

Parameters
statusThe slave structure to work on
Returns
A ModbusError error code

◆ modbusSlaveInit()

ModbusError modbusSlaveInit ( ModbusSlave status)

Performs initialization of the ModbusSlave structure.

Parameters
statusThe slave structure to be initialized
Returns
A ModbusError error code.