diff options
author | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2014-09-16 15:29:27 +0200 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2014-09-16 15:29:27 +0200 |
commit | ef8ed357881b212e2debe2ccf975996bca2331c2 (patch) | |
tree | 67cb9ee9821bd1fb9b9813b038ffbc06a7847ddd | |
parent | b111a3a6fb4b6c2892fe3b1c9cdf72d8e8f9ea8d (diff) | |
parent | f1761c1236edce428278f7a9e8aa1091097eaa57 (diff) | |
download | qtserialport-ef8ed357881b212e2debe2ccf975996bca2331c2.tar.gz |
Merge remote-tracking branch 'origin/5.3' into 5.4
Conflicts:
.qmake.conf
Change-Id: I5887622f233275703cca8cc2a5db4aaaccc72d97
-rw-r--r-- | src/serialport/qserialport.cpp | 9 | ||||
-rw-r--r-- | src/serialport/qserialport.h | 3 | ||||
-rw-r--r-- | src/serialport/qserialport_win.cpp | 19 | ||||
-rw-r--r-- | src/serialport/qserialport_win_p.h | 2 | ||||
-rw-r--r-- | tests/auto/qserialport/tst_qserialport.cpp | 164 |
5 files changed, 179 insertions, 18 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp index 2e1bac5..297bf62 100644 --- a/src/serialport/qserialport.cpp +++ b/src/serialport/qserialport.cpp @@ -71,7 +71,9 @@ QSerialPortPrivateData::QSerialPortPrivateData(QSerialPort *q) , policy(QSerialPort::IgnorePolicy) , dataTerminalReady(false) , requestToSend(false) +#if QT_DEPRECATED_SINCE(5,3) , settingsRestoredOnClose(true) +#endif , q_ptr(q) { } @@ -572,6 +574,7 @@ 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); @@ -587,9 +590,10 @@ bool QSerialPort::settingsRestoredOnClose() const Q_D(const QSerialPort); return d->settingsRestoredOnClose; } - +#endif // QT_DEPRECATED_SINCE(5,3) /*! \fn void QSerialPort::settingsRestoredOnCloseChanged(bool restore) + \obsolete This signal is emitted after the flag which specifies to restore the previous settings while closing the serial port has been changed. The new @@ -1069,6 +1073,7 @@ 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); @@ -1093,7 +1098,7 @@ QSerialPort::DataErrorPolicy QSerialPort::dataErrorPolicy() const Q_D(const QSerialPort); return d->policy; } - +#endif // QT_DEPRECATED_SINCE(5, 2) /*! \fn void QSerialPort::dataErrorPolicyChanged(DataErrorPolicy policy) \obsolete diff --git a/src/serialport/qserialport.h b/src/serialport/qserialport.h index f114fd4..a8f9a05 100644 --- a/src/serialport/qserialport.h +++ b/src/serialport/qserialport.h @@ -255,9 +255,7 @@ Q_SIGNALS: void parityChanged(QSerialPort::Parity parity); void stopBitsChanged(QSerialPort::StopBits stopBits); void flowControlChanged(QSerialPort::FlowControl flowControl); -#if QT_DEPRECATED_SINCE(5, 2) void dataErrorPolicyChanged(QSerialPort::DataErrorPolicy policy); -#endif void dataTerminalReadyChanged(bool set); void requestToSendChanged(bool set); void error(QSerialPort::SerialPortError serialPortError); @@ -279,6 +277,7 @@ private: Q_PRIVATE_SLOT(d_func(), bool _q_completeAsyncCommunication()) Q_PRIVATE_SLOT(d_func(), bool _q_completeAsyncRead()) Q_PRIVATE_SLOT(d_func(), bool _q_completeAsyncWrite()) + Q_PRIVATE_SLOT(d_func(), bool _q_startAsyncWrite()) #endif }; diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 9daa728..32b8d0a 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -251,7 +251,7 @@ bool QSerialPortPrivate::setRequestToSend(bool set) bool QSerialPortPrivate::flush() { - return startAsyncWrite(); + return _q_startAsyncWrite(); } bool QSerialPortPrivate::clear(QSerialPort::Directions directions) @@ -305,7 +305,7 @@ void QSerialPortPrivate::startWriting() if (!writeStarted) { if (!startAsyncWriteTimer) { startAsyncWriteTimer = new QTimer(q); - q->connect(startAsyncWriteTimer, SIGNAL(timeout()), q, SLOT(_q_completeAsyncWrite())); + q->connect(startAsyncWriteTimer, SIGNAL(timeout()), q, SLOT(_q_startAsyncWrite())); startAsyncWriteTimer->setSingleShot(true); } startAsyncWriteTimer->start(0); @@ -319,7 +319,7 @@ bool QSerialPortPrivate::waitForReadyRead(int msecs) QElapsedTimer stopWatch; stopWatch.start(); - if (!writeStarted && !startAsyncWrite()) + if (!writeStarted && !_q_startAsyncWrite()) return false; const qint64 initialReadBufferSize = readBuffer.size(); @@ -373,7 +373,7 @@ bool QSerialPortPrivate::waitForBytesWritten(int msecs) QElapsedTimer stopWatch; stopWatch.start(); - if (!writeStarted && !startAsyncWrite()) + if (!writeStarted && !_q_startAsyncWrite()) return false; forever { @@ -535,17 +535,18 @@ bool QSerialPortPrivate::_q_completeAsyncWrite() Q_Q(QSerialPort); if (writeStarted) { - writeStarted = false; const qint64 bytesTransferred = handleOverlappedResult(QSerialPort::Output, writeCompletionOverlapped); - if (bytesTransferred == qint64(-1)) + if (bytesTransferred == qint64(-1)) { + writeStarted = false; return false; - if (bytesTransferred > 0) { + } else if (bytesTransferred > 0) { writeBuffer.free(bytesTransferred); emit q->bytesWritten(bytesTransferred); } + writeStarted = false; } - return startAsyncWrite(); + return _q_startAsyncWrite(); } bool QSerialPortPrivate::startAsyncCommunication() @@ -597,7 +598,7 @@ bool QSerialPortPrivate::startAsyncRead() return true; } -bool QSerialPortPrivate::startAsyncWrite() +bool QSerialPortPrivate::_q_startAsyncWrite() { Q_Q(QSerialPort); diff --git a/src/serialport/qserialport_win_p.h b/src/serialport/qserialport_win_p.h index 7ec7a2e..93f8591 100644 --- a/src/serialport/qserialport_win_p.h +++ b/src/serialport/qserialport_win_p.h @@ -100,7 +100,7 @@ public: bool startAsyncCommunication(); bool startAsyncRead(); - bool startAsyncWrite(); + bool _q_startAsyncWrite(); bool emulateErrorPolicy(); void emitReadyRead(); diff --git a/tests/auto/qserialport/tst_qserialport.cpp b/tests/auto/qserialport/tst_qserialport.cpp index 9a7cf6d..d296f6d 100644 --- a/tests/auto/qserialport/tst_qserialport.cpp +++ b/tests/auto/qserialport/tst_qserialport.cpp @@ -38,6 +38,7 @@ Q_DECLARE_METATYPE(QSerialPort::SerialPortError); Q_DECLARE_METATYPE(QIODevice::OpenMode); Q_DECLARE_METATYPE(QIODevice::OpenModeFlag); +Q_DECLARE_METATYPE(Qt::ConnectionType); class tst_QSerialPort : public QObject { @@ -100,6 +101,12 @@ private slots: void synchronousReadWrite(); + void asynchronousWriteByBytesWritten_data(); + void asynchronousWriteByBytesWritten(); + + void asynchronousWriteByTimer_data(); + void asynchronousWriteByTimer(); + protected slots: void handleBytesWrittenAndExitLoopSlot(qint64 bytesWritten); void handleBytesWrittenAndExitLoopSlot2(qint64 bytesWritten); @@ -455,12 +462,13 @@ void tst_QSerialPort::twoStageSynchronousLoopback() senderPort.waitForBytesWritten(waitMsecs); QTest::qSleep(waitMsecs); receiverPort.waitForReadyRead(waitMsecs); - QCOMPARE(newlineArray.size(), receiverPort.bytesAvailable()); + QCOMPARE(qint64(newlineArray.size()), receiverPort.bytesAvailable()); + receiverPort.write(receiverPort.readAll()); receiverPort.waitForBytesWritten(waitMsecs); QTest::qSleep(waitMsecs); senderPort.waitForReadyRead(waitMsecs); - QCOMPARE(newlineArray.size(), senderPort.bytesAvailable()); + QCOMPARE(qint64(newlineArray.size()), receiverPort.bytesAvailable()); QCOMPARE(newlineArray, senderPort.readAll()); // second stage @@ -468,12 +476,12 @@ void tst_QSerialPort::twoStageSynchronousLoopback() senderPort.waitForBytesWritten(waitMsecs); QTest::qSleep(waitMsecs); receiverPort.waitForReadyRead(waitMsecs); - QCOMPARE(newlineArray.size(), receiverPort.bytesAvailable()); + QCOMPARE(qint64(newlineArray.size()), receiverPort.bytesAvailable()); receiverPort.write(receiverPort.readAll()); receiverPort.waitForBytesWritten(waitMsecs); QTest::qSleep(waitMsecs); senderPort.waitForReadyRead(waitMsecs); - QCOMPARE(newlineArray.size(), senderPort.bytesAvailable()); + QCOMPARE(qint64(newlineArray.size()), receiverPort.bytesAvailable()); QCOMPARE(newlineArray, senderPort.readAll()); } @@ -503,5 +511,153 @@ void tst_QSerialPort::synchronousReadWrite() QCOMPARE(writeData, readData); } +class AsyncReader : public QObject +{ + Q_OBJECT +public: + explicit AsyncReader(QSerialPort &port, Qt::ConnectionType connectionType, int expectedBytesCount) + : serialPort(port), expectedBytesCount(expectedBytesCount) + { + connect(&serialPort, SIGNAL(readyRead()), this, SLOT(receive()), connectionType); + } + +private slots: + void receive() + { + if (serialPort.bytesAvailable() < expectedBytesCount) + return; + tst_QSerialPort::exitLoop(); + } + +private: + QSerialPort &serialPort; + const int expectedBytesCount; +}; + +class AsyncWriterByBytesWritten : public QObject +{ + Q_OBJECT +public: + explicit AsyncWriterByBytesWritten( + QSerialPort &port, Qt::ConnectionType connectionType, const QByteArray &dataToWrite) + : serialPort(port), writeChunkSize(0) + { + writeBuffer.setData(dataToWrite); + writeBuffer.open(QIODevice::ReadOnly); + connect(&serialPort, SIGNAL(bytesWritten(qint64)), this, SLOT(send()), connectionType); + send(); + } + +private slots: + void send() + { + if (writeBuffer.bytesAvailable() > 0) + serialPort.write(writeBuffer.read(++writeChunkSize)); + } + +private: + QSerialPort &serialPort; + QBuffer writeBuffer; + int writeChunkSize; +}; + +void tst_QSerialPort::asynchronousWriteByBytesWritten_data() +{ + QTest::addColumn<Qt::ConnectionType>("readConnectionType"); + QTest::addColumn<Qt::ConnectionType>("writeConnectionType"); + + QTest::newRow("BothQueued") << Qt::QueuedConnection << Qt::QueuedConnection; + QTest::newRow("BothDirect") << Qt::DirectConnection << Qt::DirectConnection; + QTest::newRow("ReadDirectWriteQueued") << Qt::DirectConnection << Qt::QueuedConnection; + QTest::newRow("ReadQueuedWriteDirect") << Qt::QueuedConnection << Qt::DirectConnection; +} + +void tst_QSerialPort::asynchronousWriteByBytesWritten() +{ +#ifdef Q_OS_WIN + clearReceiver(); +#endif + + QFETCH(Qt::ConnectionType, readConnectionType); + QFETCH(Qt::ConnectionType, writeConnectionType); + + QSerialPort receiverPort(m_receiverPortName); + QVERIFY(receiverPort.open(QSerialPort::ReadOnly)); + AsyncReader reader(receiverPort, readConnectionType, alphabetArray.size()); + + QSerialPort senderPort(m_senderPortName); + QVERIFY(senderPort.open(QSerialPort::WriteOnly)); + AsyncWriterByBytesWritten writer(senderPort, writeConnectionType, alphabetArray); + + enterLoop(1); + QVERIFY2(!timeout(), "Timed out when waiting for the read or write."); + QCOMPARE(receiverPort.bytesAvailable(), qint64(alphabetArray.size())); + QCOMPARE(receiverPort.readAll(), alphabetArray); +} + +class AsyncWriterByTimer : public QObject +{ + Q_OBJECT +public: + explicit AsyncWriterByTimer( + QSerialPort &port, Qt::ConnectionType connectionType, const QByteArray &dataToWrite) + : serialPort(port), writeChunkSize(0) + { + writeBuffer.setData(dataToWrite); + writeBuffer.open(QIODevice::ReadOnly); + connect(&timer, SIGNAL(timeout()), this, SLOT(send()), connectionType); + timer.start(0); + } + +private slots: + void send() + { + if (writeBuffer.bytesAvailable() > 0) + serialPort.write(writeBuffer.read(++writeChunkSize)); + else + timer.stop(); + } + +private: + QSerialPort &serialPort; + QBuffer writeBuffer; + int writeChunkSize; + QTimer timer; +}; + +void tst_QSerialPort::asynchronousWriteByTimer_data() +{ + QTest::addColumn<Qt::ConnectionType>("readConnectionType"); + QTest::addColumn<Qt::ConnectionType>("writeConnectionType"); + + QTest::newRow("BothQueued") << Qt::QueuedConnection << Qt::QueuedConnection; + QTest::newRow("BothDirect") << Qt::DirectConnection << Qt::DirectConnection; + QTest::newRow("ReadDirectWriteQueued") << Qt::DirectConnection << Qt::QueuedConnection; + QTest::newRow("ReadQueuedWriteDirect") << Qt::QueuedConnection << Qt::DirectConnection; +} + +void tst_QSerialPort::asynchronousWriteByTimer() +{ +#ifdef Q_OS_WIN + clearReceiver(); +#endif + + QFETCH(Qt::ConnectionType, readConnectionType); + QFETCH(Qt::ConnectionType, writeConnectionType); + + QSerialPort receiverPort(m_receiverPortName); + QVERIFY(receiverPort.open(QSerialPort::ReadOnly)); + AsyncReader reader(receiverPort, readConnectionType, alphabetArray.size()); + + QSerialPort senderPort(m_senderPortName); + QVERIFY(senderPort.open(QSerialPort::WriteOnly)); + AsyncWriterByTimer writer(senderPort, writeConnectionType, alphabetArray); + + enterLoop(1); + QVERIFY2(!timeout(), "Timed out when waiting for the read or write."); + QCOMPARE(receiverPort.bytesAvailable(), qint64(alphabetArray.size())); + QCOMPARE(receiverPort.readAll(), alphabetArray); +} + QTEST_MAIN(tst_QSerialPort) #include "tst_qserialport.moc" |