ESPHome  2024.5.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 class ModbusDevice;
12 
13 class Modbus : public uart::UARTDevice, public Component {
14  public:
15  Modbus() = default;
16 
17  void setup() override;
18 
19  void loop() override;
20 
21  void dump_config() override;
22 
23  void register_device(ModbusDevice *device) { this->devices_.push_back(device); }
24 
25  float get_setup_priority() const override;
26 
27  void send(uint8_t address, uint8_t function_code, uint16_t start_address, uint16_t number_of_entities,
28  uint8_t payload_len = 0, const uint8_t *payload = nullptr);
29  void send_raw(const std::vector<uint8_t> &payload);
30  void set_flow_control_pin(GPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; }
32  void set_send_wait_time(uint16_t time_in_ms) { send_wait_time_ = time_in_ms; }
33  void set_disable_crc(bool disable_crc) { disable_crc_ = disable_crc; }
34 
35  protected:
37 
38  bool parse_modbus_byte_(uint8_t byte);
39  uint16_t send_wait_time_{250};
41  std::vector<uint8_t> rx_buffer_;
42  uint32_t last_modbus_byte_{0};
43  uint32_t last_send_{0};
44  std::vector<ModbusDevice *> devices_;
45 };
46 
47 class ModbusDevice {
48  public:
49  void set_parent(Modbus *parent) { parent_ = parent; }
50  void set_address(uint8_t address) { address_ = address; }
51  virtual void on_modbus_data(const std::vector<uint8_t> &data) = 0;
52  virtual void on_modbus_error(uint8_t function_code, uint8_t exception_code) {}
53  void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len = 0,
54  const uint8_t *payload = nullptr) {
55  this->parent_->send(this->address_, function, start_address, number_of_entities, payload_len, payload);
56  }
57  void send_raw(const std::vector<uint8_t> &payload) { this->parent_->send_raw(payload); }
58  // If more than one device is connected block sending a new command before a response is received
59  bool waiting_for_response() { return parent_->waiting_for_response != 0; }
60 
61  protected:
62  friend Modbus;
63 
65  uint8_t address_;
66 };
67 
68 } // namespace modbus
69 } // namespace esphome
std::vector< uint8_t > rx_buffer_
Definition: modbus.h:41
bool parse_modbus_byte_(uint8_t byte)
Definition: modbus.cpp:38
void set_send_wait_time(uint16_t time_in_ms)
Definition: modbus.h:32
uint32_t last_send_
Definition: modbus.h:43
void send_raw(const std::vector< uint8_t > &payload)
Definition: modbus.cpp:204
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:153
std::vector< ModbusDevice * > devices_
Definition: modbus.h:44
float get_setup_priority() const override
Definition: modbus.cpp:148
UARTComponent * parent_
Definition: uart.h:68
GPIOPin * flow_control_pin_
Definition: modbus.h:36
void set_flow_control_pin(GPIOPin *flow_control_pin)
Definition: modbus.h:30
void dump_config() override
Definition: modbus.cpp:142
uint16_t send_wait_time_
Definition: modbus.h:39
void send_raw(const std::vector< uint8_t > &payload)
Definition: modbus.h:57
void set_disable_crc(bool disable_crc)
Definition: modbus.h:33
void set_address(uint8_t address)
Definition: modbus.h:50
void set_parent(Modbus *parent)
Definition: modbus.h:49
uint32_t last_modbus_byte_
Definition: modbus.h:42
uint8_t waiting_for_response
Definition: modbus.h:31
virtual void on_modbus_error(uint8_t function_code, uint8_t exception_code)
Definition: modbus.h:52
void loop() override
Definition: modbus.cpp:15
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void register_device(ModbusDevice *device)
Definition: modbus.h:23
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:53
void setup() override
Definition: modbus.cpp:10