1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#ifndef QMODBUSDEVICE_H
4#define QMODBUSDEVICE_H
5
6#include <QtCore/qobject.h>
7#include <QtCore/qiodevice.h>
8#include <QtSerialBus/qtserialbusglobal.h>
9
10QT_BEGIN_NAMESPACE
11
12class QModbusDevicePrivate;
13
14class Q_SERIALBUS_EXPORT QModbusDevice : public QObject
15{
16 Q_OBJECT
17 Q_DECLARE_PRIVATE(QModbusDevice)
18
19public:
20 enum Error {
21 NoError,
22 ReadError,
23 WriteError,
24 ConnectionError,
25 ConfigurationError,
26 TimeoutError,
27 ProtocolError,
28 ReplyAbortedError,
29 UnknownError,
30 InvalidResponseError
31 };
32 Q_ENUM(Error)
33
34 enum State {
35 UnconnectedState,
36 ConnectingState,
37 ConnectedState,
38 ClosingState
39 };
40 Q_ENUM(State)
41
42 enum ConnectionParameter {
43 SerialPortNameParameter,
44 SerialParityParameter,
45 SerialBaudRateParameter,
46 SerialDataBitsParameter,
47 SerialStopBitsParameter,
48
49 NetworkPortParameter,
50 NetworkAddressParameter
51 };
52 Q_ENUM(ConnectionParameter)
53
54 enum IntermediateError
55 {
56 ResponseCrcError,
57 ResponseRequestMismatch
58 };
59 Q_ENUM(IntermediateError)
60
61 explicit QModbusDevice(QObject *parent = nullptr);
62 ~QModbusDevice();
63
64 QVariant connectionParameter(ConnectionParameter parameter) const;
65 void setConnectionParameter(ConnectionParameter parameter, const QVariant &value);
66
67 bool connectDevice();
68 void disconnectDevice();
69
70 State state() const;
71
72 Error error() const;
73 QString errorString() const;
74
75 QIODevice *device() const;
76
77Q_SIGNALS:
78 void errorOccurred(QModbusDevice::Error error);
79 void stateChanged(QModbusDevice::State state);
80
81protected:
82 QModbusDevice(QModbusDevicePrivate &dd, QObject *parent = nullptr);
83
84 void setState(QModbusDevice::State newState);
85 void setError(const QString &errorText, QModbusDevice::Error error);
86 virtual bool open() = 0;
87 virtual void close() = 0;
88};
89
90Q_DECLARE_TYPEINFO(QModbusDevice::Error, Q_PRIMITIVE_TYPE);
91Q_DECLARE_TYPEINFO(QModbusDevice::State, Q_PRIMITIVE_TYPE);
92Q_DECLARE_TYPEINFO(QModbusDevice::ConnectionParameter, Q_PRIMITIVE_TYPE);
93Q_DECLARE_TYPEINFO(QModbusDevice::IntermediateError, Q_PRIMITIVE_TYPE);
94
95QT_END_NAMESPACE
96
97#endif // QMODBUSDEVICE_H
98

source code of qtserialbus/src/serialbus/qmodbusdevice.h