summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2015-08-27 12:26:01 +0300
committerDenis Shienkov <denis.shienkov@gmail.com>2015-08-28 13:53:54 +0000
commit2610d81ead1fc755606e2bcd1418235bc0051d80 (patch)
treeecb50ad909f02b8ac2cb6b23c0fe740c8cc2777b
parent462f074efbf6ac2cf03b818fa4103f5529be0291 (diff)
downloadqtserialport-2610d81ead1fc755606e2bcd1418235bc0051d80.tar.gz
Unify common error strings in QSerialPortErrorInfo
Change-Id: I1275e80988274e2409455217d5ffb03014962604 Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com> Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r--src/serialport/qserialport.cpp65
-rw-r--r--src/serialport/qserialport_p.h8
-rw-r--r--src/serialport/qserialport_unix.cpp8
-rw-r--r--src/serialport/qserialport_win.cpp6
-rw-r--r--src/serialport/qserialport_wince.cpp14
5 files changed, 71 insertions, 30 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp
index 74604b0..54a3f47 100644
--- a/src/serialport/qserialport.cpp
+++ b/src/serialport/qserialport.cpp
@@ -44,6 +44,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;
+ }
+ }
+}
+
QSerialPortPrivate::QSerialPortPrivate()
: readBufferMaxSize(0)
, writeBuffer(InitialBufferSize)
@@ -522,7 +567,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;
}
@@ -562,7 +607,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;
}
@@ -875,7 +920,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;
}
@@ -922,7 +967,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;
}
@@ -972,7 +1017,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;
}
@@ -1002,7 +1047,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;
}
@@ -1024,7 +1069,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;
}
@@ -1086,7 +1131,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;
}
@@ -1312,7 +1357,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;
}
@@ -1342,7 +1387,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 f3b1eb8..070a316 100644
--- a/src/serialport/qserialport_p.h
+++ b/src/serialport/qserialport_p.h
@@ -109,12 +109,8 @@ QString serialPortLockFilePath(const QString &portName);
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 612d84a..b0f339a 100644
--- a/src/serialport/qserialport_unix.cpp
+++ b/src/serialport/qserialport_unix.cpp
@@ -958,7 +958,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;
}
@@ -1092,11 +1092,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 0c61d36..ce95e41 100644
--- a/src/serialport/qserialport_win.cpp
+++ b/src/serialport/qserialport_win.cpp
@@ -612,12 +612,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::FramingError, QSerialPort::tr("ParityError error detected while reading")));
+ setError(QSerialPortErrorInfo(QSerialPort::FramingError));
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 b49dd75..7e019b3 100644
--- a/src/serialport/qserialport_wince.cpp
+++ b/src/serialport/qserialport_wince.cpp
@@ -491,7 +491,7 @@ bool QSerialPortPrivate::notifyRead()
if (!sucessResult) {
buffer.chop(bytesToRead);
- setError(QSerialPortErrorInfo(QSerialPort::ReadError, QSerialPort::tr("Error reading from device")));
+ setError(QSerialPortErrorInfo(QSerialPort::ReadError));
return false;
}
@@ -534,7 +534,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;
}
@@ -557,7 +557,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;
}
@@ -568,12 +568,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::FramingError, QSerialPort::tr("ParityError error detected while reading")));
+ setError(QSerialPortErrorInfo(QSerialPort::FramingError));
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")));
}
@@ -711,7 +711,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);