ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
esphome::modbus_server::ServerRegister Class Referencefinal

#include <modbus_server.h>

Public Types

template<typename T >
using ReadFn = optional<T> (*)(uint16_t address)
 
template<typename T >
using WriteFn = bool (*)(uint16_t address, T value)
 

Public Member Functions

 ServerRegister (uint16_t address, SensorValueType value_type, uint8_t register_count)
 
template<SensorValueType VALUE_TYPE>
void set_read_lambda (ReadFn< RegisterValueType< VALUE_TYPE > > read_fn)
 VALUE_TYPE must be this register's value_type; read() and write() cast back by it.
 
template<SensorValueType VALUE_TYPE>
void set_write_lambda (WriteFn< RegisterValueType< VALUE_TYPE > > write_fn)
 
bool is_readable () const
 
bool is_writable () const
 
optional< int64_t > read () const
 Calls the read lambda and returns its value as a raw number (floats as their IEEE 754 bits).
 
bool write (int64_t number) const
 Converts a raw number (floats as their IEEE 754 bits) to the register's type and calls the write lambda.
 
const char * format_value (int64_t value, char *buf, size_t buf_size) const
 
void set_allow_partial_read (bool allow_partial_read)
 

Data Fields

uint16_t address {0}
 
SensorValueType value_type {SensorValueType::RAW}
 
uint8_t register_count {0}
 
bool allow_partial_read {false}
 

Static Public Attributes

static constexpr size_t FORMAT_VALUE_BUF_SIZE = 44
 

Detailed Description

Definition at line 23 of file modbus_server.h.

Member Typedef Documentation

◆ ReadFn

template<typename T >
using esphome::modbus_server::ServerRegister::ReadFn = optional<T> (*)(uint16_t address)

Definition at line 25 of file modbus_server.h.

◆ WriteFn

template<typename T >
using esphome::modbus_server::ServerRegister::WriteFn = bool (*)(uint16_t address, T value)

Definition at line 26 of file modbus_server.h.

Constructor & Destructor Documentation

◆ ServerRegister()

esphome::modbus_server::ServerRegister::ServerRegister ( uint16_t address,
SensorValueType value_type,
uint8_t register_count )
inline

Definition at line 28 of file modbus_server.h.

Member Function Documentation

◆ format_value()

const char * esphome::modbus_server::ServerRegister::format_value ( int64_t value,
char * buf,
size_t buf_size ) const

Definition at line 75 of file modbus_server.cpp.

◆ is_readable()

bool esphome::modbus_server::ServerRegister::is_readable ( ) const
inline

Definition at line 40 of file modbus_server.h.

◆ is_writable()

bool esphome::modbus_server::ServerRegister::is_writable ( ) const
inline

Definition at line 41 of file modbus_server.h.

◆ read()

optional< int64_t > esphome::modbus_server::ServerRegister::read ( ) const

Calls the read lambda and returns its value as a raw number (floats as their IEEE 754 bits).

Definition at line 46 of file modbus_server.cpp.

◆ set_allow_partial_read()

void esphome::modbus_server::ServerRegister::set_allow_partial_read ( bool allow_partial_read)
inline

Definition at line 55 of file modbus_server.h.

◆ set_read_lambda()

template<SensorValueType VALUE_TYPE>
void esphome::modbus_server::ServerRegister::set_read_lambda ( ReadFn< RegisterValueType< VALUE_TYPE > > read_fn)
inline

VALUE_TYPE must be this register's value_type; read() and write() cast back by it.

An empty optional declines the read, answering the whole request with SERVICE_DEVICE_FAILURE.

Definition at line 33 of file modbus_server.h.

◆ set_write_lambda()

template<SensorValueType VALUE_TYPE>
void esphome::modbus_server::ServerRegister::set_write_lambda ( WriteFn< RegisterValueType< VALUE_TYPE > > write_fn)
inline

Definition at line 36 of file modbus_server.h.

◆ write()

bool esphome::modbus_server::ServerRegister::write ( int64_t number) const

Converts a raw number (floats as their IEEE 754 bits) to the register's type and calls the write lambda.

Definition at line 62 of file modbus_server.cpp.

Field Documentation

◆ address

uint16_t esphome::modbus_server::ServerRegister::address {0}

Definition at line 57 of file modbus_server.h.

◆ allow_partial_read

bool esphome::modbus_server::ServerRegister::allow_partial_read {false}

Definition at line 61 of file modbus_server.h.

◆ FORMAT_VALUE_BUF_SIZE

size_t esphome::modbus_server::ServerRegister::FORMAT_VALUE_BUF_SIZE = 44
staticconstexpr

Definition at line 49 of file modbus_server.h.

◆ register_count

uint8_t esphome::modbus_server::ServerRegister::register_count {0}

Definition at line 59 of file modbus_server.h.

◆ value_type

SensorValueType esphome::modbus_server::ServerRegister::value_type {SensorValueType::RAW}

Definition at line 58 of file modbus_server.h.


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