mirror of
https://github.com/esphome/esphome.git
synced 2026-10-06 19:06:37 +00:00
[modbus] Rename core enums to EntityType, FunctionCode and ExceptionCode (#17844)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
5e2d428e69
commit
9ac4039c01
@@ -6,7 +6,7 @@ from esphome.components import modbus
|
||||
from esphome.components.modbus.helpers import (
|
||||
MODBUS_REGISTER_TYPE,
|
||||
TYPE_REGISTER_MAP,
|
||||
ModbusRegisterType,
|
||||
EntityType,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ADDRESS, CONF_ID, CONF_LAMBDA, CONF_NAME, CONF_OFFSET
|
||||
@@ -217,13 +217,13 @@ async def register_modbus_device(var, config):
|
||||
|
||||
def function_code_to_register(function_code):
|
||||
FUNCTION_CODE_TYPE_MAP = {
|
||||
"read_coils": ModbusRegisterType.COIL,
|
||||
"read_discrete_inputs": ModbusRegisterType.DISCRETE_INPUT,
|
||||
"read_holding_registers": ModbusRegisterType.HOLDING,
|
||||
"read_input_registers": ModbusRegisterType.INPUT_REGISTER,
|
||||
"write_single_coil": ModbusRegisterType.COIL,
|
||||
"write_single_register": ModbusRegisterType.HOLDING,
|
||||
"write_multiple_coils": ModbusRegisterType.COIL,
|
||||
"write_multiple_registers": ModbusRegisterType.HOLDING,
|
||||
"read_coils": EntityType.COIL,
|
||||
"read_discrete_inputs": EntityType.DISCRETE_INPUT,
|
||||
"read_holding_registers": EntityType.HOLDING,
|
||||
"read_input_registers": EntityType.INPUT_REGISTER,
|
||||
"write_single_coil": EntityType.COIL,
|
||||
"write_single_register": EntityType.HOLDING,
|
||||
"write_multiple_coils": EntityType.COIL,
|
||||
"write_multiple_registers": EntityType.HOLDING,
|
||||
}
|
||||
return FUNCTION_CODE_TYPE_MAP[function_code]
|
||||
|
||||
@@ -11,8 +11,8 @@ void ModbusBinarySensor::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
bool value;
|
||||
|
||||
switch (this->register_type) {
|
||||
case ModbusRegisterType::DISCRETE_INPUT:
|
||||
case ModbusRegisterType::COIL:
|
||||
case EntityType::DISCRETE_INPUT:
|
||||
case EntityType::COIL:
|
||||
// offset for coil is the actual number of the coil not the byte offset
|
||||
value = modbus::helpers::bit_from_packed(this->offset, data);
|
||||
break;
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace esphome::modbus_controller {
|
||||
|
||||
class ModbusBinarySensor final : public Component, public binary_sensor::BinarySensor, public SensorItem {
|
||||
public:
|
||||
ModbusBinarySensor(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
ModbusBinarySensor(EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
@@ -20,7 +20,7 @@ class ModbusBinarySensor final : public Component, public binary_sensor::BinaryS
|
||||
this->skip_updates = skip_updates;
|
||||
this->force_new_range = force_new_range;
|
||||
|
||||
if (register_type == ModbusRegisterType::COIL || register_type == ModbusRegisterType::DISCRETE_INPUT) {
|
||||
if (register_type == EntityType::COIL || register_type == EntityType::DISCRETE_INPUT) {
|
||||
this->register_count = offset + 1;
|
||||
} else {
|
||||
this->register_count = 1;
|
||||
|
||||
@@ -95,7 +95,7 @@ void ModbusController::process_modbus_data_(const ModbusCommandItem *response) {
|
||||
response->on_data_func(response->register_type, response->register_address, response->payload);
|
||||
}
|
||||
|
||||
void ModbusController::on_error(std::span<const uint8_t> request_pdu, modbus::ModbusExceptionCode exception_code) {
|
||||
void ModbusController::on_error(std::span<const uint8_t> request_pdu, modbus::ExceptionCode exception_code) {
|
||||
// The request function code (request_pdu[0]) already carries what the log needs; the exception bit only
|
||||
// ever appears on the response, so no masking is needed here.
|
||||
const uint8_t function_code = request_pdu.empty() ? 0 : request_pdu[0];
|
||||
@@ -116,7 +116,7 @@ void ModbusController::on_error(std::span<const uint8_t> request_pdu, modbus::Mo
|
||||
}
|
||||
}
|
||||
|
||||
SensorSet ModbusController::find_sensors_(ModbusRegisterType register_type, uint16_t start_address) const {
|
||||
SensorSet ModbusController::find_sensors_(EntityType register_type, uint16_t start_address) const {
|
||||
auto reg_it = std::find_if(
|
||||
std::begin(this->register_ranges_), std::end(this->register_ranges_),
|
||||
[=](RegisterRange const &r) { return (r.start_address == start_address && r.register_type == register_type); });
|
||||
@@ -130,7 +130,7 @@ SensorSet ModbusController::find_sensors_(ModbusRegisterType register_type, uint
|
||||
// not found
|
||||
return {};
|
||||
}
|
||||
void ModbusController::on_register_data(ModbusRegisterType register_type, uint16_t start_address,
|
||||
void ModbusController::on_register_data(EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
ESP_LOGV(TAG, "data for register address : 0x%X : ", start_address);
|
||||
|
||||
@@ -164,18 +164,18 @@ void ModbusController::update_range_(RegisterRange &r) {
|
||||
r.skip_updates_counter);
|
||||
if (r.skip_updates_counter == 0) {
|
||||
// if a custom command is used the user supplied custom_data is only available in the SensorItem.
|
||||
if (r.register_type == ModbusRegisterType::CUSTOM) {
|
||||
if (r.register_type == EntityType::CUSTOM) {
|
||||
auto sensors = this->find_sensors_(r.register_type, r.start_address);
|
||||
if (!sensors.empty()) {
|
||||
auto sensor = sensors.cbegin();
|
||||
auto command_item = ModbusCommandItem::create_custom_command(
|
||||
this, (*sensor)->custom_data,
|
||||
[this](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->on_register_data(ModbusRegisterType::CUSTOM, start_address, data);
|
||||
[this](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->on_register_data(EntityType::CUSTOM, start_address, data);
|
||||
});
|
||||
command_item.register_address = (*sensor)->start_address;
|
||||
command_item.register_count = (*sensor)->register_count;
|
||||
command_item.function_code = ModbusFunctionCode::CUSTOM;
|
||||
command_item.function_code = FunctionCode::CUSTOM;
|
||||
queue_command(command_item);
|
||||
}
|
||||
} else {
|
||||
@@ -237,7 +237,7 @@ size_t ModbusController::create_register_ranges_() {
|
||||
// this is not the first register in range so it might be possible
|
||||
// to reuse the last register or extend the current range
|
||||
if (!curr->force_new_range && r.register_type == curr->register_type &&
|
||||
curr->register_type != ModbusRegisterType::CUSTOM) {
|
||||
curr->register_type != EntityType::CUSTOM) {
|
||||
if (curr->start_address == (r.start_address + r.register_count - prev->register_count) &&
|
||||
curr->register_count == prev->register_count && curr->get_register_size() == prev->get_register_size()) {
|
||||
// this register can re-use the data from the previous register
|
||||
@@ -347,7 +347,7 @@ void ModbusController::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
void ModbusController::on_write_register_response(ModbusRegisterType register_type, uint16_t start_address,
|
||||
void ModbusController::on_write_register_response(EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
ESP_LOGV(TAG, "Command ACK 0x%X %d ", modbus::helpers::get_data<uint16_t>(data, 0),
|
||||
modbus::helpers::get_data<int16_t>(data, 1));
|
||||
@@ -362,9 +362,8 @@ void ModbusController::dump_sensors_() {
|
||||
}
|
||||
|
||||
ModbusCommandItem ModbusCommandItem::create_read_command(
|
||||
ModbusController *modbusdevice, ModbusRegisterType register_type, uint16_t start_address, uint16_t register_count,
|
||||
std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler) {
|
||||
ModbusController *modbusdevice, EntityType register_type, uint16_t start_address, uint16_t register_count,
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)> &&handler) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = register_type;
|
||||
@@ -375,16 +374,15 @@ ModbusCommandItem ModbusCommandItem::create_read_command(
|
||||
return cmd;
|
||||
}
|
||||
|
||||
ModbusCommandItem ModbusCommandItem::create_read_command(ModbusController *modbusdevice,
|
||||
ModbusRegisterType register_type, uint16_t start_address,
|
||||
uint16_t register_count) {
|
||||
ModbusCommandItem ModbusCommandItem::create_read_command(ModbusController *modbusdevice, EntityType register_type,
|
||||
uint16_t start_address, uint16_t register_count) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = register_type;
|
||||
cmd.function_code = modbus::helpers::modbus_register_read_function(register_type);
|
||||
cmd.register_address = start_address;
|
||||
cmd.register_count = register_count;
|
||||
cmd.on_data_func = [modbusdevice](ModbusRegisterType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice](EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_register_data(register_type, start_address, data);
|
||||
};
|
||||
@@ -396,11 +394,11 @@ ModbusCommandItem ModbusCommandItem::create_write_multiple_command(ModbusControl
|
||||
const std::vector<uint16_t> &values) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = ModbusRegisterType::HOLDING;
|
||||
cmd.function_code = ModbusFunctionCode::WRITE_MULTIPLE_REGISTERS;
|
||||
cmd.register_type = EntityType::HOLDING;
|
||||
cmd.function_code = FunctionCode::WRITE_MULTIPLE_REGISTERS;
|
||||
cmd.register_address = start_address;
|
||||
cmd.register_count = register_count;
|
||||
cmd.on_data_func = [modbusdevice, cmd](ModbusRegisterType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice, cmd](EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_write_register_response(cmd.register_type, start_address, data);
|
||||
};
|
||||
@@ -416,11 +414,11 @@ ModbusCommandItem ModbusCommandItem::create_write_single_coil(ModbusController *
|
||||
bool value) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = ModbusRegisterType::COIL;
|
||||
cmd.function_code = ModbusFunctionCode::WRITE_SINGLE_COIL;
|
||||
cmd.register_type = EntityType::COIL;
|
||||
cmd.function_code = FunctionCode::WRITE_SINGLE_COIL;
|
||||
cmd.register_address = address;
|
||||
cmd.register_count = 1;
|
||||
cmd.on_data_func = [modbusdevice, cmd](ModbusRegisterType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice, cmd](EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_write_register_response(cmd.register_type, start_address, data);
|
||||
};
|
||||
@@ -433,11 +431,11 @@ ModbusCommandItem ModbusCommandItem::create_write_multiple_coils(ModbusControlle
|
||||
const std::vector<bool> &values) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = ModbusRegisterType::COIL;
|
||||
cmd.function_code = ModbusFunctionCode::WRITE_MULTIPLE_COILS;
|
||||
cmd.register_type = EntityType::COIL;
|
||||
cmd.function_code = FunctionCode::WRITE_MULTIPLE_COILS;
|
||||
cmd.register_address = start_address;
|
||||
cmd.register_count = values.size();
|
||||
cmd.on_data_func = [modbusdevice, cmd](ModbusRegisterType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice, cmd](EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_write_register_response(cmd.register_type, start_address, data);
|
||||
};
|
||||
@@ -465,11 +463,11 @@ ModbusCommandItem ModbusCommandItem::create_write_single_command(ModbusControlle
|
||||
uint16_t value) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.register_type = ModbusRegisterType::HOLDING;
|
||||
cmd.function_code = ModbusFunctionCode::WRITE_SINGLE_REGISTER;
|
||||
cmd.register_type = EntityType::HOLDING;
|
||||
cmd.function_code = FunctionCode::WRITE_SINGLE_REGISTER;
|
||||
cmd.register_address = start_address;
|
||||
cmd.register_count = 1; // not used here anyways
|
||||
cmd.on_data_func = [modbusdevice, cmd](ModbusRegisterType register_type, uint16_t start_address,
|
||||
cmd.on_data_func = [modbusdevice, cmd](EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
modbusdevice->on_write_register_response(cmd.register_type, start_address, data);
|
||||
};
|
||||
@@ -482,13 +480,12 @@ ModbusCommandItem ModbusCommandItem::create_write_single_command(ModbusControlle
|
||||
|
||||
ModbusCommandItem ModbusCommandItem::create_custom_command(
|
||||
ModbusController *modbusdevice, const std::vector<uint8_t> &values,
|
||||
std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler) {
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)> &&handler) {
|
||||
ModbusCommandItem cmd;
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.function_code = ModbusFunctionCode::CUSTOM;
|
||||
cmd.function_code = FunctionCode::CUSTOM;
|
||||
if (handler == nullptr) {
|
||||
cmd.on_data_func = [](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
cmd.on_data_func = [](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
ESP_LOGI(TAG, "Custom Command sent");
|
||||
};
|
||||
} else {
|
||||
@@ -501,13 +498,12 @@ ModbusCommandItem ModbusCommandItem::create_custom_command(
|
||||
|
||||
ModbusCommandItem ModbusCommandItem::create_custom_command(
|
||||
ModbusController *modbusdevice, const std::vector<uint16_t> &values,
|
||||
std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler) {
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)> &&handler) {
|
||||
ModbusCommandItem cmd = {};
|
||||
cmd.modbusdevice = modbusdevice;
|
||||
cmd.function_code = ModbusFunctionCode::CUSTOM;
|
||||
cmd.function_code = FunctionCode::CUSTOM;
|
||||
if (handler == nullptr) {
|
||||
cmd.on_data_func = [](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
cmd.on_data_func = [](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
ESP_LOGI(TAG, "Custom Command sent");
|
||||
};
|
||||
} else {
|
||||
@@ -522,7 +518,7 @@ ModbusCommandItem ModbusCommandItem::create_custom_command(
|
||||
}
|
||||
|
||||
bool ModbusCommandItem::send() {
|
||||
if (this->function_code != ModbusFunctionCode::CUSTOM) {
|
||||
if (this->function_code != FunctionCode::CUSTOM) {
|
||||
modbusdevice->send(uint8_t(this->function_code), this->register_address, this->register_count, this->payload.size(),
|
||||
this->payload.empty() ? nullptr : &this->payload[0]);
|
||||
} else {
|
||||
@@ -537,7 +533,7 @@ bool ModbusCommandItem::send() {
|
||||
bool ModbusCommandItem::is_equal(const ModbusCommandItem &other) {
|
||||
// for custom commands we have to check for identical payloads, since
|
||||
// address/count/type fields will be set to zero
|
||||
return this->function_code == ModbusFunctionCode::CUSTOM
|
||||
return this->function_code == FunctionCode::CUSTOM
|
||||
? this->payload == other.payload
|
||||
: other.register_address == this->register_address && other.register_count == this->register_count &&
|
||||
other.register_type == this->register_type && other.function_code == this->function_code;
|
||||
|
||||
@@ -17,22 +17,30 @@ namespace esphome::modbus_controller {
|
||||
|
||||
class ModbusController;
|
||||
|
||||
using modbus::EntityType;
|
||||
using modbus::ExceptionCode;
|
||||
using modbus::FunctionCode;
|
||||
using modbus::helpers::SensorValueType;
|
||||
|
||||
// Remove before 2027.2.0 - deprecated names re-exported so external components keep their warning window
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
using modbus::ModbusExceptionCode;
|
||||
using modbus::ModbusFunctionCode;
|
||||
using modbus::ModbusRegisterType;
|
||||
using modbus::ModbusExceptionCode;
|
||||
using modbus::helpers::SensorValueType;
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
// Remove before 2026.10.0 — these helpers have moved to modbus::helpers
|
||||
ESPDEPRECATED("Use modbus::helpers::value_type_is_float() instead. Removed in 2026.10.0", "2026.4.0")
|
||||
inline bool value_type_is_float(SensorValueType v) { return modbus::helpers::value_type_is_float(v); }
|
||||
|
||||
ESPDEPRECATED("Use modbus::helpers::modbus_register_read_function() instead. Removed in 2026.10.0", "2026.4.0")
|
||||
inline ModbusFunctionCode modbus_register_read_function(ModbusRegisterType reg_type) {
|
||||
inline FunctionCode modbus_register_read_function(EntityType reg_type) {
|
||||
return modbus::helpers::modbus_register_read_function(reg_type);
|
||||
}
|
||||
|
||||
ESPDEPRECATED("Use modbus::helpers::modbus_register_write_function() instead. Removed in 2026.10.0", "2026.4.0")
|
||||
inline ModbusFunctionCode modbus_register_write_function(ModbusRegisterType reg_type) {
|
||||
inline FunctionCode modbus_register_write_function(EntityType reg_type) {
|
||||
return modbus::helpers::modbus_register_write_function(reg_type);
|
||||
}
|
||||
|
||||
@@ -102,7 +110,7 @@ class SensorItem {
|
||||
|
||||
void set_custom_data(const std::vector<uint8_t> &data) { custom_data = data; }
|
||||
size_t virtual get_register_size() const {
|
||||
if (register_type == ModbusRegisterType::COIL || register_type == ModbusRegisterType::DISCRETE_INPUT) {
|
||||
if (register_type == EntityType::COIL || register_type == EntityType::DISCRETE_INPUT) {
|
||||
return 1;
|
||||
} else { // if CONF_RESPONSE_BYTES is used override the default
|
||||
return response_bytes > 0 ? response_bytes : register_count * 2;
|
||||
@@ -110,7 +118,7 @@ class SensorItem {
|
||||
}
|
||||
// Override register size for modbus devices not using 1 register for one dword
|
||||
void set_register_size(uint8_t register_size) { response_bytes = register_size; }
|
||||
ModbusRegisterType register_type{ModbusRegisterType::CUSTOM};
|
||||
EntityType register_type{EntityType::CUSTOM};
|
||||
SensorValueType sensor_value_type{SensorValueType::RAW};
|
||||
uint16_t start_address{0};
|
||||
uint32_t bitmask{0};
|
||||
@@ -157,7 +165,7 @@ using SensorSet = std::set<SensorItem *, SensorItemsComparator>;
|
||||
|
||||
struct RegisterRange {
|
||||
uint16_t start_address;
|
||||
ModbusRegisterType register_type;
|
||||
EntityType register_type;
|
||||
uint8_t register_count;
|
||||
uint16_t skip_updates; // the config value
|
||||
SensorSet sensors; // all sensors of this range
|
||||
@@ -170,10 +178,9 @@ class ModbusCommandItem {
|
||||
ModbusController *modbusdevice{nullptr};
|
||||
uint16_t register_address{0};
|
||||
uint16_t register_count{0};
|
||||
ModbusFunctionCode function_code{ModbusFunctionCode::CUSTOM};
|
||||
ModbusRegisterType register_type{ModbusRegisterType::CUSTOM};
|
||||
std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
on_data_func;
|
||||
FunctionCode function_code{FunctionCode::CUSTOM};
|
||||
EntityType register_type{EntityType::CUSTOM};
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)> on_data_func;
|
||||
std::vector<uint8_t> payload = {};
|
||||
bool send();
|
||||
/// Check if the command should be retried based on the max_retries parameter
|
||||
@@ -189,10 +196,10 @@ class ModbusCommandItem {
|
||||
* @param handler function called when the response is received
|
||||
* @return ModbusCommandItem with the prepared command
|
||||
*/
|
||||
static ModbusCommandItem create_read_command(
|
||||
ModbusController *modbusdevice, ModbusRegisterType register_type, uint16_t start_address, uint16_t register_count,
|
||||
std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler);
|
||||
static ModbusCommandItem create_read_command(ModbusController *modbusdevice, EntityType register_type,
|
||||
uint16_t start_address, uint16_t register_count,
|
||||
std::function<void(EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data)> &&handler);
|
||||
/** Create modbus read command
|
||||
* Function code 02-04
|
||||
* @param modbusdevice pointer to the device to execute the command
|
||||
@@ -201,7 +208,7 @@ class ModbusCommandItem {
|
||||
* @param register_count number of registers to read
|
||||
* @return ModbusCommandItem with the prepared command
|
||||
*/
|
||||
static ModbusCommandItem create_read_command(ModbusController *modbusdevice, ModbusRegisterType register_type,
|
||||
static ModbusCommandItem create_read_command(ModbusController *modbusdevice, EntityType register_type,
|
||||
uint16_t start_address, uint16_t register_count);
|
||||
/** Create modbus read command
|
||||
* Function code 02-04
|
||||
@@ -251,7 +258,7 @@ class ModbusCommandItem {
|
||||
*/
|
||||
static ModbusCommandItem create_custom_command(
|
||||
ModbusController *modbusdevice, const std::vector<uint8_t> &values,
|
||||
std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler = nullptr);
|
||||
|
||||
/** Create custom modbus command
|
||||
@@ -263,7 +270,7 @@ class ModbusCommandItem {
|
||||
*/
|
||||
static ModbusCommandItem create_custom_command(
|
||||
ModbusController *modbusdevice, const std::vector<uint16_t> &values,
|
||||
std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
std::function<void(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
|
||||
&&handler = nullptr);
|
||||
|
||||
bool is_equal(const ModbusCommandItem &other);
|
||||
@@ -296,13 +303,12 @@ class ModbusController final : public PollingComponent, public modbus::ModbusCli
|
||||
/// called when a modbus response was parsed without errors
|
||||
void on_response(std::span<const uint8_t> request_pdu, std::span<const uint8_t> response_pdu) override;
|
||||
/// called when a modbus error response was received
|
||||
void on_error(std::span<const uint8_t> request_pdu, modbus::ModbusExceptionCode exception_code) override;
|
||||
void on_error(std::span<const uint8_t> request_pdu, modbus::ExceptionCode exception_code) override;
|
||||
/// default delegate called by process_modbus_data when a response has retrieved from the incoming queue
|
||||
void on_register_data(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data);
|
||||
void on_register_data(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data);
|
||||
/// default delegate called by process_modbus_data when a response for a write response has retrieved from the
|
||||
/// incoming queue
|
||||
void on_write_register_response(ModbusRegisterType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data);
|
||||
void on_write_register_response(EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data);
|
||||
/// Allow a duplicate command to be sent
|
||||
void set_allow_duplicate_commands(bool allow_duplicate_commands) {
|
||||
this->allow_duplicate_commands_ = allow_duplicate_commands;
|
||||
@@ -338,7 +344,7 @@ class ModbusController final : public PollingComponent, public modbus::ModbusCli
|
||||
/// parse sensormap_ and create range of sequential addresses
|
||||
size_t create_register_ranges_();
|
||||
// find register in sensormap. Returns iterator with all registers having the same start address
|
||||
SensorSet find_sensors_(ModbusRegisterType register_type, uint16_t start_address) const;
|
||||
SensorSet find_sensors_(EntityType register_type, uint16_t start_address) const;
|
||||
/// submit the read command for the address range to the send queue
|
||||
void update_range_(RegisterRange &r);
|
||||
/// parse incoming modbus data
|
||||
|
||||
@@ -57,7 +57,7 @@ void ModbusNumber::control(float value) {
|
||||
format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
|
||||
write_cmd = ModbusCommandItem::create_custom_command(
|
||||
this->parent_, data,
|
||||
[this, write_cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
[this, write_cmd](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->parent_->on_write_register_response(write_cmd.register_type, this->start_address, data);
|
||||
});
|
||||
} else {
|
||||
@@ -77,7 +77,7 @@ void ModbusNumber::control(float value) {
|
||||
this->parent_, this->start_address + this->offset / 2, this->register_count, data);
|
||||
}
|
||||
// publish new value
|
||||
write_cmd.on_data_func = [this, write_cmd, value](ModbusRegisterType register_type, uint16_t start_address,
|
||||
write_cmd.on_data_func = [this, write_cmd, value](EntityType register_type, uint16_t start_address,
|
||||
const std::vector<uint8_t> &data) {
|
||||
// gets called when the write command is ack'd from the device
|
||||
this->parent_->on_write_register_response(write_cmd.register_type, start_address, data);
|
||||
|
||||
@@ -12,7 +12,7 @@ using value_to_data_t = std::function<float>(float);
|
||||
|
||||
class ModbusNumber final : public number::Number, public Component, public SensorItem {
|
||||
public:
|
||||
ModbusNumber(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
ModbusNumber(EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
SensorValueType value_type, int register_count, uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
|
||||
@@ -89,7 +89,7 @@ void ModbusBinaryOutput::write_state(bool state) {
|
||||
format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
|
||||
cmd = ModbusCommandItem::create_custom_command(
|
||||
this->parent_, data,
|
||||
[this, cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
[this, cmd](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->parent_->on_write_register_response(cmd.register_type, this->start_address, data);
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace esphome::modbus_controller {
|
||||
class ModbusFloatOutput final : public output::FloatOutput, public Component, public SensorItem {
|
||||
public:
|
||||
ModbusFloatOutput(uint16_t start_address, uint8_t offset, SensorValueType value_type, int register_count) {
|
||||
this->register_type = ModbusRegisterType::HOLDING;
|
||||
this->register_type = EntityType::HOLDING;
|
||||
this->start_address = start_address;
|
||||
this->offset = offset;
|
||||
this->bitmask = 0xFFFFFFFF;
|
||||
@@ -44,7 +44,7 @@ class ModbusFloatOutput final : public output::FloatOutput, public Component, pu
|
||||
class ModbusBinaryOutput final : public output::BinaryOutput, public Component, public SensorItem {
|
||||
public:
|
||||
ModbusBinaryOutput(uint16_t start_address, uint8_t offset) {
|
||||
this->register_type = ModbusRegisterType::COIL;
|
||||
this->register_type = EntityType::COIL;
|
||||
this->start_address = start_address;
|
||||
this->bitmask = 0xFFFFFFFF;
|
||||
this->sensor_value_type = SensorValueType::BIT;
|
||||
|
||||
@@ -13,7 +13,7 @@ class ModbusSelect final : public Component, public select::Select, public Senso
|
||||
public:
|
||||
ModbusSelect(SensorValueType sensor_value_type, uint16_t start_address, uint8_t register_count, uint16_t skip_updates,
|
||||
bool force_new_range, std::vector<int64_t> mapping) {
|
||||
this->register_type = ModbusRegisterType::HOLDING; // not configurable
|
||||
this->register_type = EntityType::HOLDING; // not configurable
|
||||
this->sensor_value_type = sensor_value_type;
|
||||
this->start_address = start_address;
|
||||
this->offset = 0; // not configurable
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace esphome::modbus_controller {
|
||||
|
||||
class ModbusSensor final : public Component, public sensor::Sensor, public SensorItem {
|
||||
public:
|
||||
ModbusSensor(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
ModbusSensor(EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
SensorValueType value_type, int register_count, uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
|
||||
@@ -30,8 +30,8 @@ bool ModbusSwitch::assumed_state() { return this->assumed_state_; }
|
||||
void ModbusSwitch::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||
bool value = false;
|
||||
switch (this->register_type) {
|
||||
case ModbusRegisterType::DISCRETE_INPUT:
|
||||
case ModbusRegisterType::COIL:
|
||||
case EntityType::DISCRETE_INPUT:
|
||||
case EntityType::COIL:
|
||||
// offset for coil is the actual number of the coil not the byte offset
|
||||
value = modbus::helpers::bit_from_packed(this->offset, data);
|
||||
break;
|
||||
@@ -82,13 +82,13 @@ void ModbusSwitch::write_state(bool state) {
|
||||
format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
|
||||
cmd = ModbusCommandItem::create_custom_command(
|
||||
this->parent_, data,
|
||||
[this, cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
[this, cmd](EntityType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
|
||||
this->parent_->on_write_register_response(cmd.register_type, this->start_address, data);
|
||||
});
|
||||
} else {
|
||||
ESP_LOGV(TAG, "write_state '%s': new value = %s type = %d address = %X offset = %x", this->get_name().c_str(),
|
||||
ONOFF(state), (int) this->register_type, this->start_address, this->offset);
|
||||
if (this->register_type == ModbusRegisterType::COIL) {
|
||||
if (this->register_type == EntityType::COIL) {
|
||||
// offset for coil and discrete inputs is the coil/register number not bytes
|
||||
if (this->use_write_multiple_) {
|
||||
std::vector<bool> states{state};
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace esphome::modbus_controller {
|
||||
|
||||
class ModbusSwitch final : public Component, public switch_::Switch, public SensorItem {
|
||||
public:
|
||||
ModbusSwitch(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
ModbusSwitch(EntityType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask,
|
||||
uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
@@ -19,7 +19,7 @@ class ModbusSwitch final : public Component, public switch_::Switch, public Sens
|
||||
this->sensor_value_type = SensorValueType::BIT;
|
||||
this->skip_updates = skip_updates;
|
||||
this->register_count = 1;
|
||||
if (register_type == ModbusRegisterType::HOLDING || register_type == ModbusRegisterType::COIL) {
|
||||
if (register_type == EntityType::HOLDING || register_type == EntityType::COIL) {
|
||||
this->start_address += offset;
|
||||
this->offset = 0;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ enum class RawEncoding { NONE = 0, HEXBYTES = 1, COMMA = 2, ANSI = 3 };
|
||||
|
||||
class ModbusTextSensor final : public Component, public text_sensor::TextSensor, public SensorItem {
|
||||
public:
|
||||
ModbusTextSensor(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint8_t register_count,
|
||||
ModbusTextSensor(EntityType register_type, uint16_t start_address, uint8_t offset, uint8_t register_count,
|
||||
uint16_t response_bytes, RawEncoding encode, uint16_t skip_updates, bool force_new_range) {
|
||||
this->register_type = register_type;
|
||||
this->start_address = start_address;
|
||||
|
||||
Reference in New Issue
Block a user