summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2015-06-26 16:40:44 +0300
committerDenis Shienkov <denis.shienkov@gmail.com>2015-07-10 09:22:22 +0000
commit198e75ad7e2c829fb6ca393ad6deb492efb2ef0e (patch)
treebd97b13161391c6784a440a6483023e9a6361612
parenteb2ab3da76802a18f784e4f72a200e83ad2edc6c (diff)
downloadqtserialport-198e75ad7e2c829fb6ca393ad6deb492efb2ef0e.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. (cherry-picked from 8919ea86c80001be7e826f5363f1b7548c7a8091) Change-Id: Ia7e4d617b863e2131175c52812cdf426ed963795 Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r--src/serialport/qserialport.cpp48
-rw-r--r--src/serialport/qserialport.h2
-rw-r--r--src/serialport/qserialport_p.h13
-rw-r--r--src/serialport/qserialport_unix.cpp170
-rw-r--r--src/serialport/qserialport_unix_p.h10
-rw-r--r--src/serialport/qserialport_win.cpp178
-rw-r--r--src/serialport/qserialport_win_p.h4
-rw-r--r--src/serialport/qserialport_wince.cpp101
-rw-r--r--src/serialport/qserialport_wince_p.h4
9 files changed, 244 insertions, 286 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp
index 0c31cf4..c8216c1 100644
--- a/src/serialport/qserialport.cpp
+++ b/src/serialport/qserialport.cpp
@@ -90,6 +90,15 @@ int QSerialPortPrivateData::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
@@ -509,14 +518,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;
}
@@ -549,7 +558,7 @@ void QSerialPort::close()
{
Q_D(QSerialPort);
if (!isOpen()) {
- setError(QSerialPort::NotOpenError);
+ d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError));
return;
}
@@ -860,7 +869,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;
}
@@ -907,7 +916,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;
}
@@ -957,7 +966,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;
}
@@ -987,7 +996,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;
}
@@ -1009,7 +1018,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;
}
@@ -1071,7 +1080,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;
}
@@ -1122,7 +1131,8 @@ QSerialPort::SerialPortError QSerialPort::error() const
void QSerialPort::clearError()
{
- setError(QSerialPort::NoError);
+ Q_D(QSerialPort);
+ d->setError(QSerialPortErrorInfo(QSerialPort::NoError));
}
/*!
@@ -1295,7 +1305,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;
}
@@ -1324,7 +1334,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;
}
@@ -1371,20 +1381,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
diff --git a/src/serialport/qserialport.h b/src/serialport/qserialport.h
index 6296163..8597232 100644
--- a/src/serialport/qserialport.h
+++ b/src/serialport/qserialport.h
@@ -284,8 +284,6 @@ protected:
qint64 writeData(const char *data, qint64 maxSize) Q_DECL_OVERRIDE;
private:
- void setError(QSerialPort::SerialPortError error, const QString &errorString = QString());
-
QSerialPortPrivate * const d_ptr;
Q_DISABLE_COPY(QSerialPort)
diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h
index 17af974..bafa107 100644
--- a/src/serialport/qserialport_p.h
+++ b/src/serialport/qserialport_p.h
@@ -61,6 +61,19 @@
QT_BEGIN_NAMESPACE
+class QSerialPortErrorInfo
+{
+public:
+ explicit QSerialPortErrorInfo(QSerialPort::SerialPortError errorCode = QSerialPort::UnknownError,
+ const QString &errorString = QString())
+ : errorCode(errorCode)
+ , errorString(errorString)
+ {
+ }
+ QSerialPort::SerialPortError errorCode;
+ QString errorString;
+};
+
class QSerialPortPrivateData
{
public:
diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp
index c5a71ae..e6e0604 100644
--- a/src/serialport/qserialport_unix.cpp
+++ b/src/serialport/qserialport_unix.cpp
@@ -170,20 +170,18 @@ QSerialPortPrivate::QSerialPortPrivate(QSerialPort *q)
bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
{
- Q_Q(QSerialPort);
-
QString lockFilePath = serialPortLockFilePath(QSerialPortInfoPrivate::portNameFromSystemLocation(systemLocation));
bool isLockFileEmpty = lockFilePath.isEmpty();
if (isLockFileEmpty) {
qWarning("Failed to create a lock file for opening the device");
- q->setError(QSerialPort::PermissionError);
+ setError(QSerialPortErrorInfo(QSerialPort::PermissionError));
return false;
}
QScopedPointer<QLockFile> newLockFileScopedPointer(new QLockFile(lockFilePath));
if (!newLockFileScopedPointer->tryLock()) {
- q->setError(QSerialPort::PermissionError);
+ setError(QSerialPortErrorInfo(QSerialPort::PermissionError));
return false;
}
@@ -204,7 +202,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
descriptor = qt_safe_open(systemLocation.toLocal8Bit().constData(), flags);
if (descriptor == -1) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -220,16 +218,14 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
void QSerialPortPrivate::close()
{
- Q_Q(QSerialPort);
-
if (settingsRestoredOnClose) {
if (::tcsetattr(descriptor, TCSANOW, &restoredTermios) == -1)
- q->setError(decodeSystemError());
+ setError(getSystemError());
}
#ifdef TIOCNXCL
if (::ioctl(descriptor, TIOCNXCL) == -1)
- q->setError(decodeSystemError());
+ setError(getSystemError());
#endif
if (readNotifier) {
@@ -245,7 +241,7 @@ void QSerialPortPrivate::close()
}
if (qt_safe_close(descriptor) == -1)
- q->setError(decodeSystemError());
+ setError(getSystemError());
lockFileScopedPointer.reset(Q_NULLPTR);
@@ -256,12 +252,10 @@ void QSerialPortPrivate::close()
QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
{
- Q_Q(QSerialPort);
-
int arg = 0;
if (::ioctl(descriptor, TIOCMGET, &arg) == -1) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return QSerialPort::NoSignal;
}
@@ -315,11 +309,9 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
bool QSerialPortPrivate::setDataTerminalReady(bool set)
{
- Q_Q(QSerialPort);
-
int status = TIOCM_DTR;
if (::ioctl(descriptor, set ? TIOCMBIS : TIOCMBIC, &status) == -1) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -328,11 +320,9 @@ bool QSerialPortPrivate::setDataTerminalReady(bool set)
bool QSerialPortPrivate::setRequestToSend(bool set)
{
- Q_Q(QSerialPort);
-
int status = TIOCM_RTS;
if (::ioctl(descriptor, set ? TIOCMBIS : TIOCMBIC, &status) == -1) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -346,11 +336,9 @@ bool QSerialPortPrivate::flush()
bool QSerialPortPrivate::clear(QSerialPort::Directions directions)
{
- Q_Q(QSerialPort);
-
if (::tcflush(descriptor, (directions == QSerialPort::AllDirections)
? TCIOFLUSH : (directions & QSerialPort::Input) ? TCIFLUSH : TCOFLUSH) == -1) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -359,10 +347,8 @@ bool QSerialPortPrivate::clear(QSerialPort::Directions directions)
bool QSerialPortPrivate::sendBreak(int duration)
{
- Q_Q(QSerialPort);
-
if (::tcsendbreak(descriptor, duration) == -1) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -371,10 +357,8 @@ bool QSerialPortPrivate::sendBreak(int duration)
bool QSerialPortPrivate::setBreakEnabled(bool set)
{
- Q_Q(QSerialPort);
-
if (::ioctl(descriptor, set ? TIOCSBRK : TIOCCBRK) == -1) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -442,22 +426,22 @@ bool QSerialPortPrivate::setBaudRate()
&& setBaudRate(outputBaudRate, QSerialPort::Output));
}
-QSerialPort::SerialPortError
+QSerialPortErrorInfo
QSerialPortPrivate::setBaudRate_helper(qint32 baudRate,
QSerialPort::Directions directions)
{
if ((directions & QSerialPort::Input) && ::cfsetispeed(&currentTermios, baudRate) < 0)
- return decodeSystemError();
+ return getSystemError();
if ((directions & QSerialPort::Output) && ::cfsetospeed(&currentTermios, baudRate) < 0)
- return decodeSystemError();
+ return getSystemError();
- return QSerialPort::NoError;
+ return QSerialPortErrorInfo(QSerialPort::NoError);
}
#if defined(Q_OS_LINUX)
-QSerialPort::SerialPortError
+QSerialPortErrorInfo
QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
struct serial_struct currentSerialInfo;
@@ -469,7 +453,7 @@ QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions
currentSerialInfo.flags &= ~ASYNC_SPD_CUST;
currentSerialInfo.custom_divisor = 0;
if (::ioctl(descriptor, TIOCSSERIAL, &currentSerialInfo) == -1)
- return decodeSystemError();
+ return getSystemError();
}
return setBaudRate_helper(baudRate, directions);
@@ -477,7 +461,7 @@ QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions
#else
-QSerialPort::SerialPortError
+QSerialPortErrorInfo
QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
return setBaudRate_helper(baudRate, directions);
@@ -487,7 +471,7 @@ QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions
#if defined(Q_OS_LINUX)
-QSerialPort::SerialPortError
+QSerialPortErrorInfo
QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
Q_UNUSED(directions);
@@ -497,14 +481,14 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d
::memset(&currentSerialInfo, 0, sizeof(currentSerialInfo));
if (::ioctl(descriptor, TIOCGSERIAL, &currentSerialInfo) == -1)
- return decodeSystemError();
+ return getSystemError();
currentSerialInfo.flags &= ~ASYNC_SPD_MASK;
currentSerialInfo.flags |= (ASYNC_SPD_CUST /* | ASYNC_LOW_LATENCY*/);
currentSerialInfo.custom_divisor = currentSerialInfo.baud_base / baudRate;
if (currentSerialInfo.custom_divisor == 0)
- return QSerialPort::UnsupportedOperationError;
+ return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError);
if (currentSerialInfo.custom_divisor * baudRate != currentSerialInfo.baud_base) {
qWarning("Baud rate of serial port %s is set to %d instead of %d: divisor %f unsupported",
@@ -514,31 +498,31 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d
}
if (::ioctl(descriptor, TIOCSSERIAL, &currentSerialInfo) == -1)
- return decodeSystemError();
+ return getSystemError();
return setBaudRate_helper(B38400, directions);
}
#elif defined(Q_OS_MAC)
-QSerialPort::SerialPortError
+QSerialPortErrorInfo
QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
Q_UNUSED(directions);
#if defined (MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
if (::ioctl(descriptor, IOSSIOSPEED, &baudRate) == -1)
- return decodeSystemError();
+ return getSystemError();
- return QSerialPort::NoError;
+ return QSerialPortErrorInfo(QSerialPort::NoError);
#endif
- return QSerialPort::UnsupportedOperationError;
+ return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError);
}
#elif defined (Q_OS_QNX)
-QSerialPort::SerialPortError
+QSerialPortErrorInfo
QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
// On QNX, the values of the 'Bxxxx' constants are set to 'xxxx' (i.e.
@@ -550,36 +534,34 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d
#else
-QSerialPort::SerialPortError
+QSerialPortErrorInfo
QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
Q_UNUSED(baudRate);
Q_UNUSED(directions);
- return QSerialPort::UnsupportedOperationError;
+ return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError);
}
#endif
bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
- Q_Q(QSerialPort);
-
if (baudRate <= 0) {
- q->setError(QSerialPort::UnsupportedOperationError);
+ setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError));
return false;
}
const qint32 unixBaudRate = QSerialPortPrivate::settingFromBaudRate(baudRate);
- const QSerialPort::SerialPortError error = (unixBaudRate > 0)
+ const QSerialPortErrorInfo error = (unixBaudRate > 0)
? setStandardBaudRate(unixBaudRate, directions)
: setCustomBaudRate(baudRate, directions);
- if (error == QSerialPort::NoError)
+ if (error.errorCode == QSerialPort::NoError)
return updateTermios();
- q->setError(error);
+ setError(error);
return false;
}
@@ -736,12 +718,12 @@ bool QSerialPortPrivate::readNotification()
const qint64 readBytes = readFromPort(ptr, bytesToRead);
if (readBytes <= 0) {
- QSerialPort::SerialPortError error = decodeSystemError();
- if (error != QSerialPort::ResourceError)
- error = QSerialPort::ReadError;
+ QSerialPortErrorInfo error = getSystemError();
+ if (error.errorCode != QSerialPort::ResourceError)
+ error.errorCode = QSerialPort::ReadError;
else
setReadNotificationEnabled(false);
- q->setError(error);
+ setError(error);
readBuffer.chop(bytesToRead);
return false;
}
@@ -768,18 +750,16 @@ bool QSerialPortPrivate::readNotification()
bool QSerialPortPrivate::startAsyncWrite()
{
- Q_Q(QSerialPort);
-
if (writeBuffer.isEmpty() || writeSequenceStarted)
return true;
// Attempt to write it all in one chunk.
qint64 written = writeToPort(writeBuffer.readPointer(), writeBuffer.nextDataBlockSize());
if (written < 0) {
- QSerialPort::SerialPortError error = decodeSystemError();
- if (error != QSerialPort::ResourceError)
- error = QSerialPort::WriteError;
- q->setError(error);
+ QSerialPortErrorInfo error = getSystemError();
+ if (error.errorCode != QSerialPort::ResourceError)
+ error.errorCode = QSerialPort::WriteError;
+ setError(error);
return false;
}
@@ -817,15 +797,13 @@ bool QSerialPortPrivate::completeAsyncWrite()
inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode)
{
- Q_Q(QSerialPort);
-
#ifdef TIOCEXCL
if (::ioctl(descriptor, TIOCEXCL) == -1)
- q->setError(decodeSystemError());
+ setError(getSystemError());
#endif
if (::tcgetattr(descriptor, &restoredTermios) == -1) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -865,69 +843,72 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize)
bool QSerialPortPrivate::updateTermios()
{
- Q_Q(QSerialPort);
-
if (::tcsetattr(descriptor, TCSANOW, &currentTermios) == -1) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
return true;
}
-QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError() const
+QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const
{
- QSerialPort::SerialPortError error;
- switch (errno) {
+ if (systemErrorCode == -1)
+ systemErrorCode = errno;
+
+ QSerialPortErrorInfo error;
+ error.errorString = qt_error_string(systemErrorCode);
+
+ switch (systemErrorCode) {
case ENODEV:
- error = QSerialPort::DeviceNotFoundError;
+ error.errorCode = QSerialPort::DeviceNotFoundError;
break;
#ifdef ENOENT
case ENOENT:
- error = QSerialPort::DeviceNotFoundError;
+ error.errorCode = QSerialPort::DeviceNotFoundError;
break;
#endif
case EACCES:
- error = QSerialPort::PermissionError;
+ error.errorCode = QSerialPort::PermissionError;
break;
case EBUSY:
- error = QSerialPort::PermissionError;
+ error.errorCode = QSerialPort::PermissionError;
break;
case EAGAIN:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
case EIO:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
case EBADF:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
#ifdef Q_OS_MAC
case ENXIO:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
#endif
#ifdef EINVAL
case EINVAL:
- error = QSerialPort::UnsupportedOperationError;
+ error.errorCode = QSerialPort::UnsupportedOperationError;
break;
#endif
#ifdef ENOIOCTLCMD
case ENOIOCTLCMD:
- error = QSerialPort::UnsupportedOperationError;
+ error.errorCode = QSerialPort::UnsupportedOperationError;
break;
#endif
#ifdef ENOTTY
case ENOTTY:
- error = QSerialPort::UnsupportedOperationError;
+ error.errorCode = QSerialPort::UnsupportedOperationError;
break;
#endif
#ifdef EPERM
case EPERM:
- error = QSerialPort::PermissionError;
+ error.errorCode = QSerialPort::PermissionError;
break;
#endif
default:
- error = QSerialPort::UnknownError;
+ error.errorCode = QSerialPort::UnknownError;
break;
}
return error;
@@ -971,8 +952,6 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor
bool checkRead, bool checkWrite,
int msecs)
{
- Q_Q(QSerialPort);
-
Q_ASSERT(selectForRead);
Q_ASSERT(selectForWrite);
@@ -992,11 +971,11 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor
const int ret = ::select(descriptor + 1, &fdread, &fdwrite, 0, msecs < 0 ? 0 : &tv);
if (ret < 0) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
if (ret == 0) {
- q->setError(QSerialPort::TimeoutError);
+ setError(QSerialPortErrorInfo(QSerialPort::TimeoutError));
return false;
}
@@ -1083,8 +1062,6 @@ qint64 QSerialPortPrivate::writePerChar(const char *data, qint64 maxSize)
qint64 QSerialPortPrivate::readPerChar(char *data, qint64 maxSize)
{
- Q_Q(QSerialPort);
-
qint64 ret = 0;
quint8 const charMask = (0xFF >> (8 - dataBits));
@@ -1130,13 +1107,16 @@ qint64 QSerialPortPrivate::readPerChar(char *data, qint64 maxSize)
switch (policy) {
case QSerialPort::SkipPolicy:
continue; //ignore received character
- case QSerialPort::StopReceivingPolicy:
+ case QSerialPort::StopReceivingPolicy: {
+ QSerialPortErrorInfo error;
if (parity != QSerialPort::NoParity)
- q->setError(QSerialPort::ParityError);
+ error.errorCode = QSerialPort::ParityError;
else
- q->setError(*data == '\0' ?
- QSerialPort::BreakConditionError : QSerialPort::FramingError);
+ error.errorCode = *data == '\0' ?
+ QSerialPort::BreakConditionError : QSerialPort::FramingError;
+ setError(error);
return ++ret; //abort receiving
+ }
break;
case QSerialPort::UnknownPolicy:
// Unknown error policy is used! Falling back to PassZeroPolicy
diff --git a/src/serialport/qserialport_unix_p.h b/src/serialport/qserialport_unix_p.h
index 0aee078..8ffb0c5 100644
--- a/src/serialport/qserialport_unix_p.h
+++ b/src/serialport/qserialport_unix_p.h
@@ -133,6 +133,8 @@ public:
bool setFlowControl(QSerialPort::FlowControl flowControl);
bool setDataErrorPolicy(QSerialPort::DataErrorPolicy policy);
+ void setError(const QSerialPortErrorInfo &errorInfo);
+
bool readNotification();
bool startAsyncWrite();
bool completeAsyncWrite();
@@ -163,13 +165,13 @@ private:
bool initialize(QIODevice::OpenMode mode);
bool updateTermios();
- QSerialPort::SerialPortError setBaudRate_helper(qint32 baudRate,
+ QSerialPortErrorInfo setBaudRate_helper(qint32 baudRate,
QSerialPort::Directions directions);
- QSerialPort::SerialPortError setCustomBaudRate(qint32 baudRate,
+ QSerialPortErrorInfo setCustomBaudRate(qint32 baudRate,
QSerialPort::Directions directions);
- QSerialPort::SerialPortError setStandardBaudRate(qint32 baudRate,
+ QSerialPortErrorInfo setStandardBaudRate(qint32 baudRate,
QSerialPort::Directions directions);
- QSerialPort::SerialPortError decodeSystemError() const;
+ QSerialPortErrorInfo getSystemError(int systemErrorCode = -1) const;
bool isReadNotificationEnabled() const;
void setReadNotificationEnabled(bool enable);
diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp
index 5379b13..d0bb24c 100644
--- a/src/serialport/qserialport_win.cpp
+++ b/src/serialport/qserialport_win.cpp
@@ -123,8 +123,6 @@ QSerialPortPrivate::~QSerialPortPrivate()
bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
{
- Q_Q(QSerialPort);
-
DWORD desiredAccess = 0;
originalEventMask = EV_ERR;
@@ -139,7 +137,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
desiredAccess, 0, Q_NULLPTR, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, Q_NULLPTR);
if (handle == INVALID_HANDLE_VALUE) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -152,10 +150,8 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
void QSerialPortPrivate::close()
{
- Q_Q(QSerialPort);
-
if (!::CancelIo(handle))
- q->setError(decodeSystemError());
+ setError(getSystemError());
setReadNotificationEnabled(false);
setWriteNotificationEnabled(false);
@@ -172,25 +168,23 @@ void QSerialPortPrivate::close()
if (settingsRestoredOnClose) {
if (!::SetCommState(handle, &restoredDcb))
- q->setError(decodeSystemError());
+ setError(getSystemError());
else if (!::SetCommTimeouts(handle, &restoredCommTimeouts))
- q->setError(decodeSystemError());
+ setError(getSystemError());
}
if (!::CloseHandle(handle))
- q->setError(decodeSystemError());
+ setError(getSystemError());
handle = INVALID_HANDLE_VALUE;
}
QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
{
- Q_Q(QSerialPort);
-
DWORD modemStat = 0;
if (!::GetCommModemStatus(handle, &modemStat)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return QSerialPort::NoSignal;
}
@@ -209,7 +203,7 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
if (!::DeviceIoControl(handle, IOCTL_SERIAL_GET_DTRRTS, Q_NULLPTR, 0,
&modemStat, sizeof(modemStat),
&bytesReturned, Q_NULLPTR)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return ret;
}
@@ -223,10 +217,8 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
bool QSerialPortPrivate::setDataTerminalReady(bool set)
{
- Q_Q(QSerialPort);
-
if (!::EscapeCommFunction(handle, set ? SETDTR : CLRDTR)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -236,10 +228,8 @@ bool QSerialPortPrivate::setDataTerminalReady(bool set)
bool QSerialPortPrivate::setRequestToSend(bool set)
{
- Q_Q(QSerialPort);
-
if (!::EscapeCommFunction(handle, set ? SETRTS : CLRRTS)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -253,8 +243,6 @@ bool QSerialPortPrivate::flush()
bool QSerialPortPrivate::clear(QSerialPort::Directions directions)
{
- Q_Q(QSerialPort);
-
DWORD flags = 0;
if (directions & QSerialPort::Input)
flags |= PURGE_RXABORT | PURGE_RXCLEAR;
@@ -263,7 +251,7 @@ bool QSerialPortPrivate::clear(QSerialPort::Directions directions)
actualBytesToWrite = 0;
}
if (!::PurgeComm(handle, flags)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -291,10 +279,8 @@ bool QSerialPortPrivate::sendBreak(int duration)
bool QSerialPortPrivate::setBreakEnabled(bool set)
{
- Q_Q(QSerialPort);
-
if (set ? !::SetCommBreak(handle) : !::ClearCommBreak(handle)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -399,10 +385,8 @@ bool QSerialPortPrivate::setBaudRate()
bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
- Q_Q(QSerialPort);
-
if (directions != QSerialPort::AllDirections) {
- q->setError(QSerialPort::UnsupportedOperationError);
+ setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError));
return false;
}
currentDcb.BaudRate = baudRate;
@@ -546,18 +530,16 @@ bool QSerialPortPrivate::_q_completeAsyncWrite()
bool QSerialPortPrivate::startAsyncCommunication()
{
- Q_Q(QSerialPort);
-
if (!setCommunicationNotificationEnabled(true))
return false;
initializeOverlappedStructure(communicationOverlapped);
if (!::WaitCommEvent(handle, &triggeredEventMask, &communicationOverlapped)) {
- QSerialPort::SerialPortError error = decodeSystemError();
- if (error != QSerialPort::NoError) {
- if (error == QSerialPort::PermissionError)
- error = QSerialPort::ResourceError;
- q->setError(error);
+ QSerialPortErrorInfo error = getSystemError();
+ if (error.errorCode != QSerialPort::NoError) {
+ if (error.errorCode == QSerialPort::PermissionError)
+ error.errorCode = QSerialPort::ResourceError;
+ setError(error);
return false;
}
}
@@ -566,8 +548,6 @@ bool QSerialPortPrivate::startAsyncCommunication()
bool QSerialPortPrivate::startAsyncRead()
{
- Q_Q(QSerialPort);
-
if (readStarted)
return true;
@@ -591,13 +571,13 @@ bool QSerialPortPrivate::startAsyncRead()
return true;
}
- QSerialPort::SerialPortError error = decodeSystemError();
- if (error != QSerialPort::NoError) {
- if (error == QSerialPort::PermissionError)
- error = QSerialPort::ResourceError;
- if (error != QSerialPort::ResourceError)
- error = QSerialPort::ReadError;
- q->setError(error);
+ QSerialPortErrorInfo error = getSystemError();
+ if (error.errorCode != QSerialPort::NoError) {
+ if (error.errorCode == QSerialPort::PermissionError)
+ error.errorCode = QSerialPort::ResourceError;
+ if (error.errorCode != QSerialPort::ResourceError)
+ error.errorCode = QSerialPort::ReadError;
+ setError(error);
return false;
}
@@ -607,8 +587,6 @@ bool QSerialPortPrivate::startAsyncRead()
bool QSerialPortPrivate::_q_startAsyncWrite()
{
- Q_Q(QSerialPort);
-
if (writeBuffer.isEmpty() || writeStarted)
return true;
@@ -622,11 +600,11 @@ bool QSerialPortPrivate::_q_startAsyncWrite()
writeBytes,
Q_NULLPTR, &writeCompletionOverlapped)) {
- QSerialPort::SerialPortError error = decodeSystemError();
- if (error != QSerialPort::NoError) {
- if (error != QSerialPort::ResourceError)
- error = QSerialPort::WriteError;
- q->setError(error);
+ QSerialPortErrorInfo error = getSystemError();
+ if (error.errorCode != QSerialPort::NoError) {
+ if (error.errorCode != QSerialPort::ResourceError)
+ error.errorCode = QSerialPort::WriteError;
+ setError(error);
return false;
}
}
@@ -691,35 +669,33 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize)
void QSerialPortPrivate::handleLineStatusErrors()
{
- Q_Q(QSerialPort);
-
DWORD errors = 0;
if (!::ClearCommError(handle, &errors, Q_NULLPTR)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return;
}
+ QSerialPortErrorInfo error;
+
if (errors & CE_FRAME) {
- q->setError(QSerialPort::FramingError);
+ error.errorCode = QSerialPort::FramingError;
} else if (errors & CE_RXPARITY) {
- q->setError(QSerialPort::ParityError);
+ error.errorCode = QSerialPort::ParityError;
parityErrorOccurred = true;
} else if (errors & CE_BREAK) {
- q->setError(QSerialPort::BreakConditionError);
- } else {
- q->setError(QSerialPort::UnknownError);
+ error.errorCode = QSerialPort::BreakConditionError;
}
+
+ setError(error);
}
inline bool QSerialPortPrivate::initialize()
{
- Q_Q(QSerialPort);
-
::ZeroMemory(&restoredDcb, sizeof(restoredDcb));
restoredDcb.DCBlength = sizeof(restoredDcb);
if (!::GetCommState(handle, &restoredDcb)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -740,7 +716,7 @@ inline bool QSerialPortPrivate::initialize()
return false;
if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -751,7 +727,7 @@ inline bool QSerialPortPrivate::initialize()
return false;
if (!::SetCommMask(handle, originalEventMask)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -763,10 +739,8 @@ inline bool QSerialPortPrivate::initialize()
bool QSerialPortPrivate::updateDcb()
{
- Q_Q(QSerialPort);
-
if (!::SetCommState(handle, &currentDcb)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
return true;
@@ -774,10 +748,8 @@ bool QSerialPortPrivate::updateDcb()
bool QSerialPortPrivate::updateCommTimeouts()
{
- Q_Q(QSerialPort);
-
if (!::SetCommTimeouts(handle, &currentCommTimeouts)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
return true;
@@ -785,62 +757,70 @@ bool QSerialPortPrivate::updateCommTimeouts()
qint64 QSerialPortPrivate::overlappedResult(OVERLAPPED &overlapped)
{
- Q_Q(QSerialPort);
-
DWORD bytesTransferred = 0;
if (!::GetOverlappedResult(handle, &overlapped, &bytesTransferred, FALSE)) {
- const QSerialPort::SerialPortError error = decodeSystemError();
- if (error == QSerialPort::NoError)
+ QSerialPortErrorInfo error = getSystemError();
+ if (error.errorCode == QSerialPort::NoError)
return qint64(0);
- if (error != QSerialPort::ResourceError) {
+ if (error.errorCode != QSerialPort::ResourceError) {
if (&overlapped == &readCompletionOverlapped)
- q->setError(QSerialPort::ReadError);
+ error.errorCode = QSerialPort::ReadError;
else if (&overlapped == &writeCompletionOverlapped)
- q->setError(QSerialPort::WriteError);
- else
- q->setError(error);
+ error.errorCode = QSerialPort::WriteError;
+ setError(error);
return qint64(-1);
}
}
return bytesTransferred;
}
-QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError() const
+QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const
{
- QSerialPort::SerialPortError error;
- switch (::GetLastError()) {
+ if (systemErrorCode == -1)
+ systemErrorCode = ::GetLastError();
+
+ QSerialPortErrorInfo error;
+ error.errorString = qt_error_string(systemErrorCode);
+
+ switch (systemErrorCode) {
+ case ERROR_SUCCESS:
+ error.errorCode = QSerialPort::NoError;
+ break;
case ERROR_IO_PENDING:
- error = QSerialPort::NoError;
+ error.errorCode = QSerialPort::NoError;
break;
case ERROR_MORE_DATA:
- error = QSerialPort::NoError;
+ error.errorCode = QSerialPort::NoError;
break;
case ERROR_FILE_NOT_FOUND:
- error = QSerialPort::DeviceNotFoundError;
+ error.errorCode = QSerialPort::DeviceNotFoundError;
break;
case ERROR_INVALID_NAME:
- error = QSerialPort::DeviceNotFoundError;
+ error.errorCode = QSerialPort::DeviceNotFoundError;
break;
case ERROR_ACCESS_DENIED:
- error = QSerialPort::PermissionError;
+ error.errorCode = QSerialPort::PermissionError;
break;
case ERROR_INVALID_HANDLE:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
case ERROR_INVALID_PARAMETER:
- error = QSerialPort::UnsupportedOperationError;
+ error.errorCode = QSerialPort::UnsupportedOperationError;
break;
case ERROR_BAD_COMMAND:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
case ERROR_DEVICE_REMOVED:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
case ERROR_OPERATION_ABORTED:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
+ break;
+ case WAIT_TIMEOUT:
+ error.errorCode = QSerialPort::TimeoutError;
break;
default:
- error = QSerialPort::UnknownError;
+ error.errorCode = QSerialPort::UnknownError;
break;
}
return error;
@@ -848,8 +828,6 @@ QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError() const
bool QSerialPortPrivate::waitAnyEvent(int msecs, HANDLE *triggeredEvent)
{
- Q_Q(QSerialPort);
-
QVector<HANDLE> handles;
if (communicationOverlapped.hEvent)
@@ -864,11 +842,11 @@ bool QSerialPortPrivate::waitAnyEvent(int msecs, HANDLE *triggeredEvent)
FALSE, // wait any event
msecs == -1 ? INFINITE : msecs);
if (waitResult == WAIT_TIMEOUT) {
- q->setError(QSerialPort::TimeoutError, qt_error_string(WAIT_TIMEOUT));
+ setError(getSystemError(WAIT_TIMEOUT));
return false;
}
if (waitResult >= DWORD(WAIT_OBJECT_0 + handles.count())) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -885,7 +863,7 @@ bool QSerialPortPrivate::setCommunicationNotificationEnabled(bool enable)
} else if (enable) {
communicationOverlapped.hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
if (!communicationOverlapped.hEvent) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
communicationNotifier = new QWinEventNotifier(q);
@@ -907,7 +885,7 @@ bool QSerialPortPrivate::setReadNotificationEnabled(bool enable)
} else if (enable) {
readCompletionOverlapped.hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
if (!readCompletionOverlapped.hEvent) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
readCompletionNotifier = new QWinEventNotifier(q);
@@ -929,7 +907,7 @@ bool QSerialPortPrivate::setWriteNotificationEnabled(bool enable)
} else if (enable) {
writeCompletionOverlapped.hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
if (!writeCompletionOverlapped.hEvent) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
writeCompletionNotifier = new QWinEventNotifier(q);
diff --git a/src/serialport/qserialport_win_p.h b/src/serialport/qserialport_win_p.h
index 5a0817a..39813fc 100644
--- a/src/serialport/qserialport_win_p.h
+++ b/src/serialport/qserialport_win_p.h
@@ -100,8 +100,10 @@ public:
bool setFlowControl(QSerialPort::FlowControl flowControl);
bool setDataErrorPolicy(QSerialPort::DataErrorPolicy policy);
+ void setError(const QSerialPortErrorInfo &errorInfo);
+
void handleLineStatusErrors();
- QSerialPort::SerialPortError decodeSystemError() const;
+ QSerialPortErrorInfo getSystemError(int systemErrorCode = -1) const;
bool _q_completeAsyncCommunication();
bool _q_completeAsyncRead();
diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp
index 453769e..c76eaac 100644
--- a/src/serialport/qserialport_wince.cpp
+++ b/src/serialport/qserialport_wince.cpp
@@ -194,8 +194,6 @@ QSerialPortPrivate::QSerialPortPrivate(QSerialPort *q)
bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
{
- Q_Q(QSerialPort);
-
DWORD desiredAccess = 0;
DWORD eventMask = EV_ERR;
@@ -212,7 +210,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
desiredAccess, 0, Q_NULLPTR, OPEN_EXISTING, 0, Q_NULLPTR);
if (handle == INVALID_HANDLE_VALUE) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -243,12 +241,10 @@ void QSerialPortPrivate::close()
QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
{
- Q_Q(QSerialPort);
-
DWORD modemStat = 0;
if (!::GetCommModemStatus(handle, &modemStat)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return QSerialPort::NoSignal;
}
@@ -267,7 +263,7 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
if (!::DeviceIoControl(handle, IOCTL_SERIAL_GET_DTRRTS, Q_NULLPTR, 0,
&modemStat, sizeof(modemStat),
&bytesReturned, Q_NULLPTR)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return ret;
}
@@ -281,10 +277,8 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
bool QSerialPortPrivate::setDataTerminalReady(bool set)
{
- Q_Q(QSerialPort);
-
if (!::EscapeCommFunction(handle, set ? SETDTR : CLRDTR)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -294,10 +288,8 @@ bool QSerialPortPrivate::setDataTerminalReady(bool set)
bool QSerialPortPrivate::setRequestToSend(bool set)
{
- Q_Q(QSerialPort);
-
if (!::EscapeCommFunction(handle, set ? SETRTS : CLRRTS)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -334,10 +326,8 @@ bool QSerialPortPrivate::sendBreak(int duration)
bool QSerialPortPrivate::setBreakEnabled(bool set)
{
- Q_Q(QSerialPort);
-
if (set ? !::SetCommBreak(handle) : !::ClearCommBreak(handle)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -410,10 +400,8 @@ bool QSerialPortPrivate::setBaudRate()
bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions)
{
- Q_Q(QSerialPort);
-
if (directions != QSerialPort::AllDirections) {
- q->setError(QSerialPort::UnsupportedOperationError);
+ setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError));
return false;
}
currentDcb.BaudRate = baudRate;
@@ -524,11 +512,11 @@ bool QSerialPortPrivate::notifyRead()
if (!sucessResult) {
readBuffer.truncate(bytesToRead);
- q->setError(QSerialPort::ReadError);
+ setError(QSerialPortErrorInfo(QSerialPort::ReadError));
return false;
}
- buffer.chop(bytesToRead - qMax(readBytes, DWORD(0)));
+ readBuffer.chop(bytesToRead - qMax(readBytes, DWORD(0)));
// Process emulate policy.
if ((policy != QSerialPort::IgnorePolicy) && parityErrorOccurred) {
@@ -567,7 +555,7 @@ bool QSerialPortPrivate::notifyWrite()
DWORD bytesWritten = 0;
if (!::WriteFile(handle, ptr, nextSize, &bytesWritten, Q_NULLPTR)) {
- q->setError(QSerialPort::WriteError);
+ setError(QSerialPortErrorInfo(QSerialPort::WriteError));
return false;
}
@@ -587,31 +575,31 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize)
return maxSize;
}
-void QSerialPortPrivate::processIoErrors(bool error)
+void QSerialPortPrivate::processIoErrors(bool hasError)
{
- Q_Q(QSerialPort);
-
- if (error) {
- q->setError(QSerialPort::ResourceError);
+ if (hasError) {
+ setError(QSerialPortErrorInfo(QSerialPort::ResourceError));
return;
}
DWORD errors = 0;
if (!::ClearCommError(handle, &errors, Q_NULLPTR)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return;
}
+ QSerialPortErrorInfo error;
+
if (errors & CE_FRAME) {
- q->setError(QSerialPort::FramingError);
+ error.errorCode = QSerialPort::FramingError;
} else if (errors & CE_RXPARITY) {
- q->setError(QSerialPort::ParityError);
+ error.errorCode = QSerialPort::ParityError;
parityErrorOccurred = true;
} else if (errors & CE_BREAK) {
- q->setError(QSerialPort::BreakConditionError);
- } else {
- q->setError(QSerialPort::UnknownError);
+ error.errorCode = QSerialPort::BreakConditionError;
}
+
+ setError(error);
}
inline bool QSerialPortPrivate::initialize(DWORD eventMask)
@@ -622,7 +610,7 @@ inline bool QSerialPortPrivate::initialize(DWORD eventMask)
restoredDcb.DCBlength = sizeof(restoredDcb);
if (!::GetCommState(handle, &restoredDcb)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -641,7 +629,7 @@ inline bool QSerialPortPrivate::initialize(DWORD eventMask)
return false;
if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
@@ -659,8 +647,6 @@ inline bool QSerialPortPrivate::initialize(DWORD eventMask)
bool QSerialPortPrivate::updateDcb()
{
- Q_Q(QSerialPort);
-
QMutexLocker locker(&settingsChangeMutex);
DWORD eventMask = 0;
@@ -673,7 +659,7 @@ bool QSerialPortPrivate::updateDcb()
// Change parameters
bool ret = ::SetCommState(handle, &currentDcb);
if (!ret)
- q->setError(decodeSystemError());
+ setError(getSystemError());
// Restore the event mask
::SetCommMask(handle, eventMask);
@@ -682,48 +668,51 @@ bool QSerialPortPrivate::updateDcb()
bool QSerialPortPrivate::updateCommTimeouts()
{
- Q_Q(QSerialPort);
-
if (!::SetCommTimeouts(handle, &currentCommTimeouts)) {
- q->setError(decodeSystemError());
+ setError(getSystemError());
return false;
}
return true;
}
-QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError() const
+QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const
{
- QSerialPort::SerialPortError error;
- switch (::GetLastError()) {
+ if (systemErrorCode == -1)
+ systemErrorCode = ::GetLastError();
+
+ QSerialPortErrorInfo error;
+ error.errorString = qt_error_string(systemErrorCode);
+
+ switch (systemErrorCode) {
case ERROR_IO_PENDING:
- error = QSerialPort::NoError;
+ error.errorCode = QSerialPort::NoError;
break;
case ERROR_MORE_DATA:
- error = QSerialPort::NoError;
+ error.errorCode = QSerialPort::NoError;
break;
case ERROR_FILE_NOT_FOUND:
- error = QSerialPort::DeviceNotFoundError;
+ error.errorCode = QSerialPort::DeviceNotFoundError;
break;
case ERROR_INVALID_NAME:
- error = QSerialPort::DeviceNotFoundError;
+ error.errorCode = QSerialPort::DeviceNotFoundError;
break;
case ERROR_ACCESS_DENIED:
- error = QSerialPort::PermissionError;
+ error.errorCode = QSerialPort::PermissionError;
break;
case ERROR_INVALID_HANDLE:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
case ERROR_INVALID_PARAMETER:
- error = QSerialPort::UnsupportedOperationError;
+ error.errorCode = QSerialPort::UnsupportedOperationError;
break;
case ERROR_BAD_COMMAND:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
case ERROR_DEVICE_REMOVED:
- error = QSerialPort::ResourceError;
+ error.errorCode = QSerialPort::ResourceError;
break;
default:
- error = QSerialPort::UnknownError;
+ error.errorCode = QSerialPort::UnknownError;
break;
}
return error;
@@ -733,8 +722,6 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor
bool checkRead, bool checkWrite,
int msecs)
{
- Q_Q(QSerialPort);
-
DWORD eventMask = 0;
// FIXME: Here the situation is not properly handled with zero timeout:
// breaker can work out before you call a method WaitCommEvent()
@@ -744,7 +731,7 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor
breaker.stop();
if (breaker.isWorked()) {
- q->setError(QSerialPort::TimeoutError);
+ setError(QSerialPortErrorInfo(QSerialPort::TimeoutError));
} else {
if (checkRead) {
Q_ASSERT(selectForRead);
diff --git a/src/serialport/qserialport_wince_p.h b/src/serialport/qserialport_wince_p.h
index 0a4b800..af9ebd4 100644
--- a/src/serialport/qserialport_wince_p.h
+++ b/src/serialport/qserialport_wince_p.h
@@ -98,8 +98,10 @@ public:
bool setFlowControl(QSerialPort::FlowControl flowControl);
bool setDataErrorPolicy(QSerialPort::DataErrorPolicy policy);
+ void setError(const QSerialPortErrorInfo &errorInfo);
+
void processIoErrors(bool error);
- QSerialPort::SerialPortError decodeSystemError() const;
+ QSerialPortErrorInfo getSystemError(int systemErrorCode = -1) const;
bool notifyRead();
bool notifyWrite();