ESPHome  2022.9.1
modbus.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 namespace esphome {
7 namespace modbus {
8 
9 class ModbusDevice;
10 
11 class Modbus : public uart::UARTDevice, public Component {
12  public:
13  Modbus() = default;
14 
15  void setup() override;
16 
17  void loop() override;
18 
19  void dump_config() override;
20 
21  void register_device(ModbusDevice *device) { this->devices_.push_back(device); }
22 
23  float get_setup_priority() const override;
24 
25  void send(uint8_t address, uint8_t function_code, uint16_t start_address, uint16_t number_of_entities,
26  uint8_t payload_len = 0, const uint8_t *payload = nullptr);
27  void send_raw(const std::vector<uint8_t> &payload);
28  void set_flow_control_pin(GPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; }
30  void set_send_wait_time(uint16_t time_in_ms) { send_wait_time_ = time_in_ms; }
31 
32  protected:
34 
35  bool parse_modbus_byte_(uint8_t byte);
36  uint16_t send_wait_time_{250};
37  std::vector<uint8_t> rx_buffer_;
38  uint32_t last_modbus_byte_{0};
39  uint32_t last_send_{0};
40  std::vector<ModbusDevice *> devices_;
41 };
42 
43 class ModbusDevice {
44  public:
45  void set_parent(Modbus *parent) { parent_ = parent; }
46  void set_address(uint8_t address) { address_ = address; }
47  virtual void on_modbus_data(const std::vector<uint8_t> &data) = 0;
48  virtual void on_modbus_error(uint8_t function_code, uint8_t exception_code) {}
49  void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len = 0,
50  const uint8_t *payload = nullptr) {
51  this->parent_->send(this->address_, function, start_address, number_of_entities, payload_len, payload);
52  }
53  void send_raw(const std::vector<uint8_t> &payload) { this->parent_->send_raw(payload); }
54  // If more than one device is connected block sending a new command before a response is received
55  bool waiting_for_response() { return parent_->waiting_for_response != 0; }
56 
57  protected:
58  friend Modbus;
59 
61  uint8_t address_;
62 };
63 
64 } // namespace modbus
65 } // namespace esphome
std::vector< uint8_t > rx_buffer_
Definition: modbus.h:37
bool parse_modbus_byte_(uint8_t byte)
Definition: modbus.cpp:38
void set_send_wait_time(uint16_t time_in_ms)
Definition: modbus.h:30
uint32_t last_send_
Definition: modbus.h:39
void send_raw(const std::vector< uint8_t > &payload)
Definition: modbus.cpp:199
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:148
std::vector< ModbusDevice * > devices_
Definition: modbus.h:40
float get_setup_priority() const override
Definition: modbus.cpp:143
UARTComponent * parent_
Definition: uart.h:68
GPIOPin * flow_control_pin_
Definition: modbus.h:33
void set_flow_control_pin(GPIOPin *flow_control_pin)
Definition: modbus.h:28
void dump_config() override
Definition: modbus.cpp:138
uint16_t send_wait_time_
Definition: modbus.h:36
void send_raw(const std::vector< uint8_t > &payload)
Definition: modbus.h:53
void set_address(uint8_t address)
Definition: modbus.h:46
void set_parent(Modbus *parent)
Definition: modbus.h:45
uint32_t last_modbus_byte_
Definition: modbus.h:38
uint8_t waiting_for_response
Definition: modbus.h:29
virtual void on_modbus_error(uint8_t function_code, uint8_t exception_code)
Definition: modbus.h:48
void loop() override
Definition: modbus.cpp:15
Definition: a4988.cpp:4
void register_device(ModbusDevice *device)
Definition: modbus.h:21
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:49
void setup() override
Definition: modbus.cpp:10