一、serialportreader.h
#ifndef SERIALPORTREADER_H
#define SERIALPORTREADER_H
#include <QByteArray>
#include <QSerialPort>
#include <QTextStream>
#include <QTimer>
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
class SerialPortReader : public QObject
{
Q_OBJECT
public:
explicit SerialPortReader(QSerialPort *serialPort, QObject *parent = nullptr);
private slots:
void handleReadyRead();
void handleTimeout();
void handleError(QSerialPort::SerialPortError error);
private:
QSerialPort *m_serialPort = nullptr;
QByteArray m_readData;
QTextStream m_standardOutput;
QTimer m_timer;
};
#endif // SERIALPORTREADER_H
二、serialportreader.cpp
#include "serialportreader.h"
#include <QCoreApplication>
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, &QSerialPort::errorOccurred, this, &SerialPortReader::handleError);
connect(&m_timer, &QTimer::timeout, this, &SerialPortReader::handleTimeout);
m_timer.start(5000);
}
void SerialPortReader::handleReadyRead()
{
m_readData.append(m_serialPort->readAll());
if (!m_timer.isActive())
m_timer.start(5000);
}
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;
} else {
m_standardOutput << QObject::tr("Data successfully received from port %1")
.arg(m_serialPort->portName())
<< endl;
m_standardOutput << m_readData << endl;
}
}
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;
QCoreApplication::exit(1);
}
}
三、main.cpp(调用串口)
#include "serialportreader.h"
#include <QCoreApplication>
#include <QSerialPort>
#include <QStringList>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication coreApplication(argc, argv);
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;
//return 1;
}
QSerialPort serialPort;
const QString serialPortName = "COM1";//argumentList.at(1);
serialPort.setPortName(serialPortName);
const int serialPortBaudRate = QSerialPort::Baud9600;//(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;
return 1;
}
SerialPortReader serialPortReader(&serialPort);
return coreApplication.exec();
}