Core Classes๏ƒ

This section provides detailed documentation for the core classes in libmodbus.

๐Ÿ“Š Data Model Classes๏ƒ

ModbusDataModel๏ƒ

The primary data storage class for all Modbus data types.

class ModbusDataModel๏ƒ

Configurable Modbus data model with support for all four data types.

This class implements a complete Modbus data model that stores:

  • Coils (read/write discrete outputs)

  • Discrete Inputs (read-only discrete inputs)

  • Holding Registers (read/write 16-bit registers)

  • Input Registers (read-only 16-bit registers)

The implementation uses vectors for dynamic sizing and supports both default limits that comply with Modbus specifications and custom limits for specialized applications.

See also

ModbusFunctionCode for supported operations

Note

All data addresses are 0-based internally, following common programming conventions, even though Modbus addresses are traditionally 1-based.

Public Functions

inline ModbusDataModel()๏ƒ

Default constructor with standard Modbus limits.

Creates a data model with the default limits as specified in the Modbus application protocol specification:

  • 2000 discrete inputs

  • 2000 coils

  • 125 input registers

  • 125 holding registers

All values are initialized to false/0.

inline ModbusDataModel(size_t maxDiscreteInputs, size_t maxCoils, size_t maxInputRegisters, size_t maxHoldingRegisters)๏ƒ

Custom constructor with user-defined limits.

Creates a data model with custom limits for specialized applications that may need different capacity than the standard Modbus limits.

Note

All values are initialized to false/0.

Parameters:
  • maxDiscreteInputs โ€“ Maximum number of discrete inputs

  • maxCoils โ€“ Maximum number of coils

  • maxInputRegisters โ€“ Maximum number of input registers

  • maxHoldingRegisters โ€“ Maximum number of holding registers

inline size_t getMaxDiscreteInputs() const๏ƒ

Get the maximum number of discrete inputs.

Returns:

Maximum number of discrete inputs this data model can store

inline size_t getMaxCoils() const๏ƒ

Get the maximum number of coils.

Returns:

Maximum number of coils this data model can store

inline size_t getMaxInputRegisters() const๏ƒ

Get the maximum number of input registers.

Returns:

Maximum number of input registers this data model can store

inline size_t getMaxHoldingRegisters() const๏ƒ

Get the maximum number of holding registers.

Returns:

Maximum number of holding registers this data model can store

DiscreteInputType readDiscreteInput(uint16_t index) const๏ƒ

Read a discrete input value.

Note

This function performs bounds checking and returns false for invalid indices

Parameters:

index โ€“ 0-based index of the discrete input to read

Returns:

Value of the discrete input (true/false), or false if index is out of range

CoilType readCoil(uint16_t index) const๏ƒ

Read a coil value.

Note

This function performs bounds checking and returns false for invalid indices

Parameters:

index โ€“ 0-based index of the coil to read

Returns:

Value of the coil (true/false), or false if index is out of range

InputRegisterType readInputRegister(uint16_t index) const๏ƒ

Read an input register value.

Note

This function performs bounds checking and returns 0 for invalid indices

Parameters:

index โ€“ 0-based index of the input register to read

Returns:

Value of the input register (0-65535), or 0 if index is out of range

HoldingRegisterType readHoldingRegister(uint16_t index) const๏ƒ

Read a holding register value.

Note

This function performs bounds checking and returns 0 for invalid indices

Parameters:

index โ€“ 0-based index of the holding register to read

Returns:

Value of the holding register (0-65535), or 0 if index is out of range

void writeCoil(uint16_t index, CoilType value)๏ƒ

Write a single coil value.

Note

This function performs bounds checking and ignores writes to invalid indices

Parameters:
  • index โ€“ 0-based index of the coil to write

  • value โ€“ New value for the coil (true/false)

void writeHoldingRegister(uint16_t index, HoldingRegisterType value)๏ƒ

Write a single holding register value.

Note

This function performs bounds checking and ignores writes to invalid indices

Parameters:
  • index โ€“ 0-based index of the holding register to write

  • value โ€“ New value for the holding register (0-65535)

void writeMultipleCoils(uint16_t start_index, const CoilType values[], size_t num_values)๏ƒ

Write multiple coil values starting at a specified address.

Note

This function performs bounds checking and only writes values that fit within the valid address range. Partial writes may occur if the range extends beyond the maximum address.

Parameters:
  • start_index โ€“ 0-based starting index for the write operation

  • values โ€“ Array of coil values to write

  • num_values โ€“ Number of values to write from the array

void writeMultipleHoldingRegisters(uint16_t start_index, const HoldingRegisterType values[], size_t num_values)๏ƒ

Write multiple holding register values starting at a specified address.

Note

This function performs bounds checking and only writes values that fit within the valid address range. Partial writes may occur if the range extends beyond the maximum address.

Parameters:
  • start_index โ€“ 0-based starting index for the write operation

  • values โ€“ Array of holding register values to write

  • num_values โ€“ Number of values to write from the array

void setDiscreteInput(uint16_t index, DiscreteInputType value)๏ƒ

Set a discrete input value (for testing/initialization)

Note

This function is primarily intended for testing and initialization. In a real Modbus device, discrete inputs would typically be updated by hardware or other system components, not by Modbus commands.

Note

This function performs bounds checking and ignores writes to invalid indices

Parameters:
  • index โ€“ 0-based index of the discrete input to set

  • value โ€“ New value for the discrete input (true/false)

void setInputRegister(uint16_t index, InputRegisterType value)๏ƒ

Set an input register value (for testing/initialization)

Note

This function is primarily intended for testing and initialization. In a real Modbus device, input registers would typically be updated by hardware or other system components, not by Modbus commands.

Note

This function performs bounds checking and ignores writes to invalid indices

Parameters:
  • index โ€“ 0-based index of the input register to set

  • value โ€“ New value for the input register (0-65535)

inline std::vector<DiscreteInputType> &getDiscreteInputs()๏ƒ

Get direct access to discrete inputs vector.

Deprecated:

This method is deprecated. Use the read/write methods instead.

Warning

Direct access bypasses bounds checking and may lead to undefined behavior

Returns:

Reference to the internal discrete inputs vector

inline std::vector<CoilType> &getCoils()๏ƒ

Get direct access to coils vector.

Deprecated:

This method is deprecated. Use the read/write methods instead.

Warning

Direct access bypasses bounds checking and may lead to undefined behavior

Returns:

Reference to the internal coils vector

inline std::vector<InputRegisterType> &getInputRegisters()๏ƒ

Get direct access to input registers vector.

Deprecated:

This method is deprecated. Use the read/write methods instead.

Warning

Direct access bypasses bounds checking and may lead to undefined behavior

Returns:

Reference to the internal input registers vector

inline std::vector<HoldingRegisterType> &getHoldingRegisters()๏ƒ

Get direct access to holding registers vector.

Deprecated:

This method is deprecated. Use the read/write methods instead.

Warning

Direct access bypasses bounds checking and may lead to undefined behavior

Returns:

Reference to the internal holding registers vector

Public Static Attributes

static constexpr size_t DEFAULT_MAX_DISCRETE_INPUTS = 2000๏ƒ

Default maximum number of discrete inputs (2000 per Modbus spec)

static constexpr size_t DEFAULT_MAX_COILS = 2000๏ƒ

Default maximum number of coils (2000 per Modbus spec)

static constexpr size_t DEFAULT_MAX_INPUT_REGISTERS = 125๏ƒ

Default maximum number of input registers (125 per Modbus spec)

static constexpr size_t DEFAULT_MAX_HOLDING_REGISTERS = 125๏ƒ

Default maximum number of holding registers (125 per Modbus spec)

static constexpr size_t MAX_DISCREET_INPUT = DEFAULT_MAX_DISCRETE_INPUTS๏ƒ

Legacy constant for maximum discrete inputs.

Deprecated:

Use DEFAULT_MAX_DISCRETE_INPUTS

static constexpr size_t MAX_COILS = DEFAULT_MAX_COILS๏ƒ

Legacy constant for maximum coils.

Deprecated:

Use DEFAULT_MAX_COILS

static constexpr size_t MAX_INPUT_REGISTERS = DEFAULT_MAX_INPUT_REGISTERS๏ƒ

Legacy constant for maximum input registers.

Deprecated:

Use DEFAULT_MAX_INPUT_REGISTERS

static constexpr size_t MAX_HOLDING_REGISTERS = DEFAULT_MAX_HOLDING_REGISTERS๏ƒ

Legacy constant for maximum holding registers.

Deprecated:

Use DEFAULT_MAX_HOLDING_REGISTERS

๐Ÿ–ฅ๏ธ Server Classes๏ƒ

ModbusBaseServer๏ƒ

Abstract base class for all Modbus server implementations.

class ModbusBaseServer๏ƒ

Abstract base class for all Modbus server implementations.

This class provides the foundation for Modbus server implementations across different transport protocols (RTU, ASCII, TCP). It manages the data model and command registry that are common to all protocols.

The class uses the Command pattern to handle different function codes, with each function code mapped to a specific command implementation. This design allows for easy extension and modification of supported function codes.

Note

This is an abstract base class. Use the protocol-specific derived classes (ModbusRtuServer, etc.) for actual server implementation.

Subclassed by ModbusRtuServer

Public Functions

inline ModbusBaseServer()๏ƒ

Constructor initializes the command registry.

Registers all supported Modbus function codes with their corresponding command implementations. Currently supports:

  • FC 01: Read Coils

  • FC 02: Read Discrete Inputs

  • FC 03: Read Holding Registers

  • FC 04: Read Input Registers

  • FC 05: Write Single Coil

  • FC 06: Write Single Register

  • FC 08: Diagnostics

  • FC 15: Write Multiple Coils

  • FC 16: Write Multiple Registers

inline virtual std::vector<uint8_t> process(const std::vector<uint8_t> &requestData)๏ƒ

Process a Modbus request and generate a response.

This is the main entry point for request processing. Each protocol-specific derived class must implement this method to handle the protocol-specific frame formatting and validation.

Note

The base implementation returns an empty vector. Derived classes must override this method to provide actual functionality.

Parameters:

requestData โ€“ Raw request data as received from the transport layer

Returns:

Raw response data to be sent back, or empty vector if no response

Public Members

ModbusDataModel data๏ƒ

Modbus data model instance.

Contains all four types of Modbus data: coils, discrete inputs, holding registers, and input registers. This data model is shared across all command implementations.

std::map<ModbusFunctionCode, std::unique_ptr<ModbusCommand>> commands๏ƒ

Command registry mapping function codes to command implementations.

This map contains the association between Modbus function codes and their corresponding command implementations. New function codes can be supported by adding entries to this map.

ModbusRtuServer๏ƒ

RTU protocol-specific server implementation.

class ModbusRtuServer : public ModbusBaseServer๏ƒ

Modbus RTU server implementation.

Implements the Modbus RTU (Remote Terminal Unit) protocol for serial communication. This class handles RTU-specific frame processing including:

  • CRC validation and generation

  • Slave address checking

  • Binary frame formatting

  • Exception response generation

The RTU protocol uses binary encoding and CRC-16 checksums for error detection. Frames are transmitted without start/stop delimiters, relying on timing gaps for frame synchronization.

Note

This server is configured to respond to slave address 1 by default. Broadcast messages (address 0) are processed but no response is sent.

Public Functions

virtual std::vector<uint8_t> process(const std::vector<uint8_t> &requestData)๏ƒ

Process an RTU request and generate an RTU response.

This method handles the complete RTU request processing pipeline:

  1. Frame validation (minimum size, CRC check)

  2. Slave address verification

  3. Function code lookup and command execution

  4. Response frame generation and CRC calculation

The method performs the following validations:

  • Minimum frame size (4 bytes: address + function + CRC)

  • CRC integrity check

  • Slave address matching (responds to address 1 only)

  • Function code support check

Exception responses are generated for:

  • Unsupported function codes

  • Invalid request parameters

  • Data access errors

Note

No response is generated for:

  • Invalid CRC

  • Wrong slave address

  • Broadcast messages (address 0)

Parameters:

requestData โ€“ Raw RTU frame data including slave address and CRC

Returns:

Complete RTU response frame, or empty vector if no response needed

๐Ÿ–ผ๏ธ Frame Classes๏ƒ

ModbusFrame๏ƒ

Core Protocol Data Unit (PDU) implementation.

class ModbusFrame๏ƒ

Core Modbus Protocol Data Unit (PDU) implementation.

This class represents the core Modbus PDU that is common to all Modbus variants (RTU, ASCII, TCP). It contains the function code, frame data, and exception information. The PDU is embedded within the various transport-specific frame formats.

The frame structure is:

  • Function Code (1 byte)

  • Data (0-252 bytes)

  • Exception Code (1 byte, only for exception frames)

Public Functions

inline ModbusFrame()๏ƒ

Default constructor.

Creates an uninitialized frame with NONE type, NONE function code, and no exception code.

inline ModbusFrame(ModbusFrameType type, ModbusFunctionCode func)๏ƒ

Constructor with frame type and function code.

Parameters:
  • type โ€“ The type of frame (REQUEST, RESPONSE, or EXCEPTION)

  • func โ€“ The Modbus function code

Public Members

ModbusFrameType frameType๏ƒ

Type of frame (request/response/exception)

ModbusFunctionCode functionCode๏ƒ

Modbus function code.

std::vector<uint8_t> frameData๏ƒ

Frame data payload.

ModbusExceptionCode exceptionCode๏ƒ

Exception code (only used for exception frames)

ModbusRtuFrame๏ƒ

RTU protocol-specific frame implementation.

class ModbusRtuFrame๏ƒ

Modbus RTU (Remote Terminal Unit) frame implementation.

Implements the Modbus RTU frame format used for serial communication. RTU frames use binary encoding and CRC-16 checksums for error detection.

Normal frame structure: | Slave Address (1 byte) | Function Code (1 byte) | Data (n bytes) | CRC (2 bytes) |

Exception frame structure: | Slave Address (1 byte) | Function Code + 0x80 (1 byte) | Exception Code (1 byte) | CRC (2 bytes) |

Note

The CRC is calculated using the Modbus CRC-16 algorithm over all bytes except the CRC itself, with low byte transmitted first.

Public Functions

inline ModbusRtuFrame()๏ƒ

Default constructor.

Creates an RTU frame with slave address 0 and checksum 0.

inline ModbusRtuFrame(uint8_t addr)๏ƒ

Constructor with slave address.

Parameters:

addr โ€“ Slave address (1-247, 0 for broadcast)

std::vector<uint8_t> serialize()๏ƒ

Serialize the RTU frame to a byte vector.

Converts the RTU frame to its binary representation suitable for transmission over a serial link. The function automatically calculates and appends the CRC-16 checksum.

Note

The CRC is calculated using the Modbus CRC-16 algorithm

Returns:

Vector of bytes representing the serialized frame

void deserialize(ModbusFrameType frameType, const std::vector<uint8_t> &data)๏ƒ

Deserialize a byte vector into an RTU frame.

Parses a received byte vector and populates the RTU frame fields. The function extracts the slave address, function code, data, and CRC.

Note

This function does not validate the CRC - that should be done by the caller before calling this function.

Parameters:
  • frameType โ€“ Expected frame type (REQUEST, RESPONSE, or EXCEPTION)

  • data โ€“ Byte vector containing the raw frame data

Public Members

uint8_t slaveaddr๏ƒ

Slave address (1-247, 0 for broadcast)

ModbusFrame pdu๏ƒ

Protocol Data Unit.

uint16_t checksum๏ƒ

CRC-16 checksum.

ModbusAsciiFrame๏ƒ

ASCII protocol frame implementation (planned).

class ModbusAsciiFrame๏ƒ

Modbus ASCII frame implementation.

Implements the Modbus ASCII frame format used for serial communication. ASCII frames use hexadecimal character encoding and LRC checksums.

Frame structure: | Start โ€˜:โ€™ | Address (2 hex chars) | Function (2 hex chars) | Data (hex chars) | LRC (2 hex chars) | End CRLF |

Note

ASCII frames are longer than RTU frames but are human-readable and less susceptible to transmission errors.

Warning

ASCII implementation is currently incomplete (TODO)

Public Functions

std::vector<uint8_t> serialize()๏ƒ

Serialize the ASCII frame to a byte vector.

Todo:

This method is not yet implemented

Returns:

Vector of bytes representing the serialized frame

void deserialize(const std::vector<uint8_t> &data)๏ƒ

Deserialize a byte vector into an ASCII frame.

Todo:

This method is not yet implemented

Parameters:

data โ€“ Byte vector containing the raw frame data

Public Members

uint8_t start๏ƒ

Start character (always โ€˜:โ€™)

uint8_t address๏ƒ

Slave address.

ModbusFrame pdu๏ƒ

Protocol Data Unit.

uint16_t checksum๏ƒ

LRC checksum.

uint16_t end๏ƒ

End characters (CR LF)

ModbusTcpFrame๏ƒ

TCP protocol frame implementation (planned).

class ModbusTcpFrame๏ƒ

Modbus TCP frame implementation.

Implements the Modbus TCP frame format used for Ethernet communication. TCP frames include an MBAP header followed by the PDU. No checksum is needed as TCP provides reliable delivery.

Frame structure: | MBAP Header (7 bytes) | PDU (1-253 bytes) |

Warning

TCP implementation is currently incomplete (TODO)

Public Functions

std::vector<uint8_t> serialize()๏ƒ

Serialize the TCP frame to a byte vector.

Todo:

This method is not yet implemented

Returns:

Vector of bytes representing the serialized frame

void deserialize(const std::vector<uint8_t> &data)๏ƒ

Deserialize a byte vector into a TCP frame.

Todo:

This method is not yet implemented

Parameters:

data โ€“ Byte vector containing the raw frame data

Public Members

MbapHeader mbapHeader๏ƒ

MBAP header.

ModbusFrame pdu๏ƒ

Protocol Data Unit.

MbapHeader๏ƒ

MBAP header for Modbus TCP frames.

class MbapHeader๏ƒ

Modbus Application Protocol (MBAP) header for TCP frames.

The MBAP header is used in Modbus TCP to provide transaction identification, protocol identification, length information, and unit identification.

Header structure (7 bytes): | Transaction ID (2 bytes) | Protocol ID (2 bytes) | Length (2 bytes) | Unit ID (1 byte) |

Public Functions

inline MbapHeader()๏ƒ

Default constructor.

Creates an MBAP header with all fields set to 0.

inline MbapHeader(uint16_t transactionId, uint16_t protocolId, uint16_t length, uint8_t unitId)๏ƒ

Constructor with all header fields.

Parameters:
  • transactionId โ€“ Transaction identifier

  • protocolId โ€“ Protocol identifier (should be 0 for Modbus)

  • length โ€“ Length of following bytes

  • unitId โ€“ Unit identifier

Public Members

uint16_t transactionId๏ƒ

Transaction identifier for matching requests/responses.

uint16_t protocolId๏ƒ

Protocol identifier (always 0 for Modbus)

uint16_t length๏ƒ

Number of following bytes (PDU length + 1)

uint8_t unitId๏ƒ

Unit identifier (slave address equivalent)

๐ŸŽฎ Command Classes๏ƒ

ModbusCommand๏ƒ

Abstract base class for all command implementations.

class ModbusCommand๏ƒ

Abstract base class for all Modbus command implementations.

This class defines the interface for Modbus command processing using the Command pattern. Each specific function code has its own derived command class that implements the execute() method.

The class also provides common validation methods that are used by multiple command implementations to ensure consistent error handling and compliance with Modbus specifications.

Subclassed by DiagnosticsCommand, ReadCoilCommand, ReadDiscreteInputCommand, ReadHoldingRegisterCommand, ReadInputRegisterCommand, WriteCoilCommand, WriteHoldingRegisterCommand, WriteMultipleCoilsCommand, WriteMultipleRegistersCommand

Public Functions

virtual ~ModbusCommand() = default๏ƒ

Virtual destructor for proper polymorphic destruction.

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request) = 0๏ƒ

Execute the command with given data model and request.

This is the main entry point for command execution. Each derived class implements this method to handle its specific function code.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming Modbus request frame

Returns:

Response frame (normal response or exception)

Read Command Classes๏ƒ

class ReadCoilCommand : public ModbusCommand๏ƒ

Implementation of Read Coils function (FC 01)

Reads the status of coils (discrete outputs) from the data model. Returns a bit-packed response where each bit represents one coil state.

Request format:

  • Starting Address (2 bytes)

  • Quantity of Coils (2 bytes)

Response format:

  • Byte Count (1 byte)

  • Coil Status (n bytes, bit-packed)

Note

Coils are packed 8 per byte, LSB first

Public Functions

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request)๏ƒ

Execute the Read Coils command.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming request frame

Returns:

Response frame containing coil status or exception

class ReadDiscreteInputCommand : public ModbusCommand๏ƒ

Implementation of Read Discrete Inputs function (FC 02)

Reads the status of discrete inputs from the data model. Returns a bit-packed response where each bit represents one input state.

Request format:

  • Starting Address (2 bytes)

  • Quantity of Inputs (2 bytes)

Response format:

  • Byte Count (1 byte)

  • Input Status (n bytes, bit-packed)

Note

Inputs are packed 8 per byte, LSB first

Public Functions

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request)๏ƒ

Execute the Read Discrete Inputs command.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming request frame

Returns:

Response frame containing input status or exception

class ReadHoldingRegisterCommand : public ModbusCommand๏ƒ

Implementation of Read Holding Registers function (FC 03)

Reads holding register values from the data model. Returns register values in big-endian format.

Request format:

  • Starting Address (2 bytes)

  • Quantity of Registers (2 bytes)

Response format:

  • Byte Count (1 byte)

  • Register Values (n*2 bytes, big-endian)

Public Functions

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request)๏ƒ

Execute the Read Holding Registers command.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming request frame

Returns:

Response frame containing register values or exception

class ReadInputRegisterCommand : public ModbusCommand๏ƒ

Implementation of Read Input Registers function (FC 04)

Reads input register values from the data model. Returns register values in big-endian format.

Request format:

  • Starting Address (2 bytes)

  • Quantity of Registers (2 bytes)

Response format:

  • Byte Count (1 byte)

  • Register Values (n*2 bytes, big-endian)

Public Functions

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request)๏ƒ

Execute the Read Input Registers command.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming request frame

Returns:

Response frame containing register values or exception

Write Command Classes๏ƒ

class WriteCoilCommand : public ModbusCommand๏ƒ

Implementation of Write Single Coil function (FC 05)

Writes a single coil value to the data model.

Request format:

  • Output Address (2 bytes)

  • Output Value (2 bytes: 0x0000 for OFF, 0xFF00 for ON)

Response format:

  • Echo of the complete request

Public Functions

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request)๏ƒ

Execute the Write Single Coil command.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming request frame

Returns:

Response frame (echo of request) or exception

class WriteHoldingRegisterCommand : public ModbusCommand๏ƒ

Implementation of Write Single Register function (FC 06)

Writes a single holding register value to the data model.

Request format:

  • Register Address (2 bytes)

  • Register Value (2 bytes)

Response format:

  • Echo of the complete request

Public Functions

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request)๏ƒ

Execute the Write Single Register command.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming request frame

Returns:

Response frame (echo of request) or exception

class WriteMultipleCoilsCommand : public ModbusCommand๏ƒ

Implementation of Write Multiple Coils function (FC 15)

Writes multiple coil values to the data model. Coil values are transmitted as bit-packed bytes.

Request format:

  • Starting Address (2 bytes)

  • Quantity of Outputs (2 bytes)

  • Byte Count (1 byte)

  • Outputs Value (n bytes, bit-packed)

Response format:

  • Starting Address (2 bytes)

  • Quantity of Outputs (2 bytes)

Note

Coils are packed 8 per byte, LSB first

Public Functions

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request)๏ƒ

Execute the Write Multiple Coils command.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming request frame

Returns:

Response frame containing address and quantity or exception

class WriteMultipleRegistersCommand : public ModbusCommand๏ƒ

Implementation of Write Multiple Registers function (FC 16)

Writes multiple holding register values to the data model. Register values are transmitted in big-endian format.

Request format:

  • Starting Address (2 bytes)

  • Quantity of Registers (2 bytes)

  • Byte Count (1 byte)

  • Registers Value (n*2 bytes, big-endian)

Response format:

  • Starting Address (2 bytes)

  • Quantity of Registers (2 bytes)

Public Functions

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request)๏ƒ

Execute the Write Multiple Registers command.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming request frame

Returns:

Response frame containing address and quantity or exception

Diagnostic Command Classes๏ƒ

class DiagnosticsCommand : public ModbusCommand๏ƒ

Implementation of Diagnostics function (FC 08)

Provides a series of diagnostic and testing functions for Modbus communication. Currently supports basic sub-functions like Return Query Data and communication restart.

Request format:

  • Sub-function (2 bytes)

  • Data (2 bytes, sub-function specific)

Response format:

  • Sub-function (2 bytes, echo)

  • Data (2 bytes, sub-function specific)

Note

Only basic diagnostic sub-functions are currently implemented

Public Functions

virtual ModbusFrame execute(ModbusDataModel &data, const ModbusFrame &request)๏ƒ

Execute the Diagnostics command.

Parameters:
  • data โ€“ Reference to the Modbus data model

  • request โ€“ The incoming request frame

Returns:

Response frame containing diagnostic response or exception

๐Ÿงฎ Utility Classes๏ƒ

Checksum๏ƒ

Static utility class for checksum calculations.

class Checksum๏ƒ

Static utility class for Modbus checksum calculations.

This class provides static methods for calculating checksums used in different Modbus transport protocols. The checksums are essential for error detection in serial communications where data corruption can occur.

Note

All methods are static and the class is not meant to be instantiated.

Public Static Functions

static uint8_t calculateLRC(const uint8_t *pucFrame, uint16_t usLen)๏ƒ

Calculate LRC (Longitudinal Redundancy Check) for Modbus ASCII.

Calculates the LRC checksum used by Modbus ASCII protocol. The LRC is computed by adding all bytes in the message and taking the twoโ€™s complement of the result.

Algorithm:

  1. Add all bytes in the frame (excluding the LRC itself)

  2. Take the twoโ€™s complement of the sum

  3. Return the result as a single byte

Note

The LRC is calculated over the binary representation of the ASCII characters, not the ASCII characters themselves.

Parameters:
  • pucFrame โ€“ Pointer to the frame data

  • usLen โ€“ Length of the frame data in bytes

Returns:

LRC checksum value (8-bit)

static uint16_t calculateCRC16(const uint8_t *buffer, uint16_t length)๏ƒ

Calculate CRC-16 checksum for Modbus RTU.

Calculates the CRC-16 checksum used by Modbus RTU protocol. This implementation uses the standard Modbus CRC-16 algorithm with polynomial 0xA001 (reversed representation of 0x8005).

Algorithm:

  1. Initialize CRC register to 0xFFFF

  2. For each byte: a. XOR byte with low byte of CRC register b. For each bit (8 iterations):

    • If LSB of CRC is 1: shift right and XOR with 0xA001

    • If LSB of CRC is 0: shift right only

  3. Return final CRC value

Note

The CRC is transmitted LSB first in RTU frames. This function returns the CRC in native byte order; the caller is responsible for proper byte ordering.

Note

This is the standard Modbus CRC-16 algorithm as specified in the Modbus over Serial Line Specification and Implementation Guide V1.02.

Parameters:
  • buffer โ€“ Pointer to the data buffer

  • length โ€“ Length of the data buffer in bytes

Returns:

CRC-16 checksum value (16-bit)