From 26458aef426cde967433035d2af0ab244db3aec0 Mon Sep 17 00:00:00 2001 From: Laszlo Papp Date: Sat, 26 Oct 2013 10:53:16 +0100 Subject: Add a new simple and command line async writer example Change-Id: I8f33126de5c78da9a4a0dbddd16eecde7044f0c5 Reviewed-by: Sergey Belyashov Reviewed-by: Denis Shienkov --- examples/serialport/cwriterasync/cwriterasync.pro | 20 ++++ examples/serialport/cwriterasync/main.cpp | 119 +++++++++++++++++++++ .../serialport/cwriterasync/serialportwriter.cpp | 103 ++++++++++++++++++ .../serialport/cwriterasync/serialportwriter.h | 81 ++++++++++++++ examples/serialport/doc/cwriterasync.qdoc | 46 ++++++++ examples/serialport/examples.qdoc | 1 + examples/serialport/serialport.pro | 2 +- 7 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 examples/serialport/cwriterasync/cwriterasync.pro create mode 100644 examples/serialport/cwriterasync/main.cpp create mode 100644 examples/serialport/cwriterasync/serialportwriter.cpp create mode 100644 examples/serialport/cwriterasync/serialportwriter.h create mode 100644 examples/serialport/doc/cwriterasync.qdoc (limited to 'examples') diff --git a/examples/serialport/cwriterasync/cwriterasync.pro b/examples/serialport/cwriterasync/cwriterasync.pro new file mode 100644 index 0000000..8a64ceb --- /dev/null +++ b/examples/serialport/cwriterasync/cwriterasync.pro @@ -0,0 +1,20 @@ +QT = core + +greaterThan(QT_MAJOR_VERSION, 4) { + QT += serialport +} else { + include($$QTSERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf) +} + +CONFIG += console +CONFIG -= app_bundle + +TARGET = cwriterasync +TEMPLATE = app + +HEADERS += \ + serialportwriter.h + +SOURCES += \ + main.cpp \ + serialportwriter.cpp diff --git a/examples/serialport/cwriterasync/main.cpp b/examples/serialport/cwriterasync/main.cpp new file mode 100644 index 0000000..4277914 --- /dev/null +++ b/examples/serialport/cwriterasync/main.cpp @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "serialportwriter.h" + +#include + +#include +#include +#include +#include + +QT_USE_NAMESPACE + +int main(int argc, char *argv[]) +{ + QCoreApplication coreApplication(argc, argv); + int argumentCount = QCoreApplication::arguments().size(); + QStringList argumentList = QCoreApplication::arguments(); + + QTextStream standardOutput(stdout); + + if (argumentCount == 1) { + standardOutput << QObject::tr("Usage: %1 [baudrate]").arg(argumentList.first()) << endl; + return 1; + } + + QSerialPort serialPort; + QString serialPortName = argumentList.at(1); + serialPort.setPortName(serialPortName); + + if (!serialPort.open(QIODevice::WriteOnly)) { + standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + return 1; + } + + int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600; + if (!serialPort.setBaudRate(serialPortBaudRate)) { + standardOutput << QObject::tr("Failed to set 9600 baud for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + return 1; + } + + if (!serialPort.setDataBits(QSerialPort::Data8)) { + standardOutput << QObject::tr("Failed to set 8 data bits for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + return 1; + } + + if (!serialPort.setParity(QSerialPort::NoParity)) { + standardOutput << QObject::tr("Failed to set no parity for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + return 1; + } + + if (!serialPort.setStopBits(QSerialPort::OneStop)) { + standardOutput << QObject::tr("Failed to set 1 stop bit for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + return 1; + } + + if (!serialPort.setFlowControl(QSerialPort::NoFlowControl)) { + standardOutput << QObject::tr("Failed to set no flow control for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + return 1; + } + + QFile dataFile; + if (!dataFile.open(stdin, QIODevice::ReadOnly)) { + standardOutput << QObject::tr("Failed to open stdin for reading") << endl; + return 1; + } + + QByteArray writeData(dataFile.readAll()); + dataFile.close(); + + if (writeData.isEmpty()) { + standardOutput << QObject::tr("Either no data was currently available on the standard input for reading, or an error occurred for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + return 1; + } + + SerialPortWriter serialPortWriter(&serialPort); + serialPortWriter.write(writeData); + + return coreApplication.exec(); +} diff --git a/examples/serialport/cwriterasync/serialportwriter.cpp b/examples/serialport/cwriterasync/serialportwriter.cpp new file mode 100644 index 0000000..99de005 --- /dev/null +++ b/examples/serialport/cwriterasync/serialportwriter.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "serialportwriter.h" + +#include + +QT_USE_NAMESPACE + +SerialPortWriter::SerialPortWriter(QSerialPort *serialPort, QObject *parent) + : QObject(parent) + , m_serialPort(serialPort) + , m_standardOutput(stdout) + , m_bytesWritten(0) +{ + m_timer.setSingleShot(true); + connect(m_serialPort, SIGNAL(bytesWritten(qint64)), SLOT(handleBytesWritten(qint64))); + connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), SLOT(handleError(QSerialPort::SerialPortError))); + connect(&m_timer, SIGNAL(timeout()), SLOT(handleTimeout())); +} + +SerialPortWriter::~SerialPortWriter() +{ +} + +void SerialPortWriter::handleBytesWritten(qint64 bytes) +{ + m_bytesWritten += bytes; + if (m_bytesWritten == m_writeData.size()) { + m_bytesWritten = 0; + m_standardOutput << QObject::tr("Data successfully sent to port %1").arg(m_serialPort->portName()) << endl; + QCoreApplication::quit(); + } +} + +void SerialPortWriter::handleTimeout() +{ + m_standardOutput << QObject::tr("Operation timed out for port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl; + QCoreApplication::exit(1); +} + +void SerialPortWriter::handleError(QSerialPort::SerialPortError serialPortError) +{ + if (serialPortError == QSerialPort::WriteError) { + m_standardOutput << QObject::tr("An I/O error occurred while writing the data to port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl; + QCoreApplication::exit(1); + } +} + +void SerialPortWriter::write(const QByteArray &writeData) +{ + m_writeData = writeData; + + qint64 bytesWritten = m_serialPort->write(writeData); + + if (bytesWritten == -1) { + m_standardOutput << QObject::tr("Failed to write the data to port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl; + QCoreApplication::exit(1); + } else if (bytesWritten != m_writeData.size()) { + m_standardOutput << QObject::tr("Failed to write all the data to port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl; + QCoreApplication::exit(1); + } + + m_timer.start(5000); +} diff --git a/examples/serialport/cwriterasync/serialportwriter.h b/examples/serialport/cwriterasync/serialportwriter.h new file mode 100644 index 0000000..5c77879 --- /dev/null +++ b/examples/serialport/cwriterasync/serialportwriter.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SERIALPORTWRITER_H +#define SERIALPORTWRITER_H + +#include + +#include +#include +#include +#include + +QT_USE_NAMESPACE + +QT_BEGIN_NAMESPACE + +QT_END_NAMESPACE + +class SerialPortWriter : public QObject +{ + Q_OBJECT + +public: + SerialPortWriter(QSerialPort *serialPort, QObject *parent = 0); + ~SerialPortWriter(); + + void write(const QByteArray &writeData); + +private slots: + void handleBytesWritten(qint64 bytes); + void handleTimeout(); + void handleError(QSerialPort::SerialPortError error); + +private: + QSerialPort *m_serialPort; + QByteArray m_writeData; + QTextStream m_standardOutput; + qint64 m_bytesWritten; + QTimer m_timer; +}; + +#endif diff --git a/examples/serialport/doc/cwriterasync.qdoc b/examples/serialport/doc/cwriterasync.qdoc new file mode 100644 index 0000000..884435b --- /dev/null +++ b/examples/serialport/doc/cwriterasync.qdoc @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Laszlo Papp +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example cwriterasync + \title Command Line Writer Async Example + \ingroup qtserialport-examples + + The Command Line Writer Async example shows how to use the QSerialPort class + for sending data asynchronously over the selected serial port with the + desired settings. + + \image cwriterasync-example.png Screenshot of the Command Line Writer Async + example + + This command line writer async example sends data asynchronously over the + selected serial port in a console, provided by the QSerialPort class. + + For sending data asynchronously over the selected serial port, use the + \l{QIODevice::write()}{write()} method and + \l{QIODevice::bytesWritten()}{bytesWritten()} signal. +*/ diff --git a/examples/serialport/examples.qdoc b/examples/serialport/examples.qdoc index 17523a9..af73fc6 100644 --- a/examples/serialport/examples.qdoc +++ b/examples/serialport/examples.qdoc @@ -38,6 +38,7 @@ \li \l blockingmaster \li \l blockingslave \li \l cenumerator + \li \l cwriterasync \li \l cwritersync \li \l creadersync \li \l enumerator diff --git a/examples/serialport/serialport.pro b/examples/serialport/serialport.pro index ada85c8..57f6ec5 100644 --- a/examples/serialport/serialport.pro +++ b/examples/serialport/serialport.pro @@ -1,6 +1,6 @@ TEMPLATE = subdirs CONFIG += ordered -SUBDIRS = cenumerator creadersync cwritersync +SUBDIRS = cenumerator creadersync cwriterasync cwritersync greaterThan(QT_MAJOR_VERSION, 4) { !isEmpty(QT.widgets.name):SUBDIRS += enumerator terminal blockingmaster blockingslave } else { -- cgit v1.2.1