5#ifndef LIGHTMODBUS_DEBUG
6#define LIGHTMODBUS_DEBUG
35 const char *
what() const noexcept
override
55 const char *
what() const noexcept
override
75 const char *
what() const noexcept
override
101#ifdef LIGHTMODBUS_SLAVE
212#ifdef LIGHTMODBUS_MASTER
214#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_PDU_HEADER(f_suffix, ...) \
215 void buildRequest##f_suffix##PDU(__VA_ARGS__)
217#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_PDU_BODY(f_suffix, ...) \
219 ModbusErrorInfo err = modbusBuildRequest##f_suffix##PDU(&m_master, __VA_ARGS__); \
220 m_ok = modbusIsOk(err); \
221 throwErrorInfo(err); \
224#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_RTU_HEADER(f_suffix, ...) \
225 void buildRequest##f_suffix##RTU(uint8_t address, __VA_ARGS__)
227#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_RTU_BODY(f_suffix, ...) \
229 ModbusErrorInfo err = modbusBuildRequest##f_suffix##RTU(&m_master, address, __VA_ARGS__); \
230 m_ok = modbusIsOk(err); \
231 throwErrorInfo(err); \
234#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_TCP_HEADER(f_suffix, ...) \
235 void buildRequest##f_suffix##TCP(uint16_t transactionID, uint8_t unitID, __VA_ARGS__)
237#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_TCP_BODY(f_suffix, ...) \
239 ModbusErrorInfo err = modbusBuildRequest##f_suffix##TCP(&m_master, transactionID, unitID, __VA_ARGS__); \
240 m_ok = modbusIsOk(err); \
241 throwErrorInfo(err); \
280 const uint8_t *request,
281 uint8_t requestLength,
282 const uint8_t *response,
283 uint8_t responseLength)
297 const uint8_t *request,
298 uint8_t requestLength,
299 const uint8_t *response,
300 uint8_t responseLength)
313 const uint8_t *request,
314 uint8_t requestLength,
315 const uint8_t *response,
316 uint8_t responseLength)
ModbusExceptionCode
Represents a Modbus exception code.
Definition base.h:230
ModbusError
Represtents different kinds of errors.
Definition base.h:137
@ MODBUS_ERROR_OTHER
Other error.
Definition base.h:223
@ MODBUS_OK
No error.
Definition base.h:143
static ModbusError modbusGetRequestError(ModbusErrorInfo err)
Returns request error from ModbusErrorInfo.
Definition base.h:462
static uint8_t modbusIsOk(ModbusErrorInfo err)
Checks if ModbusErrorInfo contains an error.
Definition base.h:442
static ModbusError modbusGetGeneralError(ModbusErrorInfo err)
Returns general error from ModbusErrorInfo.
Definition base.h:452
ModbusError modbusDefaultAllocator(ModbusBuffer *buffer, uint16_t size, void *context)
The default memory allocator based on realloc()
Definition base.impl.h:21
ModbusError(* ModbusAllocator)(struct ModbusBuffer *buffer, uint16_t size, void *context)
Pointer to a memory allocator function.
Definition base.h:259
static ModbusError modbusGetResponseError(ModbusErrorInfo err)
Returns response error from ModbusErrorInfo.
Definition base.h:472
General error exception class.
Definition lightmodbus.hpp:68
const char * what() const noexcept override
Definition lightmodbus.hpp:75
GeneralError(ModbusError err)
Definition lightmodbus.hpp:70
Represents a Modbus master device.
Definition lightmodbus.hpp:249
uint16_t getRequestLength() const
Definition lightmodbus.hpp:391
void parseResponsePDU(uint8_t address, const uint8_t *request, uint8_t requestLength, const uint8_t *response, uint8_t responseLength)
Definition lightmodbus.hpp:278
void setUserPointer(void *ptr)
Definition lightmodbus.hpp:403
Master & operator=(Master &&)=delete
void freeRequest()
Definition lightmodbus.hpp:397
const uint8_t * getRequest() const
Definition lightmodbus.hpp:385
ModbusMaster m_master
Definition lightmodbus.hpp:413
void parseResponseRTU(const uint8_t *request, uint8_t requestLength, const uint8_t *response, uint8_t responseLength)
Definition lightmodbus.hpp:296
void * getUserPointer() const
Definition lightmodbus.hpp:408
Master(ModbusDataCallback dataCallback, ModbusMasterExceptionCallback exceptionCallback=nullptr, ModbusAllocator allocator=modbusDefaultAllocator, ModbusMasterFunctionHandler *functions=modbusMasterDefaultFunctions, uint16_t functionCount=modbusMasterDefaultFunctionCount)
Definition lightmodbus.hpp:251
Master(const Master &)=delete
bool m_ok
Definition lightmodbus.hpp:414
void parseResponseTCP(const uint8_t *request, uint8_t requestLength, const uint8_t *response, uint8_t responseLength)
Definition lightmodbus.hpp:312
~Master()
Definition lightmodbus.hpp:267
Master & operator=(const Master &)=delete
Request error exception class.
Definition lightmodbus.hpp:28
const char * what() const noexcept override
Definition lightmodbus.hpp:35
RequestError(ModbusError err)
Definition lightmodbus.hpp:30
Response error exception class.
Definition lightmodbus.hpp:48
ResponseError(ModbusError err)
Definition lightmodbus.hpp:50
const char * what() const noexcept override
Definition lightmodbus.hpp:55
Represents a Modbus slave device.
Definition lightmodbus.hpp:107
void parseRequestRTU(uint8_t address, const uint8_t *frame, uint16_t length)
Definition lightmodbus.hpp:143
void parseRequestTCP(const uint8_t *frame, uint16_t length)
Definition lightmodbus.hpp:150
void buildExceptionRTU(uint8_t address, uint8_t function, ModbusExceptionCode code)
Definition lightmodbus.hpp:164
Slave & operator=(Slave &&)=delete
bool m_ok
Definition lightmodbus.hpp:208
Slave & operator=(const Slave &)=delete
~Slave()
Definition lightmodbus.hpp:125
const uint8_t * getResponse() const
Definition lightmodbus.hpp:178
Slave(ModbusRegisterCallback registerCallback, ModbusSlaveExceptionCallback exceptionCallback=nullptr, ModbusAllocator allocator=modbusDefaultAllocator, ModbusSlaveFunctionHandler *functions=modbusSlaveDefaultFunctions, uint16_t functionCount=modbusSlaveDefaultFunctionCount)
Definition lightmodbus.hpp:109
void buildExceptionTCP(uint16_t transactionID, uint8_t unitID, uint8_t function, ModbusExceptionCode code)
Definition lightmodbus.hpp:171
Slave(const Slave &)=delete
void freeResponse()
Definition lightmodbus.hpp:190
uint16_t getResponseLength() const
Definition lightmodbus.hpp:184
ModbusSlave m_slave
Definition lightmodbus.hpp:207
void buildExceptionPDU(uint8_t function, ModbusExceptionCode code)
Definition lightmodbus.hpp:157
void setUserPointer(void *ptr)
Definition lightmodbus.hpp:196
void * getUserPointer() const
Definition lightmodbus.hpp:201
void parseRequestPDU(const uint8_t *frame, uint16_t length)
Definition lightmodbus.hpp:136
const char * modbusErrorStr(ModbusError err)
Returns a string containing the name of the ModbusError value.
Definition debug.impl.h:26
The main library header file (include this one)
#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_PDU_HEADER(f_suffix,...)
Definition lightmodbus.hpp:214
#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_PDU_BODY(f_suffix,...)
Definition lightmodbus.hpp:217
#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_TCP_BODY(f_suffix,...)
Definition lightmodbus.hpp:237
#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_RTU_BODY(f_suffix,...)
Definition lightmodbus.hpp:227
#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_RTU_HEADER(f_suffix,...)
Definition lightmodbus.hpp:224
#define LIGHTMODBUS_DEFINE_MEMBER_BUILD_TCP_HEADER(f_suffix,...)
Definition lightmodbus.hpp:234
static void modbusMasterFreeRequest(ModbusMaster *status)
Frees memory allocated for master's request frame.
Definition master.h:172
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 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
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 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 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
The liblightmodbus C++ namepsace.
Definition lightmodbus.hpp:21
static void throwErrorInfo(ModbusErrorInfo err)
Throws an exception based on ModbusErrorInfo.
Definition lightmodbus.hpp:91
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 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 const uint8_t * modbusSlaveGetResponse(const ModbusSlave *status)
Returns a pointer to the response generated by the slave.
Definition slave.h:147
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
Master device status.
Definition master.h:73
Associates Modbus function ID with a pointer to a parsing function.
Definition slave.h:28
Slave device status.
Definition slave.h:90