A class representing a slave device in a Modbus network.
More...
#include <SlaveDevice.h>
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.
◆ 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 |
◆ readCoils()
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
-
T | The type of data to be read from the coils. This type must meet the ModbusData concept requirements. |
- Parameters
-
reg | The register address to start reading from. |
coilNum | The number of coils to read. |
- Returns
- A
SlaveReturn
object containing the read data and the status of the read request.
◆ readDiscreteInputs()
Reads data from the discrete inputs of a Modbus slave device.
- Template Parameters
-
T | The type of data to be read from the discrete inputs. This type must meet the ModbusData concept requirements. |
- Parameters
-
reg | The register address to start reading from. |
- Returns
- A
SlaveReturn
object containing the read data and the status of the read request.
◆ readHolding()
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
-
T | The type of data to be read from the holding registers. This type must meet the ModbusData concept requirements. |
- Parameters
-
reg | The register address to start reading from. |
- Returns
- A
SlaveReturn
object containing the read data and the status of the read request.
◆ readInputs()
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
-
T | The type of data to be read from the input registers. This type must meet the ModbusData concept requirements. |
- Parameters
-
reg | The register address to start reading from. |
- Returns
- A
SlaveReturn
object containing the read data and the status of the read request.
◆ sendRequest()
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
-
request | Struct containing the request |
data | void* 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()
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
-
T | The type of data to be written to the coils. This type must meet the ModbusData concept requirements. |
- Parameters
-
reg | The register address to start writing to. |
data | The data to write to the coils. |
coilNum | The 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, |
|
|
T |
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
-
T | The type of data to be written to the holding registers. This type must meet the ModbusData concept requirements. |
- Parameters
-
reg | The register address to start writing to. |
data | The data to write to the holding registers. |
- Returns
- A
ModbusError
object indicating the status of the write request.
◆ 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: