diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2012-12-03 22:31:49 +0400 |
---|---|---|
committer | Denis Shienkov <denis.shienkov@gmail.com> | 2012-12-05 10:41:18 +0100 |
commit | 125a2bb81b15eb0c80c7e0aa8bf0d66d67489892 (patch) | |
tree | d7fd90b82138f6897d378db84475b2525a37f890 | |
parent | 6cc3c66ef0f8237434bcfbc39b66ccadae949bc4 (diff) | |
download | qtserialport-125a2bb81b15eb0c80c7e0aa8bf0d66d67489892.tar.gz |
Refactoring for BlockingMaster and BlockingSlave examples
Renamed classes and methods to a more appropriate in meaning.
Change-Id: I69f1ef2fb30f3e207e506fea60eeca3195bf70ed
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r-- | examples/blockingmaster/blockingmaster.pro | 8 | ||||
-rw-r--r-- | examples/blockingmaster/dialog.cpp (renamed from examples/blockingmaster/blockingmasterwidget.cpp) | 25 | ||||
-rw-r--r-- | examples/blockingmaster/dialog.h (renamed from examples/blockingmaster/blockingmasterwidget.h) | 18 | ||||
-rw-r--r-- | examples/blockingmaster/main.cpp | 6 | ||||
-rw-r--r-- | examples/blockingmaster/masterthread.cpp (renamed from examples/blockingmaster/transactionthread.cpp) | 54 | ||||
-rw-r--r-- | examples/blockingmaster/masterthread.h (renamed from examples/blockingmaster/transactionthread.h) | 16 | ||||
-rw-r--r-- | examples/blockingslave/blockingslave.pro | 8 | ||||
-rw-r--r-- | examples/blockingslave/dialog.cpp (renamed from examples/blockingslave/blockingslavewidget.cpp) | 19 | ||||
-rw-r--r-- | examples/blockingslave/dialog.h (renamed from examples/blockingslave/blockingslavewidget.h) | 18 | ||||
-rw-r--r-- | examples/blockingslave/main.cpp | 6 | ||||
-rw-r--r-- | examples/blockingslave/slavethread.cpp (renamed from examples/blockingslave/transactionthread.cpp) | 54 | ||||
-rw-r--r-- | examples/blockingslave/slavethread.h (renamed from examples/blockingslave/transactionthread.h) | 16 |
12 files changed, 125 insertions, 123 deletions
diff --git a/examples/blockingmaster/blockingmaster.pro b/examples/blockingmaster/blockingmaster.pro index 22f1498..a8a6a10 100644 --- a/examples/blockingmaster/blockingmaster.pro +++ b/examples/blockingmaster/blockingmaster.pro @@ -8,10 +8,10 @@ TARGET = blockingmaster TEMPLATE = app HEADERS += \ - blockingmasterwidget.h \ - transactionthread.h + dialog.h \ + masterthread.h SOURCES += \ main.cpp \ - blockingmasterwidget.cpp \ - transactionthread.cpp + dialog.cpp \ + masterthread.cpp diff --git a/examples/blockingmaster/blockingmasterwidget.cpp b/examples/blockingmaster/dialog.cpp index 7cff89a..b93928c 100644 --- a/examples/blockingmaster/blockingmasterwidget.cpp +++ b/examples/blockingmaster/dialog.cpp @@ -39,7 +39,7 @@ ** ****************************************************************************/ -#include "blockingmasterwidget.h" +#include "dialog.h" #include <QLabel> #include <QLineEdit> @@ -52,8 +52,9 @@ QT_USE_NAMESPACE_SERIALPORT -BlockingMasterWidget::BlockingMasterWidget(QWidget *parent) - : QWidget(parent), transactionCount(0) +Dialog::Dialog(QWidget *parent) + : QDialog(parent) + , transactionCount(0) , serialPortLabel(new QLabel(tr("Serial port:"))) , serialPortComboBox(new QComboBox()) , waitResponseLabel(new QLabel(tr("Wait response, msec:"))) @@ -86,7 +87,7 @@ BlockingMasterWidget::BlockingMasterWidget(QWidget *parent) serialPortComboBox->setFocus(); connect(runButton, SIGNAL(clicked()), - this, SLOT(runMaster())); + this, SLOT(transaction())); connect(&thread, SIGNAL(response(QString)), this, SLOT(showResponse(QString))); connect(&thread, SIGNAL(error(QString)), @@ -95,17 +96,17 @@ BlockingMasterWidget::BlockingMasterWidget(QWidget *parent) this, SLOT(processTimeout(QString))); } -void BlockingMasterWidget::runMaster() +void Dialog::transaction() { setControlsEnabled(false); statusLabel->setText(tr("Status: Running, connected to port %1.") .arg(serialPortComboBox->currentText())); - thread.startNewTransaction(serialPortComboBox->currentText(), - waitResponseSpinBox->value(), - requestLineEdit->text()); + thread.transaction(serialPortComboBox->currentText(), + waitResponseSpinBox->value(), + requestLineEdit->text()); } -void BlockingMasterWidget::showResponse(const QString &s) +void Dialog::showResponse(const QString &s) { setControlsEnabled(true); trafficLabel->setText(tr("Traffic, transaction #%1:" @@ -114,21 +115,21 @@ void BlockingMasterWidget::showResponse(const QString &s) .arg(++transactionCount).arg(requestLineEdit->text()).arg(s)); } -void BlockingMasterWidget::processError(const QString &s) +void Dialog::processError(const QString &s) { setControlsEnabled(true); statusLabel->setText(tr("Status: Not running, %1.").arg(s)); trafficLabel->setText(tr("No traffic.")); } -void BlockingMasterWidget::processTimeout(const QString &s) +void Dialog::processTimeout(const QString &s) { setControlsEnabled(true); statusLabel->setText(tr("Status: Running, %1.").arg(s)); trafficLabel->setText(tr("No traffic.")); } -void BlockingMasterWidget::setControlsEnabled(bool enable) +void Dialog::setControlsEnabled(bool enable) { runButton->setEnabled(enable); serialPortComboBox->setEnabled(enable); diff --git a/examples/blockingmaster/blockingmasterwidget.h b/examples/blockingmaster/dialog.h index 5db7c86..7989b95 100644 --- a/examples/blockingmaster/blockingmasterwidget.h +++ b/examples/blockingmaster/dialog.h @@ -39,12 +39,12 @@ ** ****************************************************************************/ -#ifndef BLOCKINGMASTERWIDGET_H -#define BLOCKINGMASTERWIDGET_H +#ifndef DIALOG_H +#define DIALOG_H -#include <QWidget> +#include <QDialog> -#include "transactionthread.h" +#include "masterthread.h" class QLabel; class QLineEdit; @@ -52,15 +52,15 @@ class QSpinBox; class QPushButton; class QComboBox; -class BlockingMasterWidget : public QWidget +class Dialog : public QDialog { Q_OBJECT public: - BlockingMasterWidget(QWidget *parent = 0); + Dialog(QWidget *parent = 0); private slots: - void runMaster(); + void transaction(); void showResponse(const QString &s); void processError(const QString &s); void processTimeout(const QString &s); @@ -80,7 +80,7 @@ private: QLabel *statusLabel; QPushButton *runButton; - TransactionThread thread; + MasterThread thread; }; -#endif // BLOCKINGMASTERWIDGET_H +#endif // DIALOG_H diff --git a/examples/blockingmaster/main.cpp b/examples/blockingmaster/main.cpp index 395ed25..695d85c 100644 --- a/examples/blockingmaster/main.cpp +++ b/examples/blockingmaster/main.cpp @@ -41,12 +41,12 @@ #include <QApplication> -#include "blockingmasterwidget.h" +#include "dialog.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); - BlockingMasterWidget w; - w.show(); + Dialog dialog; + dialog.show(); return app.exec(); } diff --git a/examples/blockingmaster/transactionthread.cpp b/examples/blockingmaster/masterthread.cpp index 8843d19..6e97060 100644 --- a/examples/blockingmaster/transactionthread.cpp +++ b/examples/blockingmaster/masterthread.cpp @@ -39,7 +39,7 @@ ** ****************************************************************************/ -#include "transactionthread.h" +#include "masterthread.h" #include <QtAddOnSerialPort/serialport.h> @@ -47,12 +47,12 @@ QT_USE_NAMESPACE_SERIALPORT -TransactionThread::TransactionThread(QObject *parent) +MasterThread::MasterThread(QObject *parent) : QThread(parent), waitTimeout(0), quit(false) { } -TransactionThread::~TransactionThread() +MasterThread::~MasterThread() { mutex.lock(); quit = true; @@ -61,12 +61,12 @@ TransactionThread::~TransactionThread() wait(); } -void TransactionThread::startNewTransaction(const QString &port, int transactionWaitTimeout, const QString &request) +void MasterThread::transaction(const QString &portName, int waitTimeout, const QString &request) { QMutexLocker locker(&mutex); - portName = port; - waitTimeout = transactionWaitTimeout; - requestText = request; + this->portName = portName; + this->waitTimeout = waitTimeout; + this->request = request; if (!isRunning()) start(); @@ -74,28 +74,28 @@ void TransactionThread::startNewTransaction(const QString &port, int transaction cond.wakeOne(); } -void TransactionThread::run() +void MasterThread::run() { - bool currentSerialPortNameChanged = false; + bool currentPortNameChanged = false; mutex.lock(); - QString currentSerialPortName; - if (currentSerialPortName != portName) { - currentSerialPortName = portName; - currentSerialPortNameChanged = true; + QString currentPortName; + if (currentPortName != portName) { + currentPortName = portName; + currentPortNameChanged = true; } - int currentTransactionWaitTimeout = waitTimeout; - QString currentRequestText = requestText; + int currentWaitTimeout = waitTimeout; + QString currentRequest = request; mutex.unlock(); SerialPort serial; while (!quit) { - if (currentSerialPortNameChanged) { + if (currentPortNameChanged) { serial.close(); - serial.setPort(currentSerialPortName); + serial.setPort(currentPortName); if (!serial.open(QIODevice::ReadWrite)) { emit error(tr("Can't open %1, error code %2") @@ -135,18 +135,18 @@ void TransactionThread::run() } // write request - QByteArray requestData = currentRequestText.toLocal8Bit(); + QByteArray requestData = currentRequest.toLocal8Bit(); serial.write(requestData); if (serial.waitForBytesWritten(waitTimeout)) { // read response - if (serial.waitForReadyRead(currentTransactionWaitTimeout)) { + if (serial.waitForReadyRead(currentWaitTimeout)) { QByteArray responseData = serial.readAll(); while (serial.waitForReadyRead(10)) responseData += serial.readAll(); - QString responseText(responseData); - emit response(responseText); + QString response(responseData); + emit this->response(response); } else { emit timeout(tr("Wait read response timeout %1") .arg(QTime::currentTime().toString())); @@ -158,14 +158,14 @@ void TransactionThread::run() mutex.lock(); cond.wait(&mutex); - if (currentSerialPortName != portName) { - currentSerialPortName = portName; - currentSerialPortNameChanged = true; + if (currentPortName != portName) { + currentPortName = portName; + currentPortNameChanged = true; } else { - currentSerialPortNameChanged = false; + currentPortNameChanged = false; } - currentTransactionWaitTimeout = waitTimeout; - currentRequestText = requestText; + currentWaitTimeout = waitTimeout; + currentRequest = request; mutex.unlock(); } } diff --git a/examples/blockingmaster/transactionthread.h b/examples/blockingmaster/masterthread.h index 3e01395..d4f51f2 100644 --- a/examples/blockingmaster/transactionthread.h +++ b/examples/blockingmaster/masterthread.h @@ -39,22 +39,22 @@ ** ****************************************************************************/ -#ifndef TRANSACTIONTHREAD_H -#define TRANSACTIONTHREAD_H +#ifndef MASTERTHREAD_H +#define MASTERTHREAD_H #include <QThread> #include <QMutex> #include <QWaitCondition> -class TransactionThread : public QThread +class MasterThread : public QThread { Q_OBJECT public: - TransactionThread(QObject *parent = 0); - ~TransactionThread(); + MasterThread(QObject *parent = 0); + ~MasterThread(); - void startNewTransaction(const QString &port, int transactionWaitTimeout, const QString &request); + void transaction(const QString &portName, int waitTimeout, const QString &request); void run(); signals: @@ -64,11 +64,11 @@ signals: private: QString portName; - QString requestText; + QString request; int waitTimeout; QMutex mutex; QWaitCondition cond; bool quit; }; -#endif // TRANSACTIONTHREAD_H +#endif // MASTERTHREAD_H diff --git a/examples/blockingslave/blockingslave.pro b/examples/blockingslave/blockingslave.pro index b358e0e..e127b30 100644 --- a/examples/blockingslave/blockingslave.pro +++ b/examples/blockingslave/blockingslave.pro @@ -8,10 +8,10 @@ TARGET = blockingslave TEMPLATE = app HEADERS += \ - blockingslavewidget.h \ - transactionthread.h + dialog.h \ + slavethread.h SOURCES += \ main.cpp \ - blockingslavewidget.cpp \ - transactionthread.cpp + dialog.cpp \ + slavethread.cpp diff --git a/examples/blockingslave/blockingslavewidget.cpp b/examples/blockingslave/dialog.cpp index 9af7ec6..043ad3e 100644 --- a/examples/blockingslave/blockingslavewidget.cpp +++ b/examples/blockingslave/dialog.cpp @@ -39,7 +39,7 @@ ** ****************************************************************************/ -#include "blockingslavewidget.h" +#include "dialog.h" #include <QLabel> #include <QLineEdit> @@ -52,8 +52,9 @@ QT_USE_NAMESPACE_SERIALPORT -BlockingSlaveWidget::BlockingSlaveWidget(QWidget *parent) - : QWidget(parent), transactionCount(0) +Dialog::Dialog(QWidget *parent) + : QDialog(parent) + , transactionCount(0) , serialPortLabel(new QLabel(tr("Serial port:"))) , serialPortComboBox(new QComboBox()) , waitRequestLabel(new QLabel(tr("Wait request, msec:"))) @@ -86,7 +87,7 @@ BlockingSlaveWidget::BlockingSlaveWidget(QWidget *parent) serialPortComboBox->setFocus(); connect(runButton, SIGNAL(clicked()), - this, SLOT(runSlave())); + this, SLOT(startSlave())); connect(&thread, SIGNAL(request(QString)), this, SLOT(showRequest(QString))); connect(&thread, SIGNAL(error(QString)), @@ -102,7 +103,7 @@ BlockingSlaveWidget::BlockingSlaveWidget(QWidget *parent) this, SLOT(activateRunButton())); } -void BlockingSlaveWidget::runSlave() +void Dialog::startSlave() { runButton->setEnabled(false); statusLabel->setText(tr("Status: Running, connected to port %1.") @@ -112,7 +113,7 @@ void BlockingSlaveWidget::runSlave() responseLineEdit->text()); } -void BlockingSlaveWidget::showRequest(const QString &s) +void Dialog::showRequest(const QString &s) { trafficLabel->setText(tr("Traffic, transaction #%1:" "\n\r-request: %2" @@ -120,19 +121,19 @@ void BlockingSlaveWidget::showRequest(const QString &s) .arg(++transactionCount).arg(s).arg(responseLineEdit->text())); } -void BlockingSlaveWidget::processError(const QString &s) +void Dialog::processError(const QString &s) { activateRunButton(); statusLabel->setText(tr("Status: Not running, %1.").arg(s)); trafficLabel->setText(tr("No traffic.")); } -void BlockingSlaveWidget::processTimeout(const QString &s) +void Dialog::processTimeout(const QString &s) { statusLabel->setText(tr("Status: Running, %1.").arg(s)); trafficLabel->setText(tr("No traffic.")); } -void BlockingSlaveWidget::activateRunButton() +void Dialog::activateRunButton() { runButton->setEnabled(true); } diff --git a/examples/blockingslave/blockingslavewidget.h b/examples/blockingslave/dialog.h index 99ef2ab..7d1db7e 100644 --- a/examples/blockingslave/blockingslavewidget.h +++ b/examples/blockingslave/dialog.h @@ -39,12 +39,12 @@ ** ****************************************************************************/ -#ifndef BLOCKINGSLAVEWIDGET_H -#define BLOCKINGSLAVEWIDGET_H +#ifndef DIALOG_H +#define DIALOG_H -#include <QWidget> +#include <QDialog> -#include "transactionthread.h" +#include "slavethread.h" class QLabel; class QLineEdit; @@ -52,15 +52,15 @@ class QComboBox; class QSpinBox; class QPushButton; -class BlockingSlaveWidget : public QWidget +class Dialog : public QDialog { Q_OBJECT public: - BlockingSlaveWidget(QWidget *parent = 0); + Dialog(QWidget *parent = 0); private slots: - void runSlave(); + void startSlave(); void showRequest(const QString &s); void processError(const QString &s); void processTimeout(const QString &s); @@ -78,7 +78,7 @@ private: QLabel *statusLabel; QPushButton *runButton; - TransactionThread thread; + SlaveThread thread; }; -#endif // BLOCKINGSLAVEWIDGET_H +#endif // DIALOG_H diff --git a/examples/blockingslave/main.cpp b/examples/blockingslave/main.cpp index cbccb2e..695d85c 100644 --- a/examples/blockingslave/main.cpp +++ b/examples/blockingslave/main.cpp @@ -41,12 +41,12 @@ #include <QApplication> -#include "blockingslavewidget.h" +#include "dialog.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); - BlockingSlaveWidget w; - w.show(); + Dialog dialog; + dialog.show(); return app.exec(); } diff --git a/examples/blockingslave/transactionthread.cpp b/examples/blockingslave/slavethread.cpp index bc6f105..3ff370e 100644 --- a/examples/blockingslave/transactionthread.cpp +++ b/examples/blockingslave/slavethread.cpp @@ -39,7 +39,7 @@ ** ****************************************************************************/ -#include "transactionthread.h" +#include "slavethread.h" #include <QtAddOnSerialPort/serialport.h> @@ -47,12 +47,12 @@ QT_USE_NAMESPACE_SERIALPORT -TransactionThread::TransactionThread(QObject *parent) +SlaveThread::SlaveThread(QObject *parent) : QThread(parent), waitTimeout(0), quit(false) { } -TransactionThread::~TransactionThread() +SlaveThread::~SlaveThread() { mutex.lock(); quit = true; @@ -60,39 +60,39 @@ TransactionThread::~TransactionThread() wait(); } -void TransactionThread::startSlave(const QString &port, int transactionWaitTimeout, const QString &response) +void SlaveThread::startSlave(const QString &portName, int waitTimeout, const QString &response) { QMutexLocker locker(&mutex); - portName = port; - waitTimeout = transactionWaitTimeout; - responseText = response; + this->portName = portName; + this->waitTimeout = waitTimeout; + this->response = response; if (!isRunning()) start(); } -void TransactionThread::run() +void SlaveThread::run() { - bool currentSerialPortNameChanged = false; + bool currentPortNameChanged = false; mutex.lock(); - QString currentSerialPortName; - if (currentSerialPortName != portName) { - currentSerialPortName = portName; - currentSerialPortNameChanged = true; + QString currentPortName; + if (currentPortName != portName) { + currentPortName = portName; + currentPortNameChanged = true; } - int currentTransactionWaitTimeout = waitTimeout; - QString currentResponseText = responseText; + int currentWaitTimeout = waitTimeout; + QString currentRespone = response; mutex.unlock(); SerialPort serial; while (!quit) { - if (currentSerialPortNameChanged) { + if (currentPortNameChanged) { serial.close(); - serial.setPort(currentSerialPortName); + serial.setPort(currentPortName); if (!serial.open(QIODevice::ReadWrite)) { emit error(tr("Can't open %1, error code %2") @@ -131,7 +131,7 @@ void TransactionThread::run() } } - if (serial.waitForReadyRead(currentTransactionWaitTimeout)) { + if (serial.waitForReadyRead(currentWaitTimeout)) { // read all request QByteArray requestData = serial.readAll(); @@ -139,11 +139,11 @@ void TransactionThread::run() requestData += serial.readAll(); // write all response - QByteArray responseData = currentResponseText.toLocal8Bit(); + QByteArray responseData = currentRespone.toLocal8Bit(); serial.write(responseData); if (serial.waitForBytesWritten(waitTimeout)) { - QString requestText(requestData); - emit request(requestText); + QString request(requestData); + emit this->request(request); } else { emit timeout(tr("Wait write response timeout %1") .arg(QTime::currentTime().toString())); @@ -154,14 +154,14 @@ void TransactionThread::run() } mutex.lock(); - if (currentSerialPortName != portName) { - currentSerialPortName = portName; - currentSerialPortNameChanged = true; + if (currentPortName != portName) { + currentPortName = portName; + currentPortNameChanged = true; } else { - currentSerialPortNameChanged = false; + currentPortNameChanged = false; } - currentTransactionWaitTimeout = waitTimeout; - currentResponseText = responseText; + currentWaitTimeout = waitTimeout; + currentRespone = response; mutex.unlock(); } } diff --git a/examples/blockingslave/transactionthread.h b/examples/blockingslave/slavethread.h index 352ab8e..a29cb3a 100644 --- a/examples/blockingslave/transactionthread.h +++ b/examples/blockingslave/slavethread.h @@ -39,22 +39,22 @@ ** ****************************************************************************/ -#ifndef TRANSACTIONTHREAD_H -#define TRANSACTIONTHREAD_H +#ifndef SLAVETHREAD_H +#define SLAVETHREAD_H #include <QThread> #include <QMutex> #include <QWaitCondition> -class TransactionThread : public QThread +class SlaveThread : public QThread { Q_OBJECT public: - TransactionThread(QObject *parent = 0); - ~TransactionThread(); + SlaveThread(QObject *parent = 0); + ~SlaveThread(); - void startSlave(const QString &port, int transactionWaitTimeout, const QString &response); + void startSlave(const QString &portName, int waitTimeout, const QString &response); void run(); signals: @@ -64,10 +64,10 @@ signals: private: QString portName; - QString responseText; + QString response; int waitTimeout; QMutex mutex; bool quit; }; -#endif // TRANSACTIONTHREAD_H +#endif // SLAVETHREAD_H |