Skip to content

Commit

Permalink
[XingfengYang] #N/A first
Browse files Browse the repository at this point in the history
  • Loading branch information
nerososft committed Dec 24, 2020
1 parent 369e61f commit 0749091
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 36 deletions.
43 changes: 25 additions & 18 deletions SerialPortReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@

#include <QCoreApplication>

SerialPortReader::SerialPortReader(QSerialPort *serialPort, std::function<void(QByteArray)> onDataHandler,
std::function<void(QSerialPort::SerialPortError)> onErrorHandler,
std::function<void()> 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<void (QByteArray)> onDataHandler, std::function<void (QSerialPort::SerialPortError)> onErrorHandler, std::function<void (int)> 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()
Expand All @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions SerialPortReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ class SerialPortReader : public QObject
Q_OBJECT

public:
explicit SerialPortReader(QSerialPort *serialPort, std::function<void(QByteArray)> onDataHandler,
std::function<void(QSerialPort::SerialPortError)> onErrorHandler,
std::function<void()> onTimeHandler, QObject *parent = nullptr);
explicit SerialPortReader(QObject *parent = nullptr);

void init(QSerialPort *serialPort, std::function<void(QByteArray)> onDataHandler,
std::function<void(QSerialPort::SerialPortError)> onErrorHandler,
std::function<void(int)> onTimeHandler);

void conn(int sec);

void send(QString command);

private slots:
void handleReadyRead();
Expand All @@ -30,9 +36,11 @@ private slots:
QByteArray readData;
QTimer timer;

int waitTime = 0;

std::function<void(QByteArray)> onDataHandler;
std::function<void(QSerialPort::SerialPortError)> onErrorHandler;
std::function<void()> onTimeHandler;
std::function<void(int)> onTimeHandler;
};

#endif // SERIALPORTREADER_H
48 changes: 39 additions & 9 deletions serialtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("<p style=\"color:'red'\">Failed to open port!</p>");
}else{
this->ui->connectionConnectBtn->setEnabled(false);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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("<p style=\"color:'green';padding:0;margin:0\">");
str.append(QString(data));
str.append("</p>");
this->ui->recvViewer->append(str);
}

void SerialTool::onTimeout()
void SerialTool::onTimeout(int sec)
{
this->ui->recvViewer->append("time out.");
QString str;
str.append("<p style=\"color:'orange';padding:0;margin:0\">");
str.append(QString(sec/1000));
str.append(" seconds no data.");
str.append("</p>");
this->ui->recvViewer->append(str);
}

void SerialTool::onSerialError(QSerialPort::SerialPortError serialPortError)
{
this->ui->recvViewer->append("error.");
QString str;
str.append("<p style=\"color:'red';padding:0;margin:0\">");
str.append("error");
str.append("</p>");
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();
}
11 changes: 10 additions & 1 deletion serialtool.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QDockWidget>
#include <QSerialPortInfo>
#include <QSerialPort>
#include <SerialPortReader.h>

namespace Ui {
class SerialTool;
Expand Down Expand Up @@ -34,21 +35,29 @@ private slots:

void on_connectionCloseBtn_clicked();

void on_commandClearBtn_clicked();

void on_sendBtn_clicked();

void on_recvClearBtn_clicked();

private:
Ui::SerialTool *ui;

QList<QSerialPortInfo> serialList;

QSerialPort serialPort;

SerialPortReader serialPortReader;

bool connectionState = false;

void initUi();

void initSerialPorts();

void onDataReceived(QByteArray data);
void onTimeout();
void onTimeout(int sec);
void onSerialError(QSerialPort::SerialPortError serialPortError);

void chanageConnectButtonState();
Expand Down
8 changes: 4 additions & 4 deletions serialtool.ui
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@
<property name="title">
<string>命令:</string>
</property>
<widget class="QPushButton" name="commandSendBtn_4">
<widget class="QPushButton" name="sendBtn">
<property name="geometry">
<rect>
<x>254</x>
Expand All @@ -419,7 +419,7 @@
<string>发送</string>
</property>
</widget>
<widget class="QPlainTextEdit" name="plainTextEdit_2">
<widget class="QPlainTextEdit" name="command">
<property name="geometry">
<rect>
<x>10</x>
Expand All @@ -429,7 +429,7 @@
</rect>
</property>
</widget>
<widget class="QPushButton" name="commandSendBtn_5">
<widget class="QPushButton" name="commandClearBtn">
<property name="geometry">
<rect>
<x>4</x>
Expand Down Expand Up @@ -465,7 +465,7 @@
</rect>
</property>
</widget>
<widget class="QPushButton" name="commandSendBtn_6">
<widget class="QPushButton" name="recvClearBtn">
<property name="geometry">
<rect>
<x>5</x>
Expand Down

0 comments on commit 0749091

Please sign in to comment.