diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2015-08-27 12:26:01 +0300 |
---|---|---|
committer | Denis Shienkov <denis.shienkov@gmail.com> | 2015-09-02 19:10:52 +0000 |
commit | 67b92082e6f75151e05f74b53fc55e1bde00ce9e (patch) | |
tree | efd5ef6c6794d04c99bcf7903da8927edc87c946 | |
parent | 3f645a6f0c1d1efb37d6c943035867485f707bea (diff) | |
download | qtserialport-67b92082e6f75151e05f74b53fc55e1bde00ce9e.tar.gz |
Unify common error strings in QSerialPortErrorInfo
(cherry-picked from 2610d81ead1fc755606e2bcd1418235bc0051d80)
Change-Id: I1275e80988274e2409455217d5ffb03014962604
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r-- | src/serialport/qserialport.cpp | 65 | ||||
-rw-r--r-- | src/serialport/qserialport_p.h | 8 | ||||
-rw-r--r-- | src/serialport/qserialport_unix.cpp | 8 | ||||
-rw-r--r-- | src/serialport/qserialport_win.cpp | 6 | ||||
-rw-r--r-- | src/serialport/qserialport_wince.cpp | 14 |
5 files changed, 71 insertions, 30 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index fd64aca..a8ecfbd 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -62,6 +62,51 @@ QT_BEGIN_NAMESPACE +QSerialPortErrorInfo::QSerialPortErrorInfo(QSerialPort::SerialPortError newErrorCode, + const QString &newErrorString) + : errorCode(newErrorCode) + , errorString(newErrorString) +{ + if (errorString.isNull()) { + switch (errorCode) { + case QSerialPort::NoError: + errorString = QSerialPort::tr("No error"); + break; + case QSerialPort::OpenError: + errorString = QSerialPort::tr("Device is already open"); + break; + case QSerialPort::NotOpenError: + errorString = QSerialPort::tr("Device is not open"); + break; + case QSerialPort::TimeoutError: + errorString = QSerialPort::tr("Operation timed out"); + break; + case QSerialPort::ParityError: + errorString = QSerialPort::tr("Parity error detected while reading"); + break; + case QSerialPort::BreakConditionError: + errorString = QSerialPort::tr("Break condition detected while reading"); + break; + case QSerialPort::FramingError: + errorString = QSerialPort::tr("Framing error detected while reading"); + break; + case QSerialPort::ReadError: + errorString = QSerialPort::tr("Error reading from device"); + break; + case QSerialPort::WriteError: + errorString = QSerialPort::tr("Error writing to device"); + break; + case QSerialPort::ResourceError: + errorString = QSerialPort::tr("Device disappeared from the system"); + break; + default: + // an empty string will be interpreted as "Unknown error" + // from the QIODevice::errorString() + break; + } + } +} + QSerialPortPrivateData::QSerialPortPrivateData(QSerialPort *q) : readBufferMaxSize(0) , readBuffer(InitialBufferSize) @@ -518,7 +563,7 @@ bool QSerialPort::open(OpenMode mode) Q_D(QSerialPort); if (isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::OpenError, tr("Device is already open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::OpenError)); return false; } @@ -558,7 +603,7 @@ void QSerialPort::close() { Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); return; } @@ -869,7 +914,7 @@ bool QSerialPort::setDataTerminalReady(bool set) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -916,7 +961,7 @@ bool QSerialPort::setRequestToSend(bool set) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -966,7 +1011,7 @@ QSerialPort::PinoutSignals QSerialPort::pinoutSignals() Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return QSerialPort::NoSignal; } @@ -996,7 +1041,7 @@ bool QSerialPort::flush() Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1018,7 +1063,7 @@ bool QSerialPort::clear(Directions directions) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1080,7 +1125,7 @@ bool QSerialPort::setDataErrorPolicy(DataErrorPolicy policy) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1305,7 +1350,7 @@ bool QSerialPort::sendBreak(int duration) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1334,7 +1379,7 @@ bool QSerialPort::setBreakEnabled(bool set) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); qWarning("%s: device not open", Q_FUNC_INFO); return false; } diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h index bafa107..fa83eec 100644 --- a/src/serialport/qserialport_p.h +++ b/src/serialport/qserialport_p.h @@ -64,12 +64,8 @@ QT_BEGIN_NAMESPACE class QSerialPortErrorInfo { public: - explicit QSerialPortErrorInfo(QSerialPort::SerialPortError errorCode = QSerialPort::UnknownError, - const QString &errorString = QString()) - : errorCode(errorCode) - , errorString(errorString) - { - } + explicit QSerialPortErrorInfo(QSerialPort::SerialPortError newErrorCode = QSerialPort::UnknownError, + const QString &newErrorString = QString()); QSerialPort::SerialPortError errorCode; QString errorString; }; diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index 7bd547e..574bfc5 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -976,7 +976,7 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor return false; } if (ret == 0) { - setError(QSerialPortErrorInfo(QSerialPort::TimeoutError, QSerialPort::tr("Operation timed out"))); + setError(QSerialPortErrorInfo(QSerialPort::TimeoutError)); return false; } @@ -1110,11 +1110,11 @@ qint64 QSerialPortPrivate::readPerChar(char *data, qint64 maxSize) continue; //ignore received character case QSerialPort::StopReceivingPolicy: { if (parity != QSerialPort::NoParity) - setError(QSerialPortErrorInfo(QSerialPort::ParityError, QSerialPort::tr("Parity error detected while reading"))); + setError(QSerialPortErrorInfo(QSerialPort::ParityError)); else if (*data == '\0') - setError(QSerialPortErrorInfo(QSerialPort::BreakConditionError, QSerialPort::tr("Break condition detected while reading"))); + setError(QSerialPortErrorInfo(QSerialPort::BreakConditionError)); else - setError(QSerialPortErrorInfo(QSerialPort::FramingError, QSerialPort::tr("Framing error detected while reading"))); + setError(QSerialPortErrorInfo(QSerialPort::FramingError)); return ++ret; //abort receiving } break; diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index b63368d..c4374f9 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -692,12 +692,12 @@ void QSerialPortPrivate::handleLineStatusErrors() } if (errors & CE_FRAME) { - setError(QSerialPortErrorInfo(QSerialPort::FramingError, QSerialPort::tr("Framing error detected while reading"))); + setError(QSerialPortErrorInfo(QSerialPort::FramingError)); } else if (errors & CE_RXPARITY) { - setError(QSerialPortErrorInfo(QSerialPort::ParityError, QSerialPort::tr("Parity error detected while reading"))); + setError(QSerialPortErrorInfo(QSerialPort::ParityError)); parityErrorOccurred = true; } else if (errors & CE_BREAK) { - setError(QSerialPortErrorInfo(QSerialPort::BreakConditionError, QSerialPort::tr("Break condition detected while reading"))); + setError(QSerialPortErrorInfo(QSerialPort::BreakConditionError)); } else { setError(QSerialPortErrorInfo(QSerialPort::UnknownError, QSerialPort::tr("Unknown streaming error"))); } diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index c27435f..530873b 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -512,7 +512,7 @@ bool QSerialPortPrivate::notifyRead() if (!sucessResult) { readBuffer.truncate(bytesToRead); - setError(QSerialPortErrorInfo(QSerialPort::ReadError, QSerialPort::tr("Error reading from device"))); + setError(QSerialPortErrorInfo(QSerialPort::ReadError)); return false; } @@ -555,7 +555,7 @@ bool QSerialPortPrivate::notifyWrite() DWORD bytesWritten = 0; if (!::WriteFile(handle, ptr, nextSize, &bytesWritten, Q_NULLPTR)) { - setError(QSerialPortErrorInfo(QSerialPort::WriteError, QSerialPort::tr("Error writing to device"))); + setError(QSerialPortErrorInfo(QSerialPort::WriteError)); return false; } @@ -578,7 +578,7 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) void QSerialPortPrivate::processIoErrors(bool hasError) { if (hasError) { - setError(QSerialPortErrorInfo(QSerialPort::ResourceError, QSerialPort::tr("Device disappeared from the system"))); + setError(QSerialPortErrorInfo(QSerialPort::ResourceError)); return; } @@ -589,12 +589,12 @@ void QSerialPortPrivate::processIoErrors(bool hasError) } if (errors & CE_FRAME) { - setError(QSerialPortErrorInfo(QSerialPort::FramingError, QSerialPort::tr("Framing error detected while reading"))); + setError(QSerialPortErrorInfo(QSerialPort::FramingError)); } else if (errors & CE_RXPARITY) { - setError(QSerialPortErrorInfo(QSerialPort::ParityError, QSerialPort::tr("Parity error detected while reading"))); + setError(QSerialPortErrorInfo(QSerialPort::ParityError)); parityErrorOccurred = true; } else if (errors & CE_BREAK) { - setError(QSerialPortErrorInfo(QSerialPort::BreakConditionError, QSerialPort::tr("Break condition detected while reading"))); + setError(QSerialPortErrorInfo(QSerialPort::BreakConditionError)); } else { setError(QSerialPortErrorInfo(QSerialPort::UnknownError, QSerialPort::tr("Unknown streaming error"))); } @@ -732,7 +732,7 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor breaker.stop(); if (breaker.isWorked()) { - setError(QSerialPortErrorInfo(QSerialPort::TimeoutError, QSerialPort::tr("Operation timed out"))); + setError(QSerialPortErrorInfo(QSerialPort::TimeoutError)); } else { if (checkRead) { Q_ASSERT(selectForRead); |