Dynamic Modbus Master
A dynamic wrapper for the modbus master part of esp-modbus written in C++.
 
Loading...
Searching...
No Matches
dynamic_modbus_master::slave::SlaveDevice Class Reference

A class representing a slave device in a Modbus network. More...

#include <SlaveDevice.h>

+ Inheritance diagram for dynamic_modbus_master::slave::SlaveDevice:
+ Collaboration diagram for dynamic_modbus_master::slave::SlaveDevice:

Public Member Functions

 SlaveDevice (uint8_t address, uint8_t retries)
 A class representing a slave device in a Modbus network.
 
 ~SlaveDevice () override=default
 
template<ModbusData T>
ModbusError writeHolding (uint16_t reg, T data) const
 Writes data to the holding registers of a Modbus slave device.
 
template<ModbusData T>
SlaveReturn< T > readHolding (uint16_t reg) const
 Reads data from the holding registers of a Modbus slave device.
 
template<ModbusData T>
ModbusError writeCoils (uint16_t reg, const T data, uint16_t coilNum) const
 Writes data to the coils of a Modbus slave device.
 
template<ModbusData T>
SlaveReturn< T > readCoils (uint16_t reg, uint16_t coilNum)
 Reads data from the coils of a Modbus slave device.
 
template<ModbusData T>
SlaveReturn< T > readInputs (uint16_t reg)
 Reads data from the input registers of a Modbus slave device.
 
template<ModbusData T>
SlaveReturn< T > readDiscreteInputs (uint16_t reg)
 Reads data from the discrete inputs of a Modbus slave device.
 
- Public Member Functions inherited from dynamic_modbus_master::slave::SlaveDeviceIfc< SlaveDevice >
virtual ~SlaveDeviceIfc ()=default
 
ModbusError writeHolding (uint16_t reg, const D &data) const
 Write data to a holding register.
 
SlaveReturn< D > readHolding (uint16_t reg) const
 Read data from a holding register.
 
ModbusError writeCoils (uint16_t reg, const D &data, uint16_t coilNum)
 Write data to a group of coils.
 
SlaveReturn< D > readCoils (uint16_t reg, uint16_t coilNum)
 Read data from a group of coils.
 
SlaveReturn< D > readInputs (uint16_t reg)
 Read data from input registers.
 
SlaveReturn< D > readDiscreteInputs (uint16_t reg)
 Reads data from discrete inputs in a Modbus slave device.
 

Private Member Functions

ModbusError sendRequest (mb_param_request_t request, void *data) const
 Helper function to send a modbus request.
 

Private Attributes

uint8_t m_address
 
uint8_t m_retries
 

Detailed Description

A class representing a slave device in a Modbus network.

Tested Example Implementation of dynamic_modbus_master::slave::SlaveDeviceIfc, can be used as-is and will be most supported. However this can also serve as a template for a potential custom implementation.

Provides a standardised object to write to and read from a SlaveDevice, whether via Aggregation or via Inheritance.

Constructor & Destructor Documentation

◆ SlaveDevice()

dynamic_modbus_master::slave::SlaveDevice::SlaveDevice ( uint8_t  address,
uint8_t  retries 
)

A class representing a slave device in a Modbus network.

This class provides functionality to initialize and communicate with a slave device using the Modbus protocol.

◆ ~SlaveDevice()

dynamic_modbus_master::slave::SlaveDevice::~SlaveDevice ( )
overridedefault

Member Function Documentation

◆ readCoils()

template<ModbusData T>
SlaveReturn< T > dynamic_modbus_master::slave::SlaveDevice::readCoils ( uint16_t  reg,
uint16_t  coilNum 
)
inline

Reads data from the coils of a Modbus slave device.

This function sends a Modbus request to read data from the coils of a specified register address in a slave device. The function takes the register address and the number of coils to read as parameters and returns a SlaveReturn object containing the read data and any error that occurred.

Template Parameters
TThe type of data to be read from the coils. This type must meet the ModbusData concept requirements.
Parameters
regThe register address to start reading from.
coilNumThe number of coils to read.
Returns
A SlaveReturn object containing the read data and the status of the read request.

◆ readDiscreteInputs()

template<ModbusData T>
SlaveReturn< T > dynamic_modbus_master::slave::SlaveDevice::readDiscreteInputs ( uint16_t  reg)
inline

Reads data from the discrete inputs of a Modbus slave device.

Template Parameters
TThe type of data to be read from the discrete inputs. This type must meet the ModbusData concept requirements.
Parameters
regThe register address to start reading from.
Returns
A SlaveReturn object containing the read data and the status of the read request.

◆ readHolding()

template<ModbusData T>
SlaveReturn< T > dynamic_modbus_master::slave::SlaveDevice::readHolding ( uint16_t  reg) const
inline

Reads data from the holding registers of a Modbus slave device.

This function sends a Modbus request to read data from the holding registers of a specified register address in a slave device. The function takes the register address as a parameter and returns a SlaveReturn object containing the read data and any error that occurred.

Template Parameters
TThe type of data to be read from the holding registers. This type must meet the ModbusData concept requirements.
Parameters
regThe register address to start reading from.
Returns
A SlaveReturn object containing the read data and the status of the read request.

◆ readInputs()

template<ModbusData T>
SlaveReturn< T > dynamic_modbus_master::slave::SlaveDevice::readInputs ( uint16_t  reg)
inline

Reads data from the input registers of a Modbus slave device.

This function sends a Modbus request to read data from the input registers of a specified register address in a slave device. The function takes the register address as a parameter and returns a SlaveReturn object containing the read data and any error that occurred.

Template Parameters
TThe type of data to be read from the input registers. This type must meet the ModbusData concept requirements.
Parameters
regThe register address to start reading from.
Returns
A SlaveReturn object containing the read data and the status of the read request.

◆ sendRequest()

ModbusError dynamic_modbus_master::slave::SlaveDevice::sendRequest ( mb_param_request_t  request,
void data 
) const
private

Helper function to send a modbus request.

This function handles all requests in a consistent manner and if a timeout occurs, re-attempts the request for the specified amount of time.

Parameters
requestStruct containing the request
datavoid* pointing at the target data, in case of reading requests, the data will be written to here, in case of writing requests, the data will be read from here.
Returns
ModbusError containing either the error.
Possible Error Codes:

◆ writeCoils()

template<ModbusData T>
ModbusError dynamic_modbus_master::slave::SlaveDevice::writeCoils ( uint16_t  reg,
const data,
uint16_t  coilNum 
) const
inline

Writes data to the coils of a Modbus slave device.

This function sends a Modbus request to write data to the coils of a specified register address in a slave device. The function takes the register address, the data to write, and the number of coils to write as parameters and returns a ModbusError object indicating the status of the write request.

If the data type T is bool and the number of coils is 1, this function uses Modbus function code 0x05. If the data type T is not bool and the number of coils is greater than 1, this function uses Modbus function code 0x0F. Otherwise, an INVALID_ARG error is returned.

Template Parameters
TThe type of data to be written to the coils. This type must meet the ModbusData concept requirements.
Parameters
regThe register address to start writing to.
dataThe data to write to the coils.
coilNumThe number of coils to write.
Returns
A ModbusError object indicating the status of the write request.

◆ writeHolding()

template<ModbusData T>
ModbusError dynamic_modbus_master::slave::SlaveDevice::writeHolding ( uint16_t  reg,
data 
) const
inline

Writes data to the holding registers of a Modbus slave device.

This function sends a Modbus request to write data to the holding registers of a specified register address in a slave device. The function takes the register address and the data to write as parameters and returns a ModbusError object indicating the status of the write request.

This method determines what Modbus Function to use at compile time, based on the type of the Data provided. If Type T can be represented in a single Holding register, it will use Function Code 0x06, if it can be represented in a Natural Number of Holding Registers larger than 1 it will use Function Code 0x10. If it cannot be represented in a Natural Number of Holding Registers compilation will fail. See dynamic_modbus_master::ModbusData

Template Parameters
TThe type of data to be written to the holding registers. This type must meet the ModbusData concept requirements.
Parameters
regThe register address to start writing to.
dataThe data to write to the holding registers.
Returns
A ModbusError object indicating the status of the write request.

Member Data Documentation

◆ m_address

uint8_t dynamic_modbus_master::slave::SlaveDevice::m_address
private

◆ m_retries

uint8_t dynamic_modbus_master::slave::SlaveDevice::m_retries
private

The documentation for this class was generated from the following files: