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

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

#include "master.h"
#include "master_func.h"
+ Include dependency graph for master.impl.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

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 transactionID, uint8_t unitID)
 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.
 

Variables

ModbusMasterFunctionHandler modbusMasterDefaultFunctions []
 Default array of supported functions. Length is stored in modbusMasterDefaultFunctionCount.
 
const uint8_t modbusMasterDefaultFunctionCount = sizeof(modbusMasterDefaultFunctions) / sizeof(modbusMasterDefaultFunctions[0]) - 1
 Stores length of modbusMasterDefaultFunctions array.
 

Detailed Description

Master's types and basic functions (implementation)

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:

◆ 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:

◆ 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:

◆ 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 = sizeof(modbusMasterDefaultFunctions) / sizeof(modbusMasterDefaultFunctions[0]) - 1

Stores length of modbusMasterDefaultFunctions array.

◆ modbusMasterDefaultFunctions

ModbusMasterFunctionHandler modbusMasterDefaultFunctions[]

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

Contents are controlled by defining LIGHTMODBUS_FxxM macros.