diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2015-06-26 16:40:44 +0300 |
---|---|---|
committer | Denis Shienkov <denis.shienkov@gmail.com> | 2015-07-08 13:22:25 +0000 |
commit | 8919ea86c80001be7e826f5363f1b7548c7a8091 (patch) | |
tree | 14d8b27710aec73d50d579849616353e1e0a007e /src/serialport/qserialport.cpp | |
parent | 1eaa42c99d8442a40718b8560903b0bfeb401a0e (diff) | |
download | qtserialport-8919ea86c80001be7e826f5363f1b7548c7a8091.tar.gz |
Improve the processing of errors
Sometimes the error string would contain a wrong description which
did not correspond to the system error code. The reason was
qt_error_string() being called too late, when the system error
might have already been overwritten.
The error processing is now in QSPP::getSystemError(), which returns both
the error code and the error description as soon as possible.
* Now the QSPP::getSystemError() returns the new class
QSerialPortErrorInfo which contains all necessary fields.
* The new method QSPP::setError() which accepts the
QSerialPortErrorInfo as input parameter is used.
* The old private method QSP::setError() is removed, because
it is not used anywhere.
Change-Id: Ia7e4d617b863e2131175c52812cdf426ed963795
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Diffstat (limited to 'src/serialport/qserialport.cpp')
-rw-r--r-- | src/serialport/qserialport.cpp | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index 1aea7ca..b6c4252 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -97,6 +97,15 @@ int QSerialPortPrivate::timeoutValue(int msecs, int elapsed) return qMax(msecs, 0); } +void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo) +{ + Q_Q(QSerialPort); + + error = errorInfo.errorCode; + q->setErrorString(errorInfo.errorString); + emit q->error(error); +} + /*! \class QSerialPort @@ -512,14 +521,14 @@ bool QSerialPort::open(OpenMode mode) Q_D(QSerialPort); if (isOpen()) { - setError(QSerialPort::OpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::OpenError)); return false; } // Define while not supported modes. static const OpenMode unsupportedModes = Append | Truncate | Text | Unbuffered; if ((mode & unsupportedModes) || mode == NotOpen) { - setError(QSerialPort::UnsupportedOperationError); + d->setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); return false; } @@ -552,7 +561,7 @@ void QSerialPort::close() { Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); return; } @@ -862,7 +871,7 @@ bool QSerialPort::setDataTerminalReady(bool set) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -909,7 +918,7 @@ bool QSerialPort::setRequestToSend(bool set) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -959,7 +968,7 @@ QSerialPort::PinoutSignals QSerialPort::pinoutSignals() Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return QSerialPort::NoSignal; } @@ -989,7 +998,7 @@ bool QSerialPort::flush() Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1011,7 +1020,7 @@ bool QSerialPort::clear(Directions directions) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1073,7 +1082,7 @@ bool QSerialPort::setDataErrorPolicy(DataErrorPolicy policy) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1124,7 +1133,8 @@ QSerialPort::SerialPortError QSerialPort::error() const void QSerialPort::clearError() { - setError(QSerialPort::NoError); + Q_D(QSerialPort); + d->setError(QSerialPortErrorInfo(QSerialPort::NoError)); } /*! @@ -1294,7 +1304,7 @@ bool QSerialPort::sendBreak(int duration) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1323,7 +1333,7 @@ bool QSerialPort::setBreakEnabled(bool set) Q_D(QSerialPort); if (!isOpen()) { - setError(QSerialPort::NotOpenError); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1387,20 +1397,6 @@ qint64 QSerialPort::writeData(const char *data, qint64 maxSize) return d->writeData(data, maxSize); } -void QSerialPort::setError(QSerialPort::SerialPortError serialPortError, const QString &errorString) -{ - Q_D(QSerialPort); - - d->error = serialPortError; - - if (errorString.isNull() && (serialPortError != QSerialPort::NoError)) - setErrorString(qt_error_string(-1)); - else - setErrorString(errorString); - - emit error(serialPortError); -} - #include "moc_qserialport.cpp" QT_END_NAMESPACE |