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

Common types and functions (header) More...

#include <stdint.h>
+ Include dependency graph for base.h:
+ This graph shows which files directly or indirectly include this file:

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.
 

Detailed Description

Common types and functions (header)

Macro Definition Documentation

◆ LIGHTMODBUS_ALWAYS_INLINE

#define LIGHTMODBUS_ALWAYS_INLINE   __attribute__((always_inline))

◆ LIGHTMODBUS_RET_ERROR

#define LIGHTMODBUS_RET_ERROR   LIGHTMODBUS_WARN_UNUSED ModbusErrorInfo

Return type for library functions returning ModbusErrorInfo that should be handled properly.

◆ LIGHTMODBUS_WARN_UNUSED

#define LIGHTMODBUS_WARN_UNUSED   __attribute__((warn_unused_result))

◆ MODBUS_ERROR_SOURCE_GENERAL

#define MODBUS_ERROR_SOURCE_GENERAL   0u

General library error - can be caused by providing an incorrect argument or a internal library error.

◆ MODBUS_ERROR_SOURCE_REQUEST

#define MODBUS_ERROR_SOURCE_REQUEST   1u

The request frame contains errors.

◆ MODBUS_ERROR_SOURCE_RESERVED

#define MODBUS_ERROR_SOURCE_RESERVED   3u

Reserved for future use.

◆ MODBUS_ERROR_SOURCE_RESPONSE

#define MODBUS_ERROR_SOURCE_RESPONSE   2u

The response frame contains errors.

◆ MODBUS_GENERAL_ERROR

#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.

◆ MODBUS_MAKE_ERROR

#define MODBUS_MAKE_ERROR (   s,
 
)    ((ModbusErrorInfo){.source = (s), .error = (e)})

Constructs a ModbusErrorInfo object from a ModbusErrorCode and a MODBUS_ERROR_SOURCE_* macro.

◆ MODBUS_NO_ERROR

#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.

◆ MODBUS_PDU_MAX

#define MODBUS_PDU_MAX   253

Maximum length of a PDU.

◆ MODBUS_PDU_MIN

#define MODBUS_PDU_MIN   1

Minimum length of a PDU.

◆ MODBUS_REQUEST_ERROR

#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.

◆ MODBUS_RESPONSE_ERROR

#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.

◆ MODBUS_RTU_ADU_MAX

#define MODBUS_RTU_ADU_MAX   256

Maximum length of ADU in Modbus RTU.

◆ MODBUS_RTU_ADU_MIN

#define MODBUS_RTU_ADU_MIN   4

Minimum length of ADU in Modbus RTU.

◆ MODBUS_RTU_ADU_PADDING

#define MODBUS_RTU_ADU_PADDING   3

Number of extra bytes added to the PDU in Modbus RTU.

◆ MODBUS_RTU_PDU_OFFSET

#define MODBUS_RTU_PDU_OFFSET   1

Offset of PDU relative to the frame beginning in Modbus RTU.

◆ MODBUS_TCP_ADU_MAX

#define MODBUS_TCP_ADU_MAX   260

Maximum length of ADU in Modbus TCP.

◆ MODBUS_TCP_ADU_MIN

#define MODBUS_TCP_ADU_MIN   8

Minimum length of ADU in Modbus TCP.

◆ MODBUS_TCP_ADU_PADDING

#define MODBUS_TCP_ADU_PADDING   7

Number of extra bytes added to the PDU in Modbus TCP.

◆ MODBUS_TCP_PDU_OFFSET

#define MODBUS_TCP_PDU_OFFSET   7

Offset of PDU relative to the frame beginning in Modbus TCP.

Typedef Documentation

◆ ModbusAllocator

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.

◆ ModbusBuffer

◆ ModbusDataType

Represents different Modbus data types.

◆ ModbusError

typedef enum ModbusError ModbusError

Represtents different kinds of errors.

See also
ModbusErrorInfo

◆ ModbusErrorInfo

Richer error represenation - source and type of error.

See also
ModbusError
modbusIsOk()
modbusGetGeneralError()
modbusGetRequestError()
modbusGetResponseError()
modbusGetErrorSource()
modbusGetErrorCode()
Warning
You should not be accessing 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.

◆ ModbusExceptionCode

Represents a Modbus exception code.

Enumeration Type Documentation

◆ ModbusDataType

Represents different Modbus data types.

Enumerator
MODBUS_HOLDING_REGISTER 

Holding register.

MODBUS_INPUT_REGISTER 

Input register.

MODBUS_COIL 

Coil.

MODBUS_DISCRETE_INPUT 

Discrete input.

◆ ModbusError

Represtents different kinds of errors.

See also
ModbusErrorInfo
Enumerator
MODBUS_OK 

No error.

Returned when everything is fine.

MODBUS_ERROR_OK 

Same as MODBUS_OK.

MODBUS_ERROR_LENGTH 

Invalid frame length.

  • The provided frame is either too long or too short
  • The data length declared in frame does not match the actual frame length
MODBUS_ERROR_ALLOC 

Memory allocation error.

The allocator has failed to allocate requested amount of memory or free it.

MODBUS_ERROR_FUNCTION 

Invalid function.

MODBUS_ERROR_COUNT 

Invalid register count.

Provided register count is 0 or exceeds maximum allowed value.

MODBUS_ERROR_INDEX 

Invalid index value.

MODBUS_ERROR_VALUE 

Invalid register value.

MODBUS_ERROR_RANGE 

Invalid register range.

Returned when accessing count registers starting at index would cause a 16-bit unsigned int overflow.

MODBUS_ERROR_CRC 

CRC invalid.

Note
Only in Modbus RTU
MODBUS_ERROR_BAD_PROTOCOL 

Invalid protocol ID (nonzero)

Note
Only in Modbus TCP
MODBUS_ERROR_BAD_TRANSACTION 

Mismatched transaction ID.

Note
Only in Modbus TCP

The transaction identifier in the response does not match the one in the request frame.

MODBUS_ERROR_ADDRESS 

Invalid slave address.

MODBUS_ERROR_OTHER 

Other error.

◆ ModbusExceptionCode

Represents a Modbus exception code.

Enumerator
MODBUS_EXCEP_NONE 
MODBUS_EXCEP_ILLEGAL_FUNCTION 

Illegal function code.

MODBUS_EXCEP_ILLEGAL_ADDRESS 

Illegal data address.

MODBUS_EXCEP_ILLEGAL_VALUE 

Illegal data value.

MODBUS_EXCEP_SLAVE_FAILURE 

Slave could not process the request.

MODBUS_EXCEP_ACK 

Acknowledge.

MODBUS_EXCEP_NACK 

Negative acknowledge.

Function Documentation

◆ modbusBitsToBytes()

static uint16_t modbusBitsToBytes ( uint16_t  n)
inlinestatic

Returns number of bytes necessary to hold given number of bits.

Parameters
nNumber of bits
Returns
Number of bytes requred to hold n bits
+ Here is the caller graph for this function:

◆ modbusBufferAllocateADU()

ModbusError modbusBufferAllocateADU ( ModbusBuffer buffer,
uint16_t  pduSize,
void *  context 
)

Allocates memory to hold Modbus ADU.

Parameters
pduSizesize of the PDU in bytes
contextcontext pointer passed on to the allocator
Returns
MODBUS_OK on success
MODBUS_ERROR_ALLOC on allocation failure

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.

+ Here is the caller graph for this function:

◆ modbusBufferDestroy()

void modbusBufferDestroy ( ModbusBuffer buffer,
void *  context 
)

Equivalent of modbusBufferFree() \copydetail modbusBufferFree()

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusBufferFree()

void modbusBufferFree ( ModbusBuffer buffer,
void *  context 
)

Frees memory allocated inside the buffer.

Parameters
contextcontext pointer passed on to the allocator
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusBufferInit()

ModbusErrorInfo modbusBufferInit ( ModbusBuffer buffer,
ModbusAllocator  allocator 
)

Initializes a buffer for use.

Parameters
allocatorMemory allocator to be used by the buffer
Returns
MODBUS_NO_ERROR() on success
+ Here is the caller graph for this function:

◆ modbusBufferModePDU()

static void modbusBufferModePDU ( ModbusBuffer buffer)
inlinestatic

Prepares buffer to only store a Modbus PDU.

+ Here is the caller graph for this function:

◆ modbusBufferModeRTU()

static void modbusBufferModeRTU ( ModbusBuffer buffer)
inlinestatic

Prepares buffer to store a Modbus RTU message.

+ Here is the caller graph for this function:

◆ modbusBufferModeTCP()

static void modbusBufferModeTCP ( ModbusBuffer buffer)
inlinestatic

Prepares buffer to store a Modbus TCP message.

+ Here is the caller graph for this function:

◆ modbusCheckRangeU16()

static uint8_t modbusCheckRangeU16 ( uint16_t  index,
uint16_t  count 
)
inlinestatic

Checks whether provided address range causes an uint16_t overflow.

Parameters
indexindex of the first register in the range
countnumber of registers in the range
Returns
true if the range causes an overflow
+ Here is the caller graph for this function:

◆ modbusCRC()

uint16_t modbusCRC ( const uint8_t *  data,
uint16_t  length 
)

Calculates 16-bit Modbus CRC of provided data.

Parameters
dataA pointer to the data to be processed
lengthNumber of bytes, starting at the data pointer, to process
Returns
16-bit Modbus CRC value
+ Here is the caller graph for this function:

◆ modbusDefaultAllocator()

ModbusError modbusDefaultAllocator ( ModbusBuffer buffer,
uint16_t  size,
void *  context 
)

The default memory allocator based on realloc()

Parameters
buffera pointer to the buffer to be reallocated
sizenew desired buffer size in bytes
contextuser's context pointer
Returns
MODBUS_ERROR_ALLOC on allocation failure
MODBUS_OK on success
See also
allocators

◆ modbusGetErrorCode()

static ModbusError modbusGetErrorCode ( ModbusErrorInfo  err)
inlinestatic

Returns ModbusError contained in ModbusErrorInfo.

Returns
MdobusError contained in ModbusErrorInfo
+ Here is the caller graph for this function:

◆ modbusGetErrorSource()

static uint8_t modbusGetErrorSource ( ModbusErrorInfo  err)
inlinestatic

Returns uint8_t describing error source of ModbusErrorInfo.

Returns
error source
See also
MODBUS_ERROR_SOURCE_GENERAL
MODBUS_ERROR_SOURCE_REQUEST
MODBUS_ERROR_SOURCE_REQUEST
+ Here is the caller graph for this function:

◆ modbusGetGeneralError()

static ModbusError modbusGetGeneralError ( ModbusErrorInfo  err)
inlinestatic

Returns general error from ModbusErrorInfo.

Returns
ModbusError if ModbusErrorInfo contains an error from MODBUS_ERROR_SOURCE_GENERAL
MODBUS_OK otherwise
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusGetRequestError()

static ModbusError modbusGetRequestError ( ModbusErrorInfo  err)
inlinestatic

Returns request error from ModbusErrorInfo.

Returns
ModbusError if ModbusErrorInfo contains an error from MODBUS_ERROR_SOURCE_REQUEST
MODBUS_OK otherwise
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusGetResponseError()

static ModbusError modbusGetResponseError ( ModbusErrorInfo  err)
inlinestatic

Returns response error from ModbusErrorInfo.

Returns
ModbusError if ModbusErrorInfo contains an error from MODBUS_ERROR_SOURCE_RESPONSE
MODBUS_OK otherwise
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusIsOk()

static uint8_t modbusIsOk ( ModbusErrorInfo  err)
inlinestatic

Checks if ModbusErrorInfo contains an error.

Returns
true if ModbusErrorInfo contains an error
See also
MODBUS_NO_ERROR()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusMaskRead()

static uint8_t modbusMaskRead ( const uint8_t *  mask,
uint16_t  n 
)
inlinestatic

Reads n-th bit from an array.

Parameters
maskA pointer to the array
nNumber of the bit to be read
Returns
The bit value
+ Here is the caller graph for this function:

◆ modbusMaskWrite()

static void modbusMaskWrite ( uint8_t *  mask,
uint16_t  n,
uint8_t  value 
)
inlinestatic

Writes n-th bit in an array.

Parameters
maskA pointer to the array
nNumber of the bit to write
valueBit value to be written
+ Here is the caller graph for this function:

◆ modbusPackRTU()

static ModbusError modbusPackRTU ( uint8_t *  frame,
uint16_t  length,
uint8_t  address 
)
inlinestatic

Sets up address and CRC in a Modbus RTU frame.

Parameters
framePointer to the frame data
lengthLength of the frame (valid range: 4 - 256)
addressAddress of the slave
Returns
MODBUS_OK on success
MODBUS_ERROR_LENGTH if the length of the frame is invalid
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusPackTCP()

static ModbusError modbusPackTCP ( uint8_t *  frame,
uint16_t  length,
uint16_t  transactionID,
uint8_t  unitID 
)
inlinestatic

Sets up the MBAP header in a Modbus TCP frame.

Parameters
framePointer to the frame data
lengthLength of the frame (valid range: 8 - 260)
transactionIDTCP transaction ID
unitIDSlave unit ID
Returns
MODBUS_OK on success
MODBUS_ERROR_LENGTH if the length of the frame is invalid
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusRBE()

static uint16_t modbusRBE ( const uint8_t *  p)
inlinestatic

Safely reads a big-endian 16-bit word from provided pointer.

+ Here is the caller graph for this function:

◆ modbusRLE()

static uint16_t modbusRLE ( const uint8_t *  p)
inlinestatic

Safely reads a little-endian 16-bit word from provided pointer.

+ Here is the caller graph for this function:

◆ modbusUnpackRTU()

static ModbusError modbusUnpackRTU ( const uint8_t *  frame,
uint16_t  length,
uint8_t  checkCRC,
const uint8_t **  pdu,
uint16_t *  pduLength,
uint8_t *  address 
)
inlinestatic

Unpacks data from a Modbus RTU frame and optionally checks CRC.

Parameters
framePointer to the frame data
lengthLength of the frame (valid range: 4 - 256)
checkCRCControls whether the CRC of the frame should be checked
pduOutput: pointer to the PDU
pduLengthOutput: length of the PDU
addressOutput: Slave address
Returns
MODBUS_OK on success
MODBUS_ERROR_LENGTH if the length of the frame is invalid
MODBUS_ERROR_CRC if the CRC is incorrect
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusUnpackTCP()

static ModbusError modbusUnpackTCP ( const uint8_t *  frame,
uint16_t  length,
const uint8_t **  pdu,
uint16_t *  pduLength,
uint16_t *  transactionID,
uint8_t *  unitID 
)
inlinestatic

Unpacks data from a Modbus TCP frame.

Parameters
framePointer to the frame data
lengthLength of the frame (valid range: 8 - 260)
pduOutput: pointer to the PDU
pduLengthOutput: length of the PDU
transactionIDOutput: TCP transaction ID
unitIDOutput: Slave unit ID
Returns
MODBUS_OK on success
MODBUS_ERROR_LENGTH if the length of the frame is invalid or if the length declared inside the frame does not match the actual frame length
MODBUS_ERROR_BAD_PROTOCOL if the protocol ID declared in frame is not 0
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusWBE()

static uint16_t modbusWBE ( uint8_t *  p,
uint16_t  val 
)
inlinestatic

Safely writes a big-endian 16-bit word to provided pointer.

+ Here is the caller graph for this function:

◆ modbusWLE()

static uint16_t modbusWLE ( uint8_t *  p,
uint16_t  val 
)
inlinestatic

Safely writes a little-endian 16-bit word to provided pointer.

+ Here is the caller graph for this function: