supla-device
Loading...
Searching...
No Matches
modbus_configurator.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_MODBUS_MODBUS_CONFIGURATOR_H_
5#define SRC_SUPLA_MODBUS_MODBUS_CONFIGURATOR_H_
6
7#include <supla/element.h>
8
9namespace Supla {
10
11namespace Modbus {
12
13enum class Role : uint8_t {
14 NotSet = MODBUS_ROLE_NOT_SET,
15 Master = MODBUS_ROLE_MASTER,
16 Slave = MODBUS_ROLE_SLAVE
17};
18
19enum class ModeSerial : uint8_t {
20 Disabled = MODBUS_SERIAL_MODE_DISABLED,
21 Rtu = MODBUS_SERIAL_MODE_RTU,
22 Ascii = MODBUS_SERIAL_MODE_ASCII
23};
24
25enum class ModeNetwork : uint8_t {
26 Disabled = MODBUS_NETWORK_MODE_DISABLED,
27 Tcp = MODBUS_NETWORK_MODE_TCP,
28 Udp = MODBUS_NETWORK_MODE_UDP
29};
30
31enum class SerialStopBits : uint8_t {
32 One = MODBUS_SERIAL_STOP_BITS_ONE,
33 OneAndHalf = MODBUS_SERIAL_STOP_BITS_ONE_AND_HALF,
34 Two = MODBUS_SERIAL_STOP_BITS_TWO
35};
36
37#pragma pack(push, 1)
39 ModeSerial mode = ModeSerial::Disabled;
40 int baudrate = 19200;
41 SerialStopBits stopBits = SerialStopBits::One;
42};
43
45 ModeNetwork mode = ModeNetwork::Disabled;
46 int port = 502;
47};
48
50 struct {
51 uint8_t master: 1;
52 uint8_t slave: 1;
53 uint8_t rtu: 1;
54 uint8_t ascii: 1;
55 uint8_t tcp: 1;
56 uint8_t udp: 1;
57 } protocol;
58 struct {
59 uint8_t b4800 : 1;
60 uint8_t b9600 : 1; // modbus mandatory
61 uint8_t b19200 : 1; // modbus mandatory
62 uint8_t b38400 : 1;
63 uint8_t b57600 : 1;
64 uint8_t b115200 : 1;
65 } baudrate;
66 struct {
67 uint8_t one: 1;
68 uint8_t oneAndHalf: 1;
69 uint8_t two: 1;
70 } stopBits;
71
73 bool operator==(const ConfigProperties &other) const;
74 bool operator!=(const ConfigProperties &other) const;
75 bool operator==(const ModbusConfigProperties &other) const;
76 bool operator!=(const ModbusConfigProperties &other) const;
77};
78
79struct Config {
80 Role role = Role::NotSet;
81 uint8_t modbusAddress = 1; // only for slave
82 uint16_t slaveTimeoutMs = 0;
83 SerialConfig serial;
84 NetworkConfig network;
85
86 bool operator==(const Config &other) const;
87 bool operator!=(const Config &other) const;
88 Config &operator=(const Config &other) = default;
89 bool validateAndFix(const ConfigProperties &properties);
90};
91
92#pragma pack(pop)
93
95 public:
97
98 void onLoadConfig(SuplaDeviceClass *) override;
99 void onDeviceConfigChange(uint64_t fieldBit) override;
100 void onInit() override;
101
102 bool isNetworkModeEnabled() const;
103 bool isSerialModeEnabled() const;
104 bool isModbusDisabled() const;
105 bool isMaster() const;
106 bool isSlave() const;
107 bool isRtuMode() const;
108 bool isAsciiMode() const;
109
110 void setConfig(const Supla::Modbus::Config &config);
111 void storeConfig(bool local = false) const;
112 void printConfig() const;
113 const Supla::Modbus::Config &getConfig() const;
114 const Supla::Modbus::ConfigProperties &getProperties() const;
115 void setProperties(const Supla::Modbus::ConfigProperties &newProperties);
116
117 protected:
118 bool configChanged = false;
119 bool initDone = false;
122};
123
124} // namespace Modbus
125} // namespace Supla
126
127#endif // SRC_SUPLA_MODBUS_MODBUS_CONFIGURATOR_H_
Definition SuplaDevice.h:167
Base class for all elements of SuplaDevice.
Definition element.h:27
Definition modbus_configurator.h:94
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition modbus_configurator.cpp:183
void onDeviceConfigChange(uint64_t fieldBit) override
Method called when device config is changed.
Definition modbus_configurator.cpp:216
void onLoadConfig(SuplaDeviceClass *) override
First method called on element in SuplaDevice.begin().
Definition modbus_configurator.cpp:188
Definition proto.h:3298
Definition modbus_configurator.h:49
Definition modbus_configurator.h:79
Definition modbus_configurator.h:44
Definition modbus_configurator.h:38