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

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

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

Go to the source code of this file.

Data Structures

struct  ModbusMasterFunctionHandler
 Associates Modbus function ID with a pointer to a response parsing function. More...
 
struct  ModbusDataCallbackArgs
 Arguments for the data callback. More...
 
struct  ModbusMaster
 Master device status. More...
 

Typedefs

typedef struct ModbusMaster ModbusMaster
 
typedef ModbusErrorInfo(* ModbusResponseParsingFunction) (ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)
 A pointer to a response parsing function.
 
typedef struct ModbusMasterFunctionHandler ModbusMasterFunctionHandler
 Associates Modbus function ID with a pointer to a response parsing function.
 
typedef struct ModbusDataCallbackArgs ModbusDataCallbackArgs
 Arguments for the data callback.
 
typedef ModbusError(* ModbusDataCallback) (const ModbusMaster *status, const ModbusDataCallbackArgs *args)
 A pointer to a callback used for handling data incoming to master.
 
typedef ModbusError(* ModbusMasterExceptionCallback) (const ModbusMaster *status, uint8_t address, uint8_t function, ModbusExceptionCode code)
 A pointer to a callback called when a Modbus exception is generated (for master)
 

Functions

ModbusErrorInfo modbusMasterInit (ModbusMaster *status, ModbusDataCallback dataCallback, ModbusMasterExceptionCallback exceptionCallback, ModbusAllocator allocator, const ModbusMasterFunctionHandler *functions, uint8_t functionCount)
 Initializes a ModbusMaster struct.
 
void modbusMasterDestroy (ModbusMaster *status)
 Deinitializes a ModbusMaster struct.
 
ModbusErrorInfo modbusBeginRequestPDU (ModbusMaster *status)
 Begins a PDU-only request.
 
ModbusErrorInfo modbusEndRequestPDU (ModbusMaster *status)
 Finalizes a PDU-only request.
 
ModbusErrorInfo modbusBeginRequestRTU (ModbusMaster *status)
 Begins a RTU request.
 
ModbusErrorInfo modbusEndRequestRTU (ModbusMaster *status, uint8_t address)
 Finalizes a Modbus RTU request.
 
ModbusErrorInfo modbusBeginRequestTCP (ModbusMaster *status)
 Begins a TCP request.
 
ModbusErrorInfo modbusEndRequestTCP (ModbusMaster *status, uint16_t transaction, uint8_t unit)
 Finalizes a Modbus TCP request.
 
ModbusErrorInfo modbusParseResponsePDU (ModbusMaster *status, uint8_t address, const uint8_t *request, uint8_t requestLength, const uint8_t *response, uint8_t responseLength)
 Parses a PDU section of a slave response.
 
ModbusErrorInfo modbusParseResponseRTU (ModbusMaster *status, const uint8_t *request, uint16_t requestLength, const uint8_t *response, uint16_t responseLength)
 Parses a Modbus RTU slave response.
 
ModbusErrorInfo modbusParseResponseTCP (ModbusMaster *status, const uint8_t *request, uint16_t requestLength, const uint8_t *response, uint16_t responseLength)
 Parses a Modbus TCP slave response.
 
static const uint8_t * modbusMasterGetRequest (const ModbusMaster *status)
 Returns a pointer to the request generated by the master.
 
static uint16_t modbusMasterGetRequestLength (const ModbusMaster *status)
 Returns the length of the request generated by the master.
 
static void modbusMasterSetUserPointer (ModbusMaster *status, void *ptr)
 Allows user to set the custom context pointer.
 
static void * modbusMasterGetUserPointer (const ModbusMaster *status)
 Retreieves the custom context pointer.
 
static ModbusError modbusMasterAllocateRequest (ModbusMaster *status, uint16_t pduSize)
 Allocates memory for the request frame.
 
static void modbusMasterFreeRequest (ModbusMaster *status)
 Frees memory allocated for master's request frame.
 

Variables

ModbusMasterFunctionHandler modbusMasterDefaultFunctions []
 Default array of supported functions. Length is stored in modbusMasterDefaultFunctionCount.
 
const uint8_t modbusMasterDefaultFunctionCount
 Stores length of modbusMasterDefaultFunctions array.
 

Detailed Description

Master's types and basic functions (header)

Typedef Documentation

◆ ModbusDataCallback

typedef ModbusError(* ModbusDataCallback) (const ModbusMaster *status, const ModbusDataCallbackArgs *args)

A pointer to a callback used for handling data incoming to master.

See also
master-data-callback

◆ ModbusDataCallbackArgs

Arguments for the data callback.

◆ ModbusMaster

typedef struct ModbusMaster ModbusMaster

◆ ModbusMasterExceptionCallback

typedef ModbusError(* ModbusMasterExceptionCallback) (const ModbusMaster *status, uint8_t address, uint8_t function, ModbusExceptionCode code)

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

See also
master-exception-callback

◆ ModbusMasterFunctionHandler

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

◆ ModbusResponseParsingFunction

typedef ModbusErrorInfo(* ModbusResponseParsingFunction) (ModbusMaster *status, uint8_t address, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength, const uint8_t *responsePDU, uint8_t responseLength)

A pointer to a response parsing function.

Function Documentation

◆ modbusBeginRequestPDU()

ModbusErrorInfo modbusBeginRequestPDU ( ModbusMaster status)

Begins a PDU-only request.

Returns
MODBUS_NO_ERROR()
+ Here is the call graph for this function:

◆ modbusBeginRequestRTU()

ModbusErrorInfo modbusBeginRequestRTU ( ModbusMaster status)

Begins a RTU request.

Returns
MODBUS_NO_ERROR()
+ Here is the call graph for this function:

◆ modbusBeginRequestTCP()

ModbusErrorInfo modbusBeginRequestTCP ( ModbusMaster status)

Begins a TCP request.

Returns
MODBUS_NO_ERROR()
+ Here is the call graph for this function:

◆ modbusEndRequestPDU()

ModbusErrorInfo modbusEndRequestPDU ( ModbusMaster status)

Finalizes a PDU-only request.

Returns
MODBUS_NO_ERROR()

◆ modbusEndRequestRTU()

ModbusErrorInfo modbusEndRequestRTU ( ModbusMaster status,
uint8_t  address 
)

Finalizes a Modbus RTU request.

Returns
MODBUS_GENERAL_ERROR(LENGTH) if the allocated frame has invalid length
MODBUS_NO_ERROR() on success
+ Here is the call graph for this function:

◆ modbusEndRequestTCP()

ModbusErrorInfo modbusEndRequestTCP ( ModbusMaster status,
uint16_t  transactionID,
uint8_t  unitID 
)

Finalizes a Modbus TCP request.

Parameters
transactionIDModbus TCP transaction identifier
unitIDModbus TCP Unit ID
Returns
MODBUS_GENERAL_ERROR(LENGTH) if the allocated frame has invalid length
MODBUS_NO_ERROR() on success
+ Here is the call graph for this function:

◆ modbusMasterAllocateRequest()

static ModbusError modbusMasterAllocateRequest ( ModbusMaster status,
uint16_t  pduSize 
)
inlinestatic

Allocates memory for the request frame.

Parameters
pduSizesize of the PDU section of the frame. 0 implies no request at all.
Returns
MODBUS_ERROR_ALLOC on allocation failure
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusMasterDestroy()

void modbusMasterDestroy ( ModbusMaster status)

Deinitializes a ModbusMaster struct.

Parameters
statusModbusMaster struct to be destroyed
Note
This does not free the memory pointed to by the status pointer and only cleans up the interals ofthe ModbusMaster struct.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusMasterFreeRequest()

static void modbusMasterFreeRequest ( ModbusMaster status)
inlinestatic

Frees memory allocated for master's request frame.

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

◆ modbusMasterGetRequest()

static const uint8_t * modbusMasterGetRequest ( const ModbusMaster status)
inlinestatic

Returns a pointer to the request generated by the master.

+ Here is the caller graph for this function:

◆ modbusMasterGetRequestLength()

static uint16_t modbusMasterGetRequestLength ( const ModbusMaster status)
inlinestatic

Returns the length of the request generated by the master.

+ Here is the caller graph for this function:

◆ modbusMasterGetUserPointer()

static void * modbusMasterGetUserPointer ( const ModbusMaster status)
inlinestatic

Retreieves the custom context pointer.

+ Here is the caller graph for this function:

◆ modbusMasterInit()

ModbusErrorInfo modbusMasterInit ( ModbusMaster status,
ModbusDataCallback  dataCallback,
ModbusMasterExceptionCallback  exceptionCallback,
ModbusAllocator  allocator,
const ModbusMasterFunctionHandler functions,
uint8_t  functionCount 
)

Initializes a ModbusMaster struct.

Parameters
statusModbusMaster struct to be initialized
dataCallbackCallback function for handling incoming data (may be required by used parsing functions)
exceptionCallbackCallback function for handling slave exceptions (optional)
allocatorMemory allocator to be used (see modbusDefaultAllocator()) (required)
functionsPointer to an array of supported function handlers (required). The lifetime of this array must not be shorter than the lifetime of the master.
functionCountNumber of elements in the functions array (required)
Returns
MODBUS_NO_ERROR() on success
See also
modbusDefaultAllocator()
modbusMasterDefaultFunctions
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusMasterSetUserPointer()

static void modbusMasterSetUserPointer ( ModbusMaster status,
void *  ptr 
)
inlinestatic

Allows user to set the custom context pointer.

+ Here is the caller graph for this function:

◆ modbusParseResponsePDU()

ModbusErrorInfo modbusParseResponsePDU ( ModbusMaster status,
uint8_t  address,
const uint8_t *  request,
uint8_t  requestLength,
const uint8_t *  response,
uint8_t  responseLength 
)

Parses a PDU section of a slave response.

Parameters
addressValue to be reported as slave address
requestPointer to the PDU section of the request frame
requestLengthLength of the request PDU (valid range: 1 - 253)
responsePointer to the PDU section of the response
responseLengthLength of the response PDU (valid range: 1 - 253)
Returns
MODBUS_REQUEST_ERROR(LENGTH) if the request has invalid length
MODBUS_RESPONSE_ERROR(LENGTH) if the response has invalid length
MODBUS_RESPONSE_ERROR(FUNCTION) if the function code in request doesn't match the one in response
MODBUS_GENERAL_ERROR(FUNCTION) if the function code is not supported
Result from the parsing function on success (modbusParseRequest*() functions)
+ Here is the caller graph for this function:

◆ modbusParseResponseRTU()

ModbusErrorInfo modbusParseResponseRTU ( ModbusMaster status,
const uint8_t *  request,
uint16_t  requestLength,
const uint8_t *  response,
uint16_t  responseLength 
)

Parses a Modbus RTU slave response.

Parameters
requestPointer to the request frame
requestLengthLength of the request (valid range: 4 - 256)
responsePointer to the response frame
responseLengthLength of the response (valid range: 4 - 256)
Returns
MODBUS_REQUEST_ERROR(LENGTH) if the request has invalid length
MODBUS_RESPONSE_ERROR(LENGTH) if the response has invalid length
MODBUS_REQUEST_ERROR(CRC) if the request CRC is invalid
MODBUS_RESPONSE_ERROR(CRC) if the response CRC is invalid
MODBUS_RESPONSE_ERROR(ADDRESS) if the address is 0 or if request/response addressess don't match
Result of modbusParseResponsePDU() otherwise
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusParseResponseTCP()

ModbusErrorInfo modbusParseResponseTCP ( ModbusMaster status,
const uint8_t *  request,
uint16_t  requestLength,
const uint8_t *  response,
uint16_t  responseLength 
)

Parses a Modbus TCP slave response.

Parameters
requestPointer to the request frame
requestLengthLength of the request (valid range: 8 - 260)
responsePointer to the response frame
responseLengthLength of the response (valid range: 8 - 260)
Returns
MODBUS_REQUEST_ERROR(LENGTH) if the request has invalid length or if the request frame has different from declared one
MODBUS_RESPONSE_ERROR(LENGTH) if the response has invalid length or if the response frame has different from declared one
MODBUS_REQUEST_ERROR(BAD_PROTOCOL) if the protocol ID in request is not 0
MODBUS_RESPONSE_ERROR(BAD_PROTOCOL) if the protocol ID in response is not 0
MODBUS_RESPONSE_ERROR(BAD_TRANSACTION) if the transaction ID in request is not the same as in response
MODBUS_RESPONSE_ERROR(ADDRESS) if the address in response is not the same as in request
Result of modbusParseResponsePDU() otherwise
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ modbusMasterDefaultFunctionCount

const uint8_t modbusMasterDefaultFunctionCount
extern

Stores length of modbusMasterDefaultFunctions array.

◆ modbusMasterDefaultFunctions

ModbusMasterFunctionHandler modbusMasterDefaultFunctions[]
extern

Default array of supported functions. Length is stored in modbusMasterDefaultFunctionCount.

Contents are controlled by defining LIGHTMODBUS_FxxM macros.