diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2012-11-09 22:24:58 +0000 |
---|---|---|
committer | Denis Shienkov <denis.shienkov@gmail.com> | 2012-11-19 21:02:18 +0100 |
commit | 78606e49f3817d6d2877e47273be65a3aeda18b5 (patch) | |
tree | a0c62642d15dad9601bf38fe16a7ad906d106d6e /examples/blockingslave | |
parent | 3875a3df242df9759182c6aa40eff9f2bb2250f8 (diff) | |
download | qtserialport-78606e49f3817d6d2877e47273be65a3aeda18b5.tar.gz |
Added new examples BlockingMaster and BlockingSlave
These examples show how to work with I/O in a blocking mode without
using signals/slots.
Note: For these examples, the documentation is not yet done, must be
done on future.
Change-Id: I0029e8dd43f935629eb77529b2df5bb75d31e091
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Diffstat (limited to 'examples/blockingslave')
-rw-r--r-- | examples/blockingslave/blockingslave.pro | 17 | ||||
-rw-r--r-- | examples/blockingslave/blockingslavewidget.cpp | 138 | ||||
-rw-r--r-- | examples/blockingslave/blockingslavewidget.h | 84 | ||||
-rw-r--r-- | examples/blockingslave/main.cpp | 52 | ||||
-rw-r--r-- | examples/blockingslave/transactionthread.cpp | 167 | ||||
-rw-r--r-- | examples/blockingslave/transactionthread.h | 73 |
6 files changed, 531 insertions, 0 deletions
diff --git a/examples/blockingslave/blockingslave.pro b/examples/blockingslave/blockingslave.pro new file mode 100644 index 0000000..b358e0e --- /dev/null +++ b/examples/blockingslave/blockingslave.pro @@ -0,0 +1,17 @@ +greaterThan(QT_MAJOR_VERSION, 4) { + QT += widgets serialport +} else { + include($$SERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf) +} + +TARGET = blockingslave +TEMPLATE = app + +HEADERS += \ + blockingslavewidget.h \ + transactionthread.h + +SOURCES += \ + main.cpp \ + blockingslavewidget.cpp \ + transactionthread.cpp diff --git a/examples/blockingslave/blockingslavewidget.cpp b/examples/blockingslave/blockingslavewidget.cpp new file mode 100644 index 0000000..c099f7e --- /dev/null +++ b/examples/blockingslave/blockingslavewidget.cpp @@ -0,0 +1,138 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov <scapig@yandex.ru> +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "blockingslavewidget.h" + +#include <QLabel> +#include <QLineEdit> +#include <QComboBox> +#include <QSpinBox> +#include <QPushButton> +#include <QGridLayout> + +#include <serialportinfo.h> + +QT_USE_NAMESPACE_SERIALPORT + +BlockingSlaveWidget::BlockingSlaveWidget(QWidget *parent) + : QWidget(parent), transactionCount(0) + , serialPortLabel(new QLabel(tr("Serial port:"))) + , serialPortComboBox(new QComboBox()) + , waitRequestLabel(new QLabel(tr("Wait request, msec:"))) + , waitRequestSpinBox(new QSpinBox()) + , responseLabel(new QLabel(tr("Response:"))) + , responseLineEdit(new QLineEdit(tr("Hello, I'm Slave."))) + , trafficLabel(new QLabel(tr("No traffic."))) + , statusLabel(new QLabel(tr("Status: Not running."))) + , runButton(new QPushButton(tr("Start"))) +{ + waitRequestSpinBox->setRange(0, 10000); + waitRequestSpinBox->setValue(10000); + + foreach (const SerialPortInfo &info, SerialPortInfo::availablePorts()) + serialPortComboBox->addItem(info.portName()); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(serialPortLabel, 0, 0); + mainLayout->addWidget(serialPortComboBox, 0, 1); + mainLayout->addWidget(waitRequestLabel, 1, 0); + mainLayout->addWidget(waitRequestSpinBox, 1, 1); + mainLayout->addWidget(runButton, 0, 2, 2, 1); + mainLayout->addWidget(responseLabel, 2, 0); + mainLayout->addWidget(responseLineEdit, 2, 1, 1, 3); + mainLayout->addWidget(trafficLabel, 3, 0, 1, 4); + mainLayout->addWidget(statusLabel, 4, 0, 1, 5); + setLayout(mainLayout); + + setWindowTitle(tr("Blocking Slave")); + serialPortComboBox->setFocus(); + + connect(runButton, SIGNAL(clicked()), + this, SLOT(runSlave())); + connect(&thread, SIGNAL(request(QString)), + this, SLOT(showRequest(QString))); + connect(&thread, SIGNAL(error(QString)), + this, SLOT(processError(QString))); + connect(&thread, SIGNAL(timeout(QString)), + this, SLOT(processTimeout(QString))); + + connect(serialPortComboBox, SIGNAL(currentIndexChanged(QString)), + this, SLOT(activateRunButton())); + connect(waitRequestSpinBox, SIGNAL(valueChanged(int)), + this, SLOT(activateRunButton())); + connect(responseLineEdit, SIGNAL(textChanged(QString)), + this, SLOT(activateRunButton())); +} + +void BlockingSlaveWidget::runSlave() +{ + runButton->setEnabled(false); + statusLabel->setText(tr("Status: Running, connected to port %1.") + .arg(serialPortComboBox->currentText())); + thread.startNewSlave(serialPortComboBox->currentText(), + waitRequestSpinBox->value(), + responseLineEdit->text()); +} + +void BlockingSlaveWidget::showRequest(const QString &s) +{ + trafficLabel->setText(tr("Traffic, transaction #%1:" + "\n\r-request: %2" + "\n\r-response: %3") + .arg(++transactionCount).arg(s).arg(responseLineEdit->text())); +} + +void BlockingSlaveWidget::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) +{ + statusLabel->setText(tr("Status: Running, %1.").arg(s)); + trafficLabel->setText(tr("No traffic.")); +} +void BlockingSlaveWidget::activateRunButton() +{ + runButton->setEnabled(true); +} diff --git a/examples/blockingslave/blockingslavewidget.h b/examples/blockingslave/blockingslavewidget.h new file mode 100644 index 0000000..99ef2ab --- /dev/null +++ b/examples/blockingslave/blockingslavewidget.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov <scapig@yandex.ru> +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BLOCKINGSLAVEWIDGET_H +#define BLOCKINGSLAVEWIDGET_H + +#include <QWidget> + +#include "transactionthread.h" + +class QLabel; +class QLineEdit; +class QComboBox; +class QSpinBox; +class QPushButton; + +class BlockingSlaveWidget : public QWidget +{ + Q_OBJECT + +public: + BlockingSlaveWidget(QWidget *parent = 0); + +private slots: + void runSlave(); + void showRequest(const QString &s); + void processError(const QString &s); + void processTimeout(const QString &s); + void activateRunButton(); + +private: + int transactionCount; + QLabel *serialPortLabel; + QComboBox *serialPortComboBox; + QLabel *waitRequestLabel; + QSpinBox *waitRequestSpinBox; + QLabel *responseLabel; + QLineEdit *responseLineEdit; + QLabel *trafficLabel; + QLabel *statusLabel; + QPushButton *runButton; + + TransactionThread thread; +}; + +#endif // BLOCKINGSLAVEWIDGET_H diff --git a/examples/blockingslave/main.cpp b/examples/blockingslave/main.cpp new file mode 100644 index 0000000..cbccb2e --- /dev/null +++ b/examples/blockingslave/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov <scapig@yandex.ru> +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "blockingslavewidget.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + BlockingSlaveWidget w; + w.show(); + return app.exec(); +} diff --git a/examples/blockingslave/transactionthread.cpp b/examples/blockingslave/transactionthread.cpp new file mode 100644 index 0000000..6e158c8 --- /dev/null +++ b/examples/blockingslave/transactionthread.cpp @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov <scapig@yandex.ru> +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "transactionthread.h" + +#include <serialport.h> + +#include <QTime> + +QT_USE_NAMESPACE_SERIALPORT + +TransactionThread::TransactionThread(QObject *parent) + : QThread(parent), waitTimeout(0), quit(false) +{ +} + +TransactionThread::~TransactionThread() +{ + mutex.lock(); + quit = true; + mutex.unlock(); + wait(); +} + +void TransactionThread::startNewSlave(const QString &port, int transactionWaitTimeout, const QString &response) +{ + QMutexLocker locker(&mutex); + portName = port; + waitTimeout = transactionWaitTimeout; + responseText = response; + + if (!isRunning()) + start(); +} + +void TransactionThread::run() +{ + bool currentSerialPortNameChanged = false; + + mutex.lock(); + QString currentSerialPortName; + if (currentSerialPortName != portName) { + currentSerialPortName = portName; + currentSerialPortNameChanged = true; + } + + int currentTransactionWaitTimeout = waitTimeout; + QString currentResponseText = responseText; + mutex.unlock(); + + SerialPort serial; + + while (!quit) { + + if (currentSerialPortNameChanged) { + serial.close(); + serial.setPort(currentSerialPortName); + + if (!serial.open(QIODevice::ReadWrite)) { + emit error(tr("Can't open %1, error code %2") + .arg(portName).arg(serial.error())); + return; + } + + if (!serial.setRate(9600)) { + emit error(tr("Can't set rate 9600 baud to port %1, error code %2") + .arg(portName).arg(serial.error())); + return; + } + + if (!serial.setDataBits(SerialPort::Data8)) { + emit error(tr("Can't set 8 data bits to port %1, error code %2") + .arg(portName).arg(serial.error())); + return; + } + + if (!serial.setParity(SerialPort::NoParity)) { + emit error(tr("Can't set no patity to port %1, error code %2") + .arg(portName).arg(serial.error())); + return; + } + + if (!serial.setStopBits(SerialPort::OneStop)) { + emit error(tr("Can't set 1 stop bit to port %1, error code %2") + .arg(portName).arg(serial.error())); + return; + } + + if (!serial.setFlowControl(SerialPort::NoFlowControl)) { + emit error(tr("Can't set no flow control to port %1, error code %2") + .arg(portName).arg(serial.error())); + return; + } + } + + if (serial.waitForReadyRead(currentTransactionWaitTimeout)) { + + // read all request + QByteArray requestData = serial.readAll(); + while (serial.waitForReadyRead(10)) + requestData += serial.readAll(); + + // write all response + QByteArray responseData = currentResponseText.toLocal8Bit(); + serial.write(responseData); + if (serial.waitForBytesWritten(waitTimeout)) { + QString requestText(requestData); + emit request(requestText); + } else { + emit timeout(tr("Wait write response timeout %1") + .arg(QTime::currentTime().toString())); + } + } else { + emit timeout(tr("Wait read request timeout %1") + .arg(QTime::currentTime().toString())); + } + + mutex.lock(); + if (currentSerialPortName != portName) { + currentSerialPortName = portName; + currentSerialPortNameChanged = true; + } else { + currentSerialPortNameChanged = false; + } + currentTransactionWaitTimeout = waitTimeout; + currentResponseText = responseText; + mutex.unlock(); + } +} diff --git a/examples/blockingslave/transactionthread.h b/examples/blockingslave/transactionthread.h new file mode 100644 index 0000000..3982d94 --- /dev/null +++ b/examples/blockingslave/transactionthread.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Denis Shienkov <scapig@yandex.ru> +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TRANSACTIONTHREAD_H +#define TRANSACTIONTHREAD_H + +#include <QThread> +#include <QMutex> +#include <QWaitCondition> + +class TransactionThread : public QThread +{ + Q_OBJECT + +public: + TransactionThread(QObject *parent = 0); + ~TransactionThread(); + + void startNewSlave(const QString &port, int transactionWaitTimeout, const QString &response); + void run(); + +signals: + void request(const QString &s); + void error(const QString &s); + void timeout(const QString &s); + +private: + QString portName; + QString responseText; + int waitTimeout; + QMutex mutex; + bool quit; +}; + +#endif // TRANSACTIONTHREAD_H |