liblightmodbus 3.0
A lightweight, header-only, hardware-agnostic Modbus RTU/TCP library
Loading...
Searching...
No Matches
master.h
Go to the documentation of this file.
1#ifndef LIGHTMODBUS_MASTER_H
2#define LIGHTMODBUS_MASTER_H
3
4#include <stdint.h>
5#include <stddef.h>
6#include "base.h"
7
14
19 ModbusMaster *status,
20 uint8_t address,
21 uint8_t function,
22 const uint8_t *requestPDU,
23 uint8_t requestLength,
24 const uint8_t *responsePDU,
25 uint8_t responseLength);
26
35
47
53 const ModbusMaster *status,
54 const ModbusDataCallbackArgs *args);
55
61 const ModbusMaster *status,
62 uint8_t address,
63 uint8_t function,
65
85
87 ModbusMaster *status,
88 ModbusDataCallback dataCallback,
89 ModbusMasterExceptionCallback exceptionCallback,
90 ModbusAllocator allocator,
91 const ModbusMasterFunctionHandler *functions,
92 uint8_t functionCount);
93
95
101LIGHTMODBUS_RET_ERROR modbusEndRequestTCP(ModbusMaster *status, uint16_t transaction, uint8_t unit);
102
104 ModbusMaster *status,
105 uint8_t address,
106 const uint8_t *request,
107 uint8_t requestLength,
108 const uint8_t *response,
109 uint8_t responseLength);
110
112 ModbusMaster *status,
113 const uint8_t *request,
114 uint16_t requestLength,
115 const uint8_t *response,
116 uint16_t responseLength);
117
119 ModbusMaster *status,
120 const uint8_t *request,
121 uint16_t requestLength,
122 const uint8_t *response,
123 uint16_t responseLength);
124
128LIGHTMODBUS_WARN_UNUSED static inline const uint8_t *modbusMasterGetRequest(const ModbusMaster *status)
129{
130 return status->request.data;
131}
132
137{
138 return status->request.length;
139}
140
144static inline void modbusMasterSetUserPointer(ModbusMaster *status, void *ptr)
145{
146 status->context = ptr;
147}
148
152static inline void *modbusMasterGetUserPointer(const ModbusMaster *status)
153{
154 return status->context;
155}
156
163 ModbusMaster *status,
164 uint16_t pduSize)
165{
166 return modbusBufferAllocateADU(&status->request, pduSize, modbusMasterGetUserPointer(status));
167}
168
172static inline void modbusMasterFreeRequest(ModbusMaster *status)
173{
175}
176
178extern const uint8_t modbusMasterDefaultFunctionCount;
179
180#endif
Common types and functions (header)
void modbusBufferFree(ModbusBuffer *buffer, void *context)
Frees memory allocated inside the buffer.
Definition base.impl.h:68
#define LIGHTMODBUS_WARN_UNUSED
Definition base.h:19
ModbusExceptionCode
Represents a Modbus exception code.
Definition base.h:230
ModbusDataType
Represents different Modbus data types.
Definition base.h:244
ModbusError
Represtents different kinds of errors.
Definition base.h:137
ModbusError modbusBufferAllocateADU(ModbusBuffer *buffer, uint16_t pduSize, void *context)
Allocates memory to hold Modbus ADU.
Definition base.impl.h:98
#define LIGHTMODBUS_RET_ERROR
Return type for library functions returning ModbusErrorInfo that should be handled properly.
Definition base.h:49
ModbusError(* ModbusAllocator)(struct ModbusBuffer *buffer, uint16_t size, void *context)
Pointer to a memory allocator function.
Definition base.h:259
static ModbusError modbusMasterAllocateRequest(ModbusMaster *status, uint16_t pduSize)
Allocates memory for the request frame.
Definition master.h:162
static void modbusMasterFreeRequest(ModbusMaster *status)
Frees memory allocated for master's request frame.
Definition master.h:172
ModbusErrorInfo modbusEndRequestTCP(ModbusMaster *status, uint16_t transaction, uint8_t unit)
Finalizes a Modbus TCP request.
Definition master.impl.h:171
static void modbusMasterSetUserPointer(ModbusMaster *status, void *ptr)
Allows user to set the custom context pointer.
Definition master.h:144
ModbusErrorInfo modbusParseResponseRTU(ModbusMaster *status, const uint8_t *request, uint16_t requestLength, const uint8_t *response, uint16_t responseLength)
Parses a Modbus RTU slave response.
Definition master.impl.h:260
static uint16_t modbusMasterGetRequestLength(const ModbusMaster *status)
Returns the length of the request generated by the master.
Definition master.h:136
static void * modbusMasterGetUserPointer(const ModbusMaster *status)
Retreieves the custom context pointer.
Definition master.h:152
ModbusErrorInfo modbusEndRequestRTU(ModbusMaster *status, uint8_t address)
Finalizes a Modbus RTU request.
Definition master.impl.h:141
ModbusErrorInfo modbusParseResponseTCP(ModbusMaster *status, const uint8_t *request, uint16_t requestLength, const uint8_t *response, uint16_t responseLength)
Parses a Modbus TCP slave response.
Definition master.impl.h:329
void modbusMasterDestroy(ModbusMaster *status)
Deinitializes a ModbusMaster struct.
Definition master.impl.h:102
static const uint8_t * modbusMasterGetRequest(const ModbusMaster *status)
Returns a pointer to the request generated by the master.
Definition master.h:128
ModbusError(* ModbusDataCallback)(const ModbusMaster *status, const ModbusDataCallbackArgs *args)
A pointer to a callback used for handling data incoming to master.
Definition master.h:52
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.
Definition master.h:18
ModbusErrorInfo modbusEndRequestPDU(ModbusMaster *status)
Finalizes a PDU-only request.
Definition master.impl.h:121
ModbusMasterFunctionHandler modbusMasterDefaultFunctions[]
Default array of supported functions. Length is stored in modbusMasterDefaultFunctionCount.
Definition master.impl.h:18
const uint8_t modbusMasterDefaultFunctionCount
Stores length of modbusMasterDefaultFunctions array.
Definition master.impl.h:63
ModbusErrorInfo modbusBeginRequestPDU(ModbusMaster *status)
Begins a PDU-only request.
Definition master.impl.h:111
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.
Definition master.impl.h:198
ModbusErrorInfo modbusBeginRequestRTU(ModbusMaster *status)
Begins a RTU request.
Definition master.impl.h:130
ModbusErrorInfo modbusMasterInit(ModbusMaster *status, ModbusDataCallback dataCallback, ModbusMasterExceptionCallback exceptionCallback, ModbusAllocator allocator, const ModbusMasterFunctionHandler *functions, uint8_t functionCount)
Initializes a ModbusMaster struct.
Definition master.impl.h:79
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)
Definition master.h:60
ModbusErrorInfo modbusBeginRequestTCP(ModbusMaster *status)
Begins a TCP request.
Definition master.impl.h:158
Stores a Modbus frame.
Definition base.h:276
uint16_t length
Length of the entire frame (PDU size + padding)
Definition base.h:282
uint8_t * data
Pointer to the frame buffer.
Definition base.h:280
Arguments for the data callback.
Definition master.h:40
uint8_t function
Function that reported this value.
Definition master.h:44
uint16_t value
Value of the register.
Definition master.h:43
uint16_t index
Index of the register.
Definition master.h:42
ModbusDataType type
Type of Modbus register.
Definition master.h:41
uint8_t address
Address of the slave.
Definition master.h:45
Richer error represenation - source and type of error.
Definition base.h:126
Associates Modbus function ID with a pointer to a response parsing function.
Definition master.h:31
uint8_t id
Definition master.h:32
ModbusResponseParsingFunction ptr
Definition master.h:33
Master device status.
Definition master.h:73
ModbusDataCallback dataCallback
A pointer to data callback (required)
Definition master.h:74
void * context
User's context pointer.
Definition master.h:83
const ModbusMasterFunctionHandler * functions
A non-owning pointer to array of function handlers.
Definition master.h:77
ModbusMasterExceptionCallback exceptionCallback
A pointer to an exception callback (optional)
Definition master.h:75
ModbusBuffer request
Stores master's request for slave.
Definition master.h:81
uint8_t functionCount
Size of functions array.
Definition master.h:78