summaryrefslogtreecommitdiff
path: root/examples/serialport/blockingmaster/masterthread.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/serialport/blockingmaster/masterthread.cpp')
-rw-r--r--examples/serialport/blockingmaster/masterthread.cpp87
1 files changed, 42 insertions, 45 deletions
diff --git a/examples/serialport/blockingmaster/masterthread.cpp b/examples/serialport/blockingmaster/masterthread.cpp
index 5ad93fd..25aa848 100644
--- a/examples/serialport/blockingmaster/masterthread.cpp
+++ b/examples/serialport/blockingmaster/masterthread.cpp
@@ -50,24 +50,21 @@
#include "masterthread.h"
-#include <QtSerialPort/QSerialPort>
-
+#include <QSerialPort>
#include <QTime>
-QT_USE_NAMESPACE
-
-MasterThread::MasterThread(QObject *parent)
- : QThread(parent), waitTimeout(0), quit(false)
+MasterThread::MasterThread(QObject *parent) :
+ QThread(parent)
{
}
//! [0]
MasterThread::~MasterThread()
{
- mutex.lock();
- quit = true;
- cond.wakeOne();
- mutex.unlock();
+ m_mutex.lock();
+ m_quit = true;
+ m_cond.wakeOne();
+ m_mutex.unlock();
wait();
}
//! [0]
@@ -75,16 +72,16 @@ MasterThread::~MasterThread()
//! [1] //! [2]
void MasterThread::transaction(const QString &portName, int waitTimeout, const QString &request)
{
- //! [1]
- QMutexLocker locker(&mutex);
- this->portName = portName;
- this->waitTimeout = waitTimeout;
- this->request = request;
- //! [3]
+//! [1]
+ const QMutexLocker locker(&m_mutex);
+ m_portName = portName;
+ m_waitTimeout = waitTimeout;
+ m_request = request;
+//! [3]
if (!isRunning())
start();
else
- cond.wakeOne();
+ m_cond.wakeOne();
}
//! [2] //! [3]
@@ -93,18 +90,18 @@ void MasterThread::run()
{
bool currentPortNameChanged = false;
- mutex.lock();
- //! [4] //! [5]
+ m_mutex.lock();
+//! [4] //! [5]
QString currentPortName;
- if (currentPortName != portName) {
- currentPortName = portName;
+ if (currentPortName != m_portName) {
+ currentPortName = m_portName;
currentPortNameChanged = true;
}
- int currentWaitTimeout = waitTimeout;
- QString currentRequest = request;
- mutex.unlock();
- //! [5] //! [6]
+ int currentWaitTimeout = m_waitTimeout;
+ QString currentRequest = m_request;
+ m_mutex.unlock();
+//! [5] //! [6]
QSerialPort serial;
if (currentPortName.isEmpty()) {
@@ -112,55 +109,55 @@ void MasterThread::run()
return;
}
- while (!quit) {
- //![6] //! [7]
+ while (!m_quit) {
+//![6] //! [7]
if (currentPortNameChanged) {
serial.close();
serial.setPortName(currentPortName);
if (!serial.open(QIODevice::ReadWrite)) {
emit error(tr("Can't open %1, error code %2")
- .arg(portName).arg(serial.error()));
+ .arg(m_portName).arg(serial.error()));
return;
}
}
- //! [7] //! [8]
+//! [7] //! [8]
// write request
- QByteArray requestData = currentRequest.toLocal8Bit();
+ const QByteArray requestData = currentRequest.toUtf8();
serial.write(requestData);
- if (serial.waitForBytesWritten(waitTimeout)) {
- //! [8] //! [10]
+ if (serial.waitForBytesWritten(m_waitTimeout)) {
+//! [8] //! [10]
// read response
if (serial.waitForReadyRead(currentWaitTimeout)) {
QByteArray responseData = serial.readAll();
while (serial.waitForReadyRead(10))
responseData += serial.readAll();
- QString response(responseData);
- //! [12]
+ const QString response = QString::fromUtf8(responseData);
+//! [12]
emit this->response(response);
- //! [10] //! [11] //! [12]
+//! [10] //! [11] //! [12]
} else {
emit timeout(tr("Wait read response timeout %1")
.arg(QTime::currentTime().toString()));
}
- //! [9] //! [11]
+//! [9] //! [11]
} else {
emit timeout(tr("Wait write request timeout %1")
.arg(QTime::currentTime().toString()));
}
- //! [9] //! [13]
- mutex.lock();
- cond.wait(&mutex);
- if (currentPortName != portName) {
- currentPortName = portName;
+//! [9] //! [13]
+ m_mutex.lock();
+ m_cond.wait(&m_mutex);
+ if (currentPortName != m_portName) {
+ currentPortName = m_portName;
currentPortNameChanged = true;
} else {
currentPortNameChanged = false;
}
- currentWaitTimeout = waitTimeout;
- currentRequest = request;
- mutex.unlock();
+ currentWaitTimeout = m_waitTimeout;
+ currentRequest = m_request;
+ m_mutex.unlock();
}
- //! [13]
+//! [13]
}