liblightmodbus
2.0
A lightweight, cross-platform Modbus RTU library
|
General Modbus slave functions. More...
Go to the source code of this file.
Data Structures | |
struct | modbusSlaveUserFunction |
Associates user defined parser function with the function ID. More... | |
struct | modbusSlave |
Represents Modbus slave device's status and configuration. More... | |
Typedefs | |
typedef struct modbusSlaveUserFunction | ModbusSlaveUserFunction |
Associates user defined parser function with the function ID. More... | |
typedef enum modbusRegisterQuery | ModbusRegisterQuery |
Represents register callback function. More... | |
typedef uint16_t(* | ModbusRegisterCallbackFunction) (ModbusRegisterQuery query, ModbusDataType datatype, uint16_t index, uint16_t value, void *ctx) |
Type representing a pointer to the user-defined register callback function. More... | |
typedef struct modbusSlave | ModbusSlave |
Represents Modbus slave device's status and configuration. More... | |
Enumerations | |
enum | modbusRegisterQuery { MODBUS_REGQ_R, MODBUS_REGQ_W, MODBUS_REGQ_R_CHECK, MODBUS_REGQ_W_CHECK } |
Represents register callback function. More... | |
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... | |
static ModbusError | modbusBuildExceptionErr (ModbusSlave *status, uint8_t function, ModbusExceptionCode code, ModbusFrameError parseError) |
Handles Modbus parsing errors. More... | |
General Modbus slave functions.
typedef uint16_t( * ModbusRegisterCallbackFunction) (ModbusRegisterQuery query, ModbusDataType datatype, uint16_t index, uint16_t value, void *ctx) |
Type representing a pointer to the user-defined register callback function.
query | A register query (see ModbusRegisterQuery) |
datatype | Modbus data type |
index | Address of the register |
value | Value of the register (only used with write access queries) |
ctx | User's context data (passed from ModbusSlave::registerCallbackContext) |
typedef enum modbusRegisterQuery ModbusRegisterQuery |
Represents register callback function.
REGISTER_CALLBACK
or COIL_CALLBACK
module (see Building liblightmodbus) typedef struct modbusSlave ModbusSlave |
Represents Modbus slave device's status and configuration.
typedef struct modbusSlaveUserFunction ModbusSlaveUserFunction |
Associates user defined parser function with the function ID.
SLAVE_USER_FUNCTIONS
module (see Building liblightmodbus) enum modbusRegisterQuery |
Represents register callback function.
REGISTER_CALLBACK
or COIL_CALLBACK
module (see Building liblightmodbus) 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().
status | The slave structure to work on |
function | Function code of function throwing the exception |
code | A Modbus exception code |
|
inlinestatic |
Handles Modbus parsing errors.
This function causes an exception frame to be built by modbusBuildException, but additionally stores the more verbose error code in the provided slave structure. This routine should be used for handling parsing errors, whilst the modbusBuildException() should be used for building exception frames with no side effects.
status | The slave structure to work on |
function | Function code of the function throwing the exception |
code | Exception code to be thrown |
parseError | More specific error code |
MODBUS_ERROR_PARSE
(when exception frame is not supposed to be built) or MODBUS_ERROR_EXCEPTION
(when exception frame has been successfully built) on success. This is because, these are the values that shall be returned by a parsing function upon facing parsing problem. 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.
status | The slave structure to work on |
ModbusError modbusSlaveEnd | ( | ModbusSlave * | status | ) |
Frees memory used by slave structure, previously initialized with modbusSlaveInit.
status | The slave structure to work on |
ModbusError modbusSlaveInit | ( | ModbusSlave * | status | ) |
Performs initialization of the ModbusSlave structure.
status | The slave structure to be initialized |