15 namespace modbus_controller {
17 class ModbusController;
103 inline uint8_t
c_to_hex(
char c) {
return (c >=
'A') ? (c >=
'a') ? (c -
'a' + 10) : (c -
'A' + 10) : (c -
'0'); }
114 if (value.length() < pos * 2 + 1)
156 template<
typename T> T
get_data(
const std::vector<uint8_t> &data,
size_t buffer_offset) {
157 if (
sizeof(T) ==
sizeof(uint8_t)) {
158 return T(data[buffer_offset]);
160 if (
sizeof(T) ==
sizeof(uint16_t)) {
161 return T((uint16_t(data[buffer_offset + 0]) << 8) | (uint16_t(data[buffer_offset + 1]) << 0));
164 if (
sizeof(T) ==
sizeof(uint32_t)) {
165 return get_data<uint16_t>(data, buffer_offset) << 16 | get_data<uint16_t>(data, (buffer_offset + 2));
168 if (
sizeof(T) ==
sizeof(uint64_t)) {
169 return static_cast<uint64_t
>(get_data<uint32_t>(data, buffer_offset)) << 32 |
170 (
static_cast<uint64_t
>(get_data<uint32_t>(data, buffer_offset + 4)));
183 auto data_byte = coil / 8;
184 return (data[data_byte] & (1 << (coil % 8))) > 0;
198 auto result = (mask & data);
199 if (result == 0 || mask == 0xFFFFFFFF) {
202 for (
size_t pos = 0; pos <
sizeof(N) << 3; pos++) {
203 if ((mask & (1 << pos)) != 0)
204 return result >> pos;
231 virtual void parse_and_publish(
const std::vector<uint8_t> &data) = 0;
238 return response_bytes > 0 ? response_bytes : register_count * 2;
249 uint8_t response_bytes{0};
251 std::vector<uint8_t> custom_data{};
252 bool force_new_range{
false};
258 std::function<
float()> read_lambda) {
259 this->address = address;
260 this->value_type = value_type;
261 this->register_count = register_count;
262 this->read_lambda = std::move(read_lambda);
301 using SensorSet = std::set<SensorItem *, SensorItemsComparator>;
314 static const size_t MAX_PAYLOAD_BYTES = 240;
315 static const uint8_t MAX_SEND_REPEATS = 5;
321 std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
323 std::vector<uint8_t> payload = {};
327 uint8_t send_countdown{MAX_SEND_REPEATS};
340 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data)>
351 uint16_t start_address, uint16_t register_count);
362 uint16_t register_count,
const std::vector<uint16_t> &values);
390 const std::vector<bool> &values);
400 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data)>
401 &&handler =
nullptr);
412 std::function<
void(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data)>
413 &&handler =
nullptr);
428 void dump_config()
override;
429 void loop()
override;
430 void setup()
override;
431 void update()
override;
440 void on_modbus_data(
const std::vector<uint8_t> &data)
override;
442 void on_modbus_error(uint8_t function_code, uint8_t exception_code)
override;
444 void on_modbus_read_registers(uint8_t function_code, uint16_t start_address, uint16_t number_of_registers)
final;
446 void on_register_data(
ModbusRegisterType register_type, uint16_t start_address,
const std::vector<uint8_t> &data);
449 void on_write_register_response(
ModbusRegisterType register_type, uint16_t start_address,
450 const std::vector<uint8_t> &data);
462 size_t create_register_ranges_();
470 bool send_next_command_();
472 void dump_sensors_();
503 float_value =
bit_cast<
float>(
static_cast<uint32_t
>(number));
505 float_value =
static_cast<float>(number);
517 val = llroundf(value);
520 std::vector<uint16_t> data;
SensorValueType sensor_value_type
void add_server_register(ServerRegister *server_register)
Registers a server register with the controller. Called by esphomes code generator.
bool operator()(const SensorItem *lhs, const SensorItem *rhs) const
uint16_t word_from_hex_str(const std::string &value, uint8_t pos)
Get a word from a hex string.
bool module_offline_
if module didn't respond the last command
std::vector< ServerRegister * > server_registers_
Collection of all server registers for this component.
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...
ModbusRegisterType register_type
std::vector< uint16_t > float_to_payload(float value, SensorValueType value_type)
virtual size_t get_register_size() const
uint16_t command_throttle_
min time in ms between sending modbus commands
This class simplifies creating components that periodically check a state.
T get_data(const std::vector< uint8_t > &data, size_t buffer_offset)
Extract data from modbus response buffer.
uint32_t last_command_timestamp_
when was the last send operation
ModbusRegisterType register_type
SensorValueType value_type
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 ...
void set_register_size(uint8_t register_size)
SensorSet sensorset_
Collection of all sensors for this component.
uint64_t qword_from_hex_str(const std::string &value, uint8_t pos)
Get a qword from a hex string.
void set_offline_skip_updates(uint16_t offline_skip_updates)
called by esphome generated code to set the offline_skip_updates
ServerRegister(uint16_t address, SensorValueType value_type, uint8_t register_count, std::function< float()> read_lambda)
uint32_t dword_from_hex_str(const std::string &value, uint8_t pos)
Get a dword from a hex string.
float payload_to_float(const std::vector< uint8_t > &data, const SensorItem &item)
Convert vector<uint8_t> response payload to float.
size_t get_command_queue_length()
get the number of queued modbus commands (should be mostly empty)
std::function< float()> read_lambda
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.
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_fro...
void add_sensor_item(SensorItem *item)
Registers a sensor with the controller. Called by esphomes code generator.
uint16_t skip_updates_counter
void set_command_throttle(uint16_t command_throttle)
called by esphome generated code to set the command_throttle period
std::list< std::unique_ptr< ModbusCommandItem > > command_queue_
Hold the pending requests to be sent.
ModbusFunctionCode modbus_register_write_function(ModbusRegisterType reg_type)
ModbusFunctionCode modbus_register_read_function(ModbusRegisterType reg_type)
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.
std::vector< RegisterRange > register_ranges_
Continuous range of modbus registers.
bool get_module_offline()
get if the module is offline, didn't respond the last command
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
void set_custom_data(const std::vector< uint8_t > &data)
Implementation of SPI Controller mode.
uint16_t offline_skip_updates_
how many updates to skip if module is offline
ModbusFunctionCode function_code
std::set< SensorItem *, SensorItemsComparator > SensorSet
std::queue< std::unique_ptr< ModbusCommandItem > > incoming_queue_
modbus response data waiting to get processed
ModbusRegisterType register_type
ModbusController * modbusdevice
uint16_t register_address