liblightmodbus  2.0
A lightweight, cross-platform Modbus RTU library
Data Structures | Typedefs | Enumerations | Functions
slave.h File Reference

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...
 

Detailed Description

General Modbus slave functions.

Note
This header file is suitable for C++

Typedef Documentation

◆ ModbusRegisterCallbackFunction

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.

See also
User-defined Modbus register callback function
Parameters
queryA register query (see ModbusRegisterQuery)
datatypeModbus data type
indexAddress of the register
valueValue of the register (only used with write access queries)
ctxUser's context data (passed from ModbusSlave::registerCallbackContext)

◆ ModbusRegisterQuery

Represents register callback function.

Note
Requires REGISTER_CALLBACK or COIL_CALLBACK module (see Building liblightmodbus)
See also
User-defined Modbus register callback function

◆ ModbusSlave

typedef struct modbusSlave ModbusSlave

Represents Modbus slave device's status and configuration.

◆ ModbusSlaveUserFunction

Associates user defined parser function with the function ID.

Note
Requires SLAVE_USER_FUNCTIONS module (see Building liblightmodbus)
See also
user-functions

Enumeration Type Documentation

◆ modbusRegisterQuery

Represents register callback function.

Note
Requires REGISTER_CALLBACK or COIL_CALLBACK module (see Building liblightmodbus)
See also
User-defined Modbus register callback function
Enumerator
MODBUS_REGQ_R 

Requests callback function to return register value.

MODBUS_REGQ_W 

Requests callback function to write the register.

MODBUS_REGQ_R_CHECK 

Asks callback function if register can be read.

MODBUS_REGQ_W_CHECK 

Asks callback function if register can be written.

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:

◆ modbusBuildExceptionErr()

static ModbusError modbusBuildExceptionErr ( ModbusSlave status,
uint8_t  function,
ModbusExceptionCode  code,
ModbusFrameError  parseError 
)
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.

Parameters
statusThe slave structure to work on
functionFunction code of the function throwing the exception
codeException code to be thrown
parseErrorMore specific error code
Returns
An ModbusError error code. Unlike most library functions, this one returns 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.
+ 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.