ESPHome  2024.10.2
modbus.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include <vector>
7 
8 namespace esphome {
9 namespace modbus {
10 
11 enum ModbusRole {
14 };
15 
16 class ModbusDevice;
17 
18 class Modbus : public uart::UARTDevice, public Component {
19  public:
20  Modbus() = default;
21 
22  void setup() override;
23 
24  void loop() override;
25 
26  void dump_config() override;
27 
28  void register_device(ModbusDevice *device) { this->devices_.push_back(device); }
29 
30  float get_setup_priority() const override;
31 
32  void send(uint8_t address, uint8_t function_code, uint16_t start_address, uint16_t number_of_entities,
33  uint8_t payload_len = 0, const uint8_t *payload = nullptr);
34  void send_raw(const std::vector<uint8_t> &payload);
35  void set_role(ModbusRole role) { this->role = role; }
36  void set_flow_control_pin(GPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; }
38  void set_send_wait_time(uint16_t time_in_ms) { send_wait_time_ = time_in_ms; }
39  void set_disable_crc(bool disable_crc) { disable_crc_ = disable_crc; }
40 
42 
43  protected:
45 
46  bool parse_modbus_byte_(uint8_t byte);
47  uint16_t send_wait_time_{250};
49  std::vector<uint8_t> rx_buffer_;
50  uint32_t last_modbus_byte_{0};
51  uint32_t last_send_{0};
52  std::vector<ModbusDevice *> devices_;
53 };
54 
55 class ModbusDevice {
56  public:
57  void set_parent(Modbus *parent) { parent_ = parent; }
58  void set_address(uint8_t address) { address_ = address; }
59  virtual void on_modbus_data(const std::vector<uint8_t> &data) = 0;
60  virtual void on_modbus_error(uint8_t function_code, uint8_t exception_code) {}
61  virtual void on_modbus_read_registers(uint8_t function_code, uint16_t start_address, uint16_t number_of_registers){};
62  void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len = 0,
63  const uint8_t *payload = nullptr) {
64  this->parent_->send(this->address_, function, start_address, number_of_entities, payload_len, payload);
65  }
66  void send_raw(const std::vector<uint8_t> &payload) { this->parent_->send_raw(payload); }
67  // If more than one device is connected block sending a new command before a response is received
68  bool waiting_for_response() { return parent_->waiting_for_response != 0; }
69 
70  protected:
71  friend Modbus;
72 
74  uint8_t address_;
75 };
76 
77 } // namespace modbus
78 } // namespace esphome
std::vector< uint8_t > rx_buffer_
Definition: modbus.h:49
bool parse_modbus_byte_(uint8_t byte)
Definition: modbus.cpp:38
void set_send_wait_time(uint16_t time_in_ms)
Definition: modbus.h:38
void set_role(ModbusRole role)
Definition: modbus.h:35
uint32_t last_send_
Definition: modbus.h:51
void send_raw(const std::vector< uint8_t > &payload)
Definition: modbus.cpp:215
void send(uint8_t address, uint8_t function_code, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
Definition: modbus.cpp:162
std::vector< ModbusDevice * > devices_
Definition: modbus.h:52
float get_setup_priority() const override
Definition: modbus.cpp:157
UARTComponent * parent_
Definition: uart.h:68
GPIOPin * flow_control_pin_
Definition: modbus.h:44
void set_flow_control_pin(GPIOPin *flow_control_pin)
Definition: modbus.h:36
void dump_config() override
Definition: modbus.cpp:151
ModbusRole role
Definition: modbus.h:41
uint16_t send_wait_time_
Definition: modbus.h:47
void send_raw(const std::vector< uint8_t > &payload)
Definition: modbus.h:66
void set_disable_crc(bool disable_crc)
Definition: modbus.h:39
void set_address(uint8_t address)
Definition: modbus.h:58
void set_parent(Modbus *parent)
Definition: modbus.h:57
uint32_t last_modbus_byte_
Definition: modbus.h:50
uint8_t waiting_for_response
Definition: modbus.h:37
virtual void on_modbus_error(uint8_t function_code, uint8_t exception_code)
Definition: modbus.h:60
void loop() override
Definition: modbus.cpp:15
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
void register_device(ModbusDevice *device)
Definition: modbus.h:28
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
Definition: modbus.h:62
virtual void on_modbus_read_registers(uint8_t function_code, uint16_t start_address, uint16_t number_of_registers)
Definition: modbus.h:61
void setup() override
Definition: modbus.cpp:10