diff options
Diffstat (limited to 'src/serialport/qserialport.cpp')
-rw-r--r-- | src/serialport/qserialport.cpp | 381 |
1 files changed, 76 insertions, 305 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index 44d6cd1..d4265b6 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -1,44 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> -** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> -** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> -** Copyright (C) 2012 Andre Hartmann <aha_1980@gmx.de> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtSerialPort module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> +// Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> +// Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> +// Copyright (C) 2012 Andre Hartmann <aha_1980@gmx.de> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qserialport.h" #include "qserialportinfo.h" @@ -99,12 +63,10 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) { Q_Q(QSerialPort); - error = errorInfo.errorCode; q->setErrorString(errorInfo.errorString); + error.setValue(errorInfo.errorCode); + error.notify(); emit q->errorOccurred(error); -#if QT_DEPRECATED_SINCE(5, 8) - emit q->error(error); -#endif } /*! @@ -235,9 +197,6 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) \value Baud38400 38400 baud. \value Baud57600 57600 baud. \value Baud115200 115200 baud. - \value UnknownBaud Unknown baud. This value is obsolete. It is provided to - keep old source code working. We strongly advise against - using it in new code. \sa QSerialPort::baudRate */ @@ -259,9 +218,6 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) is used for most kinds of data, as this size matches the size of a byte. It is almost universally used in newer applications. - \value UnknownDataBits Unknown number of bits. This value is obsolete. It - is provided to keep old source code working. We - strongly advise against using it in new code. \sa QSerialPort::dataBits */ @@ -285,9 +241,6 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) \value MarkParity Mark parity. The parity bit is always set to the mark signal condition (logical 1). It does not provide error detection information. - \value UnknownParity Unknown parity. This value is obsolete. It is - provided to keep old source code working. We - strongly advise against using it in new code. \sa QSerialPort::parity */ @@ -300,9 +253,6 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) \value OneStop 1 stop bit. \value OneAndHalfStop 1.5 stop bits. This is only for the Windows platform. \value TwoStop 2 stop bits. - \value UnknownStopBits Unknown number of stop bits. This value is obsolete. - It is provided to keep old source code working. We - strongly advise against using it in new code. \sa QSerialPort::stopBits */ @@ -315,9 +265,6 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) \value NoFlowControl No flow control. \value HardwareControl Hardware flow control (RTS/CTS). \value SoftwareControl Software flow control (XON/XOFF). - \value UnknownFlowControl Unknown flow control. This value is obsolete. It - is provided to keep old source code working. We - strongly advise against using it in new code. \sa QSerialPort::flowControl */ @@ -328,14 +275,6 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) This enum describes the possible RS-232 pinout signals. \value NoSignal No line active - \value TransmittedDataSignal TxD (Transmitted Data). This value is - obsolete. It is provided to keep old - source code working. We strongly - advise against using it in new code. - \value ReceivedDataSignal RxD (Received Data). This value is - obsolete. It is provided to keep old - source code working. We strongly - advise against using it in new code. \value DataTerminalReadySignal DTR (Data Terminal Ready). \value DataCarrierDetectSignal DCD (Data Carrier Detect). \value DataSetReadySignal DSR (Data Set Ready). @@ -349,24 +288,6 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) QSerialPort::requestToSend */ -#if QT_DEPRECATED_SINCE(5, 2) -/*! - \enum QSerialPort::DataErrorPolicy - \obsolete - - This enum describes the policies for the received symbols - while parity errors were detected. - - \value SkipPolicy Skips the bad character. - \value PassZeroPolicy Replaces bad character with zero. - \value IgnorePolicy Ignores the error for a bad character. - \value StopReceivingPolicy Stops data reception on error. - \value UnknownPolicy Unknown policy. - - \sa QSerialPort::dataErrorPolicy -*/ -#endif - /*! \enum QSerialPort::SerialPortError @@ -391,18 +312,6 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) device is open. This value was introduced in QtSerialPort 5.2. - \value ParityError Parity error detected by the hardware while - reading data. This value is obsolete. We strongly - advise against using it in new code. - - \value FramingError Framing error detected by the hardware while - reading data. This value is obsolete. We strongly - advise against using it in new code. - - \value BreakConditionError Break condition detected by the hardware on - the input line. This value is obsolete. We strongly - advise against using it in new code. - \value WriteError An I/O error occurred while writing the data. \value ReadError An I/O error occurred while reading the data. @@ -429,7 +338,6 @@ void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) */ QSerialPort::QSerialPort(QObject *parent) : QIODevice(*new QSerialPortPrivate, parent) - , d_dummy(0) { } @@ -441,7 +349,6 @@ QSerialPort::QSerialPort(QObject *parent) */ QSerialPort::QSerialPort(const QString &name, QObject *parent) : QIODevice(*new QSerialPortPrivate, parent) - , d_dummy(0) { setPortName(name); } @@ -453,7 +360,6 @@ QSerialPort::QSerialPort(const QString &name, QObject *parent) */ QSerialPort::QSerialPort(const QSerialPortInfo &serialPortInfo, QObject *parent) : QIODevice(*new QSerialPortPrivate, parent) - , d_dummy(0) { setPort(serialPortInfo); } @@ -531,10 +437,10 @@ QString QSerialPort::portName() const not set any of the port settings successfully. In that case, the port is closed automatically not to leave the port around with incorrect settings. - \warning The \a mode has to be QIODevice::ReadOnly, QIODevice::WriteOnly, - or QIODevice::ReadWrite. Other modes are unsupported. + \warning The \a mode has to be QIODeviceBase::ReadOnly, QIODeviceBase::WriteOnly, + or QIODeviceBase::ReadWrite. Other modes are unsupported. - \sa QIODevice::OpenMode, setPort() + \sa QIODeviceBase::OpenMode, setPort() */ bool QSerialPort::open(OpenMode mode) { @@ -577,57 +483,10 @@ void QSerialPort::close() } d->close(); - d->isBreakEnabled = false; + d->isBreakEnabled.setValue(false); QIODevice::close(); } -QT_WARNING_PUSH -QT_WARNING_DISABLE_DEPRECATED - -#if QT_DEPRECATED_SINCE(5, 3) -/*! - \property QSerialPort::settingsRestoredOnClose - \brief the flag which specifies to restore the previous settings when closing - the serial port. - \obsolete - - If this flag is \c true, the settings will be restored; otherwise not. - The default state of the QSerialPort class is to restore the - settings. -*/ -void QSerialPort::setSettingsRestoredOnClose(bool restore) -{ - Q_D(QSerialPort); - - if (d->settingsRestoredOnClose != restore) { - d->settingsRestoredOnClose = restore; - emit settingsRestoredOnCloseChanged(d->settingsRestoredOnClose); - } -} - -QT_WARNING_POP - -bool QSerialPort::settingsRestoredOnClose() const -{ - Q_D(const QSerialPort); - return d->settingsRestoredOnClose; -} -#endif // QT_DEPRECATED_SINCE(5,3) - -#if QT_DEPRECATED_SINCE(5, 5) -/*! - \fn void QSerialPort::settingsRestoredOnCloseChanged(bool restore) - \obsolete - - This signal is emitted after the flag which specifies to restore the - previous settings while closing the serial port has been changed. The new - flag which specifies to restore the previous settings while closing the serial - port is passed as \a restore. - - \sa QSerialPort::settingsRestoredOnClose -*/ -#endif // QT_DEPRECATED_SINCE(5, 5) - /*! \property QSerialPort::baudRate \brief the data baud rate for the desired direction @@ -712,15 +571,16 @@ qint32 QSerialPort::baudRate(Directions directions) const bool QSerialPort::setDataBits(DataBits dataBits) { Q_D(QSerialPort); - + d->dataBits.removeBindingUnlessInWrapper(); + const auto currentDataBits = d->dataBits.value(); if (!isOpen() || d->setDataBits(dataBits)) { - if (d->dataBits != dataBits) { - d->dataBits = dataBits; - emit dataBitsChanged(d->dataBits); + d->dataBits.setValueBypassingBindings(dataBits); + if (currentDataBits != dataBits) { + d->dataBits.notify(); + emit dataBitsChanged(dataBits); } return true; } - return false; } @@ -730,6 +590,11 @@ QSerialPort::DataBits QSerialPort::dataBits() const return d->dataBits; } +QBindable<QSerialPort::DataBits> QSerialPort::bindableDataBits() +{ + return &d_func()->dataBits; +} + /*! \fn void QSerialPort::dataBitsChanged(DataBits dataBits) @@ -757,15 +622,16 @@ QSerialPort::DataBits QSerialPort::dataBits() const bool QSerialPort::setParity(Parity parity) { Q_D(QSerialPort); - + d->parity.removeBindingUnlessInWrapper(); + const auto currentParity = d->parity.value(); if (!isOpen() || d->setParity(parity)) { - if (d->parity != parity) { - d->parity = parity; - emit parityChanged(d->parity); + d->parity.setValueBypassingBindings(parity); + if (currentParity != parity) { + d->parity.notify(); + emit parityChanged(parity); } return true; } - return false; } @@ -775,6 +641,11 @@ QSerialPort::Parity QSerialPort::parity() const return d->parity; } +QBindable<QSerialPort::Parity> QSerialPort::bindableParity() +{ + return &d_func()->parity; +} + /*! \fn void QSerialPort::parityChanged(Parity parity) @@ -801,15 +672,16 @@ QSerialPort::Parity QSerialPort::parity() const bool QSerialPort::setStopBits(StopBits stopBits) { Q_D(QSerialPort); - + d->stopBits.removeBindingUnlessInWrapper(); + const auto currentStopBits = d->stopBits.value(); if (!isOpen() || d->setStopBits(stopBits)) { - if (d->stopBits != stopBits) { - d->stopBits = stopBits; - emit stopBitsChanged(d->stopBits); + d->stopBits.setValueBypassingBindings(stopBits); + if (currentStopBits != stopBits) { + d->stopBits.notify(); + emit stopBitsChanged(stopBits); } return true; } - return false; } @@ -819,6 +691,11 @@ QSerialPort::StopBits QSerialPort::stopBits() const return d->stopBits; } +QBindable<bool> QSerialPort::bindableStopBits() +{ + return &d_func()->stopBits; +} + /*! \fn void QSerialPort::stopBitsChanged(StopBits stopBits) @@ -845,15 +722,16 @@ QSerialPort::StopBits QSerialPort::stopBits() const bool QSerialPort::setFlowControl(FlowControl flowControl) { Q_D(QSerialPort); - + d->flowControl.removeBindingUnlessInWrapper(); + const auto currentFlowControl = d->flowControl.value(); if (!isOpen() || d->setFlowControl(flowControl)) { - if (d->flowControl != flowControl) { - d->flowControl = flowControl; - emit flowControlChanged(d->flowControl); + d->flowControl.setValueBypassingBindings(flowControl); + if (currentFlowControl != flowControl) { + d->flowControl.notify(); + emit flowControlChanged(flowControl); } return true; } - return false; } @@ -863,6 +741,11 @@ QSerialPort::FlowControl QSerialPort::flowControl() const return d->flowControl; } +QBindable<QSerialPort::FlowControl> QSerialPort::bindableFlowControl() +{ + return &d_func()->flowControl; +} + /*! \fn void QSerialPort::flowControlChanged(FlowControl flow) @@ -1061,89 +944,6 @@ bool QSerialPort::clear(Directions directions) } /*! - \reimp - - Returns \c true if no more data is currently available for reading; otherwise - returns \c false. - - This function is most commonly used when reading data from the - serial port in a loop. For example: - - \code - // This slot is connected to QSerialPort::readyRead() - void QSerialPortClass::readyReadSlot() - { - while (!port.atEnd()) { - QByteArray data = port.read(100); - .... - } - } - \endcode - - \sa bytesAvailable(), readyRead() - */ -bool QSerialPort::atEnd() const -{ - return QIODevice::atEnd(); -} - -#if QT_DEPRECATED_SINCE(5, 2) -/*! - \property QSerialPort::dataErrorPolicy - \brief the error policy for how the process receives characters in the case where - a parity error is detected. - \obsolete - - If the setting is successful, returns \c true; otherwise returns \c false. The - default policy set is IgnorePolicy. - - \note The serial port has to be open before trying to set this property; - otherwise returns \c false and sets the NotOpenError error code. This is a bit - unusual as opposed to the regular Qt property settings of a class. However, - this is a special use case since the property is set through the interaction - with the kernel and hardware. Hence, the two scenarios cannot be completely - compared to each other. -*/ -bool QSerialPort::setDataErrorPolicy(DataErrorPolicy policy) -{ - Q_D(QSerialPort); - - if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); - qWarning("%s: device not open", Q_FUNC_INFO); - return false; - } - - if (policy != QSerialPort::IgnorePolicy) { - d->setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, - tr("The device supports only the ignoring policy"))); - return false; - } - - return true; -} - -QSerialPort::DataErrorPolicy QSerialPort::dataErrorPolicy() const -{ - return QSerialPort::IgnorePolicy; -} -#endif // QT_DEPRECATED_SINCE(5, 2) - -#if QT_DEPRECATED_SINCE(5, 5) -/*! - \fn void QSerialPort::dataErrorPolicyChanged(DataErrorPolicy policy) - \obsolete - - This signal is emitted after the error policy for how the process receives - characters in case of parity error detection has been changed. The new error - policy for how the process receives the character in case of parity error - detection is passed as \a policy. - - \sa QSerialPort::dataErrorPolicy -*/ -#endif // QT_DEPRECATED_SINCE(5, 5) - -/*! \property QSerialPort::error \brief the error status of the serial port @@ -1166,14 +966,10 @@ void QSerialPort::clearError() d->setError(QSerialPortErrorInfo(QSerialPort::NoError)); } -#if QT_DEPRECATED_SINCE(5, 8) -/*! - \fn void QSerialPort::error(SerialPortError error) - \obsolete - - Use errorOccurred() instead. -*/ -#endif +QBindable<QSerialPort::SerialPortError> QSerialPort::bindableError() const +{ + return &d_func()->error; +} /*! \fn void QSerialPort::errorOccurred(SerialPortError error) @@ -1327,37 +1123,6 @@ bool QSerialPort::waitForBytesWritten(int msecs) return d->waitForBytesWritten(msecs); } -#if QT_DEPRECATED_SINCE(5, 5) -/*! - Sends a continuous stream of zero bits during a specified period - of time \a duration in msec if the terminal is using asynchronous - serial data. If successful, returns \c true; otherwise returns \c false. - - If the duration is zero then zero bits are transmitted by at least - \c 0.25 seconds, but no more than \c 0.5 seconds. - - If the duration is non zero then zero bits are transmitted within a certain - period of time depending on the implementation. - - \note The serial port has to be open before trying to send a break - duration; otherwise returns \c false and sets the NotOpenError error code. - - \sa setBreakEnabled() -*/ -bool QSerialPort::sendBreak(int duration) -{ - Q_D(QSerialPort); - - if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); - qWarning("%s: device not open", Q_FUNC_INFO); - return false; - } - - return d->sendBreak(duration); -} -#endif // QT_DEPRECATED_SINCE(5, 5) - /*! \property QSerialPort::breakEnabled \since 5.5 @@ -1377,19 +1142,20 @@ bool QSerialPort::sendBreak(int duration) bool QSerialPort::setBreakEnabled(bool set) { Q_D(QSerialPort); - - if (!isOpen()) { + d->isBreakEnabled.removeBindingUnlessInWrapper(); + const auto currentSet = d->isBreakEnabled.value(); + if (isOpen()) { + if (d->setBreakEnabled(set)) { + d->isBreakEnabled.setValueBypassingBindings(set); + if (currentSet != set) { + d->isBreakEnabled.notify(); + emit breakEnabledChanged(set); + } + return true; + } + } else { d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); - return false; - } - - if (d->setBreakEnabled(set)) { - if (d->isBreakEnabled != set) { - d->isBreakEnabled = set; - emit breakEnabledChanged(d->isBreakEnabled); - } - return true; } return false; } @@ -1400,6 +1166,11 @@ bool QSerialPort::isBreakEnabled() const return d->isBreakEnabled; } +QBindable<bool> QSerialPort::bindableIsBreakEnabled() +{ + return &d_func()->isBreakEnabled; +} + /*! \reimp |