supla-device
Loading...
Searching...
No Matches
modbus_configurator.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_MODBUS_MODBUS_CONFIGURATOR_H_
20#define SRC_SUPLA_MODBUS_MODBUS_CONFIGURATOR_H_
21
22#include <supla/element.h>
23
24namespace Supla {
25
26namespace Modbus {
27
28enum class Role : uint8_t {
29 NotSet = MODBUS_ROLE_NOT_SET,
30 Master = MODBUS_ROLE_MASTER,
31 Slave = MODBUS_ROLE_SLAVE
32};
33
34enum class ModeSerial : uint8_t {
35 Disabled = MODBUS_SERIAL_MODE_DISABLED,
36 Rtu = MODBUS_SERIAL_MODE_RTU,
37 Ascii = MODBUS_SERIAL_MODE_ASCII
38};
39
40enum class ModeNetwork : uint8_t {
41 Disabled = MODBUS_NETWORK_MODE_DISABLED,
42 Tcp = MODBUS_NETWORK_MODE_TCP,
43 Udp = MODBUS_NETWORK_MODE_UDP
44};
45
46enum class SerialStopBits : uint8_t {
47 One = MODBUS_SERIAL_STOP_BITS_ONE,
48 OneAndHalf = MODBUS_SERIAL_STOP_BITS_ONE_AND_HALF,
49 Two = MODBUS_SERIAL_STOP_BITS_TWO
50};
51
52#pragma pack(push, 1)
54 ModeSerial mode = ModeSerial::Disabled;
55 int baudrate = 19200;
56 SerialStopBits stopBits = SerialStopBits::One;
57};
58
60 ModeNetwork mode = ModeNetwork::Disabled;
61 int port = 502;
62};
63
64struct ConfigProperties {
65 struct {
66 uint8_t master: 1;
67 uint8_t slave: 1;
68 uint8_t rtu: 1;
69 uint8_t ascii: 1;
70 uint8_t tcp: 1;
71 uint8_t udp: 1;
72 } protocol;
73 struct {
74 uint8_t b4800 : 1;
75 uint8_t b9600 : 1; // modbus mandatory
76 uint8_t b19200 : 1; // modbus mandatory
77 uint8_t b38400 : 1;
78 uint8_t b57600 : 1;
79 uint8_t b115200 : 1;
80 } baudrate;
81 struct {
82 uint8_t one: 1;
83 uint8_t oneAndHalf: 1;
84 uint8_t two: 1;
85 } stopBits;
86
87 ConfigProperties();
88 bool operator==(const ConfigProperties &other) const;
89 bool operator!=(const ConfigProperties &other) const;
90 bool operator==(const ModbusConfigProperties &other) const;
91 bool operator!=(const ModbusConfigProperties &other) const;
92};
93
94struct Config {
95 Role role = Role::NotSet;
96 uint8_t modbusAddress = 1; // only for slave
97 uint16_t slaveTimeoutMs = 0;
98 SerialConfig serial;
99 NetworkConfig network;
100
101 bool operator==(const Config &other) const;
102 bool operator!=(const Config &other) const;
103 Config &operator=(const Config &other);
104 bool validateAndFix(const ConfigProperties &properties);
105};
106
107#pragma pack(pop)
108
109class Configurator : public Supla::Element {
110 public:
111 Configurator();
112
113 void onLoadConfig(SuplaDeviceClass *) override;
114 void onDeviceConfigChange(uint64_t fieldBit) override;
115
116 bool isNetworkModeEnabled() const;
117 bool isSerialModeEnabled() const;
118 bool isModbusDisabled() const;
119 bool isMaster() const;
120 bool isSlave() const;
121 bool isRtuMode() const;
122 bool isAsciiMode() const;
123
124 void setConfig(const Supla::Modbus::Config &config);
125 void storeConfig() const;
126 void printConfig() const;
127 const Supla::Modbus::Config &getConfig() const;
128 const Supla::Modbus::ConfigProperties &getProperties() const;
129 void setProperties(const Supla::Modbus::ConfigProperties &newProperties);
130
131 protected:
132 bool configChanged = false;
135};
136
137} // namespace Modbus
138} // namespace Supla
139
140#endif // SRC_SUPLA_MODBUS_MODBUS_CONFIGURATOR_H_
Definition SuplaDevice.h:93
Base class for all elements of SuplaDevice.
Definition element.h:37
void onDeviceConfigChange(uint64_t fieldBit) override
Method called when device config is changed.
Definition modbus_configurator.cpp:230
void onLoadConfig(SuplaDeviceClass *) override
First method called on element in SuplaDevice.begin().
Definition modbus_configurator.cpp:208
Definition proto.h:2894
Definition modbus_configurator.h:64
Definition modbus_configurator.h:94
Definition modbus_configurator.h:59
Definition modbus_configurator.h:53