liblightmodbus  2.0
A lightweight, cross-platform Modbus RTU library
Typedefs | Enumerations | Functions
lightmodbus.h File Reference

Core Modbus functions. More...

Go to the source code of this file.

Typedefs

typedef enum modbusError ModbusError
 Represents a library runtime error code. More...
 
typedef enum modbusFrameError ModbusFrameError
 Provides more information on frame building/parsing error. More...
 
typedef enum modbusExceptionCode ModbusExceptionCode
 Represents a Modbus exception code, defined by the standart. More...
 
typedef enum modbusDataType ModbusDataType
 Stores information about Modbus data types. More...
 

Enumerations

enum  modbusError {
  MODBUS_ERROR_OK = 0, MODBUS_ERROR_EXCEPTION = 1, MODBUS_ERROR_ALLOC, MODBUS_ERROR_OTHER,
  MODBUS_ERROR_NULLPTR, MODBUS_ERROR_PARSE, MODBUS_ERROR_BUILD, MODBUS_OK = MODBUS_ERROR_OK
}
 Represents a library runtime error code. More...
 
enum  modbusFrameError {
  MODBUS_FERROR_OK = MODBUS_OK, MODBUS_FERROR_CRC, MODBUS_FERROR_LENGTH, MODBUS_FERROR_COUNT,
  MODBUS_FERROR_VALUE, MODBUS_FERROR_RANGE, MODBUS_FERROR_NOSRC, MODBUS_FERROR_NOREAD,
  MODBUS_FERROR_NOWRITE, MODBUS_FERROR_NOFUN, MODBUS_FERROR_BADFUN, MODBUS_FERROR_NULLFUN,
  MODBUS_FERROR_MISM_FUN, MODBUS_FERROR_MISM_ADDR, MODBUS_FERROR_MISM_INDEX, MODBUS_FERROR_MISM_COUNT,
  MODBUS_FERROR_MISM_VALUE, MODBUS_FERROR_MISM_MASK, MODBUS_FERROR_BROADCAST
}
 Provides more information on frame building/parsing error. More...
 
enum  modbusExceptionCode {
  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, defined by the standart. More...
 
enum  modbusDataType { MODBUS_HOLDING_REGISTER = 1, MODBUS_INPUT_REGISTER = 2, MODBUS_COIL = 4, MODBUS_DISCRETE_INPUT = 8 }
 Stores information about Modbus data types. More...
 

Functions

static uint16_t modbusBitsToBytes (uint16_t n)
 Converts number of bits to number of bytes required to store them. More...
 
static uint16_t modbusSwapEndian (uint16_t data)
 Swaps endianness of provided 16-bit data portion. More...
 
static uint16_t modbusMatchEndian (uint16_t data)
 Swaps endianness of provided 16-bit data portion if needed. More...
 
uint8_t modbusMaskRead (const uint8_t *mask, uint16_t maskLength, uint16_t bit)
 Reads n-th bit from an array. More...
 
uint8_t modbusMaskWrite (uint8_t *mask, uint16_t maskLength, uint16_t bit, uint8_t value)
 Writes n-th bit in an array. More...
 
uint16_t modbusCRC (const uint8_t *data, uint16_t length)
 Calculates 16-bit Modbus CRC of provided data. More...
 

Detailed Description

Core Modbus functions.

This is main header file that is ought to be included as library

Note
This header file is suitable for C++

Typedef Documentation

◆ ModbusDataType

Stores information about Modbus data types.

◆ ModbusError

typedef enum modbusError ModbusError

Represents a library runtime error code.

◆ ModbusExceptionCode

Represents a Modbus exception code, defined by the standart.

◆ ModbusFrameError

Provides more information on frame building/parsing error.

These error code should serve as an additional source of information for the user.

Enumeration Type Documentation

◆ modbusDataType

Stores information about Modbus data types.

Enumerator
MODBUS_HOLDING_REGISTER 

Holding register.

MODBUS_INPUT_REGISTER 

Input register.

MODBUS_COIL 

Coil.

MODBUS_DISCRETE_INPUT 

Discrete input.

◆ modbusError

Represents a library runtime error code.

Enumerator
MODBUS_ERROR_OK 

No error.

MODBUS_ERROR_EXCEPTION 

Indicates that slave had thrown an exception.

This exception can be thrown either by master's parsing function (indicating incoming exception frame) or by slave's building function (indicating that some problem caused the slave to build an exception frame).

Note
This error code handles the superset of problems handled by MODBUS_ERROR_PARSE.

When thrown on slave side, check ModbusSlave::lastException and ModbusSlave::parseError for more information.

MODBUS_ERROR_ALLOC 

Memory problem.

Either one of memory allocation functions returned NULL or fixed-size buffer is not big enough to fit the data (see Disabling dynamic memory allocation).Memory allocation problem

MODBUS_ERROR_OTHER 

Other reason causing the function to abort (eg. bad function parameter)

MODBUS_ERROR_NULLPTR 

A NULL pointer provided as some crucial parameter.

MODBUS_ERROR_PARSE 

Parsing error occurred - check ModbusSlave::parseError

Note
This error code is returned instead of MODBUS_ERROR_EXCEPTION when exception should have been thrown, but wasn't (eg. due to broadcasted request frame). These two error code should be treated similarly.
MODBUS_ERROR_BUILD 

Frame building error occurred - check ModbusMaster::buildError.

MODBUS_OK 

No error. Alias of MODBUS_ERROR_OK.

◆ modbusExceptionCode

Represents a Modbus exception code, defined by the standart.

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

◆ modbusFrameError

Provides more information on frame building/parsing error.

These error code should serve as an additional source of information for the user.

Enumerator
MODBUS_FERROR_OK 

Modbus frame OK. No error.

MODBUS_FERROR_CRC 

Invalid CRC.

MODBUS_FERROR_LENGTH 

Invalid frame length.

MODBUS_FERROR_COUNT 

Invalid declared data item count.

MODBUS_FERROR_VALUE 

Illegal data value (eg. when writing a single coil)

MODBUS_FERROR_RANGE 

Invalid register range.

MODBUS_FERROR_NOSRC 

There's neither callback function nor value array provided for this data type.

MODBUS_FERROR_NOREAD 

No read access to at least one of requested regsiters.

MODBUS_FERROR_NOWRITE 

No write access to one of requested regsiters.

MODBUS_FERROR_NOFUN 

Function not supported.

MODBUS_FERROR_BADFUN 

Requested a parsing function to parse a frame with wrong function code.

MODBUS_FERROR_NULLFUN 

Function overriden by user with NULL pointer.

MODBUS_FERROR_MISM_FUN 

Function request-response mismatch.

MODBUS_FERROR_MISM_ADDR 

Slave address request-response mismatch.

MODBUS_FERROR_MISM_INDEX 

Index value request-response mismatch.

MODBUS_FERROR_MISM_COUNT 

Count value request-response mismatch.

MODBUS_FERROR_MISM_VALUE 

Data value request-response mismatch.

MODBUS_FERROR_MISM_MASK 

Mask value request-response mismatch.

MODBUS_FERROR_BROADCAST 

Received response for broadcast message.

Function Documentation

◆ modbusBitsToBytes()

static uint16_t modbusBitsToBytes ( uint16_t  n)
inlinestatic

Converts number of bits to number of bytes required to store them.

Parameters
nNumber of bits
Returns
Number of bytes of required memory
+ 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 call graph for this function:
+ Here is the caller graph for this function:

◆ modbusMaskRead()

uint8_t modbusMaskRead ( const uint8_t *  mask,
uint16_t  maskLength,
uint16_t  bit 
)

Reads n-th bit from an array.

Parameters
maskA pointer to the array
maskLengthThe length of the array in bytes
bitNumber of the bit to be read
Returns
The bit value, or 255 if the bit lies outside the array.
+ Here is the caller graph for this function:

◆ modbusMaskWrite()

uint8_t modbusMaskWrite ( uint8_t *  mask,
uint16_t  maskLength,
uint16_t  bit,
uint8_t  value 
)

Writes n-th bit in an array.

Parameters
maskA pointer to the array
maskLengthThe length of the array in bytes
bitNumber of the bit to write
valueBit value to be written
Returns
Bit value on success, 255 if the bit lies outside the array.
+ Here is the caller graph for this function:

◆ modbusMatchEndian()

static uint16_t modbusMatchEndian ( uint16_t  data)
inlinestatic

Swaps endianness of provided 16-bit data portion if needed.

Note
This function works only if the system is not big-endian
Parameters
dataA 16-bit data portion.
Returns
The same data, but with bytes swapped if the system is little-endian
See also
modbusSwapEndian
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ modbusSwapEndian()

static uint16_t modbusSwapEndian ( uint16_t  data)
inlinestatic

Swaps endianness of provided 16-bit data portion.

Note
This function, unlike modbusMatchEndian, works unconditionally
Parameters
dataA 16-bit data portion.
Returns
The same data, but with bytes swapped
See also
modbusMatchEndian
+ Here is the caller graph for this function: