1/****************************************************************************
2**
3** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
4** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtSerialPort module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QSERIALPORT_H
42#define QSERIALPORT_H
43
44#include <QtCore/qiodevice.h>
45
46#include <QtSerialPort/qserialportglobal.h>
47
48QT_BEGIN_NAMESPACE
49
50class QSerialPortInfo;
51class QSerialPortPrivate;
52
53class Q_SERIALPORT_EXPORT QSerialPort : public QIODevice
54{
55 Q_OBJECT
56 Q_DECLARE_PRIVATE(QSerialPort)
57
58 Q_PROPERTY(qint32 baudRate READ baudRate WRITE setBaudRate NOTIFY baudRateChanged)
59 Q_PROPERTY(DataBits dataBits READ dataBits WRITE setDataBits NOTIFY dataBitsChanged)
60 Q_PROPERTY(Parity parity READ parity WRITE setParity NOTIFY parityChanged)
61 Q_PROPERTY(StopBits stopBits READ stopBits WRITE setStopBits NOTIFY stopBitsChanged)
62 Q_PROPERTY(FlowControl flowControl READ flowControl WRITE setFlowControl NOTIFY flowControlChanged)
63#if QT_DEPRECATED_SINCE(5, 2)
64 Q_PROPERTY(DataErrorPolicy dataErrorPolicy READ dataErrorPolicy WRITE setDataErrorPolicy NOTIFY dataErrorPolicyChanged)
65#endif
66 Q_PROPERTY(bool dataTerminalReady READ isDataTerminalReady WRITE setDataTerminalReady NOTIFY dataTerminalReadyChanged)
67 Q_PROPERTY(bool requestToSend READ isRequestToSend WRITE setRequestToSend NOTIFY requestToSendChanged)
68 Q_PROPERTY(SerialPortError error READ error RESET clearError NOTIFY error)
69#if QT_DEPRECATED_SINCE(5, 3)
70 Q_PROPERTY(bool settingsRestoredOnClose READ settingsRestoredOnClose WRITE setSettingsRestoredOnClose NOTIFY settingsRestoredOnCloseChanged)
71#endif
72 Q_PROPERTY(bool breakEnabled READ isBreakEnabled WRITE setBreakEnabled NOTIFY breakEnabledChanged)
73
74#if defined(Q_OS_WIN32)
75 typedef void* Handle;
76#else
77 typedef int Handle;
78#endif
79
80public:
81
82 enum Direction {
83 Input = 1,
84 Output = 2,
85 AllDirections = Input | Output
86 };
87 Q_FLAG(Direction)
88 Q_DECLARE_FLAGS(Directions, Direction)
89
90 enum BaudRate {
91 Baud1200 = 1200,
92 Baud2400 = 2400,
93 Baud4800 = 4800,
94 Baud9600 = 9600,
95 Baud19200 = 19200,
96 Baud38400 = 38400,
97 Baud57600 = 57600,
98 Baud115200 = 115200,
99 UnknownBaud = -1
100 };
101 Q_ENUM(BaudRate)
102
103 enum DataBits {
104 Data5 = 5,
105 Data6 = 6,
106 Data7 = 7,
107 Data8 = 8,
108 UnknownDataBits = -1
109 };
110 Q_ENUM(DataBits)
111
112 enum Parity {
113 NoParity = 0,
114 EvenParity = 2,
115 OddParity = 3,
116 SpaceParity = 4,
117 MarkParity = 5,
118 UnknownParity = -1
119 };
120 Q_ENUM(Parity)
121
122 enum StopBits {
123 OneStop = 1,
124 OneAndHalfStop = 3,
125 TwoStop = 2,
126 UnknownStopBits = -1
127 };
128 Q_ENUM(StopBits)
129
130 enum FlowControl {
131 NoFlowControl,
132 HardwareControl,
133 SoftwareControl,
134 UnknownFlowControl = -1
135 };
136 Q_ENUM(FlowControl)
137
138 enum PinoutSignal {
139 NoSignal = 0x00,
140 TransmittedDataSignal = 0x01,
141 ReceivedDataSignal = 0x02,
142 DataTerminalReadySignal = 0x04,
143 DataCarrierDetectSignal = 0x08,
144 DataSetReadySignal = 0x10,
145 RingIndicatorSignal = 0x20,
146 RequestToSendSignal = 0x40,
147 ClearToSendSignal = 0x80,
148 SecondaryTransmittedDataSignal = 0x100,
149 SecondaryReceivedDataSignal = 0x200
150 };
151 Q_FLAG(PinoutSignal)
152 Q_DECLARE_FLAGS(PinoutSignals, PinoutSignal)
153
154#if QT_DEPRECATED_SINCE(5, 2)
155#if defined(Q_CC_MSVC) && !defined(Q_CC_CLANG)
156#pragma deprecated(UnknownBaud)
157#pragma deprecated(UnknownDataBits)
158#pragma deprecated(UnknownParity)
159#pragma deprecated(UnknownStopBits)
160#pragma deprecated(UnknownFlowControl)
161#pragma deprecated(TransmittedDataSignal)
162#pragma deprecated(ReceivedDataSignal)
163#endif
164#endif
165
166#if QT_DEPRECATED_SINCE(5, 2)
167 enum DataErrorPolicy {
168 SkipPolicy,
169 PassZeroPolicy,
170 IgnorePolicy,
171 StopReceivingPolicy,
172 UnknownPolicy = -1
173 };
174 Q_ENUM(DataErrorPolicy)
175#endif
176
177 enum SerialPortError {
178 NoError,
179 DeviceNotFoundError,
180 PermissionError,
181 OpenError,
182 ParityError,
183 FramingError,
184 BreakConditionError,
185 WriteError,
186 ReadError,
187 ResourceError,
188 UnsupportedOperationError,
189 UnknownError,
190 TimeoutError,
191 NotOpenError
192 };
193 Q_ENUM(SerialPortError)
194
195#if QT_DEPRECATED_SINCE(5, 6)
196#if defined(Q_CC_MSVC) && !defined(Q_CC_CLANG)
197#pragma deprecated(ParityError)
198#pragma deprecated(FramingError)
199#pragma deprecated(BreakConditionError)
200#endif
201#endif
202
203 explicit QSerialPort(QObject *parent = nullptr);
204 explicit QSerialPort(const QString &name, QObject *parent = nullptr);
205 explicit QSerialPort(const QSerialPortInfo &info, QObject *parent = nullptr);
206 virtual ~QSerialPort();
207
208 void setPortName(const QString &name);
209 QString portName() const;
210
211 void setPort(const QSerialPortInfo &info);
212
213 bool open(OpenMode mode) override;
214 void close() override;
215
216#if QT_DEPRECATED_SINCE(5, 3)
217 QT_DEPRECATED void setSettingsRestoredOnClose(bool restore);
218 QT_DEPRECATED bool settingsRestoredOnClose() const;
219#endif
220
221 bool setBaudRate(qint32 baudRate, Directions directions = AllDirections);
222 qint32 baudRate(Directions directions = AllDirections) const;
223
224 bool setDataBits(DataBits dataBits);
225 DataBits dataBits() const;
226
227 bool setParity(Parity parity);
228 Parity parity() const;
229
230 bool setStopBits(StopBits stopBits);
231 StopBits stopBits() const;
232
233 bool setFlowControl(FlowControl flowControl);
234 FlowControl flowControl() const;
235
236 bool setDataTerminalReady(bool set);
237 bool isDataTerminalReady();
238
239 bool setRequestToSend(bool set);
240 bool isRequestToSend();
241
242 PinoutSignals pinoutSignals();
243
244 bool flush();
245 bool clear(Directions directions = AllDirections);
246 bool atEnd() const override; // ### Qt6: remove me
247
248#if QT_DEPRECATED_SINCE(5, 2)
249 QT_DEPRECATED bool setDataErrorPolicy(DataErrorPolicy policy = IgnorePolicy);
250 QT_DEPRECATED DataErrorPolicy dataErrorPolicy() const;
251#endif
252
253 SerialPortError error() const;
254 void clearError();
255
256 qint64 readBufferSize() const;
257 void setReadBufferSize(qint64 size);
258
259 bool isSequential() const override;
260
261 qint64 bytesAvailable() const override;
262 qint64 bytesToWrite() const override;
263 bool canReadLine() const override;
264
265 bool waitForReadyRead(int msecs = 30000) override;
266 bool waitForBytesWritten(int msecs = 30000) override;
267
268#if QT_DEPRECATED_SINCE(5, 5)
269 QT_DEPRECATED bool sendBreak(int duration = 0);
270#endif
271 bool setBreakEnabled(bool set = true);
272 bool isBreakEnabled() const;
273
274 Handle handle() const;
275
276Q_SIGNALS:
277 void baudRateChanged(qint32 baudRate, QSerialPort::Directions directions);
278 void dataBitsChanged(QSerialPort::DataBits dataBits);
279 void parityChanged(QSerialPort::Parity parity);
280 void stopBitsChanged(QSerialPort::StopBits stopBits);
281 void flowControlChanged(QSerialPort::FlowControl flowControl);
282#if QT_DEPRECATED_SINCE(5, 5)
283 QT_DEPRECATED void dataErrorPolicyChanged(QSerialPort::DataErrorPolicy policy);
284#endif
285 void dataTerminalReadyChanged(bool set);
286 void requestToSendChanged(bool set);
287#if QT_DEPRECATED_SINCE(5, 8)
288 void error(QSerialPort::SerialPortError serialPortError);
289#endif
290 void errorOccurred(QSerialPort::SerialPortError error);
291#if QT_DEPRECATED_SINCE(5, 5)
292 QT_DEPRECATED void settingsRestoredOnCloseChanged(bool restore);
293#endif
294 void breakEnabledChanged(bool set);
295
296protected:
297 qint64 readData(char *data, qint64 maxSize) override;
298 qint64 readLineData(char *data, qint64 maxSize) override;
299 qint64 writeData(const char *data, qint64 maxSize) override;
300
301private:
302 // ### Qt6: remove me.
303 QSerialPortPrivate * const d_dummy;
304
305 Q_DISABLE_COPY(QSerialPort)
306
307#if defined(Q_OS_WIN32)
308 Q_PRIVATE_SLOT(d_func(), bool _q_startAsyncWrite())
309#endif
310};
311
312Q_DECLARE_OPERATORS_FOR_FLAGS(QSerialPort::Directions)
313Q_DECLARE_OPERATORS_FOR_FLAGS(QSerialPort::PinoutSignals)
314
315QT_END_NAMESPACE
316
317#endif // QSERIALPORT_H
318