ESPHome  2022.11.3
Data Structures | Typedefs | Enumerations | Functions
esphome::modbus_controller Namespace Reference

Data Structures

class  ModbusBinaryOutput
 
class  ModbusBinarySensor
 
class  ModbusCommandItem
 
class  ModbusController
 Modbus controller class. More...
 
class  ModbusFloatOutput
 
class  ModbusNumber
 
class  ModbusSelect
 
class  ModbusSensor
 
class  ModbusSwitch
 
class  ModbusTextSensor
 
struct  RegisterRange
 
class  SensorItem
 
class  SensorItemsComparator
 

Typedefs

using SensorSet = std::set< SensorItem *, SensorItemsComparator >
 
using value_to_data_t = std::function< float >(float)
 

Enumerations

enum  ModbusFunctionCode {
  ModbusFunctionCode::CUSTOM = 0x00, ModbusFunctionCode::READ_COILS = 0x01, ModbusFunctionCode::READ_DISCRETE_INPUTS = 0x02, ModbusFunctionCode::READ_HOLDING_REGISTERS = 0x03,
  ModbusFunctionCode::READ_INPUT_REGISTERS = 0x04, ModbusFunctionCode::WRITE_SINGLE_COIL = 0x05, ModbusFunctionCode::WRITE_SINGLE_REGISTER = 0x06, ModbusFunctionCode::READ_EXCEPTION_STATUS = 0x07,
  ModbusFunctionCode::DIAGNOSTICS = 0x08, ModbusFunctionCode::GET_COMM_EVENT_COUNTER = 0x0B, ModbusFunctionCode::GET_COMM_EVENT_LOG = 0x0C, ModbusFunctionCode::WRITE_MULTIPLE_COILS = 0x0F,
  ModbusFunctionCode::WRITE_MULTIPLE_REGISTERS = 0x10, ModbusFunctionCode::REPORT_SERVER_ID = 0x11, ModbusFunctionCode::READ_FILE_RECORD = 0x14, ModbusFunctionCode::WRITE_FILE_RECORD = 0x15,
  ModbusFunctionCode::MASK_WRITE_REGISTER = 0x16, ModbusFunctionCode::READ_WRITE_MULTIPLE_REGISTERS = 0x17, ModbusFunctionCode::READ_FIFO_QUEUE = 0x18
}
 
enum  ModbusRegisterType : uint8_t {
  ModbusRegisterType::CUSTOM = 0x0, ModbusRegisterType::COIL = 0x01, ModbusRegisterType::DISCRETE_INPUT = 0x02, ModbusRegisterType::HOLDING = 0x03,
  ModbusRegisterType::READ = 0x04
}
 
enum  SensorValueType : uint8_t {
  SensorValueType::RAW = 0x00, SensorValueType::U_WORD = 0x1, SensorValueType::U_DWORD = 0x2, SensorValueType::S_WORD = 0x3,
  SensorValueType::S_DWORD = 0x4, SensorValueType::BIT = 0x5, SensorValueType::U_DWORD_R = 0x6, SensorValueType::S_DWORD_R = 0x7,
  SensorValueType::U_QWORD = 0x8, SensorValueType::S_QWORD = 0x9, SensorValueType::U_QWORD_R = 0xA, SensorValueType::S_QWORD_R = 0xB,
  SensorValueType::FP32 = 0xC, SensorValueType::FP32_R = 0xD
}
 
enum  RawEncoding { RawEncoding::NONE = 0, RawEncoding::HEXBYTES = 1, RawEncoding::COMMA = 2 }
 

Functions

void number_to_payload (std::vector< uint16_t > &data, int64_t value, SensorValueType value_type)
 Convert float value to vector<uint16_t> suitable for sending. More...
 
int64_t payload_to_number (const std::vector< uint8_t > &data, SensorValueType sensor_value_type, uint8_t offset, uint32_t bitmask)
 Convert vector<uint8_t> response payload to number. More...
 
ModbusFunctionCode modbus_register_read_function (ModbusRegisterType reg_type)
 
ModbusFunctionCode modbus_register_write_function (ModbusRegisterType reg_type)
 
uint8_t c_to_hex (char c)
 
uint8_t byte_from_hex_str (const std::string &value, uint8_t pos)
 Get a byte from a hex string hex_byte_from_str("1122",1) returns uint_8 value 0x22 == 34 hex_byte_from_str("1122",0) returns 0x11. More...
 
uint16_t word_from_hex_str (const std::string &value, uint8_t pos)
 Get a word from a hex string. More...
 
uint32_t dword_from_hex_str (const std::string &value, uint8_t pos)
 Get a dword from a hex string. More...
 
uint64_t qword_from_hex_str (const std::string &value, uint8_t pos)
 Get a qword from a hex string. More...
 
template<typename T >
get_data (const std::vector< uint8_t > &data, size_t buffer_offset)
 Extract data from modbus response buffer. More...
 
bool coil_from_vector (int coil, const std::vector< uint8_t > &data)
 Extract coil data from modbus response buffer Responses for coil are packed into bytes . More...
 
template<typename N >
mask_and_shift_by_rightbit (N data, uint32_t mask)
 Extract bits from value and shift right according to the bitmask if the bitmask is 0x00F0 we want the values frrom bit 5 - 8. More...
 
float payload_to_float (const std::vector< uint8_t > &data, const SensorItem &item)
 Convert vector<uint8_t> response payload to float. More...
 
std::vector< uint16_t > float_to_payload (float value, SensorValueType value_type)
 

Typedef Documentation

◆ SensorSet

Definition at line 285 of file modbus_controller.h.

◆ value_to_data_t

using esphome::modbus_controller::value_to_data_t = typedef std::function<float>(float)

Definition at line 10 of file modbus_number.h.

Enumeration Type Documentation

◆ ModbusFunctionCode

Enumerator
CUSTOM 
READ_COILS 
READ_DISCRETE_INPUTS 
READ_HOLDING_REGISTERS 
READ_INPUT_REGISTERS 
WRITE_SINGLE_COIL 
WRITE_SINGLE_REGISTER 
READ_EXCEPTION_STATUS 
DIAGNOSTICS 
GET_COMM_EVENT_COUNTER 
GET_COMM_EVENT_LOG 
WRITE_MULTIPLE_COILS 
WRITE_MULTIPLE_REGISTERS 
REPORT_SERVER_ID 
READ_FILE_RECORD 
WRITE_FILE_RECORD 
MASK_WRITE_REGISTER 
READ_WRITE_MULTIPLE_REGISTERS 
READ_FIFO_QUEUE 

Definition at line 18 of file modbus_controller.h.

◆ ModbusRegisterType

Enumerator
CUSTOM 
COIL 
DISCRETE_INPUT 
HOLDING 
READ 

Definition at line 40 of file modbus_controller.h.

◆ RawEncoding

Enumerator
NONE 
HEXBYTES 
COMMA 

Definition at line 10 of file modbus_textsensor.h.

◆ SensorValueType

Enumerator
RAW 
U_WORD 
U_DWORD 
S_WORD 
S_DWORD 
BIT 
U_DWORD_R 
S_DWORD_R 
U_QWORD 
S_QWORD 
U_QWORD_R 
S_QWORD_R 
FP32 
FP32_R 

Definition at line 48 of file modbus_controller.h.

Function Documentation

◆ byte_from_hex_str()

uint8_t esphome::modbus_controller::byte_from_hex_str ( const std::string &  value,
uint8_t  pos 
)
inline

Get a byte from a hex string hex_byte_from_str("1122",1) returns uint_8 value 0x22 == 34 hex_byte_from_str("1122",0) returns 0x11.

Parameters
valuestring containing hex encoding
positionoffset in bytes. Because each byte is encoded in 2 hex digits the position of the original byte in the hex string is byte_pos * 2
Returns
byte value

Definition at line 112 of file modbus_controller.h.

◆ c_to_hex()

uint8_t esphome::modbus_controller::c_to_hex ( char  c)
inline

Definition at line 102 of file modbus_controller.h.

◆ coil_from_vector()

bool esphome::modbus_controller::coil_from_vector ( int  coil,
const std::vector< uint8_t > &  data 
)
inline

Extract coil data from modbus response buffer Responses for coil are packed into bytes .

coil 3 is bit 3 of the first response byte coil 9 is bit 2 of the second response byte

Parameters
coilnumber of the cil
datamodbus response buffer (uint8_t)
Returns
content of coil register

Definition at line 181 of file modbus_controller.h.

◆ dword_from_hex_str()

uint32_t esphome::modbus_controller::dword_from_hex_str ( const std::string &  value,
uint8_t  pos 
)
inline

Get a dword from a hex string.

Parameters
valuestring containing hex encoding
positionoffset in bytes. Because each byte is encoded in 2 hex digits the position of the original byte in the hex string is byte_pos * 2
Returns
dword value

Definition at line 134 of file modbus_controller.h.

◆ float_to_payload()

std::vector<uint16_t> esphome::modbus_controller::float_to_payload ( float  value,
SensorValueType  value_type 
)
inline

Definition at line 482 of file modbus_controller.h.

◆ get_data()

template<typename T >
T esphome::modbus_controller::get_data ( const std::vector< uint8_t > &  data,
size_t  buffer_offset 
)

Extract data from modbus response buffer.

Parameters
Tone of supported integer data types int_8,int_16,int_32,int_64
datamodbus response buffer (uint8_t)
buffer_offsetoffset in bytes.
Returns
value of type T extracted from buffer

Definition at line 155 of file modbus_controller.h.

◆ mask_and_shift_by_rightbit()

template<typename N >
N esphome::modbus_controller::mask_and_shift_by_rightbit ( data,
uint32_t  mask 
)

Extract bits from value and shift right according to the bitmask if the bitmask is 0x00F0 we want the values frrom bit 5 - 8.

the result is then shifted right by the position if the first right set bit in the mask Useful for modbus data where more than one value is packed in a 16 bit register Example: on Epever the "Length of night" register 0x9065 encodes values of the whole night length of time as D15 - D8 = hour, D7 - D0 = minute To get the hours use mask 0xFF00 and 0x00FF for the minute

Parameters
dataan integral value between 16 aand 32 bits,
bitmaskthe bitmask to apply

Definition at line 196 of file modbus_controller.h.

◆ modbus_register_read_function()

ModbusFunctionCode esphome::modbus_controller::modbus_register_read_function ( ModbusRegisterType  reg_type)
inline

Definition at line 65 of file modbus_controller.h.

◆ modbus_register_write_function()

ModbusFunctionCode esphome::modbus_controller::modbus_register_write_function ( ModbusRegisterType  reg_type)
inline

Definition at line 84 of file modbus_controller.h.

◆ number_to_payload()

void esphome::modbus_controller::number_to_payload ( std::vector< uint16_t > &  data,
int64_t  value,
SensorValueType  value_type 
)

Convert float value to vector<uint16_t> suitable for sending.

Parameters
datatarget for payload
valuefloat value to convert
value_typedefines if 16/32 or FP32 is used
Returns
vector containing the modbus register words in correct order

Definition at line 500 of file modbus_controller.cpp.

◆ payload_to_float()

float esphome::modbus_controller::payload_to_float ( const std::vector< uint8_t > &  data,
const SensorItem item 
)
inline

Convert vector<uint8_t> response payload to float.

Parameters
datapayload with data
itemSensorItem object
Returns
float value of data

Definition at line 469 of file modbus_controller.h.

◆ payload_to_number()

int64_t esphome::modbus_controller::payload_to_number ( const std::vector< uint8_t > &  data,
SensorValueType  sensor_value_type,
uint8_t  offset,
uint32_t  bitmask 
)

Convert vector<uint8_t> response payload to number.

Parameters
datapayload with the data to convert
sensor_value_typedefines if 16/32/64 bits or FP32 is used
offsetoffset to the data in data
bitmaskbitmask used for masking and shifting
Returns
64-bit number of the payload

Definition at line 539 of file modbus_controller.cpp.

◆ qword_from_hex_str()

uint64_t esphome::modbus_controller::qword_from_hex_str ( const std::string &  value,
uint8_t  pos 
)
inline

Get a qword from a hex string.

Parameters
valuestring containing hex encoding
positionoffset in bytes. Because each byte is encoded in 2 hex digits the position of the original byte in the hex string is byte_pos * 2
Returns
qword value

Definition at line 144 of file modbus_controller.h.

◆ word_from_hex_str()

uint16_t esphome::modbus_controller::word_from_hex_str ( const std::string &  value,
uint8_t  pos 
)
inline

Get a word from a hex string.

Parameters
valuestring containing hex encoding
positionoffset in bytes. Because each byte is encoded in 2 hex digits the position of the original byte in the hex string is byte_pos * 2
Returns
word value

Definition at line 124 of file modbus_controller.h.