summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2014-12-30 13:38:05 +0300
committerDenis Shienkov <denis.shienkov@gmail.com>2015-01-15 20:57:33 +0100
commitb1005907d26a3e2f5dffaec93d656a0c1210961f (patch)
treeb5db77484d92536a995a8d822166bb0906eac8ef
parent38f43b390e5b27f6243ba781394ad2418ead6b74 (diff)
Improve usage of the break state for the Tx line
1. Now the sendBreak() method is deprecated since it is blocking and impossible to implement same behavior on different platforms using their API's. Besides, this method can be implemented via setBreakEnabled() and QTimer. 2. Introduced the new property named as "breakEnabled" which consist of setBreakEnabled() and isBreakEnabled() methods and the signal breakEnabledChanged(). Note: After opening, the port always is in non-break state. Task-number: QTBUG-36571 Change-Id: Ib808dab7eaed8cc5449c66d8186a29a7b7e45afc Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com> Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r--src/serialport/qserialport.cpp36
-rw-r--r--src/serialport/qserialport.h5
-rw-r--r--src/serialport/qserialport_p.h1
-rw-r--r--tests/auto/qserialport/tst_qserialport.cpp59
4 files changed, 93 insertions, 8 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp
index 0a432d7c..f4f7478d 100644
--- a/src/serialport/qserialport.cpp
+++ b/src/serialport/qserialport.cpp
@@ -59,6 +59,7 @@ QSerialPortPrivate::QSerialPortPrivate()
#if QT_DEPRECATED_SINCE(5,3)
, settingsRestoredOnClose(true)
#endif
+ , isBreakEnabled(false)
#if defined (Q_OS_WINCE)
, handle(INVALID_HANDLE_VALUE)
, parityErrorOccurred(false)
@@ -570,6 +571,7 @@ void QSerialPort::close()
QIODevice::close();
d->close();
+ d->isBreakEnabled = false;
}
/*!
@@ -1310,15 +1312,20 @@ bool QSerialPort::sendBreak(int duration)
}
/*!
- Controls the signal break, depending on the flag \a set.
- If successful, returns true; otherwise returns false.
-
- If \a set is true then enables the break transmission; otherwise disables.
+ \property QSerialPort::breakEnabled
+ \since 5.5
+ \brief the state of the transmission line in break
- \note The serial port has to be open before trying to set break enabled;
- otherwise returns false and sets the NotOpenError error code.
+ Returns true on success, false otherwise.
+ If the flag is true then the transmission line is in break state;
+ otherwise is in non-break state.
- \sa sendBreak()
+ \note The serial port has to be open before trying to set or get this
+ property; otherwise returns false and sets the NotOpenError error code.
+ This is a bit unusual as opposed to the regular Qt property settings of
+ a class. However, this is a special use case since the property is set
+ through the interaction with the kernel and hardware. Hence, the two
+ scenarios cannot be completely compared to each other.
*/
bool QSerialPort::setBreakEnabled(bool set)
{
@@ -1330,7 +1337,20 @@ bool QSerialPort::setBreakEnabled(bool set)
return false;
}
- return d->setBreakEnabled(set);
+ if (d->setBreakEnabled(set)) {
+ if (d->isBreakEnabled != set) {
+ d->isBreakEnabled = set;
+ emit breakEnabledChanged(d->isBreakEnabled);
+ }
+ return true;
+ }
+ return false;
+}
+
+bool QSerialPort::isBreakEnabled() const
+{
+ Q_D(const QSerialPort);
+ return d->isBreakEnabled;
}
/*!
diff --git a/src/serialport/qserialport.h b/src/serialport/qserialport.h
index 295869d7..ebd748aa 100644
--- a/src/serialport/qserialport.h
+++ b/src/serialport/qserialport.h
@@ -63,6 +63,7 @@ class Q_SERIALPORT_EXPORT QSerialPort : public QIODevice
#if QT_DEPRECATED_SINCE(5, 3)
Q_PROPERTY(bool settingsRestoredOnClose READ settingsRestoredOnClose WRITE setSettingsRestoredOnClose NOTIFY settingsRestoredOnCloseChanged)
#endif
+ Q_PROPERTY(bool breakEnabled READ isBreakEnabled WRITE setBreakEnabled NOTIFY breakEnabledChanged)
Q_ENUMS(BaudRate DataBits Parity StopBits FlowControl DataErrorPolicy SerialPortError)
Q_FLAGS(Directions PinoutSignals)
@@ -244,8 +245,11 @@ public:
bool waitForReadyRead(int msecs) Q_DECL_OVERRIDE;
bool waitForBytesWritten(int msecs) Q_DECL_OVERRIDE;
+#if QT_DEPRECATED_SINCE(5, 5)
bool sendBreak(int duration = 0);
+#endif
bool setBreakEnabled(bool set = true);
+ bool isBreakEnabled() const;
Handle handle() const;
@@ -260,6 +264,7 @@ Q_SIGNALS:
void requestToSendChanged(bool set);
void error(QSerialPort::SerialPortError serialPortError);
void settingsRestoredOnCloseChanged(bool restore);
+ void breakEnabledChanged(bool set);
protected:
qint64 readData(char *data, qint64 maxSize) Q_DECL_OVERRIDE;
diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h
index a62c3c5e..72c37b84 100644
--- a/src/serialport/qserialport_p.h
+++ b/src/serialport/qserialport_p.h
@@ -175,6 +175,7 @@ public:
bool dataTerminalReady;
bool requestToSend;
bool settingsRestoredOnClose;
+ bool isBreakEnabled;
#if defined (Q_OS_WINCE)
diff --git a/tests/auto/qserialport/tst_qserialport.cpp b/tests/auto/qserialport/tst_qserialport.cpp
index 357cef89..24f6224e 100644
--- a/tests/auto/qserialport/tst_qserialport.cpp
+++ b/tests/auto/qserialport/tst_qserialport.cpp
@@ -112,6 +112,8 @@ private slots:
void readAfterInputClear();
#endif
+ void controlBreak();
+
protected slots:
void handleBytesWrittenAndExitLoopSlot(qint64 bytesWritten);
void handleBytesWrittenAndExitLoopSlot2(qint64 bytesWritten);
@@ -736,5 +738,62 @@ void tst_QSerialPort::readAfterInputClear()
}
#endif
+class BreakReader : public QObject
+{
+ Q_OBJECT
+public:
+ explicit BreakReader(QSerialPort &port)
+ : serialPort(port)
+ {
+ connect(&serialPort, SIGNAL(readyRead()), this, SLOT(receive()));
+ }
+
+private slots:
+ void receive()
+ {
+ tst_QSerialPort::exitLoop();
+ }
+
+private:
+ QSerialPort &serialPort;
+};
+
+void tst_QSerialPort::controlBreak()
+{
+#ifdef Q_OS_WIN
+ clearReceiver();
+#endif
+
+ QSerialPort senderPort(m_senderPortName);
+ QVERIFY(senderPort.open(QSerialPort::WriteOnly));
+ QCOMPARE(senderPort.isBreakEnabled(), false);
+
+ QSignalSpy breakSpy(&senderPort, SIGNAL(breakEnabledChanged(bool)));
+ QVERIFY(breakSpy.isValid());
+
+ QSerialPort receiverPort(m_receiverPortName);
+ QVERIFY(receiverPort.open(QSerialPort::ReadOnly));
+
+ BreakReader reader(receiverPort);
+
+ QVERIFY(senderPort.setBreakEnabled(true));
+ QCOMPARE(senderPort.isBreakEnabled(), true);
+
+ enterLoop(1);
+ QVERIFY2(!timeout(), "Timed out when waiting for the read of break state.");
+ QVERIFY(receiverPort.bytesAvailable() > 0);
+
+ foreach (const char c, receiverPort.readAll()) {
+ QCOMPARE(c, char(0));
+ }
+
+ QVERIFY(senderPort.setBreakEnabled(false));
+ QCOMPARE(senderPort.isBreakEnabled(), false);
+
+ QCOMPARE(breakSpy.count(), 2);
+ QCOMPARE(qvariant_cast<bool>(breakSpy.at(0).at(0)), true);
+ QCOMPARE(qvariant_cast<bool>(breakSpy.at(1).at(0)), false);
+}
+
QTEST_MAIN(tst_QSerialPort)
#include "tst_qserialport.moc"