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/services/Serviceable.hpp>
16 #include <QJsonObject>
18 #include <QQmlListProperty>
20 #include <QLinkedList>
38 friend class test_AbstractDevice;
39 friend class test_AbstractServer;
52 FUNCTION_INVALID = QModbusPdu::Invalid,
53 FUNCTION_READ_COILS = QModbusPdu::ReadCoils,
54 FUNCTION_READ_DISCRETE_INPUTS = QModbusPdu::ReadDiscreteInputs,
55 FUNCTION_READ_HOLDING_REGISTERS = QModbusPdu::ReadHoldingRegisters,
56 FUNCTION_READ_INPUT_REGISTERS = QModbusPdu::ReadInputRegisters,
57 FUNCTION_WRITE_COIL = QModbusPdu::WriteSingleCoil,
58 FUNCTION_WRITE_HOLDING_REGISTER = QModbusPdu::WriteSingleRegister,
59 FUNCTION_READ_EXCEPTION_STATUS = QModbusPdu::ReadExceptionStatus,
60 FUNCTION_DIAGNOSTICS = QModbusPdu::Diagnostics,
61 FUNCTION_FETCH_COMM_EVENT_COUNTER = QModbusPdu::GetCommEventCounter,
62 FUNCTION_FETCH_COMM_EVENT_LOG = QModbusPdu::GetCommEventLog,
63 FUNCTION_WRITE_MULTIPLE_COILS = QModbusPdu::WriteMultipleCoils,
64 FUNCTION_WRITE_MULTIPLE_HOLDING_REGISTERS = QModbusPdu::WriteMultipleRegisters,
65 FUNCTION_REPORT_SLAVE_ID = QModbusPdu::ReportServerId,
66 FUNCTION_READ_FILE_RECORD = QModbusPdu::ReadFileRecord,
67 FUNCTION_WRITE_FILE_RECORD = QModbusPdu::WriteFileRecord,
68 FUNCTION_MASK_WRITE_HOLDING_REGISTER = QModbusPdu::MaskWriteRegister,
69 FUNCTION_READ_WRITE_MULTIPLE_HOLDING_REGISTERS = QModbusPdu::ReadWriteMultipleRegisters,
70 FUNCTION_READ_FIFO_QUEUE = QModbusPdu::ReadFifoQueue,
72 FUNCTION_UNDEFINED = QModbusPdu::UndefinedFunctionCode,
82 DIAGNOSTICS_RETURN_QUERY_DATA = 0x00,
83 DIAGNOSTICS_RESTART_COMM_OPTION = 0x01,
84 DIAGNOSTICS_RETURN_DIAGNOSTICS_REGISTER = 0x02,
85 DIAGNOSTICS_CHANGE_ASCII_INPUT_DELIMITER = 0x03,
86 DIAGNOSTICS_FORCE_LISTEN_ONLY_MODE = 0x04,
87 DIAGNOSTICS_CLEAR_COUNTERS_AND_DIAGNOSTICS_REGISTER = 0x0A,
88 DIAGNOSTICS_RETURN_BUS_MESSAGE_COUNT = 0x0B,
89 DIAGNOSTICS_RETURN_BUS_COMM_ERROR_COUNT = 0x0C,
90 DIAGNOSTICS_RETURN_BUS_EXCEPTION_ERROR_COUNT = 0x0D,
91 DIAGNOSTICS_RETURN_SLAVE_MESSAGE_COUNT = 0x0E,
92 DIAGNOSTICS_RETURN_SLAVE_NO_RESPONSE_COUNT = 0x0F,
93 DIAGNOSTICS_RETURN_SLAVE_NAK_COUNT = 0x10,
94 DIAGNOSTICS_RETURN_SLAVE_BUSY_COUNT = 0x11,
95 DIAGNOSTICS_RETURN_BUS_CHARACTER_OVERRUN_COUNT = 0x12,
96 DIAGNOSTICS_RETURN_IOP_OVERRUN_COUNT = 0x13,
97 DIAGNOSTICS_CLEAR_OVERRUN_COUNTER_AND_FLAG = 0x14,
98 DIAGNOSTICS_GET_CLEAR_MODBUS_PLUS_STATISTICS = 0x15
100 Q_ENUM(DiagnosticsSubfunction)
121 static constexpr
int MAX_READ_TCP_COILS = 1976;
126 static constexpr
int MAX_READ_RTU_COILS = 2008;
131 static constexpr
int MAX_WRITE_TCP_COILS = 1944;
136 static constexpr
int MAX_WRITE_RTU_COILS = 1976;
141 static constexpr
int MAX_READ_TCP_DISCRETE_INPUTS = 1976;
146 static constexpr
int MAX_READ_RTU_DISCRETE_INPUTS = 2008;
151 static constexpr
int MAX_READ_TCP_HOLDING_REGISTERS = 123;
156 static constexpr
int MAX_READ_RTU_HOLDING_REGISTERS = 125;
161 static constexpr
int MAX_WRITE_TCP_HOLDING_REGISTERS = 123;
166 static constexpr
int MAX_WRITE_RTU_HOLDING_REGISTERS = 125;
171 static constexpr
int MAX_READ_TCP_INPUT_REGISTERS = 123;
176 static constexpr
int MAX_READ_RTU_INPUT_REGISTERS = 125;
179 static constexpr quint16 MIN_ADDRESS = 0;
180 static constexpr quint16 MAX_ADDRESS = 65535;
182 static constexpr
int INITIAL_MAX_READ_COILS = 16;
183 static constexpr
int INITIAL_MAX_WRITE_COILS = 16;
184 static constexpr
int INITIAL_MAX_READ_DISCRETE_INPUTS = 16;
185 static constexpr
int INITIAL_MAX_WRITE_DISCRETE_INPUTS = 16;
186 static constexpr
int INITIAL_MAX_READ_HOLDING_REGISTERS = 16;
187 static constexpr
int INITIAL_MAX_WRITE_HOLDING_REGISTERS = 16;
188 static constexpr
int INITIAL_MAX_READ_INPUT_REGISTERS = 16;
189 static constexpr
int INITIAL_MAX_WRITE_INPUT_REGISTERS = 16;
190 static constexpr
State INITIAL_STATE = CLOSED;
191 static constexpr
bool INITIAL_READY = false;
193 Q_PROPERTY(
State state READ state NOTIFY stateChanged)
194 Q_PROPERTY(
bool ready READ ready NOTIFY readyChanged)
195 Q_PROPERTY(
int maxReadCoils READ maxReadCoils WRITE setMaxReadCoils NOTIFY maxReadCoilsChanged)
196 Q_PROPERTY(
int maxWriteCoils READ maxWriteCoils WRITE setMaxWriteCoils NOTIFY maxWriteCoilsChanged)
197 Q_PROPERTY(
int maxReadDiscreteInputs READ maxReadDiscreteInputs WRITE setMaxReadDiscreteInputs NOTIFY maxReadDiscreteInputsChanged)
198 Q_PROPERTY(
int maxWriteDiscreteInputs READ maxWriteDiscreteInputs WRITE setMaxWriteDiscreteInputs NOTIFY maxWriteDiscreteInputsChanged)
199 Q_PROPERTY(
int maxReadHoldingRegisters READ maxReadHoldingRegisters WRITE setMaxReadHoldingRegisters NOTIFY maxReadHoldingRegistersChanged)
200 Q_PROPERTY(
int maxWriteHoldingRegisters READ maxWriteHoldingRegisters WRITE setMaxWriteHoldingRegisters NOTIFY maxWriteHoldingRegistersChanged)
201 Q_PROPERTY(
int maxReadInputRegisters READ maxReadInputRegisters WRITE setMaxReadInputRegisters NOTIFY maxReadInputRegistersChanged)
202 Q_PROPERTY(
int maxWriteInputRegisters READ maxWriteInputRegisters WRITE setMaxWriteInputRegisters NOTIFY maxWriteInputRegistersChanged)
212 int maxReadCoils() const;
214 void setMaxReadCoils(
int maxReadCoils);
216 int maxWriteCoils() const;
218 void setMaxWriteCoils(
int maxWriteCoils);
220 int maxReadDiscreteInputs() const;
222 void setMaxReadDiscreteInputs(
int maxReadDiscreteInputs);
224 int maxWriteDiscreteInputs() const;
226 void setMaxWriteDiscreteInputs(
int maxWriteDiscreteInputs);
228 int maxReadHoldingRegisters() const;
230 void setMaxReadHoldingRegisters(
int maxReadHoldingRegisters);
232 int maxWriteHoldingRegisters() const;
234 void setMaxWriteHoldingRegisters(
int maxWriteHoldingRegisters);
236 int maxReadInputRegisters() const;
238 void setMaxReadInputRegisters(
int maxReadInputRegisters);
240 int maxWriteInputRegisters() const;
242 void setMaxWriteInputRegisters(
int maxWriteInputRegisters);
244 Coil * coilAt(quint16 address);
262 Q_INVOKABLE
void requestReadCoils(quint16 address, quint16 amount = 1,
QUuid * requestId =
nullptr);
271 Q_INVOKABLE
void requestWriteCoil(quint16 address,
bool value,
QUuid * requestId =
nullptr);
281 Q_INVOKABLE
void requestWriteMultipleCoils(quint16 address,
QJsonArray values,
QUuid * requestId =
nullptr);
293 Q_INVOKABLE
void requestReadDiscreteInputs(quint16 address, quint16 amount = 1,
QUuid * requestId =
nullptr);
303 Q_INVOKABLE
void requestWriteDiscreteInput(quint16 address,
bool value,
QUuid * requestId =
nullptr);
313 Q_INVOKABLE
void requestWriteMultipleDiscreteInputs(quint16 address,
QJsonArray values,
QUuid * requestId =
nullptr);
326 Q_INVOKABLE
void requestReadHoldingRegisters(quint16 address, quint16 amount = 1,
QUuid * requestId =
nullptr);
336 Q_INVOKABLE
void requestWriteHoldingRegister(quint16 address, quint16 value,
QUuid * requestId =
nullptr);
346 Q_INVOKABLE
void requestWriteMultipleHoldingRegisters(quint16 address,
QJsonArray values,
QUuid * requestId =
nullptr);
359 Q_INVOKABLE
void requestReadInputRegisters(quint16 address, quint16 amount = 1,
QUuid * requestId =
nullptr);
369 Q_INVOKABLE
void requestWriteInputRegister(quint16 address, quint16 value,
QUuid * requestId =
nullptr);
379 Q_INVOKABLE
void requestWriteMultipleInputRegisters(quint16 address,
QJsonArray values,
QUuid * requestId =
nullptr);
396 Q_INVOKABLE
void requestReadExceptionStatus(
QUuid * requestId =
nullptr);
403 Q_INVOKABLE
void requestFetchCommEventCounter(
QUuid * requestId =
nullptr);
410 Q_INVOKABLE
void requestFetchCommEventLog(
QUuid * requestId =
nullptr);
417 Q_INVOKABLE
void requestReportSlaveId(
QUuid * requestId =
nullptr);
429 Q_INVOKABLE
void requestMaskWriteHoldingRegister(quint16 address, quint16 andMask, quint16 orMask,
QUuid * requestId =
nullptr);
441 Q_INVOKABLE
void requestReadWriteMultipleHoldingRegisters(quint16 readAddress, quint16 amount, quint16 writeAddress,
QJsonArray values,
QUuid * requestId =
nullptr);
450 Q_INVOKABLE
void requestReadFIFOQueue(quint16 address,
QUuid * requestId =
nullptr);
461 Q_INVOKABLE
QJsonObject readFileRecordSubrequest(quint16 file, quint16 address, quint16 amount);
473 Q_INVOKABLE
void requestReadFileRecord(
QJsonArray subrequests,
QUuid * requestId =
nullptr);
496 Q_INVOKABLE
void requestWriteFileRecord(
QJsonArray subrequests,
QUuid * requestId =
nullptr);
515 virtual
void open() = 0;
517 virtual
void close() = 0;
524 void maxReadCoilsChanged();
526 void maxWriteCoilsChanged();
528 void maxReadDiscreteInputsChanged();
530 void maxWriteDiscreteInputsChanged();
532 void maxReadHoldingRegistersChanged();
534 void maxWriteHoldingRegistersChanged();
536 void maxReadInputRegistersChanged();
538 void maxWriteInputRegistersChanged();
568 virtual
void handleRequest(const
QJsonObject & request) = 0;
598 void setState(
State state);
600 void setReady(
bool ready);
605 static
void ValidatePayloadAddressKey(const
QJsonObject & json, const
QString & key = "address");
607 static
void ValidatePayloadAmountKey(const
QJsonObject & json,
int max);
609 static
void ValidatePayloadValueKeyInt(const
QJsonObject & json);
611 static
void ValidatetPayloadValueKeyBool(const
QJsonObject & json);
613 static
void ValidatePayloadReadFileRecordSubrequestsKey(const
QJsonObject & json);
615 static
void ValidatePayloadWriteFileRecordSubrequestsKey(const
QJsonObject & json);
629 static
void ValidateReadFileRecordSubresponsesKey(const
QJsonObject & json, const
QString & path = "");
631 static
void ValidateWriteFileRecordSubresponsesKey(const
QJsonObject & json, const
QString & path = "");
643 int maxReadDiscreteInputs;
644 int maxWriteDiscreteInputs;
645 int maxReadHoldingRegisters;
646 int maxWriteHoldingRegisters;
647 int maxReadInputRegisters;
648 int maxWriteInputRegisters;