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