1// Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
2// Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QSERIALPORT_H
6#define QSERIALPORT_H
7
8#include <QtCore/qiodevice.h>
9#include <QtCore/qproperty.h>
10
11#include <QtSerialPort/qserialportglobal.h>
12
13QT_BEGIN_NAMESPACE
14
15class QSerialPortInfo;
16class QSerialPortPrivate;
17
18class Q_SERIALPORT_EXPORT QSerialPort : public QIODevice
19{
20 Q_OBJECT
21 Q_DECLARE_PRIVATE(QSerialPort)
22
23 Q_PROPERTY(qint32 baudRate READ baudRate WRITE setBaudRate NOTIFY baudRateChanged)
24 Q_PROPERTY(DataBits dataBits READ dataBits WRITE setDataBits NOTIFY dataBitsChanged
25 BINDABLE bindableDataBits)
26 Q_PROPERTY(Parity parity READ parity WRITE setParity NOTIFY parityChanged BINDABLE bindableParity)
27 Q_PROPERTY(StopBits stopBits READ stopBits WRITE setStopBits NOTIFY stopBitsChanged
28 BINDABLE bindableStopBits)
29 Q_PROPERTY(FlowControl flowControl READ flowControl WRITE setFlowControl NOTIFY flowControlChanged
30 BINDABLE bindableFlowControl)
31 Q_PROPERTY(bool dataTerminalReady READ isDataTerminalReady WRITE setDataTerminalReady
32 NOTIFY dataTerminalReadyChanged)
33 Q_PROPERTY(bool requestToSend READ isRequestToSend WRITE setRequestToSend NOTIFY requestToSendChanged)
34 Q_PROPERTY(SerialPortError error READ error RESET clearError NOTIFY errorOccurred BINDABLE bindableError)
35 Q_PROPERTY(bool breakEnabled READ isBreakEnabled WRITE setBreakEnabled NOTIFY breakEnabledChanged
36 BINDABLE bindableIsBreakEnabled)
37 Q_PROPERTY(bool settingsRestoredOnClose READ settingsRestoredOnClose
38 WRITE setSettingsRestoredOnClose NOTIFY settingsRestoredOnCloseChanged
39 REVISION(6, 9))
40
41#if defined(Q_OS_WIN32)
42 typedef void* Handle;
43#else
44 typedef int Handle;
45#endif
46
47public:
48
49 enum Direction {
50 Input = 1,
51 Output = 2,
52 AllDirections = Input | Output
53 };
54 Q_FLAG(Direction)
55 Q_DECLARE_FLAGS(Directions, Direction)
56
57 enum BaudRate {
58 Baud1200 = 1200,
59 Baud2400 = 2400,
60 Baud4800 = 4800,
61 Baud9600 = 9600,
62 Baud19200 = 19200,
63 Baud38400 = 38400,
64 Baud57600 = 57600,
65 Baud115200 = 115200
66 };
67 Q_ENUM(BaudRate)
68
69 enum DataBits {
70 Data5 = 5,
71 Data6 = 6,
72 Data7 = 7,
73 Data8 = 8
74 };
75 Q_ENUM(DataBits)
76
77 enum Parity {
78 NoParity = 0,
79 EvenParity = 2,
80 OddParity = 3,
81 SpaceParity = 4,
82 MarkParity = 5
83 };
84 Q_ENUM(Parity)
85
86 enum StopBits {
87 OneStop = 1,
88 OneAndHalfStop = 3,
89 TwoStop = 2
90 };
91 Q_ENUM(StopBits)
92
93 enum FlowControl {
94 NoFlowControl,
95 HardwareControl,
96 SoftwareControl
97 };
98 Q_ENUM(FlowControl)
99
100 enum PinoutSignal {
101 NoSignal = 0x00,
102 DataTerminalReadySignal = 0x04,
103 DataCarrierDetectSignal = 0x08,
104 DataSetReadySignal = 0x10,
105 RingIndicatorSignal = 0x20,
106 RequestToSendSignal = 0x40,
107 ClearToSendSignal = 0x80,
108 SecondaryTransmittedDataSignal = 0x100,
109 SecondaryReceivedDataSignal = 0x200
110 };
111 Q_FLAG(PinoutSignal)
112 Q_DECLARE_FLAGS(PinoutSignals, PinoutSignal)
113
114 enum SerialPortError {
115 NoError,
116 DeviceNotFoundError,
117 PermissionError,
118 OpenError,
119 WriteError,
120 ReadError,
121 ResourceError,
122 UnsupportedOperationError,
123 UnknownError,
124 TimeoutError,
125 NotOpenError
126 };
127 Q_ENUM(SerialPortError)
128
129 explicit QSerialPort(QObject *parent = nullptr);
130 explicit QSerialPort(const QString &name, QObject *parent = nullptr);
131 explicit QSerialPort(const QSerialPortInfo &info, QObject *parent = nullptr);
132 virtual ~QSerialPort();
133
134 void setPortName(const QString &name);
135 QString portName() const;
136
137 void setPort(const QSerialPortInfo &info);
138
139 bool open(OpenMode mode) override;
140 void close() override;
141
142 bool setBaudRate(qint32 baudRate, Directions directions = AllDirections);
143 qint32 baudRate(Directions directions = AllDirections) const;
144
145 bool setDataBits(DataBits dataBits);
146 DataBits dataBits() const;
147 QBindable<DataBits> bindableDataBits();
148
149 bool setParity(Parity parity);
150 Parity parity() const;
151 QBindable<Parity> bindableParity();
152
153 bool setStopBits(StopBits stopBits);
154 StopBits stopBits() const;
155#if QT_SERIALPORT_REMOVED_SINCE(6, 7)
156 QBindable<bool> bindableStopBits();
157#endif
158 QBindable<StopBits> bindableStopBits(QT6_DECL_NEW_OVERLOAD);
159
160 bool setFlowControl(FlowControl flowControl);
161 FlowControl flowControl() const;
162 QBindable<FlowControl> bindableFlowControl();
163
164 bool setDataTerminalReady(bool set);
165 bool isDataTerminalReady();
166
167 bool setRequestToSend(bool set);
168 bool isRequestToSend();
169
170 PinoutSignals pinoutSignals();
171
172 bool flush();
173 bool clear(Directions directions = AllDirections);
174
175 SerialPortError error() const;
176 void clearError();
177 QBindable<SerialPortError> bindableError() const;
178
179 qint64 readBufferSize() const;
180 void setReadBufferSize(qint64 size);
181
182 qint64 writeBufferSize() const;
183 void setWriteBufferSize(qint64 size);
184
185 bool isSequential() const override;
186
187 qint64 bytesAvailable() const override;
188 qint64 bytesToWrite() const override;
189 bool canReadLine() const override;
190
191 bool waitForReadyRead(int msecs = 30000) override;
192 bool waitForBytesWritten(int msecs = 30000) override;
193
194 bool setBreakEnabled(bool set = true);
195 bool isBreakEnabled() const;
196 QBindable<bool> bindableIsBreakEnabled();
197
198 bool settingsRestoredOnClose() const;
199 void setSettingsRestoredOnClose(bool restore);
200
201 Handle handle() const;
202
203Q_SIGNALS:
204 void baudRateChanged(qint32 baudRate, QSerialPort::Directions directions);
205 void dataBitsChanged(QSerialPort::DataBits dataBits);
206 void parityChanged(QSerialPort::Parity parity);
207 void stopBitsChanged(QSerialPort::StopBits stopBits);
208 void flowControlChanged(QSerialPort::FlowControl flowControl);
209 void dataTerminalReadyChanged(bool set);
210 void requestToSendChanged(bool set);
211 void errorOccurred(QSerialPort::SerialPortError error);
212 void breakEnabledChanged(bool set);
213 void settingsRestoredOnCloseChanged(bool restore);
214
215protected:
216 qint64 readData(char *data, qint64 maxSize) override;
217 qint64 readLineData(char *data, qint64 maxSize) override;
218 qint64 writeData(const char *data, qint64 maxSize) override;
219
220private:
221 Q_DISABLE_COPY(QSerialPort)
222
223#if defined(Q_OS_WIN32)
224 Q_PRIVATE_SLOT(d_func(), bool _q_startAsyncWrite())
225 Q_PRIVATE_SLOT(d_func(), void _q_notified(quint32, quint32, OVERLAPPED*))
226#endif
227};
228
229Q_DECLARE_OPERATORS_FOR_FLAGS(QSerialPort::Directions)
230Q_DECLARE_OPERATORS_FOR_FLAGS(QSerialPort::PinoutSignals)
231
232QT_END_NAMESPACE
233
234#endif // QSERIALPORT_H
235

source code of qtserialport/src/serialport/qserialport.h