From a7a4fa4b628526a5d9138ed56bb195be71bac118 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Fri, 26 Jun 2015 11:30:19 +0300 Subject: Fix crash in QSPI::availablePorts() on OS X 10.10 CFTypeRef returned by IORegistryEntrySearchCFProperty sometimes may be of unexpected type (as CFStringRef instead of CFNumberRef). In this case the CFNumberGetValue() throws the exception. The simplest fix is to check the type in searchShortIntProperty() before calling CFNumberGetValue(): CFGetTypeID(result.as()) == CFNumberGetTypeID(). Thanks to Orest Hera. Task-number: QTBUG-46875 Change-Id: Id86993c008595f9762a08739bf4c5f5662643e92 Reviewed-by: Dyami Caliri Reviewed-by: Jake Petroules --- src/serialport/qserialportinfo_mac.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/serialport/qserialportinfo_mac.cpp b/src/serialport/qserialportinfo_mac.cpp index 558dcbd..8d4034a 100644 --- a/src/serialport/qserialportinfo_mac.cpp +++ b/src/serialport/qserialportinfo_mac.cpp @@ -72,9 +72,10 @@ static quint16 searchShortIntProperty(io_registry_entry_t ioRegistryEntry, bool &ok) { const QCFType result(searchProperty(ioRegistryEntry, propertyKey)); + const CFNumberRef ref = result.as(); quint16 value = 0; - ok = result.as() - && (::CFNumberGetValue(result.as(), kCFNumberShortType, &value) > 0); + ok = ref && (::CFGetTypeID(ref) == ::CFNumberGetTypeID()) + && (::CFNumberGetValue(ref, kCFNumberShortType, &value) > 0); return value; } -- cgit v1.2.1 From bc948f5f695908ad11a19b1336975d07ff1de0e1 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Fri, 26 Jun 2015 17:10:27 +0300 Subject: Check on CFStringRef type before of QString creation It is make sense to check the returned QCFType property on the null-pointer and on the CFStringRef type to exclude of possible exception. Thanks to Orest Hera. Change-Id: I09f784b9ce8aa471dfb53181798d0066945efc86 Reviewed-by: Dyami Caliri Reviewed-by: Jake Petroules --- src/serialport/qserialportinfo_mac.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/serialport/qserialportinfo_mac.cpp b/src/serialport/qserialportinfo_mac.cpp index 8d4034a..e917545 100644 --- a/src/serialport/qserialportinfo_mac.cpp +++ b/src/serialport/qserialportinfo_mac.cpp @@ -64,7 +64,11 @@ static QCFType searchProperty(io_registry_entry_t ioRegistryEntry, static QString searchStringProperty(io_registry_entry_t ioRegistryEntry, const QCFString &propertyKey) { - return QCFString::toQString(searchProperty(ioRegistryEntry, propertyKey).as()); + const QCFType result(searchProperty(ioRegistryEntry, propertyKey)); + const CFStringRef ref = result.as(); + if (ref && (::CFGetTypeID(ref) == ::CFStringGetTypeID())) + return QCFString::toQString(ref); + return QString(); } static quint16 searchShortIntProperty(io_registry_entry_t ioRegistryEntry, -- cgit v1.2.1 From 96365c1edc5282b6ec7332f79f2f698ce5f6b6be Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Tue, 12 May 2015 17:31:08 +0300 Subject: Get rid of QSPP::readData() Because we now (in the Qt 5.5 branch) use QIODevicePrivate's internal read buffer directly, there is no need to call buffer.read() in QSP::readData(). There is also no point in keeping the platform-specific implementations of QSPP::readData(), so remove them. Tested on Windows using the virtual com0com serial ports, and on Linux usig the virtual tty0tty serial ports. Change-Id: I136c6d10708cc6fc99a77c351c3945470530845d Reviewed-by: Oswald Buddenhagen Reviewed-by: Alex Trotsenko --- src/serialport/qserialport.cpp | 19 ++++++++++++++++++- src/serialport/qserialport_p.h | 2 -- src/serialport/qserialport_unix.cpp | 5 ----- src/serialport/qserialport_win.cpp | 16 ---------------- src/serialport/qserialport_wince.cpp | 5 ----- 5 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index 9d5aa40..b252152 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -1341,10 +1341,27 @@ bool QSerialPort::isBreakEnabled() const /*! \reimp */ +// This function does not really read anything, as we use QIODevicePrivate's +// buffer. The buffer will be read inside of QIODevice before this +// method will be called. qint64 QSerialPort::readData(char *data, qint64 maxSize) { Q_D(QSerialPort); - return d->readData(data, maxSize); + + Q_UNUSED(data); + Q_UNUSED(maxSize); + +#ifdef Q_OS_WIN32 + // We need try to start async reading to read a remainder from a driver's queue + // in case we have a limited read buffer size. Because the read notification can + // be stalled since Windows do not re-triggered an EV_RXCHAR event if a driver's + // buffer has a remainder of data ready to read until a new data will be received. + if (d->readBufferMaxSize || d->flowControl == QSerialPort::HardwareControl) + d->startAsyncRead(); +#endif + + // return 0 indicating there may be more data in the future + return qint64(0); } /*! diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h index 7da2204..9d634b1 100644 --- a/src/serialport/qserialport_p.h +++ b/src/serialport/qserialport_p.h @@ -133,8 +133,6 @@ public: bool sendBreak(int duration); bool setBreakEnabled(bool set); - qint64 readData(char *data, qint64 maxSize); - bool waitForReadyRead(int msec); bool waitForBytesWritten(int msec); diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index 1f90a18..14180ec 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -372,11 +372,6 @@ bool QSerialPortPrivate::setBreakEnabled(bool set) return true; } -qint64 QSerialPortPrivate::readData(char *data, qint64 maxSize) -{ - return buffer.read(data, maxSize); -} - bool QSerialPortPrivate::waitForReadyRead(int msecs) { QElapsedTimer stopWatch; diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index e6e6924..5f4da9d 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -252,22 +252,6 @@ bool QSerialPortPrivate::setBreakEnabled(bool set) return true; } -qint64 QSerialPortPrivate::readData(char *data, qint64 maxSize) -{ - Q_UNUSED(data); - Q_UNUSED(maxSize); - - // We need try to start async reading to read a remainder from a driver's queue - // in case we have a limited read buffer size. Because the read notification can - // be stalled since Windows do not re-triggered an EV_RXCHAR event if a driver's - // buffer has a remainder of data ready to read until a new data will be received. - if (readBufferMaxSize || flowControl == QSerialPort::HardwareControl) - startAsyncRead(); - - // return 0 indicating there may be more data in the future - return qint64(0); -} - bool QSerialPortPrivate::waitForReadyRead(int msecs) { if (!writeStarted && !_q_startAsyncWrite()) diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index b8affb6..86cbcb0 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -329,11 +329,6 @@ bool QSerialPortPrivate::setBreakEnabled(bool set) return true; } -qint64 QSerialPortPrivate::readData(char *data, qint64 maxSize) -{ - return buffer.read(data, maxSize); -} - bool QSerialPortPrivate::waitForReadyRead(int msec) { if (!buffer.isEmpty()) -- cgit v1.2.1 From ad8b9773a2f996e988d7b0c4dd439a3e21ca8c09 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Tue, 30 Jun 2015 17:43:46 +0300 Subject: Get rid of QSPP::bytesToWrite() It is not worth to spread the platform-specifics over multiple files, as the difference are actually tiny and much better expressed with an ifdef. Change-Id: I5279e05d52ce5243ad3d0655c6353e1fb18891a1 Reviewed-by: Oswald Buddenhagen --- src/serialport/qserialport.cpp | 8 +++++++- src/serialport/qserialport_p.h | 1 - src/serialport/qserialport_unix.cpp | 5 ----- src/serialport/qserialport_win.cpp | 5 ----- src/serialport/qserialport_wince.cpp | 5 ----- 5 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index b252152..1aea7ca 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -1211,7 +1211,13 @@ qint64 QSerialPort::bytesAvailable() const qint64 QSerialPort::bytesToWrite() const { Q_D(const QSerialPort); - return d->bytesToWrite() + QIODevice::bytesToWrite(); + qint64 bytes = QIODevice::bytesToWrite(); +#ifdef Q_OS_WIN32 + bytes += d->actualBytesToWrite; +#else + bytes += d->writeBuffer.size(); +#endif + return bytes; } /*! diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h index 9d634b1..a39bc22 100644 --- a/src/serialport/qserialport_p.h +++ b/src/serialport/qserialport_p.h @@ -146,7 +146,6 @@ public: QSerialPort::SerialPortError decodeSystemError(int systemErrorCode = -1) const; - qint64 bytesToWrite() const; qint64 writeData(const char *data, qint64 maxSize); static QString portNameToSystemLocation(const QString &port); diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index 14180ec..65c45d5 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -841,11 +841,6 @@ inline bool QSerialPortPrivate::initialize(QIODevice::OpenMode mode) return true; } -qint64 QSerialPortPrivate::bytesToWrite() const -{ - return writeBuffer.size(); -} - qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) { ::memcpy(writeBuffer.reserve(maxSize), data, maxSize); diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 5f4da9d..b01d2d0 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -594,11 +594,6 @@ void QSerialPortPrivate::emitReadyRead() emit q->readyRead(); } -qint64 QSerialPortPrivate::bytesToWrite() const -{ - return actualBytesToWrite; -} - qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) { Q_Q(QSerialPort); diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index 86cbcb0..25a1f07 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -559,11 +559,6 @@ bool QSerialPortPrivate::notifyWrite() return true; } -qint64 QSerialPortPrivate::bytesToWrite() const -{ - return writeBuffer.size(); -} - qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) { ::memcpy(writeBuffer.reserve(maxSize), data, maxSize); -- cgit v1.2.1 From 1c37490efc080593c2a1318e0c02f2f3c0b27dbf Mon Sep 17 00:00:00 2001 From: Tobias Koenig Date: Thu, 2 Jul 2015 15:18:10 +0200 Subject: WinCE: Fix closing the serial port Terminate the notifier thread instead of relying that the 'running' member variable is evaluated. Most of the time, the notifier thread is blocked inside the ::WaitCommEvent() call, so even if the main thread requests a close by setting 'running' to false, the condition of the while loop is not evaluated until some data come in on the serial line, which might not happen in an acceptable time frame. WinCE supports proper termination of threads, so there shouldn't be a problem with the new approach. The code was tested on a WinCE7 system, closing and reopening QSerialPort works as expected now. Change-Id: Ic311a1cc5d2a1f9a1aec6750faddd396e448dd46 Reviewed-by: Denis Shienkov --- src/serialport/qserialport_wince.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index 25a1f07..1af45f1 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -83,21 +83,19 @@ signals: public: CommEventNotifier(DWORD mask, QSerialPortPrivate *d, QObject *parent) - : QThread(parent), dptr(d), running(true) { + : QThread(parent), dptr(d) { connect(this, SIGNAL(eventMask(quint32)), this, SLOT(processNotification(quint32))); ::SetCommMask(dptr->handle, mask); } virtual ~CommEventNotifier() { - running = false; ::SetCommMask(dptr->handle, 0); - wait(); } protected: void run() Q_DECL_OVERRIDE { DWORD mask = 0; - while (running) { + while (true) { if (::WaitCommEvent(dptr->handle, &mask, FALSE)) { // Wait until complete the operation changes the port settings, // see updateDcb(). @@ -131,7 +129,6 @@ private slots: private: QSerialPortPrivate *dptr; - mutable bool running; }; class WaitCommEventBreaker : public QThread @@ -213,7 +210,9 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) void QSerialPortPrivate::close() { if (eventNotifier) { - eventNotifier->deleteLater(); + eventNotifier->terminate(); + eventNotifier->wait(); + delete eventNotifier; eventNotifier = Q_NULLPTR; } -- cgit v1.2.1 From 77c48a0e22c1048226832e6b845d72da9d5b317f Mon Sep 17 00:00:00 2001 From: Tobias Koenig Date: Thu, 2 Jul 2015 15:14:54 +0200 Subject: WinCE: Fix reading of data from serial port Do not remove all read bytes from the input buffer, but only the difference between the reserved ones and the actual read ones. Change-Id: Ib114e3972ec295f57d011ae9ffa6613313712e32 Reviewed-by: Denis Shienkov --- src/serialport/qserialport_wince.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index 1af45f1..04b00a7 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -507,7 +507,7 @@ bool QSerialPortPrivate::notifyRead() return false; } - buffer.chop(readBytes); + buffer.chop(bytesToRead - qMax(readBytes, DWORD(0))); // Process emulate policy. if ((policy != QSerialPort::IgnorePolicy) && parityErrorOccurred) { -- cgit v1.2.1 From b32740001f735be9f0938f85d325dc2cf3dbe598 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Sun, 28 Jun 2015 00:11:29 +0300 Subject: Fix reading when switching from asynchronous to synchronous approach If one wants to use the asynchronous approach (using the signals and slots), and then - the synchronous (using the waitForXX functions) then synchronous reading stalls because the waitForReadyRead() method always returns the TimeoutError code. The reason is that the signal readyRead() is emitted before the startAsyncRead() (or the startAsyncCommunication()) called inside of completeAsyncRead(). Need to emit the readyRead() only after the next asynchronous reading (the startAsyncRead() or the startAsyncCommunication()) was cocked. Now the behavior is similar to behavior of QWindowsPipeReader. Besides, the auto-test which reveals this issue is added. Tested with the virtual com0com serial ports, using the auto-tests and set of the examples. Change-Id: I64bfb871d17c179f474d6672546e532566913a7f Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov Reviewed-by: Andrzej Ostruszka --- src/serialport/qserialport_win.cpp | 14 ++-- tests/auto/qserialport/tst_qserialport.cpp | 104 +++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 6 deletions(-) diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index b01d2d0..57b7096 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -426,18 +426,20 @@ bool QSerialPortPrivate::completeAsyncRead(qint64 bytesTransferred) if (bytesTransferred > 0) { char *ptr = buffer.reserve(bytesTransferred); ::memcpy(ptr, readChunkBuffer.constData(), bytesTransferred); - if (!emulateErrorPolicy()) - emitReadyRead(); } readStarted = false; + bool result = true; if ((bytesTransferred == ReadChunkSize) && (policy == QSerialPort::IgnorePolicy)) - return startAsyncRead(); + result = startAsyncRead(); else if (readBufferMaxSize == 0 || readBufferMaxSize > buffer.size()) - return startAsyncCommunication(); - else - return true; + result = startAsyncCommunication(); + + if ((bytesTransferred > 0) && !emulateErrorPolicy()) + emitReadyRead(); + + return result; } bool QSerialPortPrivate::completeAsyncWrite(qint64 bytesTransferred) diff --git a/tests/auto/qserialport/tst_qserialport.cpp b/tests/auto/qserialport/tst_qserialport.cpp index 1a714b2..54118af 100644 --- a/tests/auto/qserialport/tst_qserialport.cpp +++ b/tests/auto/qserialport/tst_qserialport.cpp @@ -35,6 +35,8 @@ #include #include +#include + Q_DECLARE_METATYPE(QSerialPort::SerialPortError); Q_DECLARE_METATYPE(QIODevice::OpenMode); Q_DECLARE_METATYPE(QIODevice::OpenModeFlag); @@ -105,6 +107,7 @@ private slots: #ifdef Q_OS_WIN void readBufferOverflow(); void readAfterInputClear(); + void synchronousReadWriteAfterAsynchronousReadWrite(); #endif void controlBreak(); @@ -739,6 +742,107 @@ void tst_QSerialPort::readAfterInputClear() // No more bytes available QVERIFY(receiverPort.bytesAvailable() == 0); } + +class MasterTransactor : public QObject +{ + Q_OBJECT +public: + explicit MasterTransactor(const QString &name) + : serialPort(name) + { + } + +public slots: + void open() + { + if (serialPort.open(QSerialPort::ReadWrite)) { + createAsynchronousConnection(); + serialPort.write("A", 1); + } + } + +private slots: + void synchronousTransaction() + { + serialPort.write("B", 1); + if (serialPort.waitForBytesWritten(100)) { + if (serialPort.waitForReadyRead(100)) + tst_QSerialPort::exitLoop(); + } + } + + void transaction() + { + deleteAsyncronousConnection(); + synchronousTransaction(); + } + +private: + void createAsynchronousConnection() + { + connect(&serialPort, &QSerialPort::readyRead, this, &MasterTransactor::transaction); + } + + void deleteAsyncronousConnection() + { + serialPort.disconnect(); + } + + QSerialPort serialPort; +}; + +class SlaveTransactor : public QObject +{ + Q_OBJECT +public: + explicit SlaveTransactor(const QString &name) + : serialPort(new QSerialPort(name, this)) + { + connect(serialPort, &QSerialPort::readyRead, this, &SlaveTransactor::transaction); + } + +public slots: + void open() + { + if (serialPort->open(QSerialPort::ReadWrite)) + emit ready(); + } + +signals: + void ready(); + +private slots: + void transaction() + { + serialPort->write("Z", 1); + } + +private: + QSerialPort *serialPort; +}; + +void tst_QSerialPort::synchronousReadWriteAfterAsynchronousReadWrite() +{ + MasterTransactor master(m_senderPortName); + SlaveTransactor *slave = new SlaveTransactor(m_receiverPortName); + + QThread thread; + slave->moveToThread(&thread); + thread.start(); + + QObject::connect(&thread, &QThread::finished, slave, &SlaveTransactor::deleteLater); + QObject::connect(slave, &SlaveTransactor::ready, &master, &MasterTransactor::open); + + QMetaObject::invokeMethod(slave, "open", Qt::QueuedConnection); + + tst_QSerialPort::enterLoopMsecs(500); + + thread.quit(); + thread.wait(); + + QVERIFY2(!timeout(), "Timed out when testing of transactions."); +} + #endif class BreakReader : public QObject -- cgit v1.2.1 From 35aa1413c8c4878570786897a0df936fb36b4a17 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Thu, 2 Jul 2015 14:55:59 +0300 Subject: Fix the lock-file path when the App Sandbox is used on OSX Commit ad56086040e6b40565214380b083ecf986924f92 does not solve it, because a lock-file path does not contain the device name, but now this issue is fixed. Task-number: QTBUG-45338 Change-Id: I876a588b035b8a8e97ff5af76f6833ae340bf7b5 Reviewed-by: Oswald Buddenhagen Reviewed-by: Denis Shienkov --- src/serialport/qserialport_unix.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index 65c45d5..d2c0eca 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -77,6 +77,10 @@ QString serialPortLockFilePath(const QString &portName) << QStringLiteral("/run/lock") #ifdef Q_OS_ANDROID << QStringLiteral("/data/local/tmp") +#elif defined(Q_OS_OSX) + // This is the workaround to specify a temporary directory + // on OSX when running the App Sandbox feature. + << QStandardPaths::writableLocation(QStandardPaths::TempLocation); #endif ; @@ -98,13 +102,6 @@ QString serialPortLockFilePath(const QString &portName) } } -#ifdef Q_OS_MAC - // This is the workaround to specify a temporary directory - // on OSX when running the App Sandbox feature. - if (lockFilePath.isEmpty()) - lockFilePath = QStandardPaths::writableLocation(QStandardPaths::TempLocation); -#endif - if (lockFilePath.isEmpty()) { qWarning("The following directories are not readable or writable for detaling with lock files\n"); foreach (const QString &lockDirectoryPath, lockDirectoryPaths) -- cgit v1.2.1 From 1eaa42c99d8442a40718b8560903b0bfeb401a0e Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Thu, 2 Jul 2015 17:51:03 +0300 Subject: Fix opening of Exar VCP on Windows The method QSPP::initialize() calls the GetCommState() function, which returns with the DCB.BaudRate field being 0. The subsequent call of SetCommState() would fail because of that. So, we need set DCB.BaudRate to any non-zero value (for example the input baud rate, which defaults to 9600) before calling the SetCommState() function. Task-number: QTBUG-46993 Change-Id: I809246b13899733f9e0bd12b3667b23c0e43b00b Reviewed-by: Oswald Buddenhagen Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- src/serialport/qserialport_win.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 57b7096..761a26e 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -671,6 +671,8 @@ inline bool QSerialPortPrivate::initialize() if (currentDcb.fDtrControl == DTR_CONTROL_HANDSHAKE) currentDcb.fDtrControl = DTR_CONTROL_DISABLE; + currentDcb.BaudRate = inputBaudRate; + if (!updateDcb()) return false; -- cgit v1.2.1 From 8919ea86c80001be7e826f5363f1b7548c7a8091 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Fri, 26 Jun 2015 16:40:44 +0300 Subject: 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 Reviewed-by: Oswald Buddenhagen Reviewed-by: Denis Shienkov --- src/serialport/qserialport.cpp | 48 +++++----- src/serialport/qserialport.h | 2 - src/serialport/qserialport_p.h | 23 ++++- src/serialport/qserialport_unix.cpp | 170 +++++++++++++++-------------------- src/serialport/qserialport_win.cpp | 154 +++++++++++++------------------ src/serialport/qserialport_wince.cpp | 99 +++++++++----------- 6 files changed, 220 insertions(+), 276 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 diff --git a/src/serialport/qserialport.h b/src/serialport/qserialport.h index 7531e47..36ae788 100644 --- a/src/serialport/qserialport.h +++ b/src/serialport/qserialport.h @@ -276,8 +276,6 @@ protected: qint64 writeData(const char *data, qint64 maxSize) Q_DECL_OVERRIDE; private: - void setError(QSerialPort::SerialPortError error, const QString &errorString = QString()); - // ### Qt6: remove me. QSerialPortPrivate * const d_dummy; diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h index a39bc22..571b0de 100644 --- a/src/serialport/qserialport_p.h +++ b/src/serialport/qserialport_p.h @@ -106,6 +106,19 @@ class QSocketNotifier; QString serialPortLockFilePath(const QString &portName); #endif +class QSerialPortErrorInfo +{ +public: + explicit QSerialPortErrorInfo(QSerialPort::SerialPortError errorCode = QSerialPort::UnknownError, + const QString &errorString = QString()) + : errorCode(errorCode) + , errorString(errorString) + { + } + QSerialPort::SerialPortError errorCode; + QString errorString; +}; + class QSerialPortPrivate : public QIODevicePrivate { Q_DECLARE_PUBLIC(QSerialPort) @@ -144,7 +157,9 @@ public: bool setFlowControl(QSerialPort::FlowControl flowControl); bool setDataErrorPolicy(QSerialPort::DataErrorPolicy policy); - QSerialPort::SerialPortError decodeSystemError(int systemErrorCode = -1) const; + QSerialPortErrorInfo getSystemError(int systemErrorCode = -1) const; + + void setError(const QSerialPortErrorInfo &errorInfo); qint64 writeData(const char *data, qint64 maxSize); @@ -237,11 +252,11 @@ public: 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); bool isReadNotificationEnabled() const; diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index d2c0eca..a80d6b3 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -158,20 +158,18 @@ private: 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 newLockFileScopedPointer(new QLockFile(lockFilePath)); if (!newLockFileScopedPointer->tryLock()) { - q->setError(QSerialPort::PermissionError); + setError(QSerialPortErrorInfo(QSerialPort::PermissionError)); return false; } @@ -192,7 +190,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; } @@ -208,16 +206,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) { @@ -233,7 +229,7 @@ void QSerialPortPrivate::close() } if (qt_safe_close(descriptor) == -1) - q->setError(decodeSystemError()); + setError(getSystemError()); lockFileScopedPointer.reset(Q_NULLPTR); @@ -244,12 +240,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; } @@ -303,11 +297,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; } @@ -316,11 +308,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; } @@ -334,11 +324,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; } @@ -347,10 +335,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; } @@ -359,10 +345,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; } @@ -425,22 +409,22 @@ bool QSerialPortPrivate::setBaudRate() && setBaudRate(outputBaudRate, QSerialPort::Output)); } -QSerialPort::SerialPortError +QSerialPortErrorInfo QSerialPortPrivate::setBaudRate_helper(qint32 baudRate, QSerialPort::Directions directions) { if ((directions & QSerialPort::Input) && ::cfsetispeed(¤tTermios, baudRate) < 0) - return decodeSystemError(); + return getSystemError(); if ((directions & QSerialPort::Output) && ::cfsetospeed(¤tTermios, 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; @@ -452,7 +436,7 @@ QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions currentSerialInfo.flags &= ~ASYNC_SPD_CUST; currentSerialInfo.custom_divisor = 0; if (::ioctl(descriptor, TIOCSSERIAL, ¤tSerialInfo) == -1) - return decodeSystemError(); + return getSystemError(); } return setBaudRate_helper(baudRate, directions); @@ -460,7 +444,7 @@ QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions #else -QSerialPort::SerialPortError +QSerialPortErrorInfo QSerialPortPrivate::setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions) { return setBaudRate_helper(baudRate, directions); @@ -470,7 +454,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); @@ -480,14 +464,14 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d ::memset(¤tSerialInfo, 0, sizeof(currentSerialInfo)); if (::ioctl(descriptor, TIOCGSERIAL, ¤tSerialInfo) == -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", @@ -497,31 +481,31 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d } if (::ioctl(descriptor, TIOCSSERIAL, ¤tSerialInfo) == -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. @@ -533,36 +517,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; } @@ -719,12 +701,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); buffer.chop(bytesToRead); return false; } @@ -751,18 +733,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; } @@ -800,15 +780,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; } @@ -848,71 +826,72 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) bool QSerialPortPrivate::updateTermios() { - Q_Q(QSerialPort); - if (::tcsetattr(descriptor, TCSANOW, ¤tTermios) == -1) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } return true; } -QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError(int systemErrorCode) const +QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const { - Q_UNUSED(systemErrorCode); + if (systemErrorCode == -1) + systemErrorCode = errno; + + QSerialPortErrorInfo error; + error.errorString = qt_error_string(systemErrorCode); - QSerialPort::SerialPortError error; - switch (errno) { + 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; @@ -956,8 +935,6 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor bool checkRead, bool checkWrite, int msecs) { - Q_Q(QSerialPort); - Q_ASSERT(selectForRead); Q_ASSERT(selectForWrite); @@ -977,11 +954,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; } @@ -1068,8 +1045,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)); @@ -1115,13 +1090,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_win.cpp b/src/serialport/qserialport_win.cpp index 761a26e..d05c9fb 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -77,8 +77,6 @@ QT_BEGIN_NAMESPACE bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { - Q_Q(QSerialPort); - DWORD desiredAccess = 0; originalEventMask = EV_ERR; @@ -93,7 +91,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; } @@ -106,10 +104,8 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) void QSerialPortPrivate::close() { - Q_Q(QSerialPort); - if (!::CancelIo(handle)) - q->setError(decodeSystemError()); + setError(getSystemError()); if (notifier) notifier->deleteLater(); @@ -123,25 +119,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; } @@ -160,7 +154,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; } @@ -174,10 +168,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; } @@ -187,10 +179,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; } @@ -204,8 +194,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; @@ -214,7 +202,7 @@ bool QSerialPortPrivate::clear(QSerialPort::Directions directions) actualBytesToWrite = 0; } if (!::PurgeComm(handle, flags)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -242,10 +230,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; } @@ -315,10 +301,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; @@ -462,15 +446,13 @@ bool QSerialPortPrivate::completeAsyncWrite(qint64 bytesTransferred) bool QSerialPortPrivate::startAsyncCommunication() { - Q_Q(QSerialPort); - ::ZeroMemory(&communicationOverlapped, sizeof(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; } } @@ -479,8 +461,6 @@ bool QSerialPortPrivate::startAsyncCommunication() bool QSerialPortPrivate::startAsyncRead() { - Q_Q(QSerialPort); - if (readStarted) return true; @@ -501,13 +481,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; } @@ -517,8 +497,6 @@ bool QSerialPortPrivate::startAsyncRead() bool QSerialPortPrivate::_q_startAsyncWrite() { - Q_Q(QSerialPort); - if (writeBuffer.isEmpty() || writeStarted) return true; @@ -527,11 +505,11 @@ bool QSerialPortPrivate::_q_startAsyncWrite() if (!::WriteFile(handle, writeBuffer.readPointer(), 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; } } @@ -543,11 +521,9 @@ bool QSerialPortPrivate::_q_startAsyncWrite() void QSerialPortPrivate::_q_notified(DWORD numberOfBytes, DWORD errorCode, OVERLAPPED *overlapped) { - Q_Q(QSerialPort); - - const QSerialPort::SerialPortError error = decodeSystemError(errorCode); - if (error != QSerialPort::NoError) { - q->setError(error); + const QSerialPortErrorInfo error = getSystemError(errorCode); + if (error.errorCode != QSerialPort::NoError) { + setError(error); return; } @@ -616,33 +592,31 @@ 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); } OVERLAPPED *QSerialPortPrivate::waitForNotified(int msecs) { - Q_Q(QSerialPort); - OVERLAPPED *overlapped = notifier->waitForAnyNotified(msecs); if (!overlapped) { - q->setError(decodeSystemError(WAIT_TIMEOUT)); + setError(getSystemError(WAIT_TIMEOUT)); return 0; } return overlapped; @@ -656,7 +630,7 @@ inline bool QSerialPortPrivate::initialize() restoredDcb.DCBlength = sizeof(restoredDcb); if (!::GetCommState(handle, &restoredDcb)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -677,7 +651,7 @@ inline bool QSerialPortPrivate::initialize() return false; if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -688,7 +662,7 @@ inline bool QSerialPortPrivate::initialize() return false; if (!::SetCommMask(handle, originalEventMask)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -706,10 +680,8 @@ inline bool QSerialPortPrivate::initialize() bool QSerialPortPrivate::updateDcb() { - Q_Q(QSerialPort); - if (!::SetCommState(handle, ¤tDcb)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } return true; @@ -717,60 +689,60 @@ bool QSerialPortPrivate::updateDcb() bool QSerialPortPrivate::updateCommTimeouts() { - Q_Q(QSerialPort); - if (!::SetCommTimeouts(handle, ¤tCommTimeouts)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } return true; } -QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError(int systemErrorCode) const +QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const { if (systemErrorCode == -1) systemErrorCode = ::GetLastError(); - QSerialPort::SerialPortError error; + QSerialPortErrorInfo error; + error.errorString = qt_error_string(systemErrorCode); + switch (systemErrorCode) { case ERROR_SUCCESS: - error = QSerialPort::NoError; + 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 = QSerialPort::TimeoutError; + error.errorCode = QSerialPort::TimeoutError; break; default: - error = QSerialPort::UnknownError; + error.errorCode = QSerialPort::UnknownError; break; } return error; diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index 04b00a7..cd28d25 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -178,8 +178,6 @@ private: bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { - Q_Q(QSerialPort); - DWORD desiredAccess = 0; DWORD eventMask = EV_ERR; @@ -196,7 +194,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; } @@ -227,12 +225,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; } @@ -251,7 +247,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; } @@ -265,10 +261,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; } @@ -278,10 +272,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; } @@ -318,10 +310,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; } @@ -389,10 +379,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; @@ -503,7 +491,7 @@ bool QSerialPortPrivate::notifyRead() if (!sucessResult) { buffer.chop(bytesToRead); - q->setError(QSerialPort::ReadError); + setError(QSerialPortErrorInfo(QSerialPort::ReadError)); return false; } @@ -546,7 +534,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; } @@ -566,31 +554,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) @@ -601,7 +589,7 @@ inline bool QSerialPortPrivate::initialize(DWORD eventMask) restoredDcb.DCBlength = sizeof(restoredDcb); if (!::GetCommState(handle, &restoredDcb)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -620,7 +608,7 @@ inline bool QSerialPortPrivate::initialize(DWORD eventMask) return false; if (!::GetCommTimeouts(handle, &restoredCommTimeouts)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } @@ -638,8 +626,6 @@ inline bool QSerialPortPrivate::initialize(DWORD eventMask) bool QSerialPortPrivate::updateDcb() { - Q_Q(QSerialPort); - QMutexLocker locker(&settingsChangeMutex); DWORD eventMask = 0; @@ -652,7 +638,7 @@ bool QSerialPortPrivate::updateDcb() // Change parameters bool ret = ::SetCommState(handle, ¤tDcb); if (!ret) - q->setError(decodeSystemError()); + setError(getSystemError()); // Restore the event mask ::SetCommMask(handle, eventMask); @@ -661,50 +647,51 @@ bool QSerialPortPrivate::updateDcb() bool QSerialPortPrivate::updateCommTimeouts() { - Q_Q(QSerialPort); - if (!::SetCommTimeouts(handle, ¤tCommTimeouts)) { - q->setError(decodeSystemError()); + setError(getSystemError()); return false; } return true; } -QSerialPort::SerialPortError QSerialPortPrivate::decodeSystemError(int systemErrorCode) const +QSerialPortErrorInfo QSerialPortPrivate::getSystemError(int systemErrorCode) const { - Q_UNUSED(systemErrorCode); + if (systemErrorCode == -1) + systemErrorCode = ::GetLastError(); - QSerialPort::SerialPortError error; - switch (::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; @@ -714,8 +701,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() @@ -725,7 +710,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); -- cgit v1.2.1 From a2758cf594dd08a21037873f64f72166a353aa29 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Wed, 8 Jul 2015 18:26:08 +0300 Subject: Give custom descriptions to errors which had none before Change-Id: Ic083cbd58e4e775ede0cdf610f61407003834207 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- src/serialport/qserialport.cpp | 22 +++++++++++----------- src/serialport/qserialport_unix.cpp | 21 ++++++++++----------- src/serialport/qserialport_win.cpp | 14 ++++++-------- src/serialport/qserialport_wince.cpp | 22 ++++++++++------------ 4 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index b6c4252..b56b3da 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -521,14 +521,14 @@ bool QSerialPort::open(OpenMode mode) Q_D(QSerialPort); if (isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::OpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::OpenError, tr("Device is already open"))); return false; } // Define while not supported modes. static const OpenMode unsupportedModes = Append | Truncate | Text | Unbuffered; if ((mode & unsupportedModes) || mode == NotOpen) { - d->setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); + d->setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, tr("Unsupported open mode"))); return false; } @@ -561,7 +561,7 @@ void QSerialPort::close() { Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); return; } @@ -871,7 +871,7 @@ bool QSerialPort::setDataTerminalReady(bool set) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -918,7 +918,7 @@ bool QSerialPort::setRequestToSend(bool set) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -968,7 +968,7 @@ QSerialPort::PinoutSignals QSerialPort::pinoutSignals() Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); qWarning("%s: device not open", Q_FUNC_INFO); return QSerialPort::NoSignal; } @@ -998,7 +998,7 @@ bool QSerialPort::flush() Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1020,7 +1020,7 @@ bool QSerialPort::clear(Directions directions) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1082,7 +1082,7 @@ bool QSerialPort::setDataErrorPolicy(DataErrorPolicy policy) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1304,7 +1304,7 @@ bool QSerialPort::sendBreak(int duration) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); qWarning("%s: device not open", Q_FUNC_INFO); return false; } @@ -1333,7 +1333,7 @@ bool QSerialPort::setBreakEnabled(bool set) Q_D(QSerialPort); if (!isOpen()) { - d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError)); + d->setError(QSerialPortErrorInfo(QSerialPort::NotOpenError, tr("Device is not open"))); qWarning("%s: device not open", Q_FUNC_INFO); return false; } diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index a80d6b3..c87db04 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -162,14 +162,14 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode) bool isLockFileEmpty = lockFilePath.isEmpty(); if (isLockFileEmpty) { qWarning("Failed to create a lock file for opening the device"); - setError(QSerialPortErrorInfo(QSerialPort::PermissionError)); + setError(QSerialPortErrorInfo(QSerialPort::PermissionError, QSerialPort::tr("Permission error while creating lock file"))); return false; } QScopedPointer newLockFileScopedPointer(new QLockFile(lockFilePath)); if (!newLockFileScopedPointer->tryLock()) { - setError(QSerialPortErrorInfo(QSerialPort::PermissionError)); + setError(QSerialPortErrorInfo(QSerialPort::PermissionError, QSerialPort::tr("Permission error while locking the device"))); return false; } @@ -471,7 +471,7 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d currentSerialInfo.custom_divisor = currentSerialInfo.baud_base / baudRate; if (currentSerialInfo.custom_divisor == 0) - return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError); + return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, QSerialPort::tr("No suitable custom baud rate divisor")); 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", @@ -500,7 +500,7 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d return QSerialPortErrorInfo(QSerialPort::NoError); #endif - return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError); + return QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, QSerialPort::tr("Custom baud rate is not supported")); } #elif defined(Q_OS_QNX) @@ -531,7 +531,7 @@ QSerialPortPrivate::setCustomBaudRate(qint32 baudRate, QSerialPort::Directions d bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions) { if (baudRate <= 0) { - setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); + setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, QSerialPort::tr("Invalid baud rate value"))); return false; } @@ -958,7 +958,7 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor return false; } if (ret == 0) { - setError(QSerialPortErrorInfo(QSerialPort::TimeoutError)); + setError(QSerialPortErrorInfo(QSerialPort::TimeoutError, QSerialPort::tr("Operation timed out"))); return false; } @@ -1091,13 +1091,12 @@ qint64 QSerialPortPrivate::readPerChar(char *data, qint64 maxSize) case QSerialPort::SkipPolicy: continue; //ignore received character case QSerialPort::StopReceivingPolicy: { - QSerialPortErrorInfo error; if (parity != QSerialPort::NoParity) - error.errorCode = QSerialPort::ParityError; + setError(QSerialPortErrorInfo(QSerialPort::ParityError, QSerialPort::tr("Parity error detected while reading"))); + else if (*data == '\0') + setError(QSerialPortErrorInfo(QSerialPort::BreakConditionError, QSerialPort::tr("Break condition detected while reading"))); else - error.errorCode = *data == '\0' ? - QSerialPort::BreakConditionError : QSerialPort::FramingError; - setError(error); + setError(QSerialPortErrorInfo(QSerialPort::FramingError, QSerialPort::tr("Framing error detected while reading"))); return ++ret; //abort receiving } break; diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index d05c9fb..0db4b8b 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -302,7 +302,7 @@ bool QSerialPortPrivate::setBaudRate() bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions) { if (directions != QSerialPort::AllDirections) { - setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); + setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, QSerialPort::tr("Custom baud rate direction is unsupported"))); return false; } currentDcb.BaudRate = baudRate; @@ -598,18 +598,16 @@ void QSerialPortPrivate::handleLineStatusErrors() return; } - QSerialPortErrorInfo error; - if (errors & CE_FRAME) { - error.errorCode = QSerialPort::FramingError; + setError(QSerialPortErrorInfo(QSerialPort::FramingError, QSerialPort::tr("Framing error detected while reading"))); } else if (errors & CE_RXPARITY) { - error.errorCode = QSerialPort::ParityError; + setError(QSerialPortErrorInfo(QSerialPort::FramingError, QSerialPort::tr("ParityError error detected while reading"))); parityErrorOccurred = true; } else if (errors & CE_BREAK) { - error.errorCode = QSerialPort::BreakConditionError; + setError(QSerialPortErrorInfo(QSerialPort::BreakConditionError, QSerialPort::tr("Break condition detected while reading"))); + } else { + setError(QSerialPortErrorInfo(QSerialPort::UnknownError, QSerialPort::tr("Unknown streaming error"))); } - - setError(error); } OVERLAPPED *QSerialPortPrivate::waitForNotified(int msecs) diff --git a/src/serialport/qserialport_wince.cpp b/src/serialport/qserialport_wince.cpp index cd28d25..88872ad 100644 --- a/src/serialport/qserialport_wince.cpp +++ b/src/serialport/qserialport_wince.cpp @@ -380,7 +380,7 @@ bool QSerialPortPrivate::setBaudRate() bool QSerialPortPrivate::setBaudRate(qint32 baudRate, QSerialPort::Directions directions) { if (directions != QSerialPort::AllDirections) { - setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError)); + setError(QSerialPortErrorInfo(QSerialPort::UnsupportedOperationError, QSerialPort::tr("Custom baud rate direction is unsupported"))); return false; } currentDcb.BaudRate = baudRate; @@ -491,7 +491,7 @@ bool QSerialPortPrivate::notifyRead() if (!sucessResult) { buffer.chop(bytesToRead); - setError(QSerialPortErrorInfo(QSerialPort::ReadError)); + setError(QSerialPortErrorInfo(QSerialPort::ReadError, QSerialPort::tr("Error reading from device"))); return false; } @@ -534,7 +534,7 @@ bool QSerialPortPrivate::notifyWrite() DWORD bytesWritten = 0; if (!::WriteFile(handle, ptr, nextSize, &bytesWritten, Q_NULLPTR)) { - setError(QSerialPortErrorInfo(QSerialPort::WriteError)); + setError(QSerialPortErrorInfo(QSerialPort::WriteError, QSerialPort::tr("Error writing to device"))); 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)); + setError(QSerialPortErrorInfo(QSerialPort::ResourceError, QSerialPort::tr("Device disappeared from the system"))); return; } @@ -567,18 +567,16 @@ void QSerialPortPrivate::processIoErrors(bool hasError) return; } - QSerialPortErrorInfo error; - if (errors & CE_FRAME) { - error.errorCode = QSerialPort::FramingError; + setError(QSerialPortErrorInfo(QSerialPort::FramingError, QSerialPort::tr("Framing error detected while reading"))); } else if (errors & CE_RXPARITY) { - error.errorCode = QSerialPort::ParityError; + setError(QSerialPortErrorInfo(QSerialPort::FramingError, QSerialPort::tr("ParityError error detected while reading"))); parityErrorOccurred = true; } else if (errors & CE_BREAK) { - error.errorCode = QSerialPort::BreakConditionError; + setError(QSerialPortErrorInfo(QSerialPort::BreakConditionError, QSerialPort::tr("Break condition detected while reading"))); + } else { + setError(QSerialPortErrorInfo(QSerialPort::UnknownError, QSerialPort::tr("Unknown streaming error"))); } - - setError(error); } inline bool QSerialPortPrivate::initialize(DWORD eventMask) @@ -710,7 +708,7 @@ bool QSerialPortPrivate::waitForReadOrWrite(bool *selectForRead, bool *selectFor breaker.stop(); if (breaker.isWorked()) { - setError(QSerialPortErrorInfo(QSerialPort::TimeoutError)); + setError(QSerialPortErrorInfo(QSerialPort::TimeoutError, QSerialPort::tr("Operation timed out"))); } else { if (checkRead) { Q_ASSERT(selectForRead); -- cgit v1.2.1 From 84de1b10d242551a51b6c4ce5eda657c4743bc63 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Thu, 9 Jul 2015 16:09:36 +0300 Subject: Fix getting duplicate serial ports with null characters The function toStringAndTrimNullCharacter() deletes only trailing zeroes from right to left direction up to the first non-zero character were meet. This can return the wrong string in case there are the zero characters to the left of the non-zero characters. We should look for the first zero-character from left to right direction, and discard all right part of a string, including the zero-character. Task-number: QTBUG-47127 Change-Id: I4d101b49bbb153bddbe535a920d616b5fef130e9 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- src/serialport/qserialportinfo_win.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp index 546d5e1..b3333fa 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -65,11 +65,10 @@ static inline const QList& guidFlagsPairs() static QString toStringAndTrimNullCharacter(const QByteArray &buffer) { - QString result = QString::fromWCharArray(reinterpret_cast(buffer.constData()), - buffer.size() / sizeof(wchar_t)); - while (!result.isEmpty() && (result.at(result.size() - 1).unicode() == 0)) - result.chop(1); - return result; + const QString result = QString::fromWCharArray(reinterpret_cast(buffer.constData()), + buffer.size() / sizeof(wchar_t)); + const int index = result.indexOf(QChar(0)); + return index == -1 ? result : result.mid(0, index); } static QStringList portNamesFromHardwareDeviceMap() -- cgit v1.2.1 From b6c21428142fa21a13b85a0913443337141c0645 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Sun, 12 Jul 2015 14:53:44 +0300 Subject: Add missing QT_DEPRECATED_SINCE(5, 5) macro The macro was added in the header file, but not in the implementation file. Change-Id: Ia46563d19fba7dd1a1e1fbe40039f62667d32b11 Reviewed-by: Marc Mutz --- src/serialport/qserialport.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index b56b3da..d7d5090 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -1283,6 +1283,7 @@ 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 @@ -1311,6 +1312,7 @@ bool QSerialPort::sendBreak(int duration) return d->sendBreak(duration); } +#endif // QT_DEPRECATED_SINCE(5, 5) /*! \property QSerialPort::breakEnabled -- cgit v1.2.1 From fc1dc2d22781a46df2351848fa408eed96ccdd06 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Sun, 12 Jul 2015 14:59:57 +0300 Subject: Add missing QT_DEPRECATED_SINCE(5, 2) macro The macro was added in the header file, but not in the implementation file. Change-Id: I51bc62eedbaba627fb525c04dc126bf0125e7a39 Reviewed-by: Marc Mutz Reviewed-by: Denis Shienkov --- src/serialport/qserialportinfo.cpp | 2 ++ src/serialport/qserialportinfo_mac.cpp | 2 ++ src/serialport/qserialportinfo_unix.cpp | 2 ++ src/serialport/qserialportinfo_win.cpp | 2 ++ src/serialport/qserialportinfo_wince.cpp | 2 ++ 5 files changed, 10 insertions(+) diff --git a/src/serialport/qserialportinfo.cpp b/src/serialport/qserialportinfo.cpp index 26b275d..055b226 100644 --- a/src/serialport/qserialportinfo.cpp +++ b/src/serialport/qserialportinfo.cpp @@ -266,6 +266,7 @@ bool QSerialPortInfo::hasProductIdentifier() const \sa isNull() */ +#if QT_DEPRECATED_SINCE(5, 2) /*! \fn bool QSerialPortInfo::isValid() const \obsolete @@ -275,6 +276,7 @@ bool QSerialPortInfo::hasProductIdentifier() const \sa isNull(), isBusy() */ +#endif // QT_DEPRECATED_SINCE(5, 2) /*! \fn QList QSerialPortInfo::standardBaudRates() diff --git a/src/serialport/qserialportinfo_mac.cpp b/src/serialport/qserialportinfo_mac.cpp index 54abe21..380a9b8 100644 --- a/src/serialport/qserialportinfo_mac.cpp +++ b/src/serialport/qserialportinfo_mac.cpp @@ -236,11 +236,13 @@ bool QSerialPortInfo::isBusy() const return true; } +#if QT_DEPRECATED_SINCE(5, 2) bool QSerialPortInfo::isValid() const { QFile f(systemLocation()); return f.exists(); } +#endif // QT_DEPRECATED_SINCE(5, 2) QString QSerialPortInfoPrivate::portNameToSystemLocation(const QString &source) { diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp index 07587ec..2eb2bf6 100644 --- a/src/serialport/qserialportinfo_unix.cpp +++ b/src/serialport/qserialportinfo_unix.cpp @@ -463,11 +463,13 @@ bool QSerialPortInfo::isBusy() const return true; } +#if QT_DEPRECATED_SINCE(5, 2) bool QSerialPortInfo::isValid() const { QFile f(systemLocation()); return f.exists(); } +#endif // QT_DEPRECATED_SINCE(5, 2) QString QSerialPortInfoPrivate::portNameToSystemLocation(const QString &source) { diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp index 493c4d0..f6d9228 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -365,6 +365,7 @@ bool QSerialPortInfo::isBusy() const return false; } +#if QT_DEPRECATED_SINCE(5, 2) bool QSerialPortInfo::isValid() const { const HANDLE handle = ::CreateFile(reinterpret_cast(systemLocation().utf16()), @@ -378,6 +379,7 @@ bool QSerialPortInfo::isValid() const } return true; } +#endif // QT_DEPRECATED_SINCE(5, 2) QString QSerialPortInfoPrivate::portNameToSystemLocation(const QString &source) { diff --git a/src/serialport/qserialportinfo_wince.cpp b/src/serialport/qserialportinfo_wince.cpp index 78439c9..9f1980f 100644 --- a/src/serialport/qserialportinfo_wince.cpp +++ b/src/serialport/qserialportinfo_wince.cpp @@ -139,6 +139,7 @@ bool QSerialPortInfo::isBusy() const return false; } +#if QT_DEPRECATED_SINCE(5, 2) bool QSerialPortInfo::isValid() const { const HANDLE handle = ::CreateFile(reinterpret_cast(systemLocation().utf16()), @@ -152,6 +153,7 @@ bool QSerialPortInfo::isValid() const } return true; } +#endif // QT_DEPRECATED_SINCE(5, 2) QString QSerialPortInfoPrivate::portNameToSystemLocation(const QString &source) { -- cgit v1.2.1 From 01e0040dce2f7edbb7856d652b34703170918733 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Sun, 12 Jul 2015 16:04:44 +0300 Subject: Move QT_DEPRECATED_SINCE(5,2 and 5,3) macro before than qdoc comments Change-Id: I72c455d2e5bbd660c53edd4bbb0b3fa5b08d806e Reviewed-by: Marc Mutz --- src/serialport/qserialport.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index 0e60036..b546e93 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -552,6 +552,7 @@ void QSerialPort::close() d->close(); } +#if QT_DEPRECATED_SINCE(5, 3) /*! \property QSerialPort::settingsRestoredOnClose \brief the flag which specifies to restore the previous settings when closing @@ -562,7 +563,6 @@ void QSerialPort::close() The default state of the QSerialPort class is to restore the settings. */ -#if QT_DEPRECATED_SINCE(5,3) void QSerialPort::setSettingsRestoredOnClose(bool restore) { Q_D(QSerialPort); @@ -1043,6 +1043,7 @@ bool QSerialPort::atEnd() const return QIODevice::atEnd() && (!isOpen() || (d->readBuffer.size() == 0)); } +#if QT_DEPRECATED_SINCE(5, 2) /*! \property QSerialPort::dataErrorPolicy \brief the error policy for how the process receives characters in the case where @@ -1059,7 +1060,6 @@ bool QSerialPort::atEnd() const with the kernel and hardware. Hence, the two scenarios cannot be completely compared to each other. */ -#if QT_DEPRECATED_SINCE(5, 2) bool QSerialPort::setDataErrorPolicy(DataErrorPolicy policy) { Q_D(QSerialPort); -- cgit v1.2.1 From adccf2dcacd3fdd66e09c38302f9ce543d55f572 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Sun, 12 Jul 2015 16:13:38 +0300 Subject: Add QT_DEPRECATED_SINCE(5, 5) macro around qdoc comments The macro was added for the signals in the header file, but not in the implementation file. Change-Id: Ie10b6bfffcbd8012f8e50b2009d95d826d983199 Reviewed-by: Marc Mutz --- src/serialport/qserialport.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index d7d5090..e7874f0 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -597,6 +597,8 @@ bool QSerialPort::settingsRestoredOnClose() const return d->settingsRestoredOnClose; } #endif // QT_DEPRECATED_SINCE(5,3) + +#if QT_DEPRECATED_SINCE(5, 5) /*! \fn void QSerialPort::settingsRestoredOnCloseChanged(bool restore) \obsolete @@ -608,6 +610,7 @@ bool QSerialPort::settingsRestoredOnClose() const \sa QSerialPort::settingsRestoredOnClose */ +#endif // QT_DEPRECATED_SINCE(5, 5) /*! \property QSerialPort::baudRate @@ -1102,6 +1105,8 @@ QSerialPort::DataErrorPolicy QSerialPort::dataErrorPolicy() const return d->policy; } #endif // QT_DEPRECATED_SINCE(5, 2) + +#if QT_DEPRECATED_SINCE(5, 5) /*! \fn void QSerialPort::dataErrorPolicyChanged(DataErrorPolicy policy) \obsolete @@ -1113,6 +1118,7 @@ QSerialPort::DataErrorPolicy QSerialPort::dataErrorPolicy() const \sa QSerialPort::dataErrorPolicy */ +#endif // QT_DEPRECATED_SINCE(5, 5) /*! \property QSerialPort::error -- cgit v1.2.1 From 1efe8b58add8ad09a31641105be2bee8f30d87cf Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Sat, 1 Aug 2015 14:54:07 +0300 Subject: Do not use the Q_OBJECT macro when it is not required Change-Id: Ib1dcece468c8a92f63bb45af7970711c6e3f3a09 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- src/serialport/qserialport_unix.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index c87db04..81886cc 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -114,7 +114,6 @@ QString serialPortLockFilePath(const QString &portName) class ReadNotifier : public QSocketNotifier { - Q_OBJECT public: ReadNotifier(QSerialPortPrivate *d, QObject *parent) : QSocketNotifier(d->descriptor, QSocketNotifier::Read, parent) @@ -135,7 +134,6 @@ private: class WriteNotifier : public QSocketNotifier { - Q_OBJECT public: WriteNotifier(QSerialPortPrivate *d, QObject *parent) : QSocketNotifier(d->descriptor, QSocketNotifier::Write, parent) @@ -154,8 +152,6 @@ private: QSerialPortPrivate *dptr; }; -#include "qserialport_unix.moc" - bool QSerialPortPrivate::open(QIODevice::OpenMode mode) { QString lockFilePath = serialPortLockFilePath(QSerialPortInfoPrivate::portNameFromSystemLocation(systemLocation)); -- cgit v1.2.1 From 0202ceea3a33f27c881a725ec9f7c1f35b8f9b49 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Sat, 1 Aug 2015 15:09:13 +0300 Subject: Use the permanent status message in Terminal example ... because earlier was used the temporal message which disappeared in case of moving of the mouse cursor on the menu. Change-Id: I7a7cb10120ffdd242a76e91ec08554db69e0f959 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- examples/serialport/terminal/mainwindow.cpp | 29 +++++++++++++++++++---------- examples/serialport/terminal/mainwindow.h | 5 +++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/examples/serialport/terminal/mainwindow.cpp b/examples/serialport/terminal/mainwindow.cpp index 9a98456..3630b1f 100644 --- a/examples/serialport/terminal/mainwindow.cpp +++ b/examples/serialport/terminal/mainwindow.cpp @@ -38,6 +38,7 @@ #include "settingsdialog.h" #include +#include #include //! [0] @@ -60,6 +61,9 @@ MainWindow::MainWindow(QWidget *parent) : ui->actionQuit->setEnabled(true); ui->actionConfigure->setEnabled(true); + status = new QLabel; + ui->statusBar->addWidget(status); + initActionsConnections(); connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, @@ -90,18 +94,18 @@ void MainWindow::openSerialPort() serial->setStopBits(p.stopBits); serial->setFlowControl(p.flowControl); if (serial->open(QIODevice::ReadWrite)) { - console->setEnabled(true); - console->setLocalEchoEnabled(p.localEchoEnabled); - ui->actionConnect->setEnabled(false); - ui->actionDisconnect->setEnabled(true); - ui->actionConfigure->setEnabled(false); - ui->statusBar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6") - .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits) - .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl)); + console->setEnabled(true); + console->setLocalEchoEnabled(p.localEchoEnabled); + ui->actionConnect->setEnabled(false); + ui->actionDisconnect->setEnabled(true); + ui->actionConfigure->setEnabled(false); + showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6") + .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits) + .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl)); } else { QMessageBox::critical(this, tr("Error"), serial->errorString()); - ui->statusBar->showMessage(tr("Open error")); + showStatusMessage(tr("Open error")); } } //! [4] @@ -115,7 +119,7 @@ void MainWindow::closeSerialPort() ui->actionConnect->setEnabled(true); ui->actionDisconnect->setEnabled(false); ui->actionConfigure->setEnabled(true); - ui->statusBar->showMessage(tr("Disconnected")); + showStatusMessage(tr("Disconnected")); } //! [5] @@ -162,3 +166,8 @@ void MainWindow::initActionsConnections() connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about())); connect(ui->actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt())); } + +void MainWindow::showStatusMessage(const QString &message) +{ + status->setText(message); +} diff --git a/examples/serialport/terminal/mainwindow.h b/examples/serialport/terminal/mainwindow.h index 51524e1..64b9e25 100644 --- a/examples/serialport/terminal/mainwindow.h +++ b/examples/serialport/terminal/mainwindow.h @@ -43,6 +43,8 @@ QT_BEGIN_NAMESPACE +class QLabel; + namespace Ui { class MainWindow; } @@ -73,7 +75,10 @@ private: void initActionsConnections(); private: + void showStatusMessage(const QString &message); + Ui::MainWindow *ui; + QLabel *status; Console *console; SettingsDialog *settings; QSerialPort *serial; -- cgit v1.2.1 From 4328ab9709561842c680bc8bb7e4f2abc3ea3852 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Tue, 4 Aug 2015 17:14:04 +0300 Subject: Do not return a non serial port devices on FreeBSD Task-number: QTBUG-47210 Change-Id: I0dbed3e4e4f09b40f07f700652a762e68c27711d Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- src/serialport/qserialportinfo_unix.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp index b17ec34..1a55a03 100644 --- a/src/serialport/qserialportinfo_unix.cpp +++ b/src/serialport/qserialportinfo_unix.cpp @@ -84,6 +84,15 @@ static QStringList filteredDeviceFilePaths() QStringList deviceFilePaths; foreach (const QFileInfo &deviceFileInfo, deviceDir.entryInfoList()) { const QString deviceAbsoluteFilePath = deviceFileInfo.absoluteFilePath(); + +#ifdef Q_OS_FREEBSD + // it is a quick workaround to skip the non-serial devices + if (deviceFilePaths.endsWith(QStringLiteral(".init")) + || deviceFilePaths.endsWith(QStringLiteral(".lock"))) { + continue; + } +#endif + if (!deviceFilePaths.contains(deviceAbsoluteFilePath)) { deviceFilePaths.append(deviceAbsoluteFilePath); result.append(deviceAbsoluteFilePath); -- cgit v1.2.1 From 506370bfa0fc24a1fa1747fb28507d17b34dfcb4 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Tue, 4 Aug 2015 23:37:20 +0300 Subject: Fix proper handling of QSocketNotifier::event() method According to documentation on QObject::event(), we need to return true in case the event was recognized and processed. In addition, were made small modifications related to the Qt coding style. Tested on Linux with the virtual tty0tty serial ports. Change-Id: Ic9a0e7e5f4957b1c1aa0a4a593ff4621792ba637 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- src/serialport/qserialport_unix.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index 81886cc..7a42764 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -118,14 +118,17 @@ public: ReadNotifier(QSerialPortPrivate *d, QObject *parent) : QSocketNotifier(d->descriptor, QSocketNotifier::Read, parent) , dptr(d) - {} + { + } protected: - bool event(QEvent *e) Q_DECL_OVERRIDE { - bool ret = QSocketNotifier::event(e); - if (ret) + bool event(QEvent *e) Q_DECL_OVERRIDE + { + if (e->type() == QEvent::SockAct) { dptr->readNotification(); - return ret; + return true; + } + return QSocketNotifier::event(e); } private: @@ -138,14 +141,17 @@ public: WriteNotifier(QSerialPortPrivate *d, QObject *parent) : QSocketNotifier(d->descriptor, QSocketNotifier::Write, parent) , dptr(d) - {} + { + } protected: - bool event(QEvent *e) Q_DECL_OVERRIDE { - bool ret = QSocketNotifier::event(e); - if (ret) + bool event(QEvent *e) Q_DECL_OVERRIDE + { + if (e->type() == QEvent::SockAct) { dptr->completeAsyncWrite(); - return ret; + return true; + } + return QSocketNotifier::event(e); } private: -- cgit v1.2.1 From a5114f9a9249c5f3263c848d96600ea8e30f22da Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Tue, 4 Aug 2015 23:54:07 +0300 Subject: Allow to pass the flush tests using the tty0tty virtual devices The receiver on other side should be opened even if it is not used; otherwise the tests will be fails with the EINVAL error. Change-Id: I85ff942c5b7e8276f22867921239bfc8933dac77 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- tests/auto/qserialport/tst_qserialport.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/auto/qserialport/tst_qserialport.cpp b/tests/auto/qserialport/tst_qserialport.cpp index 54118af..c5c9113 100644 --- a/tests/auto/qserialport/tst_qserialport.cpp +++ b/tests/auto/qserialport/tst_qserialport.cpp @@ -327,6 +327,10 @@ void tst_QSerialPort::flush() QSKIP("flush() does not work on Windows"); #endif + // the dummy device on other side also has to be open + QSerialPort dummySerialPort(m_receiverPortName); + QVERIFY(dummySerialPort.open(QIODevice::ReadOnly)); + QSerialPort serialPort(m_senderPortName); connect(&serialPort, SIGNAL(bytesWritten(qint64)), this, SLOT(handleBytesWrittenAndExitLoopSlot(qint64))); QSignalSpy bytesWrittenSpy(&serialPort, SIGNAL(bytesWritten(qint64))); @@ -358,6 +362,10 @@ void tst_QSerialPort::doubleFlush() QSKIP("flush() does not work on Windows"); #endif + // the dummy device on other side also has to be open + QSerialPort dummySerialPort(m_receiverPortName); + QVERIFY(dummySerialPort.open(QIODevice::ReadOnly)); + QSerialPort serialPort(m_senderPortName); connect(&serialPort, SIGNAL(bytesWritten(qint64)), this, SLOT(handleBytesWrittenAndExitLoopSlot2(qint64))); QSignalSpy bytesWrittenSpy(&serialPort, SIGNAL(bytesWritten(qint64))); -- cgit v1.2.1 From a72a6179f652385b24d63ffc8f68b23a0262c268 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Wed, 5 Aug 2015 23:25:12 +0300 Subject: Use the QLatin1String at string comparison Change-Id: I448179fd0ff07c45a3a7b74c675c8cc8e0579103 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- src/serialport/qserialportinfo_unix.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp index 1a55a03..0fc5046 100644 --- a/src/serialport/qserialportinfo_unix.cpp +++ b/src/serialport/qserialportinfo_unix.cpp @@ -87,8 +87,8 @@ static QStringList filteredDeviceFilePaths() #ifdef Q_OS_FREEBSD // it is a quick workaround to skip the non-serial devices - if (deviceFilePaths.endsWith(QStringLiteral(".init")) - || deviceFilePaths.endsWith(QStringLiteral(".lock"))) { + if (deviceFilePaths.endsWith(QLatin1String(".init")) + || deviceFilePaths.endsWith(QLatin1String(".lock"))) { continue; } #endif -- cgit v1.2.1 From 1651383fd030189366f2e13de4da3d37319e705e Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Wed, 5 Aug 2015 10:48:39 +0300 Subject: Delete the notifiers immediatelly on close This avoid of the errors when the triggered notifier tries to handle an already closed device descriptor. Tested on Windows and Linux with the USB serial ports. Change-Id: Iceb2e8c202b2b8d28fc87fa3a1b817df4caf39b9 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- src/serialport/qserialport_unix.cpp | 6 ++---- src/serialport/qserialport_win.cpp | 6 ++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp index 7a42764..612d84a 100644 --- a/src/serialport/qserialport_unix.cpp +++ b/src/serialport/qserialport_unix.cpp @@ -219,14 +219,12 @@ void QSerialPortPrivate::close() #endif if (readNotifier) { - readNotifier->setEnabled(false); - readNotifier->deleteLater(); + delete readNotifier; readNotifier = Q_NULLPTR; } if (writeNotifier) { - writeNotifier->setEnabled(false); - writeNotifier->deleteLater(); + delete writeNotifier; writeNotifier = Q_NULLPTR; } diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 0db4b8b..a5386c7 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -107,8 +107,10 @@ void QSerialPortPrivate::close() if (!::CancelIo(handle)) setError(getSystemError()); - if (notifier) - notifier->deleteLater(); + if (notifier) { + delete notifier; + notifier = Q_NULLPTR; + } readStarted = false; writeStarted = false; -- cgit v1.2.1 From 1d753e0b3a6aca6e506760962e500a0a701d378d Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Wed, 5 Aug 2015 13:02:52 +0300 Subject: Get rid of incomplete and never used manual tests Change-Id: I8057ab7b7a641c8f6abde8e9063fd2ee56fc1165 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- tests/manual/manual.pro | 3 - tests/manual/qserialport/qserialport.pro | 7 - tests/manual/qserialport/tst_qserialport.cpp | 208 --------------------- tests/manual/qserialportinfo/qserialportinfo.pro | 7 - .../manual/qserialportinfo/tst_qserialportinfo.cpp | 100 ---------- tests/tests.pro | 2 +- 6 files changed, 1 insertion(+), 326 deletions(-) delete mode 100644 tests/manual/manual.pro delete mode 100644 tests/manual/qserialport/qserialport.pro delete mode 100644 tests/manual/qserialport/tst_qserialport.cpp delete mode 100644 tests/manual/qserialportinfo/qserialportinfo.pro delete mode 100644 tests/manual/qserialportinfo/tst_qserialportinfo.cpp diff --git a/tests/manual/manual.pro b/tests/manual/manual.pro deleted file mode 100644 index 5be8160..0000000 --- a/tests/manual/manual.pro +++ /dev/null @@ -1,3 +0,0 @@ -TEMPLATE = subdirs -SUBDIRS += qserialportinfo \ - qserialport diff --git a/tests/manual/qserialport/qserialport.pro b/tests/manual/qserialport/qserialport.pro deleted file mode 100644 index 3858b6b..0000000 --- a/tests/manual/qserialport/qserialport.pro +++ /dev/null @@ -1,7 +0,0 @@ -TEMPLATE = app -TARGET = tst_qserialport - -QT = core testlib -QT += serialport - -SOURCES += tst_qserialport.cpp diff --git a/tests/manual/qserialport/tst_qserialport.cpp b/tests/manual/qserialport/tst_qserialport.cpp deleted file mode 100644 index f6f3252..0000000 --- a/tests/manual/qserialport/tst_qserialport.cpp +++ /dev/null @@ -1,208 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Denis Shienkov -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtSerialPort module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL21$ -** 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 http://www.qt.io/terms-conditions. For further -** information use the contact form at http://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 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include - -#include -#include - -QT_USE_NAMESPACE - -class tst_QSerialPort : public QObject -{ - Q_OBJECT - -private slots: - void initTestCase(); - void open(); - void baudRate(); - void dataBits(); - void parity(); - void stopBits(); - void flowControl(); - -private: - QList serialPortInfoList; -}; - -void tst_QSerialPort::initTestCase() -{ - serialPortInfoList = QSerialPortInfo::availablePorts(); - - if (serialPortInfoList.isEmpty()) { - QSKIP("Test doesn't work because the serial ports are not detected."); - } -} - -void tst_QSerialPort::open() -{ - foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) { - - if (serialPortInfo.isBusy()) - continue; - - QSerialPort object1; - - // Try open and check access to port by Info - object1.setPort(serialPortInfo); - QCOMPARE(object1.portName(), serialPortInfo.portName()); - QCOMPARE(object1.open(QIODevice::ReadWrite), true); - QCOMPARE(object1.isOpen(), true); - object1.close(); - QCOMPARE(object1.isOpen(), false); - - // Try open and check access to port by Name - object1.setPortName(serialPortInfo.portName()); - QCOMPARE(object1.portName(), serialPortInfo.portName()); - QCOMPARE(object1.open(QIODevice::ReadWrite), true); - QCOMPARE(object1.isOpen(), true); - object1.close(); - QCOMPARE(object1.isOpen(), false); - - // Try open and check access to port by Location - object1.setPortName(serialPortInfo.systemLocation()); - QCOMPARE(object1.portName(), serialPortInfo.portName()); - QCOMPARE(object1.open(QIODevice::ReadWrite), true); - QCOMPARE(object1.isOpen(), true); - object1.close(); - QCOMPARE(object1.isOpen(), false); - } -} - -void tst_QSerialPort::baudRate() -{ - foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) { - - QSerialPort object1; - object1.setPortName(serialPortInfo.portName()); - QCOMPARE(object1.open(QIODevice::ReadWrite), true); - - QCOMPARE(object1.setBaudRate(QSerialPort::Baud1200), true); - QCOMPARE(object1.baudRate(), static_cast(QSerialPort::Baud1200)); - QCOMPARE(object1.setBaudRate(QSerialPort::Baud2400), true); - QCOMPARE(object1.baudRate(), static_cast(QSerialPort::Baud2400)); - QCOMPARE(object1.setBaudRate(QSerialPort::Baud4800), true); - QCOMPARE(object1.baudRate(), static_cast(QSerialPort::Baud4800)); - QCOMPARE(object1.setBaudRate(QSerialPort::Baud9600), true); - QCOMPARE(object1.baudRate(), static_cast(QSerialPort::Baud9600)); - QCOMPARE(object1.setBaudRate(QSerialPort::Baud19200), true); - QCOMPARE(object1.baudRate(), static_cast(QSerialPort::Baud19200)); - QCOMPARE(object1.setBaudRate(QSerialPort::Baud38400), true); - QCOMPARE(object1.baudRate(), static_cast(QSerialPort::Baud38400)); - QCOMPARE(object1.setBaudRate(QSerialPort::Baud57600), true); - QCOMPARE(object1.baudRate(), static_cast(QSerialPort::Baud57600)); - QCOMPARE(object1.setBaudRate(QSerialPort::Baud115200), true); - QCOMPARE(object1.baudRate(), static_cast(QSerialPort::Baud115200)); - - object1.close(); - } -} - -void tst_QSerialPort::dataBits() -{ - foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) { - - QSerialPort object1; - object1.setPortName(serialPortInfo.portName()); - QCOMPARE(object1.open(QIODevice::ReadWrite), true); - - QCOMPARE(object1.setDataBits(QSerialPort::Data8), true); - QCOMPARE(object1.dataBits(), QSerialPort::Data8); - - object1.close(); - } -} - -void tst_QSerialPort::parity() -{ - foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) { - - QSerialPort object1; - object1.setPortName(serialPortInfo.portName()); - QCOMPARE(object1.open(QIODevice::ReadWrite), true); - - QCOMPARE(object1.setParity(QSerialPort::NoParity), true); - QCOMPARE(object1.parity(), QSerialPort::NoParity); - QCOMPARE(object1.setParity(QSerialPort::EvenParity), true); - QCOMPARE(object1.parity(), QSerialPort::EvenParity); - QCOMPARE(object1.setParity(QSerialPort::OddParity), true); - QCOMPARE(object1.parity(), QSerialPort::OddParity); - QCOMPARE(object1.setParity(QSerialPort::MarkParity), true); - QCOMPARE(object1.parity(), QSerialPort::MarkParity); - QCOMPARE(object1.setParity(QSerialPort::SpaceParity), true); - QCOMPARE(object1.parity(), QSerialPort::SpaceParity); - - object1.close(); - } -} - -void tst_QSerialPort::stopBits() -{ - foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) { - - QSerialPort object1; - object1.setPortName(serialPortInfo.portName()); - QCOMPARE(object1.open(QIODevice::ReadWrite), true); - - QCOMPARE(object1.setStopBits(QSerialPort::OneStop), true); - QCOMPARE(object1.stopBits(), QSerialPort::OneStop); - // skip 1.5 stop bits - QCOMPARE(object1.setStopBits(QSerialPort::TwoStop), true); - QCOMPARE(object1.stopBits(), QSerialPort::TwoStop); - - object1.close(); - } -} - -void tst_QSerialPort::flowControl() -{ - foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) { - - QSerialPort object1; - object1.setPortName(serialPortInfo.portName()); - QCOMPARE(object1.open(QIODevice::ReadWrite), true); - - QCOMPARE(object1.setFlowControl(QSerialPort::NoFlowControl), true); - QCOMPARE(object1.flowControl(), QSerialPort::NoFlowControl); - QCOMPARE(object1.setFlowControl(QSerialPort::HardwareControl), true); - QCOMPARE(object1.flowControl(), QSerialPort::HardwareControl); - QCOMPARE(object1.setFlowControl(QSerialPort::SoftwareControl), true); - QCOMPARE(object1.flowControl(), QSerialPort::SoftwareControl); - - object1.close(); - } -} - -QTEST_MAIN(tst_QSerialPort) -#include "tst_qserialport.moc" diff --git a/tests/manual/qserialportinfo/qserialportinfo.pro b/tests/manual/qserialportinfo/qserialportinfo.pro deleted file mode 100644 index 57f6ea6..0000000 --- a/tests/manual/qserialportinfo/qserialportinfo.pro +++ /dev/null @@ -1,7 +0,0 @@ -TEMPLATE = app -TARGET = tst_qserialportinfo - -QT = core testlib -QT += serialport - -SOURCES += tst_qserialportinfo.cpp diff --git a/tests/manual/qserialportinfo/tst_qserialportinfo.cpp b/tests/manual/qserialportinfo/tst_qserialportinfo.cpp deleted file mode 100644 index 3bcc0b3..0000000 --- a/tests/manual/qserialportinfo/tst_qserialportinfo.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Denis Shienkov -** Copyright (C) 2013 Laszlo Papp -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtSerialPort module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL21$ -** 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 http://www.qt.io/terms-conditions. For further -** information use the contact form at http://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 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include - -#include -#include - -QT_USE_NAMESPACE - -class tst_QSerialPortInfo : public QObject -{ - Q_OBJECT - -private slots: - void serialPortInfoList(); - void standardBaudRateList(); - void constructors(); - void assignment(); -}; - -void tst_QSerialPortInfo::serialPortInfoList() -{ - QList list(QSerialPortInfo::availablePorts()); - QCOMPARE(list.isEmpty(), false); -} - -void tst_QSerialPortInfo::standardBaudRateList() -{ - QList list(QSerialPortInfo::standardBaudRates()); - QCOMPARE(list.isEmpty(), false); -} - -void tst_QSerialPortInfo::constructors() -{ - QSerialPortInfo serialPortInfo; - QCOMPARE(serialPortInfo.portName().isEmpty(), true); - QCOMPARE(serialPortInfo.systemLocation().isEmpty(), true); - QCOMPARE(serialPortInfo.description().isEmpty(), true); - QCOMPARE(serialPortInfo.manufacturer().isEmpty(), true); - QCOMPARE(serialPortInfo.serialNumber().isEmpty(), true); - QCOMPARE(serialPortInfo.vendorIdentifier(), quint16(0)); - QCOMPARE(serialPortInfo.productIdentifier(), quint16(0)); - QCOMPARE(serialPortInfo.hasVendorIdentifier(), false); - QCOMPARE(serialPortInfo.hasProductIdentifier(), false); - QCOMPARE(serialPortInfo.isNull(), false); - QCOMPARE(serialPortInfo.isBusy(), false); - QCOMPARE(serialPortInfo.isValid(), false); -} - -void tst_QSerialPortInfo::assignment() -{ - QList serialPortInfoList(QSerialPortInfo::availablePorts()); - - foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) { - QSerialPortInfo otherSerialPortInfo = serialPortInfo; - QCOMPARE(otherSerialPortInfo.portName(), serialPortInfo.portName()); - QCOMPARE(otherSerialPortInfo.systemLocation(), serialPortInfo.systemLocation()); - QCOMPARE(otherSerialPortInfo.description(), serialPortInfo.description()); - QCOMPARE(otherSerialPortInfo.manufacturer(), serialPortInfo.manufacturer()); - QCOMPARE(otherSerialPortInfo.serialNumber(), serialPortInfo.serialNumber()); - QCOMPARE(otherSerialPortInfo.vendorIdentifier(), serialPortInfo.vendorIdentifier()); - QCOMPARE(otherSerialPortInfo.productIdentifier(), serialPortInfo.productIdentifier()); - } -} - -QTEST_MAIN(tst_QSerialPortInfo) -#include "tst_qserialportinfo.moc" diff --git a/tests/tests.pro b/tests/tests.pro index dcc8531..157ef34 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -1,2 +1,2 @@ TEMPLATE = subdirs -SUBDIRS += auto manual +SUBDIRS += auto -- cgit v1.2.1 From c0aebf3dfe3c6837db8f91d65ac4b6b7b44e100e Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Thu, 6 Aug 2015 18:01:38 +0300 Subject: Delete the startAsyncWriteTimer object on close There are no sense to keep the startAsyncWriteTimer object when the device is closed. Because in next time the device can be opened in read-only mode, and the startAsyncWriteTimer will never used. Change-Id: I315ddfa88517087dcf708b0057a0e7c27cb1a3f1 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- src/serialport/qserialport_win.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index a5386c7..a9687a2 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -112,6 +112,11 @@ void QSerialPortPrivate::close() notifier = Q_NULLPTR; } + if (startAsyncWriteTimer) { + delete startAsyncWriteTimer; + startAsyncWriteTimer = Q_NULLPTR; + } + readStarted = false; writeStarted = false; writeBuffer.clear(); -- cgit v1.2.1