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

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