liblightmodbus 3.0
A lightweight, header-only, hardware-agnostic Modbus RTU/TCP library
Loading...
Searching...
No Matches
slave.h
Go to the documentation of this file.
1#ifndef LIGHTMODBUS_SLAVE_H
2#define LIGHTMODBUS_SLAVE_H
3
4#include <stdint.h>
5#include <stddef.h>
6#include "base.h"
7
13typedef struct ModbusSlave ModbusSlave;
14
19 ModbusSlave *status,
20 uint8_t function,
21 const uint8_t *requestPDU,
22 uint8_t requestLength);
23
32
43
55
64
70 const ModbusSlave *status,
73
79 const ModbusSlave *status,
80 uint8_t function,
82
101
102
104 ModbusSlave *status,
105 ModbusRegisterCallback registerCallback,
106 ModbusSlaveExceptionCallback exceptionCallback,
107 ModbusAllocator allocator,
108 const ModbusSlaveFunctionHandler *functions,
109 uint8_t functionCount);
110
111void modbusSlaveDestroy(ModbusSlave *status);
112
114 ModbusSlave *status,
115 uint8_t function,
117
119 ModbusSlave *status,
120 uint8_t function,
122
124 ModbusSlave *status,
125 uint8_t address,
126 uint8_t function,
128
130 ModbusSlave *status,
131 uint16_t transactionID,
132 uint8_t unitID,
133 uint8_t function,
135
136LIGHTMODBUS_RET_ERROR modbusParseRequest(ModbusSlave *status, const uint8_t *request, uint8_t requestLength);
137LIGHTMODBUS_RET_ERROR modbusParseRequestPDU(ModbusSlave *status, const uint8_t *request, uint8_t requestLength);
138LIGHTMODBUS_RET_ERROR modbusParseRequestRTU(ModbusSlave *status, uint8_t slaveAddress, const uint8_t *request, uint16_t requestLength);
139LIGHTMODBUS_RET_ERROR modbusParseRequestTCP(ModbusSlave *status, const uint8_t *request, uint16_t requestLength);
140
147LIGHTMODBUS_WARN_UNUSED static inline const uint8_t *modbusSlaveGetResponse(const ModbusSlave *status)
148{
149 return status->response.data;
150}
151
159{
160 return status->response.length;
161}
162
166static inline void modbusSlaveSetUserPointer(ModbusSlave *status, void *ptr)
167{
168 status->context = ptr;
169}
170
174static inline void *modbusSlaveGetUserPointer(const ModbusSlave *status)
175{
176 return status->context;
177}
178
185{
186 return modbusBufferAllocateADU(&status->response, pduSize, modbusSlaveGetUserPointer(status));
187}
188
192static inline void modbusSlaveFreeResponse(ModbusSlave *status)
193{
195}
196
198extern const uint8_t modbusSlaveDefaultFunctionCount;
199
200#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
ModbusSlaveFunctionHandler modbusSlaveDefaultFunctions[]
Associates function IDs with pointers to functions responsible for parsing. Length of this array is s...
Definition slave.impl.h:18
static void modbusSlaveSetUserPointer(ModbusSlave *status, void *ptr)
Allows user to set the custom context pointer.
Definition slave.h:166
ModbusErrorInfo modbusBuildExceptionRTU(ModbusSlave *status, uint8_t address, uint8_t function, ModbusExceptionCode code)
Builds a Modbus RTU exception.
Definition slave.impl.h:168
const uint8_t modbusSlaveDefaultFunctionCount
Stores length of modbusSlaveDefaultFunctions.
Definition slave.impl.h:63
ModbusErrorInfo modbusSlaveInit(ModbusSlave *status, ModbusRegisterCallback registerCallback, ModbusSlaveExceptionCallback exceptionCallback, ModbusAllocator allocator, const ModbusSlaveFunctionHandler *functions, uint8_t functionCount)
Initializes slave device.
Definition slave.impl.h:79
ModbusErrorInfo modbusBuildExceptionPDU(ModbusSlave *status, uint8_t function, ModbusExceptionCode code)
Builds an exception PDU.
Definition slave.impl.h:143
ModbusErrorInfo(* ModbusRequestParsingFunction)(ModbusSlave *status, uint8_t function, const uint8_t *requestPDU, uint8_t requestLength)
A pointer to request parsing function.
Definition slave.h:18
ModbusErrorInfo modbusBuildException(ModbusSlave *status, uint8_t function, ModbusExceptionCode code)
Builds an exception response frame.
Definition slave.impl.h:118
ModbusErrorInfo modbusParseRequestTCP(ModbusSlave *status, const uint8_t *request, uint16_t requestLength)
Parses provided Modbus TCP request frame and generates a Modbus TCP response.
Definition slave.impl.h:362
static void * modbusSlaveGetUserPointer(const ModbusSlave *status)
Retreieves the custom context pointer.
Definition slave.h:174
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.
Definition slave.impl.h:299
void modbusSlaveDestroy(ModbusSlave *status)
Frees memory allocated in the ModbusSlave struct.
Definition slave.impl.h:99
ModbusErrorInfo modbusBuildExceptionTCP(ModbusSlave *status, uint16_t transactionID, uint8_t unitID, uint8_t function, ModbusExceptionCode code)
Builds a Modbus TCP exception.
Definition slave.impl.h:207
static void modbusSlaveFreeResponse(ModbusSlave *status)
Frees memory allocated for slave's response frame.
Definition slave.h:192
static uint16_t modbusSlaveGetResponseLength(const ModbusSlave *status)
Returns the length of the response generated by the slave.
Definition slave.h:158
ModbusErrorInfo modbusParseRequestPDU(ModbusSlave *status, const uint8_t *request, uint8_t requestLength)
Parses provided PDU and generates PDU for the response frame.
Definition slave.impl.h:275
ModbusError(* ModbusRegisterCallback)(const ModbusSlave *status, const ModbusRegisterCallbackArgs *args, ModbusRegisterCallbackResult *out)
A pointer to callback for performing all register operations.
Definition slave.h:69
ModbusError(* ModbusSlaveExceptionCallback)(const ModbusSlave *status, uint8_t function, ModbusExceptionCode code)
A pointer to a callback called when a Modbus exception is generated (for slave)
Definition slave.h:78
static ModbusError modbusSlaveAllocateResponse(ModbusSlave *status, uint16_t pduSize)
Allocates memory for slave's response frame.
Definition slave.h:184
static const uint8_t * modbusSlaveGetResponse(const ModbusSlave *status)
Returns a pointer to the response generated by the slave.
Definition slave.h:147
ModbusRegisterQuery
Determines type of request made to the register callback function.
Definition slave.h:37
@ MODBUS_REGQ_W_CHECK
Request for write access.
Definition slave.h:39
@ MODBUS_REGQ_W
Write request.
Definition slave.h:41
@ MODBUS_REGQ_R_CHECK
Request for read access.
Definition slave.h:38
@ MODBUS_REGQ_R
Read request.
Definition slave.h:40
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 ...
Definition slave.impl.h:249
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
Richer error represenation - source and type of error.
Definition base.h:126
Contains arguments for the register callback function.
Definition slave.h:48
uint16_t value
Value of the register.
Definition slave.h:52
uint8_t function
Function accessing the register.
Definition slave.h:53
ModbusDataType type
Type of accessed data.
Definition slave.h:49
ModbusRegisterQuery query
Type of request made to the register.
Definition slave.h:50
uint16_t index
Index of the register.
Definition slave.h:51
Contains values returned by the slave register callback.
Definition slave.h:60
uint16_t value
Register/coil value.
Definition slave.h:62
ModbusExceptionCode exceptionCode
Exception to be reported.
Definition slave.h:61
Associates Modbus function ID with a pointer to a parsing function.
Definition slave.h:28
ModbusRequestParsingFunction ptr
Definition slave.h:30
uint8_t id
Definition slave.h:29
Slave device status.
Definition slave.h:90
uint8_t functionCount
Number of function handlers in the array (functions)
Definition slave.h:94
ModbusSlaveExceptionCallback exceptionCallback
A pointer to exception callback (optional)
Definition slave.h:92
const ModbusSlaveFunctionHandler * functions
A pointer to an array of function handlers (required)
Definition slave.h:93
ModbusRegisterCallback registerCallback
A pointer to register callback (required)
Definition slave.h:91
void * context
User's context pointer
Definition slave.h:99
ModbusBuffer response
Stores slave's response to master.
Definition slave.h:97