liblightmodbus 3.0
A lightweight, header-only, hardware-agnostic Modbus RTU/TCP library
Loading...
Searching...
No Matches
slave.h File Reference

Slave's types and basic functions (header) More...

#include <stdint.h>
#include <stddef.h>
#include "base.h"
+ Include dependency graph for slave.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ModbusSlaveFunctionHandler
 Associates Modbus function ID with a pointer to a parsing function. More...
 
struct  ModbusRegisterCallbackArgs
 Contains arguments for the register callback function. More...
 
struct  ModbusRegisterCallbackResult
 Contains values returned by the slave register callback. More...
 
struct  ModbusSlave
 Slave device status. More...
 

Typedefs

typedef struct ModbusSlave ModbusSlave
 
typedef ModbusErrorInfo(* ModbusRequestParsingFunction) (ModbusSlave *status, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength)
 A pointer to request parsing function.
 
typedef struct ModbusSlaveFunctionHandler ModbusSlaveFunctionHandler
 Associates Modbus function ID with a pointer to a parsing function.
 
typedef enum ModbusRegisterQuery ModbusRegisterQuery
 Determines type of request made to the register callback function.
 
typedef struct ModbusRegisterCallbackArgs ModbusRegisterCallbackArgs
 Contains arguments for the register callback function.
 
typedef struct ModbusRegisterCallbackResult ModbusRegisterCallbackResult
 Contains values returned by the slave register callback.
 
typedef ModbusError(* ModbusRegisterCallback) (const ModbusSlave *status, const ModbusRegisterCallbackArgs *args, ModbusRegisterCallbackResult *out)
 A pointer to callback for performing all register operations.
 
typedef ModbusError(* ModbusSlaveExceptionCallback) (const ModbusSlave *status, uint8_t function, ModbusExceptionCode code)
 A pointer to a callback called when a Modbus exception is generated (for slave)
 

Enumerations

enum  ModbusRegisterQuery { MODBUS_REGQ_R_CHECK , MODBUS_REGQ_W_CHECK , MODBUS_REGQ_R , MODBUS_REGQ_W }
 Determines type of request made to the register callback function. More...
 

Functions

ModbusErrorInfo modbusSlaveInit (ModbusSlave *status, ModbusRegisterCallback registerCallback, ModbusSlaveExceptionCallback exceptionCallback, ModbusAllocator allocator, const ModbusSlaveFunctionHandler *functions, uint8_t functionCount)
 Initializes slave device.
 
void modbusSlaveDestroy (ModbusSlave *status)
 Frees memory allocated in the ModbusSlave struct.
 
ModbusErrorInfo modbusBuildException (ModbusSlave *status, uint8_t function, ModbusExceptionCode code)
 Builds an exception response frame.
 
ModbusErrorInfo modbusBuildExceptionPDU (ModbusSlave *status, uint8_t function, ModbusExceptionCode code)
 Builds an exception PDU.
 
ModbusErrorInfo modbusBuildExceptionRTU (ModbusSlave *status, uint8_t address, uint8_t function, ModbusExceptionCode code)
 Builds a Modbus RTU exception.
 
ModbusErrorInfo modbusBuildExceptionTCP (ModbusSlave *status, uint16_t transactionID, uint8_t unitID, uint8_t function, ModbusExceptionCode code)
 Builds a Modbus TCP exception.
 
ModbusErrorInfo modbusParseRequest (ModbusSlave *status, const uint8_t *request, uint8_t requestLength)
 Parses provided PDU and generates response honorinng pduOffset and padding set in ModbusSlave during response generation.
 
ModbusErrorInfo modbusParseRequestPDU (ModbusSlave *status, const uint8_t *request, uint8_t requestLength)
 Parses provided PDU and generates PDU for the response frame.
 
ModbusErrorInfo modbusParseRequestRTU (ModbusSlave *status, uint8_t slaveAddress, const uint8_t *request, uint16_t requestLength)
 Parses provided Modbus RTU request frame and generates a Modbus RTU response.
 
ModbusErrorInfo modbusParseRequestTCP (ModbusSlave *status, const uint8_t *request, uint16_t requestLength)
 Parses provided Modbus TCP request frame and generates a Modbus TCP response.
 
static const uint8_t * modbusSlaveGetResponse (const ModbusSlave *status)
 Returns a pointer to the response generated by the slave.
 
static uint16_t modbusSlaveGetResponseLength (const ModbusSlave *status)
 Returns the length of the response generated by the slave.
 
static void modbusSlaveSetUserPointer (ModbusSlave *status, void *ptr)
 Allows user to set the custom context pointer.
 
static void * modbusSlaveGetUserPointer (const ModbusSlave *status)
 Retreieves the custom context pointer.
 
static ModbusError modbusSlaveAllocateResponse (ModbusSlave *status, uint16_t pduSize)
 Allocates memory for slave's response frame.
 
static void modbusSlaveFreeResponse (ModbusSlave *status)
 Frees memory allocated for slave's response frame.
 

Variables

ModbusSlaveFunctionHandler modbusSlaveDefaultFunctions []
 Associates function IDs with pointers to functions responsible for parsing. Length of this array is stored in modbusSlaveDefaultFunctionCount.
 
const uint8_t modbusSlaveDefaultFunctionCount
 Stores length of modbusSlaveDefaultFunctions.
 

Detailed Description

Slave's types and basic functions (header)

Typedef Documentation

◆ ModbusRegisterCallback

typedef ModbusError(* ModbusRegisterCallback) (const ModbusSlave *status, const ModbusRegisterCallbackArgs *args, ModbusRegisterCallbackResult *out)

A pointer to callback for performing all register operations.

See also
slave-register-callback

◆ ModbusRegisterCallbackArgs

Contains arguments for the register callback function.

◆ ModbusRegisterCallbackResult

Contains values returned by the slave register callback.

◆ ModbusRegisterQuery

Determines type of request made to the register callback function.

◆ ModbusRequestParsingFunction

typedef ModbusErrorInfo(* ModbusRequestParsingFunction) (ModbusSlave *status, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength)

A pointer to request parsing function.

◆ ModbusSlave

typedef struct ModbusSlave ModbusSlave

◆ ModbusSlaveExceptionCallback

typedef ModbusError(* ModbusSlaveExceptionCallback) (const ModbusSlave *status, uint8_t function, ModbusExceptionCode code)

A pointer to a callback called when a Modbus exception is generated (for slave)

See also
slave-exception-callback

◆ ModbusSlaveFunctionHandler

Associates Modbus function ID with a pointer to a parsing function.

Enumeration Type Documentation

◆ ModbusRegisterQuery

Determines type of request made to the register callback function.

Enumerator
MODBUS_REGQ_R_CHECK 

Request for read access.

MODBUS_REGQ_W_CHECK 

Request for write access.

MODBUS_REGQ_R 

Read request.

MODBUS_REGQ_W 

Write request.

Function Documentation

◆ modbusBuildException()

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

Builds an exception response frame.

Parameters
functionfunction that reported the exception
codeModbus exception code
Returns
MODBUS_GENERAL_ERROR(ALLOC) on memory allocation failure
MODBUS_NO_ERROR() on success
Note
If set, exceptionCallback from ModbusSlave is called, even if the response frame is going to be discarded (when the request was broadcast).
Warning
This function expects ModbusSlave::response::pduOffset and ModbusSlave::response::padding to be set properly! If you're looking for a function to manually build an exception please use modbusBuildExceptionPDU(), modbusBuildExceptionRTU() or modbusBuildExceptionTCP()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusBuildExceptionPDU()

ModbusErrorInfo modbusBuildExceptionPDU ( ModbusSlave status,
uint8_t  function,
ModbusExceptionCode  code 
)

Builds an exception PDU.

Parameters
functionfunction that reported the exception
codeModbus exception code
Returns
MODBUS_GENERAL_ERROR(ALLOC) on memory allocation failure
MODBUS_NO_ERROR() on success
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusBuildExceptionRTU()

ModbusErrorInfo modbusBuildExceptionRTU ( ModbusSlave status,
uint8_t  address,
uint8_t  function,
ModbusExceptionCode  code 
)

Builds a Modbus RTU exception.

Parameters
addressslave address to be reported in the excetion
functionfunction that reported the exception
codeModbus exception code
Returns
MODBUS_GENERAL_ERROR(ADDRESS) if address is 0
MODBUS_GENERAL_ERROR(ALLOC) on memory allocation failure
MODBUS_GENERAL_ERROR(LENGTH) if the allocated response has invalid length
MODBUS_NO_ERROR() on success
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusBuildExceptionTCP()

ModbusErrorInfo modbusBuildExceptionTCP ( ModbusSlave status,
uint16_t  transactionID,
uint8_t  unitID,
uint8_t  function,
ModbusExceptionCode  code 
)

Builds a Modbus TCP exception.

Parameters
transactionIDtransaction ID
unitIDunit ID to be reported in the exception
functionfunction that reported the exception
codeModbus exception code
Returns
MODBUS_GENERAL_ERROR(ALLOC) on memory allocation failure
MODBUS_GENERAL_ERROR(LENGTH) if the allocated response has invalid length
MODBUS_NO_ERROR() on success
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusParseRequest()

ModbusErrorInfo modbusParseRequest ( ModbusSlave status,
const uint8_t *  request,
uint8_t  requestLength 
)

Parses provided PDU and generates response honorinng pduOffset and padding set in ModbusSlave during response generation.

Parameters
requestpointer to the PDU data
requestLengthlength of the PDU (valid range: 1 - 253)
Returns
Any errors from parsing functions
Warning
This function expects ModbusSlave::response::pduOffset and ModbusSlave::response::padding to be set properly! If you're looking for a function to parse PDU and generate a PDU response, please use modbusParseRequestPDU() instead.
The response frame can only be accessed if modbusIsOk() called on the return value of this function evaluates to true.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusParseRequestPDU()

ModbusErrorInfo modbusParseRequestPDU ( ModbusSlave status,
const uint8_t *  request,
uint8_t  requestLength 
)

Parses provided PDU and generates PDU for the response frame.

Parameters
requestpointer to the PDU data
requestLengthlength of the PDU (valid range: 1 - 253)
Returns
MODBUS_REQUEST_ERROR(LENGTH) if length of the frame is invalid
Any errors from parsing functions
Warning
The response frame can only be accessed if modbusIsOk() called on the return value from this function evaluates to true.
The requestLength argument is of type uint8_t and not uint16_t as in case of Modbus RTU and TCP.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusParseRequestRTU()

ModbusErrorInfo modbusParseRequestRTU ( ModbusSlave status,
uint8_t  slaveAddress,
const uint8_t *  request,
uint16_t  requestLength 
)

Parses provided Modbus RTU request frame and generates a Modbus RTU response.

Parameters
slaveAddressID of the slave to match with the request
requestpointer to a Modbus RTU frame
requestLengthlength of the frame (valid range: 4 - 256)
Returns
MODBUS_REQUEST_ERROR(LENGTH) if length of the frame is invalid
MODBUS_REQUEST_ERROR(CRC) if CRC is invalid
MODBUS_REQUEST_ERROR(ADDRESS) if the request is meant for other slave
MODBUS_GENERAL_ERROR(LENGTH) if the resulting response frame has invalid length
Any errors from parsing functions
Warning
The response frame can only be accessed if modbusIsOk() called on the return value of this function evaluates to true.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusParseRequestTCP()

ModbusErrorInfo modbusParseRequestTCP ( ModbusSlave status,
const uint8_t *  request,
uint16_t  requestLength 
)

Parses provided Modbus TCP request frame and generates a Modbus TCP response.

Parameters
requestpointer to a Modbus TCP frame
requestLengthlength of the frame (valid range: 8 - 260)
Returns
MODBUS_REQUEST_ERROR(LENGTH) if length of the frame is invalid or different from the declared one
MODBUS_REQUEST_ERROR(BAD_PROTOCOL) if the frame is not a Modbus TCP message
MODBUS_GENERAL_ERROR(LENGTH) if the resulting response frame has invalid length
Any errors from parsing functions
Warning
The response frame can only be accessed if modbusIsOk() called on the return value of this function evaluates to true.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusSlaveAllocateResponse()

static ModbusError modbusSlaveAllocateResponse ( ModbusSlave status,
uint16_t  pduSize 
)
inlinestatic

Allocates memory for slave's response frame.

Parameters
pduSizesize of the PDU section. 0 if the slave doesn't want to respond.
Returns
MODBUS_ERROR_ALLOC on allocation failure
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusSlaveDestroy()

void modbusSlaveDestroy ( ModbusSlave status)

Frees memory allocated in the ModbusSlave struct.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusSlaveFreeResponse()

static void modbusSlaveFreeResponse ( ModbusSlave status)
inlinestatic

Frees memory allocated for slave's response frame.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusSlaveGetResponse()

static const uint8_t * modbusSlaveGetResponse ( const ModbusSlave status)
inlinestatic

Returns a pointer to the response generated by the slave.

Note
This function can only be used if the last call to modbusParseRequest*() was successful (i.e. modbusIsOk() was true for the return value)
+ Here is the caller graph for this function:

◆ modbusSlaveGetResponseLength()

static uint16_t modbusSlaveGetResponseLength ( const ModbusSlave status)
inlinestatic

Returns the length of the response generated by the slave.

Note
This function can only be used if the last call to modbusParseRequest*() was successful (i.e. modbusIsOk() was true for the return value)
+ Here is the caller graph for this function:

◆ modbusSlaveGetUserPointer()

static void * modbusSlaveGetUserPointer ( const ModbusSlave status)
inlinestatic

Retreieves the custom context pointer.

+ Here is the caller graph for this function:

◆ modbusSlaveInit()

ModbusErrorInfo modbusSlaveInit ( ModbusSlave status,
ModbusRegisterCallback  registerCallback,
ModbusSlaveExceptionCallback  exceptionCallback,
ModbusAllocator  allocator,
const ModbusSlaveFunctionHandler functions,
uint8_t  functionCount 
)

Initializes slave device.

Parameters
registerCallbackCallback function for handling all register operations (may be required by used parsing functions)
exceptionCallbackCallback function for handling slave exceptions (optional)
allocatorMemory allocator to be used (see modbusDefaultAllocator) (required)
functionsPointer to array of supported function handlers (required). The lifetime of this array must not be shorter than the lifetime of the slave.
functionCountNumber of function handlers in the array (required)
Returns
MODBUS_NO_ERROR() on success
Warning
This function must not be called on an already initialized ModbusSlave struct.
See also
modbusDefaultAllocator()
modbusSlaveDefaultFunctions
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusSlaveSetUserPointer()

static void modbusSlaveSetUserPointer ( ModbusSlave status,
void *  ptr 
)
inlinestatic

Allows user to set the custom context pointer.

+ Here is the caller graph for this function:

Variable Documentation

◆ modbusSlaveDefaultFunctionCount

const uint8_t modbusSlaveDefaultFunctionCount
extern

Stores length of modbusSlaveDefaultFunctions.

◆ modbusSlaveDefaultFunctions

ModbusSlaveFunctionHandler modbusSlaveDefaultFunctions[]
extern

Associates function IDs with pointers to functions responsible for parsing. Length of this array is stored in modbusSlaveDefaultFunctionCount.

Note
Contents depend on defined LIGHTMODBUS_FxxS macros!