From 074909142926c18115fd4e1cca56171601b0f2bc Mon Sep 17 00:00:00 2001 From: NeoYang Date: Thu, 24 Dec 2020 11:26:46 +0800 Subject: [PATCH] [XingfengYang] #N/A first --- SerialPortReader.cpp | 43 ++++++++++++++++++++++----------------- SerialPortReader.h | 16 +++++++++++---- serialtool.cpp | 48 +++++++++++++++++++++++++++++++++++--------- serialtool.h | 11 +++++++++- serialtool.ui | 8 ++++---- 5 files changed, 90 insertions(+), 36 deletions(-) diff --git a/SerialPortReader.cpp b/SerialPortReader.cpp index 24f871f..16f502c 100644 --- a/SerialPortReader.cpp +++ b/SerialPortReader.cpp @@ -2,20 +2,31 @@ #include -SerialPortReader::SerialPortReader(QSerialPort *serialPort, std::function onDataHandler, - std::function onErrorHandler, - std::function onTimeHandler, QObject *parent) : - QObject(parent), - serialPort(serialPort), - onDataHandler(onDataHandler), - onErrorHandler(onErrorHandler), - onTimeHandler(onTimeHandler) +SerialPortReader::SerialPortReader(QObject *parent) : + QObject(parent){ + +} + +void SerialPortReader::init(QSerialPort *serialPort, std::function onDataHandler, std::function onErrorHandler, std::function onTimeHandler) { - connect(serialPort, &QSerialPort::readyRead, this, &SerialPortReader::handleReadyRead); - connect(serialPort, &QSerialPort::errorOccurred, this, &SerialPortReader::handleError); - connect(&timer, &QTimer::timeout, this, &SerialPortReader::handleTimeout); + this->serialPort = serialPort; + this->onDataHandler = onDataHandler; + this->onErrorHandler = onErrorHandler; + this->onTimeHandler = onTimeHandler; +} - timer.start(5000); +void SerialPortReader::conn(int sec) +{ + connect(this->serialPort, &QSerialPort::readyRead, this, &SerialPortReader::handleReadyRead); + connect(this->serialPort, &QSerialPort::errorOccurred, this, &SerialPortReader::handleError); + connect(&this->timer, &QTimer::timeout, this, &SerialPortReader::handleTimeout); + this->waitTime = sec; + this->timer.start(sec); +} + +void SerialPortReader::send(QString command) +{ + serialPort->write(command.toUtf8()); } void SerialPortReader::handleReadyRead() @@ -25,17 +36,13 @@ void SerialPortReader::handleReadyRead() this->onDataHandler(dd); if (!timer.isActive()){ - timer.start(5000); + timer.start(this->waitTime); } } void SerialPortReader::handleTimeout() { - if (readData.isEmpty()) { - this->onTimeHandler(); - } else { - this->onDataHandler(readData); - } +// this->onTimeHandler(this->waitTime); } void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError) diff --git a/SerialPortReader.h b/SerialPortReader.h index a35ce21..5be9cad 100644 --- a/SerialPortReader.h +++ b/SerialPortReader.h @@ -16,9 +16,15 @@ class SerialPortReader : public QObject Q_OBJECT public: - explicit SerialPortReader(QSerialPort *serialPort, std::function onDataHandler, - std::function onErrorHandler, - std::function onTimeHandler, QObject *parent = nullptr); + explicit SerialPortReader(QObject *parent = nullptr); + + void init(QSerialPort *serialPort, std::function onDataHandler, + std::function onErrorHandler, + std::function onTimeHandler); + + void conn(int sec); + + void send(QString command); private slots: void handleReadyRead(); @@ -30,9 +36,11 @@ private slots: QByteArray readData; QTimer timer; + int waitTime = 0; + std::function onDataHandler; std::function onErrorHandler; - std::function onTimeHandler; + std::function onTimeHandler; }; #endif // SERIALPORTREADER_H diff --git a/serialtool.cpp b/serialtool.cpp index 9fe09c8..8b3be46 100644 --- a/serialtool.cpp +++ b/serialtool.cpp @@ -118,7 +118,7 @@ void SerialTool::on_connectionConnectBtn_clicked() QSerialPortInfo selectedInfo = this->serialList.at(selectederialPort); - if (!this->serialPort.open(QIODevice::ReadOnly)) { + if (!this->serialPort.open(QIODevice::ReadWrite)) { this->ui->recvViewer->setHtml("

Failed to open port!

"); }else{ this->ui->connectionConnectBtn->setEnabled(false); @@ -133,10 +133,11 @@ void SerialTool::on_connectionConnectBtn_clicked() this->ui->stopBits->setEnabled(false); this->ui->connectionRefreshBtn->setEnabled(false); - SerialPortReader serialPortReader(&this->serialPort, - std::bind(&SerialTool::onDataReceived,this,std::placeholders::_1), - std::bind(&SerialTool::onSerialError,this,std::placeholders::_1), - std::bind(&SerialTool::onTimeout,this)); + serialPortReader.init(&this->serialPort, + std::bind(&SerialTool::onDataReceived,this,std::placeholders::_1), + std::bind(&SerialTool::onSerialError,this,std::placeholders::_1), + std::bind(&SerialTool::onTimeout,this,std::placeholders::_1)); + serialPortReader.conn(5000); } } @@ -227,17 +228,46 @@ void SerialTool::on_connectionCloseBtn_clicked() this->ui->stopBits->setEnabled(true); } +void SerialTool::on_commandClearBtn_clicked() +{ + this->ui->command->clear(); +} + void SerialTool::onDataReceived(QByteArray data) { - this->ui->recvViewer->append("data recv."); + QString str; + str.append("

"); + str.append(QString(data)); + str.append("

"); + this->ui->recvViewer->append(str); } -void SerialTool::onTimeout() +void SerialTool::onTimeout(int sec) { - this->ui->recvViewer->append("time out."); + QString str; + str.append("

"); + str.append(QString(sec/1000)); + str.append(" seconds no data."); + str.append("

"); + this->ui->recvViewer->append(str); } void SerialTool::onSerialError(QSerialPort::SerialPortError serialPortError) { - this->ui->recvViewer->append("error."); + QString str; + str.append("

"); + str.append("error"); + str.append("

"); + this->ui->recvViewer->append(str); +} + +void SerialTool::on_sendBtn_clicked() +{ + QString text = this->ui->command->toPlainText(); + this->serialPortReader.send(text); +} + +void SerialTool::on_recvClearBtn_clicked() +{ + this->ui->recvViewer->clear(); } diff --git a/serialtool.h b/serialtool.h index 2f217fe..07d0658 100644 --- a/serialtool.h +++ b/serialtool.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace Ui { class SerialTool; @@ -34,6 +35,12 @@ private slots: void on_connectionCloseBtn_clicked(); + void on_commandClearBtn_clicked(); + + void on_sendBtn_clicked(); + + void on_recvClearBtn_clicked(); + private: Ui::SerialTool *ui; @@ -41,6 +48,8 @@ private slots: QSerialPort serialPort; + SerialPortReader serialPortReader; + bool connectionState = false; void initUi(); @@ -48,7 +57,7 @@ private slots: void initSerialPorts(); void onDataReceived(QByteArray data); - void onTimeout(); + void onTimeout(int sec); void onSerialError(QSerialPort::SerialPortError serialPortError); void chanageConnectButtonState(); diff --git a/serialtool.ui b/serialtool.ui index ccf8cd2..5fea9de 100644 --- a/serialtool.ui +++ b/serialtool.ui @@ -406,7 +406,7 @@ 命令: - + 254 @@ -419,7 +419,7 @@ 发送 - + 10 @@ -429,7 +429,7 @@ - + 4 @@ -465,7 +465,7 @@ - + 5