summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 0a432d7..f4f7478 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 295869d..ebd748a 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 a62c3c5..72c37b8 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 357cef8..24f6224 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"