summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/blockingmaster/blockingmaster.pro17
-rw-r--r--examples/blockingmaster/blockingmasterwidget.cpp137
-rw-r--r--examples/blockingmaster/blockingmasterwidget.h86
-rw-r--r--examples/blockingmaster/main.cpp52
-rw-r--r--examples/blockingmaster/transactionthread.cpp171
-rw-r--r--examples/blockingmaster/transactionthread.h74
-rw-r--r--examples/blockingslave/blockingslave.pro17
-rw-r--r--examples/blockingslave/blockingslavewidget.cpp138
-rw-r--r--examples/blockingslave/blockingslavewidget.h84
-rw-r--r--examples/blockingslave/main.cpp52
-rw-r--r--examples/blockingslave/transactionthread.cpp167
-rw-r--r--examples/blockingslave/transactionthread.h73
-rw-r--r--examples/examples.pro4
13 files changed, 1070 insertions, 2 deletions
diff --git a/examples/blockingmaster/blockingmaster.pro b/examples/blockingmaster/blockingmaster.pro
new file mode 100644
index 00000000..22f14980
--- /dev/null
+++ b/examples/blockingmaster/blockingmaster.pro
@@ -0,0 +1,17 @@
+greaterThan(QT_MAJOR_VERSION, 4) {
+ QT += widgets serialport
+} else {
+ include($$SERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf)
+}
+
+TARGET = blockingmaster
+TEMPLATE = app
+
+HEADERS += \
+ blockingmasterwidget.h \
+ transactionthread.h
+
+SOURCES += \
+ main.cpp \
+ blockingmasterwidget.cpp \
+ transactionthread.cpp
diff --git a/examples/blockingmaster/blockingmasterwidget.cpp b/examples/blockingmaster/blockingmasterwidget.cpp
new file mode 100644
index 00000000..e42beb6e
--- /dev/null
+++ b/examples/blockingmaster/blockingmasterwidget.cpp
@@ -0,0 +1,137 @@
+/****************************************************************************
+**
+** 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 "blockingmasterwidget.h"
+
+#include <QLabel>
+#include <QLineEdit>
+#include <QComboBox>
+#include <QSpinBox>
+#include <QPushButton>
+#include <QGridLayout>
+
+#include "serialportinfo.h"
+
+QT_USE_NAMESPACE_SERIALPORT
+
+BlockingMasterWidget::BlockingMasterWidget(QWidget *parent)
+ : QWidget(parent), transactionCount(0)
+ , serialPortLabel(new QLabel(tr("Serial port:")))
+ , serialPortComboBox(new QComboBox())
+ , waitResponseLabel(new QLabel(tr("Wait response, msec:")))
+ , waitResponseSpinBox(new QSpinBox())
+ , requestLabel(new QLabel(tr("Request:")))
+ , requestLineEdit(new QLineEdit(tr("Who are you?")))
+ , trafficLabel(new QLabel(tr("No traffic.")))
+ , statusLabel(new QLabel(tr("Status: Not running.")))
+ , runButton(new QPushButton(tr("Start")))
+{
+ foreach (const SerialPortInfo &info, SerialPortInfo::availablePorts())
+ serialPortComboBox->addItem(info.portName());
+
+ waitResponseSpinBox->setRange(0, 10000);
+ waitResponseSpinBox->setValue(1000);
+
+ QGridLayout *mainLayout = new QGridLayout;
+ mainLayout->addWidget(serialPortLabel, 0, 0);
+ mainLayout->addWidget(serialPortComboBox, 0, 1);
+ mainLayout->addWidget(waitResponseLabel, 1, 0);
+ mainLayout->addWidget(waitResponseSpinBox, 1, 1);
+ mainLayout->addWidget(runButton, 0, 2, 2, 1);
+ mainLayout->addWidget(requestLabel, 2, 0);
+ mainLayout->addWidget(requestLineEdit, 2, 1, 1, 3);
+ mainLayout->addWidget(trafficLabel, 3, 0, 1, 4);
+ mainLayout->addWidget(statusLabel, 4, 0, 1, 5);
+ setLayout(mainLayout);
+
+ setWindowTitle(tr("Blocking Master"));
+ serialPortComboBox->setFocus();
+
+ connect(runButton, SIGNAL(clicked()),
+ this, SLOT(runMaster()));
+ connect(&thread, SIGNAL(response(QString)),
+ this, SLOT(showResponse(QString)));
+ connect(&thread, SIGNAL(error(QString)),
+ this, SLOT(processError(QString)));
+ connect(&thread, SIGNAL(timeout(QString)),
+ this, SLOT(processTimeout(QString)));
+}
+
+void BlockingMasterWidget::runMaster()
+{
+ setControlsEnabled(false);
+ statusLabel->setText(tr("Status: Running, connected to port %1.")
+ .arg(serialPortComboBox->currentText()));
+ thread.startNewMaster(serialPortComboBox->currentText(),
+ waitResponseSpinBox->value(),
+ requestLineEdit->text());
+}
+
+void BlockingMasterWidget::showResponse(const QString &s)
+{
+ setControlsEnabled(true);
+ trafficLabel->setText(tr("Traffic, transaction #%1:"
+ "\n\r-request: %2"
+ "\n\r-response: %3")
+ .arg(++transactionCount).arg(requestLineEdit->text()).arg(s));
+}
+
+void BlockingMasterWidget::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)
+{
+ setControlsEnabled(true);
+ statusLabel->setText(tr("Status: Running, %1.").arg(s));
+ trafficLabel->setText(tr("No traffic."));
+}
+
+void BlockingMasterWidget::setControlsEnabled(bool enable)
+{
+ runButton->setEnabled(enable);
+ serialPortComboBox->setEnabled(enable);
+ waitResponseSpinBox->setEnabled(enable);
+ requestLineEdit->setEnabled(enable);
+}
diff --git a/examples/blockingmaster/blockingmasterwidget.h b/examples/blockingmaster/blockingmasterwidget.h
new file mode 100644
index 00000000..5db7c86d
--- /dev/null
+++ b/examples/blockingmaster/blockingmasterwidget.h
@@ -0,0 +1,86 @@
+/****************************************************************************
+**
+** 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 BLOCKINGMASTERWIDGET_H
+#define BLOCKINGMASTERWIDGET_H
+
+#include <QWidget>
+
+#include "transactionthread.h"
+
+class QLabel;
+class QLineEdit;
+class QSpinBox;
+class QPushButton;
+class QComboBox;
+
+class BlockingMasterWidget : public QWidget
+{
+ Q_OBJECT
+
+public:
+ BlockingMasterWidget(QWidget *parent = 0);
+
+private slots:
+ void runMaster();
+ void showResponse(const QString &s);
+ void processError(const QString &s);
+ void processTimeout(const QString &s);
+
+private:
+ void setControlsEnabled(bool enable);
+
+private:
+ int transactionCount;
+ QLabel *serialPortLabel;
+ QComboBox *serialPortComboBox;
+ QLabel *waitResponseLabel;
+ QSpinBox *waitResponseSpinBox;
+ QLabel *requestLabel;
+ QLineEdit *requestLineEdit;
+ QLabel *trafficLabel;
+ QLabel *statusLabel;
+ QPushButton *runButton;
+
+ TransactionThread thread;
+};
+
+#endif // BLOCKINGMASTERWIDGET_H
diff --git a/examples/blockingmaster/main.cpp b/examples/blockingmaster/main.cpp
new file mode 100644
index 00000000..395ed253
--- /dev/null
+++ b/examples/blockingmaster/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 "blockingmasterwidget.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+ BlockingMasterWidget w;
+ w.show();
+ return app.exec();
+}
diff --git a/examples/blockingmaster/transactionthread.cpp b/examples/blockingmaster/transactionthread.cpp
new file mode 100644
index 00000000..ff04e2e0
--- /dev/null
+++ b/examples/blockingmaster/transactionthread.cpp
@@ -0,0 +1,171 @@
+/****************************************************************************
+**
+** 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;
+ cond.wakeOne();
+ mutex.unlock();
+ wait();
+}
+
+void TransactionThread::startNewMaster(const QString &port, int transactionWaitTimeout, const QString &request)
+{
+ QMutexLocker locker(&mutex);
+ portName = port;
+ waitTimeout = transactionWaitTimeout;
+ requestText = request;
+
+ if (!isRunning())
+ start();
+ else
+ cond.wakeOne();
+}
+
+void TransactionThread::run()
+{
+ bool currentSerialPortNameChanged = false;
+
+ mutex.lock();
+ QString currentSerialPortName;
+ if (currentSerialPortName != portName) {
+ currentSerialPortName = portName;
+ currentSerialPortNameChanged = true;
+ }
+
+ int currentTransactionWaitTimeout = waitTimeout;
+ QString currentRequestText = requestText;
+ 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;
+ }
+ }
+
+ // write request
+ QByteArray requestData = currentRequestText.toLocal8Bit();
+ serial.write(requestData);
+ if (serial.waitForBytesWritten(waitTimeout)) {
+
+ // read response
+ if (serial.waitForReadyRead(currentTransactionWaitTimeout)) {
+ QByteArray responseData = serial.readAll();
+ while (serial.waitForReadyRead(10))
+ responseData += serial.readAll();
+
+ QString responseText(responseData);
+ emit response(responseText);
+ } else {
+ emit timeout(tr("Wait read response timeout %1")
+ .arg(QTime::currentTime().toString()));
+ }
+ } else {
+ emit timeout(tr("Wait write request timeout %1")
+ .arg(QTime::currentTime().toString()));
+ }
+
+ mutex.lock();
+ cond.wait(&mutex);
+ if (currentSerialPortName != portName) {
+ currentSerialPortName = portName;
+ currentSerialPortNameChanged = true;
+ } else {
+ currentSerialPortNameChanged = false;
+ }
+ currentTransactionWaitTimeout = waitTimeout;
+ currentRequestText = requestText;
+ mutex.unlock();
+ }
+}
diff --git a/examples/blockingmaster/transactionthread.h b/examples/blockingmaster/transactionthread.h
new file mode 100644
index 00000000..6542f050
--- /dev/null
+++ b/examples/blockingmaster/transactionthread.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** 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 startNewMaster(const QString &port, int transactionWaitTimeout, const QString &request);
+ void run();
+
+signals:
+ void response(const QString &s);
+ void error(const QString &s);
+ void timeout(const QString &s);
+
+private:
+ QString portName;
+ QString requestText;
+ int waitTimeout;
+ QMutex mutex;
+ QWaitCondition cond;
+ bool quit;
+};
+
+#endif // TRANSACTIONTHREAD_H
diff --git a/examples/blockingslave/blockingslave.pro b/examples/blockingslave/blockingslave.pro
new file mode 100644
index 00000000..b358e0e2
--- /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 00000000..c099f7e3
--- /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 00000000..99ef2abd
--- /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 00000000..cbccb2ed
--- /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 00000000..6e158c80
--- /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 00000000..3982d944
--- /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
diff --git a/examples/examples.pro b/examples/examples.pro
index 171e8ec3..49302897 100644
--- a/examples/examples.pro
+++ b/examples/examples.pro
@@ -2,7 +2,7 @@ TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS = cenumerator
greaterThan(QT_MAJOR_VERSION, 4) {
- !isEmpty(QT.widgets.name):SUBDIRS += enumerator terminal
+ !isEmpty(QT.widgets.name):SUBDIRS += enumerator terminal blockingmaster blockingslave
} else {
- SUBDIRS += enumerator terminal
+ SUBDIRS += enumerator terminal blockingmaster blockingslave
}