liblightmodbus 3.0
A lightweight, header-only, hardware-agnostic Modbus RTU/TCP library
|
Common types and functions (header) More...
#include <stdint.h>
Go to the source code of this file.
Data Structures | |
struct | ModbusErrorInfo |
Richer error represenation - source and type of error. More... | |
struct | ModbusBuffer |
Stores a Modbus frame. More... | |
Macros | |
#define | LIGHTMODBUS_WARN_UNUSED __attribute__((warn_unused_result)) |
#define | LIGHTMODBUS_ALWAYS_INLINE __attribute__((always_inline)) |
#define | MODBUS_PDU_MIN 1 |
Minimum length of a PDU. | |
#define | MODBUS_PDU_MAX 253 |
Maximum length of a PDU. | |
#define | MODBUS_RTU_ADU_MIN 4 |
Minimum length of ADU in Modbus RTU. | |
#define | MODBUS_RTU_ADU_MAX 256 |
Maximum length of ADU in Modbus RTU. | |
#define | MODBUS_RTU_ADU_PADDING 3 |
Number of extra bytes added to the PDU in Modbus RTU. | |
#define | MODBUS_RTU_PDU_OFFSET 1 |
Offset of PDU relative to the frame beginning in Modbus RTU. | |
#define | MODBUS_TCP_ADU_MIN 8 |
Minimum length of ADU in Modbus TCP. | |
#define | MODBUS_TCP_ADU_MAX 260 |
Maximum length of ADU in Modbus TCP. | |
#define | MODBUS_TCP_ADU_PADDING 7 |
Number of extra bytes added to the PDU in Modbus TCP. | |
#define | MODBUS_TCP_PDU_OFFSET 7 |
Offset of PDU relative to the frame beginning in Modbus TCP. | |
#define | LIGHTMODBUS_RET_ERROR LIGHTMODBUS_WARN_UNUSED ModbusErrorInfo |
Return type for library functions returning ModbusErrorInfo that should be handled properly. | |
#define | MODBUS_ERROR_SOURCE_GENERAL 0u |
General library error - can be caused by providing an incorrect argument or a internal library error. | |
#define | MODBUS_ERROR_SOURCE_REQUEST 1u |
The request frame contains errors. | |
#define | MODBUS_ERROR_SOURCE_RESPONSE 2u |
The response frame contains errors. | |
#define | MODBUS_ERROR_SOURCE_RESERVED 3u |
Reserved for future use. | |
#define | MODBUS_MAKE_ERROR(s, e) ((ModbusErrorInfo){.source = (s), .error = (e)}) |
Constructs a ModbusErrorInfo object from a ModbusErrorCode and a MODBUS_ERROR_SOURCE_* macro. | |
#define | MODBUS_NO_ERROR() MODBUS_MAKE_ERROR(MODBUS_ERROR_SOURCE_GENERAL, MODBUS_OK) |
Construcs a ModbusErrorInfo object for which modbusIsOK() is guaranteed to return true. | |
#define | MODBUS_GENERAL_ERROR(e) MODBUS_MAKE_ERROR(MODBUS_ERROR_SOURCE_GENERAL, (MODBUS_ERROR_##e)) |
Constructs a ModbusErrorInfo where source is set to MODBUS_ERROR_SOURCE_GENERAL and the error code is set to MODBUS_ERROR_##e . | |
#define | MODBUS_REQUEST_ERROR(e) MODBUS_MAKE_ERROR(MODBUS_ERROR_SOURCE_REQUEST, (MODBUS_ERROR_##e)) |
Constructs a ModbusErrorInfo where source is set to MODBUS_ERROR_SOURCE_REQUESTL and the error code is set to MODBUS_ERROR_##e . | |
#define | MODBUS_RESPONSE_ERROR(e) MODBUS_MAKE_ERROR(MODBUS_ERROR_SOURCE_RESPONSE, (MODBUS_ERROR_##e)) |
Constructs a ModbusErrorInfo where source is set to MODBUS_ERROR_SOURCE_RESPONSE and the error code is set to MODBUS_ERROR_##e . | |
Typedefs | |
typedef struct ModbusErrorInfo | ModbusErrorInfo |
Richer error represenation - source and type of error. | |
typedef enum ModbusError | ModbusError |
Represtents different kinds of errors. | |
typedef enum ModbusExceptionCode | ModbusExceptionCode |
Represents a Modbus exception code. | |
typedef enum ModbusDataType | ModbusDataType |
Represents different Modbus data types. | |
typedef ModbusError(* | ModbusAllocator) (struct ModbusBuffer *buffer, uint16_t size, void *context) |
Pointer to a memory allocator function. | |
typedef struct ModbusBuffer | ModbusBuffer |
Stores a Modbus frame. | |
Enumerations | |
enum | ModbusError { MODBUS_OK = 0 , MODBUS_ERROR_OK = 0 , MODBUS_ERROR_LENGTH , MODBUS_ERROR_ALLOC , MODBUS_ERROR_FUNCTION , MODBUS_ERROR_COUNT , MODBUS_ERROR_INDEX , MODBUS_ERROR_VALUE , MODBUS_ERROR_RANGE , MODBUS_ERROR_CRC , MODBUS_ERROR_BAD_PROTOCOL , MODBUS_ERROR_BAD_TRANSACTION , MODBUS_ERROR_ADDRESS , MODBUS_ERROR_OTHER } |
Represtents different kinds of errors. More... | |
enum | ModbusExceptionCode { MODBUS_EXCEP_NONE = 0 , MODBUS_EXCEP_ILLEGAL_FUNCTION = 1 , MODBUS_EXCEP_ILLEGAL_ADDRESS = 2 , MODBUS_EXCEP_ILLEGAL_VALUE = 3 , MODBUS_EXCEP_SLAVE_FAILURE = 4 , MODBUS_EXCEP_ACK = 5 , MODBUS_EXCEP_NACK = 7 } |
Represents a Modbus exception code. More... | |
enum | ModbusDataType { MODBUS_HOLDING_REGISTER = 1 , MODBUS_INPUT_REGISTER = 2 , MODBUS_COIL = 4 , MODBUS_DISCRETE_INPUT = 8 } |
Represents different Modbus data types. More... | |
Functions | |
ModbusError | modbusDefaultAllocator (ModbusBuffer *buffer, uint16_t size, void *context) |
The default memory allocator based on realloc() | |
ModbusErrorInfo | modbusBufferInit (ModbusBuffer *buffer, ModbusAllocator allocator) |
Initializes a buffer for use. | |
void | modbusBufferDestroy (ModbusBuffer *buffer, void *context) |
Equivalent of modbusBufferFree() \copydetail modbusBufferFree() | |
ModbusError | modbusBufferAllocateADU (ModbusBuffer *buffer, uint16_t pduSize, void *context) |
Allocates memory to hold Modbus ADU. | |
void | modbusBufferFree (ModbusBuffer *buffer, void *context) |
Frees memory allocated inside the buffer. | |
uint16_t | modbusCRC (const uint8_t *data, uint16_t length) |
Calculates 16-bit Modbus CRC of provided data. | |
static void | modbusBufferModePDU (ModbusBuffer *buffer) |
Prepares buffer to only store a Modbus PDU. | |
static void | modbusBufferModeRTU (ModbusBuffer *buffer) |
Prepares buffer to store a Modbus RTU message. | |
static void | modbusBufferModeTCP (ModbusBuffer *buffer) |
Prepares buffer to store a Modbus TCP message. | |
static uint8_t | modbusMaskRead (const uint8_t *mask, uint16_t n) |
Reads n-th bit from an array. | |
static void | modbusMaskWrite (uint8_t *mask, uint16_t n, uint8_t value) |
Writes n-th bit in an array. | |
static uint16_t | modbusBitsToBytes (uint16_t n) |
Returns number of bytes necessary to hold given number of bits. | |
static uint16_t | modbusRLE (const uint8_t *p) |
Safely reads a little-endian 16-bit word from provided pointer. | |
static uint16_t | modbusWLE (uint8_t *p, uint16_t val) |
Safely writes a little-endian 16-bit word to provided pointer. | |
static uint16_t | modbusRBE (const uint8_t *p) |
Safely reads a big-endian 16-bit word from provided pointer. | |
static uint16_t | modbusWBE (uint8_t *p, uint16_t val) |
Safely writes a big-endian 16-bit word to provided pointer. | |
static uint8_t | modbusCheckRangeU16 (uint16_t index, uint16_t count) |
Checks whether provided address range causes an uint16_t overflow. | |
static uint8_t | modbusGetErrorSource (ModbusErrorInfo err) |
Returns uint8_t describing error source of ModbusErrorInfo. | |
static ModbusError | modbusGetErrorCode (ModbusErrorInfo err) |
Returns ModbusError contained in ModbusErrorInfo. | |
static uint8_t | modbusIsOk (ModbusErrorInfo err) |
Checks if ModbusErrorInfo contains an error. | |
static ModbusError | modbusGetGeneralError (ModbusErrorInfo err) |
Returns general error from ModbusErrorInfo. | |
static ModbusError | modbusGetRequestError (ModbusErrorInfo err) |
Returns request error from ModbusErrorInfo. | |
static ModbusError | modbusGetResponseError (ModbusErrorInfo err) |
Returns response error from ModbusErrorInfo. | |
static ModbusError | modbusUnpackRTU (const uint8_t *frame, uint16_t length, uint8_t checkCRC, const uint8_t **pdu, uint16_t *pduLength, uint8_t *address) |
Unpacks data from a Modbus RTU frame and optionally checks CRC. | |
static ModbusError | modbusPackRTU (uint8_t *frame, uint16_t length, uint8_t address) |
Sets up address and CRC in a Modbus RTU frame. | |
static ModbusError | modbusUnpackTCP (const uint8_t *frame, uint16_t length, const uint8_t **pdu, uint16_t *pduLength, uint16_t *transactionID, uint8_t *unitID) |
Unpacks data from a Modbus TCP frame. | |
static ModbusError | modbusPackTCP (uint8_t *frame, uint16_t length, uint16_t transactionID, uint8_t unitID) |
Sets up the MBAP header in a Modbus TCP frame. | |
Common types and functions (header)
#define LIGHTMODBUS_ALWAYS_INLINE __attribute__((always_inline)) |
#define LIGHTMODBUS_RET_ERROR LIGHTMODBUS_WARN_UNUSED ModbusErrorInfo |
Return type for library functions returning ModbusErrorInfo that should be handled properly.
#define LIGHTMODBUS_WARN_UNUSED __attribute__((warn_unused_result)) |
#define MODBUS_ERROR_SOURCE_GENERAL 0u |
General library error - can be caused by providing an incorrect argument or a internal library error.
#define MODBUS_ERROR_SOURCE_REQUEST 1u |
The request frame contains errors.
#define MODBUS_ERROR_SOURCE_RESERVED 3u |
Reserved for future use.
#define MODBUS_ERROR_SOURCE_RESPONSE 2u |
The response frame contains errors.
#define MODBUS_GENERAL_ERROR | ( | e | ) | MODBUS_MAKE_ERROR(MODBUS_ERROR_SOURCE_GENERAL, (MODBUS_ERROR_##e)) |
Constructs a ModbusErrorInfo where source is set to MODBUS_ERROR_SOURCE_GENERAL
and the error code is set to MODBUS_ERROR_##e
.
#define MODBUS_MAKE_ERROR | ( | s, | |
e | |||
) | ((ModbusErrorInfo){.source = (s), .error = (e)}) |
Constructs a ModbusErrorInfo object from a ModbusErrorCode and a MODBUS_ERROR_SOURCE_*
macro.
#define MODBUS_NO_ERROR | ( | ) | MODBUS_MAKE_ERROR(MODBUS_ERROR_SOURCE_GENERAL, MODBUS_OK) |
Construcs a ModbusErrorInfo object for which modbusIsOK()
is guaranteed to return true.
#define MODBUS_PDU_MAX 253 |
Maximum length of a PDU.
#define MODBUS_PDU_MIN 1 |
Minimum length of a PDU.
#define MODBUS_REQUEST_ERROR | ( | e | ) | MODBUS_MAKE_ERROR(MODBUS_ERROR_SOURCE_REQUEST, (MODBUS_ERROR_##e)) |
Constructs a ModbusErrorInfo where source is set to MODBUS_ERROR_SOURCE_REQUESTL
and the error code is set to MODBUS_ERROR_##e
.
#define MODBUS_RESPONSE_ERROR | ( | e | ) | MODBUS_MAKE_ERROR(MODBUS_ERROR_SOURCE_RESPONSE, (MODBUS_ERROR_##e)) |
Constructs a ModbusErrorInfo where source is set to MODBUS_ERROR_SOURCE_RESPONSE
and the error code is set to MODBUS_ERROR_##e
.
#define MODBUS_RTU_ADU_MAX 256 |
Maximum length of ADU in Modbus RTU.
#define MODBUS_RTU_ADU_MIN 4 |
Minimum length of ADU in Modbus RTU.
#define MODBUS_RTU_ADU_PADDING 3 |
Number of extra bytes added to the PDU in Modbus RTU.
#define MODBUS_RTU_PDU_OFFSET 1 |
Offset of PDU relative to the frame beginning in Modbus RTU.
#define MODBUS_TCP_ADU_MAX 260 |
Maximum length of ADU in Modbus TCP.
#define MODBUS_TCP_ADU_MIN 8 |
Minimum length of ADU in Modbus TCP.
#define MODBUS_TCP_ADU_PADDING 7 |
Number of extra bytes added to the PDU in Modbus TCP.
#define MODBUS_TCP_PDU_OFFSET 7 |
Offset of PDU relative to the frame beginning in Modbus TCP.
typedef ModbusError(* ModbusAllocator) (struct ModbusBuffer *buffer, uint16_t size, void *context) |
Pointer to a memory allocator function.
Please refer to Custom allocators for more information regarding custom allocator functions.
typedef struct ModbusBuffer ModbusBuffer |
Stores a Modbus frame.
typedef enum ModbusDataType ModbusDataType |
Represents different Modbus data types.
typedef enum ModbusError ModbusError |
Represtents different kinds of errors.
typedef struct ModbusErrorInfo ModbusErrorInfo |
Richer error represenation - source and type of error.
source
and error
directly. The internal implementation of this struct may change in future releases. For this reason, please use modbusGetGeneralError(), modbusGetRequestError(), modbusGetResponseError(), modbusGetErrorSource() and modbusIsOk() instead. typedef enum ModbusExceptionCode ModbusExceptionCode |
Represents a Modbus exception code.
enum ModbusDataType |
enum ModbusError |
Represtents different kinds of errors.
enum ModbusExceptionCode |
Represents a Modbus exception code.
|
inlinestatic |
Returns number of bytes necessary to hold given number of bits.
n | Number of bits |
ModbusError modbusBufferAllocateADU | ( | ModbusBuffer * | buffer, |
uint16_t | pduSize, | ||
void * | context | ||
) |
Allocates memory to hold Modbus ADU.
pduSize | size of the PDU in bytes |
context | context pointer passed on to the allocator |
If called with pduSize == 0, the buffer is freed. Otherwise a buffer for (pduSize + buffer->padding)
bytes is allocated. This guarantees that the buffer is big enough to hold the entire ADU.
This function is responsible for managing data
, pdu
and length
fields in the buffer struct. The pdu
pointer is set up to point pduOffset
bytes after the data
pointer unless data
is a null pointer.
void modbusBufferDestroy | ( | ModbusBuffer * | buffer, |
void * | context | ||
) |
Equivalent of modbusBufferFree() \copydetail modbusBufferFree()
void modbusBufferFree | ( | ModbusBuffer * | buffer, |
void * | context | ||
) |
Frees memory allocated inside the buffer.
context | context pointer passed on to the allocator |
ModbusErrorInfo modbusBufferInit | ( | ModbusBuffer * | buffer, |
ModbusAllocator | allocator | ||
) |
Initializes a buffer for use.
allocator | Memory allocator to be used by the buffer |
|
inlinestatic |
Prepares buffer to only store a Modbus PDU.
|
inlinestatic |
Prepares buffer to store a Modbus RTU message.
|
inlinestatic |
Prepares buffer to store a Modbus TCP message.
|
inlinestatic |
Checks whether provided address range causes an uint16_t
overflow.
index | index of the first register in the range |
count | number of registers in the range |
uint16_t modbusCRC | ( | const uint8_t * | data, |
uint16_t | length | ||
) |
Calculates 16-bit Modbus CRC of provided data.
data | A pointer to the data to be processed |
length | Number of bytes, starting at the data pointer, to process |
ModbusError modbusDefaultAllocator | ( | ModbusBuffer * | buffer, |
uint16_t | size, | ||
void * | context | ||
) |
The default memory allocator based on realloc()
buffer | a pointer to the buffer to be reallocated |
size | new desired buffer size in bytes |
context | user's context pointer |
|
inlinestatic |
Returns ModbusError contained in ModbusErrorInfo.
|
inlinestatic |
Returns uint8_t describing error source of ModbusErrorInfo.
|
inlinestatic |
Returns general error from ModbusErrorInfo.
|
inlinestatic |
Returns request error from ModbusErrorInfo.
|
inlinestatic |
Returns response error from ModbusErrorInfo.
|
inlinestatic |
Checks if ModbusErrorInfo contains an error.
|
inlinestatic |
Reads n-th bit from an array.
mask | A pointer to the array |
n | Number of the bit to be read |
|
inlinestatic |
Writes n-th bit in an array.
mask | A pointer to the array |
n | Number of the bit to write |
value | Bit value to be written |
|
inlinestatic |
Sets up address and CRC in a Modbus RTU frame.
frame | Pointer to the frame data |
length | Length of the frame (valid range: 4 - 256) |
address | Address of the slave |
|
inlinestatic |
Sets up the MBAP header in a Modbus TCP frame.
frame | Pointer to the frame data |
length | Length of the frame (valid range: 8 - 260) |
transactionID | TCP transaction ID |
unitID | Slave unit ID |
|
inlinestatic |
Safely reads a big-endian 16-bit word from provided pointer.
|
inlinestatic |
Safely reads a little-endian 16-bit word from provided pointer.
|
inlinestatic |
Unpacks data from a Modbus RTU frame and optionally checks CRC.
frame | Pointer to the frame data |
length | Length of the frame (valid range: 4 - 256) |
checkCRC | Controls whether the CRC of the frame should be checked |
pdu | Output: pointer to the PDU |
pduLength | Output: length of the PDU |
address | Output: Slave address |
|
inlinestatic |
Unpacks data from a Modbus TCP frame.
frame | Pointer to the frame data |
length | Length of the frame (valid range: 8 - 260) |
pdu | Output: pointer to the PDU |
pduLength | Output: length of the PDU |
transactionID | Output: TCP transaction ID |
unitID | Output: Slave unit ID |
|
inlinestatic |
Safely writes a big-endian 16-bit word to provided pointer.
|
inlinestatic |
Safely writes a little-endian 16-bit word to provided pointer.