1 #ifndef H_EXTENSIONS_CUTEHMI_MODBUS_2_INCLUDE_CUTEHMI_MODBUS_ABSTRACTDEVICE_HPP
2 #define H_EXTENSIONS_CUTEHMI_MODBUS_2_INCLUDE_CUTEHMI_MODBUS_ABSTRACTDEVICE_HPP
4 #include "internal/common.hpp"
5 #include "internal/RegisterTraits.hpp"
6 #include "InputRegister.hpp"
7 #include "internal/HoldingRegister.hpp"
8 #include "internal/InputRegister.hpp"
9 #include "DiscreteInput.hpp"
12 #include <cutehmi/InplaceError.hpp>
13 #include <cutehmi/services/Serviceable.hpp>
17 #include <QJsonObject>
19 #include <QQmlListProperty>
21 #include <QLinkedList>
39 friend class test_AbstractDevice;
40 friend class test_AbstractServer;
53 FUNCTION_INVALID = QModbusPdu::Invalid,
54 FUNCTION_READ_COILS = QModbusPdu::ReadCoils,
55 FUNCTION_READ_DISCRETE_INPUTS = QModbusPdu::ReadDiscreteInputs,
56 FUNCTION_READ_HOLDING_REGISTERS = QModbusPdu::ReadHoldingRegisters,
57 FUNCTION_READ_INPUT_REGISTERS = QModbusPdu::ReadInputRegisters,
58 FUNCTION_WRITE_COIL = QModbusPdu::WriteSingleCoil,
59 FUNCTION_WRITE_HOLDING_REGISTER = QModbusPdu::WriteSingleRegister,
60 FUNCTION_READ_EXCEPTION_STATUS = QModbusPdu::ReadExceptionStatus,
61 FUNCTION_DIAGNOSTICS = QModbusPdu::Diagnostics,
62 FUNCTION_FETCH_COMM_EVENT_COUNTER = QModbusPdu::GetCommEventCounter,
63 FUNCTION_FETCH_COMM_EVENT_LOG = QModbusPdu::GetCommEventLog,
64 FUNCTION_WRITE_MULTIPLE_COILS = QModbusPdu::WriteMultipleCoils,
65 FUNCTION_WRITE_MULTIPLE_HOLDING_REGISTERS = QModbusPdu::WriteMultipleRegisters,
66 FUNCTION_REPORT_SLAVE_ID = QModbusPdu::ReportServerId,
67 FUNCTION_READ_FILE_RECORD = QModbusPdu::ReadFileRecord,
68 FUNCTION_WRITE_FILE_RECORD = QModbusPdu::WriteFileRecord,
69 FUNCTION_MASK_WRITE_HOLDING_REGISTER = QModbusPdu::MaskWriteRegister,
70 FUNCTION_READ_WRITE_MULTIPLE_HOLDING_REGISTERS = QModbusPdu::ReadWriteMultipleRegisters,
71 FUNCTION_READ_FIFO_QUEUE = QModbusPdu::ReadFifoQueue,
73 FUNCTION_UNDEFINED = QModbusPdu::UndefinedFunctionCode,
83 DIAGNOSTICS_RETURN_QUERY_DATA = 0x00,
84 DIAGNOSTICS_RESTART_COMM_OPTION = 0x01,
85 DIAGNOSTICS_RETURN_DIAGNOSTICS_REGISTER = 0x02,
86 DIAGNOSTICS_CHANGE_ASCII_INPUT_DELIMITER = 0x03,
87 DIAGNOSTICS_FORCE_LISTEN_ONLY_MODE = 0x04,
88 DIAGNOSTICS_CLEAR_COUNTERS_AND_DIAGNOSTICS_REGISTER = 0x0A,
89 DIAGNOSTICS_RETURN_BUS_MESSAGE_COUNT = 0x0B,
90 DIAGNOSTICS_RETURN_BUS_COMM_ERROR_COUNT = 0x0C,
91 DIAGNOSTICS_RETURN_BUS_EXCEPTION_ERROR_COUNT = 0x0D,
92 DIAGNOSTICS_RETURN_SLAVE_MESSAGE_COUNT = 0x0E,
93 DIAGNOSTICS_RETURN_SLAVE_NO_RESPONSE_COUNT = 0x0F,
94 DIAGNOSTICS_RETURN_SLAVE_NAK_COUNT = 0x10,
95 DIAGNOSTICS_RETURN_SLAVE_BUSY_COUNT = 0x11,
96 DIAGNOSTICS_RETURN_BUS_CHARACTER_OVERRUN_COUNT = 0x12,
97 DIAGNOSTICS_RETURN_IOP_OVERRUN_COUNT = 0x13,
98 DIAGNOSTICS_CLEAR_OVERRUN_COUNTER_AND_FLAG = 0x14,
99 DIAGNOSTICS_GET_CLEAR_MODBUS_PLUS_STATISTICS = 0x15
101 Q_ENUM(DiagnosticsSubfunction)
122 static constexpr
int MAX_READ_TCP_COILS = 1976;
127 static constexpr
int MAX_READ_RTU_COILS = 2008;
132 static constexpr
int MAX_WRITE_TCP_COILS = 1944;
137 static constexpr
int MAX_WRITE_RTU_COILS = 1976;
142 static constexpr
int MAX_READ_TCP_DISCRETE_INPUTS = 1976;
147 static constexpr
int MAX_READ_RTU_DISCRETE_INPUTS = 2008;
152 static constexpr
int MAX_READ_TCP_HOLDING_REGISTERS = 123;
157 static constexpr
int MAX_READ_RTU_HOLDING_REGISTERS = 125;
162 static constexpr
int MAX_WRITE_TCP_HOLDING_REGISTERS = 123;
167 static constexpr
int MAX_WRITE_RTU_HOLDING_REGISTERS = 125;
172 static constexpr
int MAX_READ_TCP_INPUT_REGISTERS = 123;
177 static constexpr
int MAX_READ_RTU_INPUT_REGISTERS = 125;
180 static constexpr quint16 MIN_ADDRESS = 0;
181 static constexpr quint16 MAX_ADDRESS = 65535;
183 static constexpr
int INITIAL_MAX_READ_COILS = 16;
184 static constexpr
int INITIAL_MAX_WRITE_COILS = 16;
185 static constexpr
int INITIAL_MAX_READ_DISCRETE_INPUTS = 16;
186 static constexpr
int INITIAL_MAX_WRITE_DISCRETE_INPUTS = 16;
187 static constexpr
int INITIAL_MAX_READ_HOLDING_REGISTERS = 16;
188 static constexpr
int INITIAL_MAX_WRITE_HOLDING_REGISTERS = 16;
189 static constexpr
int INITIAL_MAX_READ_INPUT_REGISTERS = 16;
190 static constexpr
int INITIAL_MAX_WRITE_INPUT_REGISTERS = 16;
191 static constexpr
int INITIAL_MAX_REQUESTS = 1000;
192 static constexpr
State INITIAL_STATE = CLOSED;
193 static constexpr
bool INITIAL_READY = false;
195 Q_PROPERTY(
State state READ state NOTIFY stateChanged)
197 Q_PROPERTY(
bool ready READ ready NOTIFY readyChanged)
199 Q_PROPERTY(
int maxReadCoils READ maxReadCoils WRITE setMaxReadCoils NOTIFY maxReadCoilsChanged)
201 Q_PROPERTY(
int maxWriteCoils READ maxWriteCoils WRITE setMaxWriteCoils NOTIFY maxWriteCoilsChanged)
203 Q_PROPERTY(
int maxReadDiscreteInputs READ maxReadDiscreteInputs WRITE setMaxReadDiscreteInputs NOTIFY maxReadDiscreteInputsChanged)
205 Q_PROPERTY(
int maxWriteDiscreteInputs READ maxWriteDiscreteInputs WRITE setMaxWriteDiscreteInputs NOTIFY maxWriteDiscreteInputsChanged)
207 Q_PROPERTY(
int maxReadHoldingRegisters READ maxReadHoldingRegisters WRITE setMaxReadHoldingRegisters NOTIFY maxReadHoldingRegistersChanged)
209 Q_PROPERTY(
int maxWriteHoldingRegisters READ maxWriteHoldingRegisters WRITE setMaxWriteHoldingRegisters NOTIFY maxWriteHoldingRegistersChanged)
211 Q_PROPERTY(
int maxReadInputRegisters READ maxReadInputRegisters WRITE setMaxReadInputRegisters NOTIFY maxReadInputRegistersChanged)
213 Q_PROPERTY(
int maxWriteInputRegisters READ maxWriteInputRegisters WRITE setMaxWriteInputRegisters NOTIFY maxWriteInputRegistersChanged)
215 Q_PROPERTY(
int maxRequests READ maxRequests WRITE setMaxRequests NOTIFY maxRequestsChanged)
225 int maxReadCoils() const;
227 void setMaxReadCoils(
int maxReadCoils);
229 int maxWriteCoils() const;
231 void setMaxWriteCoils(
int maxWriteCoils);
233 int maxReadDiscreteInputs() const;
235 void setMaxReadDiscreteInputs(
int maxReadDiscreteInputs);
237 int maxWriteDiscreteInputs() const;
239 void setMaxWriteDiscreteInputs(
int maxWriteDiscreteInputs);
241 int maxReadHoldingRegisters() const;
243 void setMaxReadHoldingRegisters(
int maxReadHoldingRegisters);
245 int maxWriteHoldingRegisters() const;
247 void setMaxWriteHoldingRegisters(
int maxWriteHoldingRegisters);
249 int maxReadInputRegisters() const;
251 void setMaxReadInputRegisters(
int maxReadInputRegisters);
253 int maxWriteInputRegisters() const;
255 void setMaxWriteInputRegisters(
int maxWriteInputRegisters);
257 int maxRequests() const;
259 void setMaxRequests(
int maxRequests);
261 Coil * coilAt(quint16 address);
279 Q_INVOKABLE
void requestReadCoils(quint16 address, quint16 amount = 1,
QUuid * requestId =
nullptr);
288 Q_INVOKABLE
void requestWriteCoil(quint16 address,
bool value,
QUuid * requestId =
nullptr);
298 Q_INVOKABLE
void requestWriteMultipleCoils(quint16 address,
QJsonArray values,
QUuid * requestId =
nullptr);
310 Q_INVOKABLE
void requestReadDiscreteInputs(quint16 address, quint16 amount = 1,
QUuid * requestId =
nullptr);
320 Q_INVOKABLE
void requestWriteDiscreteInput(quint16 address,
bool value,
QUuid * requestId =
nullptr);
330 Q_INVOKABLE
void requestWriteMultipleDiscreteInputs(quint16 address,
QJsonArray values,
QUuid * requestId =
nullptr);
343 Q_INVOKABLE
void requestReadHoldingRegisters(quint16 address, quint16 amount = 1,
QUuid * requestId =
nullptr);
353 Q_INVOKABLE
void requestWriteHoldingRegister(quint16 address, quint16 value,
QUuid * requestId =
nullptr);
363 Q_INVOKABLE
void requestWriteMultipleHoldingRegisters(quint16 address,
QJsonArray values,
QUuid * requestId =
nullptr);
376 Q_INVOKABLE
void requestReadInputRegisters(quint16 address, quint16 amount = 1,
QUuid * requestId =
nullptr);
386 Q_INVOKABLE
void requestWriteInputRegister(quint16 address, quint16 value,
QUuid * requestId =
nullptr);
396 Q_INVOKABLE
void requestWriteMultipleInputRegisters(quint16 address,
QJsonArray values,
QUuid * requestId =
nullptr);
413 Q_INVOKABLE
void requestReadExceptionStatus(
QUuid * requestId =
nullptr);
420 Q_INVOKABLE
void requestFetchCommEventCounter(
QUuid * requestId =
nullptr);
427 Q_INVOKABLE
void requestFetchCommEventLog(
QUuid * requestId =
nullptr);
434 Q_INVOKABLE
void requestReportSlaveId(
QUuid * requestId =
nullptr);
446 Q_INVOKABLE
void requestMaskWriteHoldingRegister(quint16 address, quint16 andMask, quint16 orMask,
QUuid * requestId =
nullptr);
458 Q_INVOKABLE
void requestReadWriteMultipleHoldingRegisters(quint16 readAddress, quint16 amount, quint16 writeAddress,
QJsonArray values,
QUuid * requestId =
nullptr);
467 Q_INVOKABLE
void requestReadFIFOQueue(quint16 address,
QUuid * requestId =
nullptr);
478 Q_INVOKABLE
QJsonObject readFileRecordSubrequest(quint16 file, quint16 address, quint16 amount);
490 Q_INVOKABLE
void requestReadFileRecord(
QJsonArray subrequests,
QUuid * requestId =
nullptr);
513 Q_INVOKABLE
void requestWriteFileRecord(
QJsonArray subrequests,
QUuid * requestId =
nullptr);
532 virtual
void open() = 0;
534 virtual
void close() = 0;
543 void maxReadCoilsChanged();
545 void maxWriteCoilsChanged();
547 void maxReadDiscreteInputsChanged();
549 void maxWriteDiscreteInputsChanged();
551 void maxReadHoldingRegistersChanged();
553 void maxWriteHoldingRegistersChanged();
555 void maxReadInputRegistersChanged();
557 void maxWriteInputRegistersChanged();
559 void maxRequestsChanged();
589 virtual
void handleRequest(const
QJsonObject & request) = 0;
619 void setState(
State state);
621 void setReady(
bool ready);
625 CUTEHMI_PROTECTED_SIGNALS:
635 static
void ValidatePayloadAddressKey(const
QJsonObject & json, const
QString & key = "address");
637 static
void ValidatePayloadAmountKey(const
QJsonObject & json,
int max);
639 static
void ValidatePayloadValueKeyInt(const
QJsonObject & json);
641 static
void ValidatetPayloadValueKeyBool(const
QJsonObject & json);
643 static
void ValidatePayloadReadFileRecordSubrequestsKey(const
QJsonObject & json);
645 static
void ValidatePayloadWriteFileRecordSubrequestsKey(const
QJsonObject & json);
659 static
void ValidateReadFileRecordSubresponsesKey(const
QJsonObject & json, const
QString & path = "");
661 static
void ValidateWriteFileRecordSubresponsesKey(const
QJsonObject & json, const
QString & path = "");
673 int maxReadDiscreteInputs;
674 int maxWriteDiscreteInputs;
675 int maxReadHoldingRegisters;
676 int maxWriteHoldingRegisters;
677 int maxReadInputRegisters;
678 int maxWriteInputRegisters;
687 state(INITIAL_STATE),
688 ready(INITIAL_READY),
689 maxReadCoils(INITIAL_MAX_READ_COILS),
690 maxWriteCoils(INITIAL_MAX_WRITE_COILS),
691 maxReadDiscreteInputs(INITIAL_MAX_READ_DISCRETE_INPUTS),
692 maxWriteDiscreteInputs(INITIAL_MAX_WRITE_DISCRETE_INPUTS),
693 maxReadHoldingRegisters(INITIAL_MAX_READ_HOLDING_REGISTERS),
694 maxWriteHoldingRegisters(INITIAL_MAX_WRITE_HOLDING_REGISTERS),
695 maxReadInputRegisters(INITIAL_MAX_READ_INPUT_REGISTERS),
696 maxWriteInputRegisters(INITIAL_MAX_WRITE_INPUT_REGISTERS),
697 maxRequests(INITIAL_MAX_REQUESTS)