summaryrefslogtreecommitdiff
path: root/examples/serialport/creaderasync
diff options
context:
space:
mode:
Diffstat (limited to 'examples/serialport/creaderasync')
-rw-r--r--examples/serialport/creaderasync/main.cpp26
-rw-r--r--examples/serialport/creaderasync/serialportreader.cpp32
-rw-r--r--examples/serialport/creaderasync/serialportreader.h17
3 files changed, 37 insertions, 38 deletions
diff --git a/examples/serialport/creaderasync/main.cpp b/examples/serialport/creaderasync/main.cpp
index dd1f755..6c669ce 100644
--- a/examples/serialport/creaderasync/main.cpp
+++ b/examples/serialport/creaderasync/main.cpp
@@ -50,37 +50,39 @@
#include "serialportreader.h"
-#include <QtSerialPort/QSerialPort>
-
-#include <QTextStream>
#include <QCoreApplication>
-#include <QFile>
+#include <QSerialPort>
#include <QStringList>
-
-QT_USE_NAMESPACE
+#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication coreApplication(argc, argv);
- int argumentCount = QCoreApplication::arguments().size();
- QStringList argumentList = QCoreApplication::arguments();
+ const int argumentCount = QCoreApplication::arguments().size();
+ const QStringList argumentList = QCoreApplication::arguments();
QTextStream standardOutput(stdout);
if (argumentCount == 1) {
- standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]").arg(argumentList.first()) << endl;
+ standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]")
+ .arg(argumentList.first())
+ << endl;
return 1;
}
QSerialPort serialPort;
- QString serialPortName = argumentList.at(1);
+ const QString serialPortName = argumentList.at(1);
serialPort.setPortName(serialPortName);
- int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600;
+ const int serialPortBaudRate = (argumentCount > 2)
+ ? argumentList.at(2).toInt() : QSerialPort::Baud9600;
serialPort.setBaudRate(serialPortBaudRate);
if (!serialPort.open(QIODevice::ReadOnly)) {
- standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl;
+ standardOutput << QObject::tr("Failed to open port %1, error: %2")
+ .arg(serialPortName)
+ .arg(serialPort.errorString())
+ << endl;
return 1;
}
diff --git a/examples/serialport/creaderasync/serialportreader.cpp b/examples/serialport/creaderasync/serialportreader.cpp
index f85dd8a..a701839 100644
--- a/examples/serialport/creaderasync/serialportreader.cpp
+++ b/examples/serialport/creaderasync/serialportreader.cpp
@@ -52,25 +52,18 @@
#include <QCoreApplication>
-QT_USE_NAMESPACE
-
-SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent)
- : QObject(parent)
- , m_serialPort(serialPort)
- , m_standardOutput(stdout)
+SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent) :
+ QObject(parent),
+ m_serialPort(serialPort),
+ m_standardOutput(stdout)
{
connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::handleReadyRead);
- connect(m_serialPort, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error),
- this, &SerialPortReader::handleError);
+ connect(m_serialPort, &QSerialPort::errorOccurred, this, &SerialPortReader::handleError);
connect(&m_timer, &QTimer::timeout, this, &SerialPortReader::handleTimeout);
m_timer.start(5000);
}
-SerialPortReader::~SerialPortReader()
-{
-}
-
void SerialPortReader::handleReadyRead()
{
m_readData.append(m_serialPort->readAll());
@@ -82,9 +75,14 @@ void SerialPortReader::handleReadyRead()
void SerialPortReader::handleTimeout()
{
if (m_readData.isEmpty()) {
- m_standardOutput << QObject::tr("No data was currently available for reading from port %1").arg(m_serialPort->portName()) << endl;
+ m_standardOutput << QObject::tr("No data was currently available "
+ "for reading from port %1")
+ .arg(m_serialPort->portName())
+ << endl;
} else {
- m_standardOutput << QObject::tr("Data successfully received from port %1").arg(m_serialPort->portName()) << endl;
+ m_standardOutput << QObject::tr("Data successfully received from port %1")
+ .arg(m_serialPort->portName())
+ << endl;
m_standardOutput << m_readData << endl;
}
@@ -94,7 +92,11 @@ void SerialPortReader::handleTimeout()
void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError)
{
if (serialPortError == QSerialPort::ReadError) {
- m_standardOutput << QObject::tr("An I/O error occurred while reading the data from port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
+ m_standardOutput << QObject::tr("An I/O error occurred while reading "
+ "the data from port %1, error: %2")
+ .arg(m_serialPort->portName())
+ .arg(m_serialPort->errorString())
+ << endl;
QCoreApplication::exit(1);
}
}
diff --git a/examples/serialport/creaderasync/serialportreader.h b/examples/serialport/creaderasync/serialportreader.h
index 6698087..53074f1 100644
--- a/examples/serialport/creaderasync/serialportreader.h
+++ b/examples/serialport/creaderasync/serialportreader.h
@@ -51,14 +51,10 @@
#ifndef SERIALPORTREADER_H
#define SERIALPORTREADER_H
-#include <QtSerialPort/QSerialPort>
-
+#include <QByteArray>
+#include <QSerialPort>
#include <QTextStream>
#include <QTimer>
-#include <QByteArray>
-#include <QObject>
-
-QT_USE_NAMESPACE
QT_BEGIN_NAMESPACE
@@ -70,7 +66,6 @@ class SerialPortReader : public QObject
public:
explicit SerialPortReader(QSerialPort *serialPort, QObject *parent = nullptr);
- ~SerialPortReader();
private slots:
void handleReadyRead();
@@ -78,10 +73,10 @@ private slots:
void handleError(QSerialPort::SerialPortError error);
private:
- QSerialPort *m_serialPort;
- QByteArray m_readData;
+ QSerialPort *m_serialPort = nullptr;
+ QByteArray m_readData;
QTextStream m_standardOutput;
- QTimer m_timer;
+ QTimer m_timer;
};
-#endif
+#endif // SERIALPORTREADER_H