本文主要是介绍Qt实现Kermit协议(五),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
3 实现
3.4 KermitFileSender
该模块实现了Kermit异步发送文件功能。
3.4.1 KermitFileSender定义
class QSerialPort;
class KermitSendFile;
class KermitFileSender : public QObject
{Q_OBJECT
public:explicit KermitFileSender(QSerialPort *serial, QObject *parent = nullptr);~KermitFileSender();signals:void start_send(QString const& fileName);void cancel_send();void gotFileSize(quint64 filesize);void progressInfo(quint32 blockNumber, quint64 bytesOfSend);void finished();void error(QString const& e);
public:void start(QString const& fileName);void stop();void cancel();
private:QThread workerThread;KermitSendFile* worker_;
};
公共接口:
- start 开始发送文件
- stop 停止传输文件
- cancel 中断传输文件
信号:
- gotFileSize 文件大小信号
- progressInfo 传输进度信号
- error 出错信号
- finished 传输结束信号
- start_send 开始发送文件信号
- cancel_send 中断传输文件信号
3.4.2 KermitFileSender实现
3.4.2.1 构造函数/析构函数
KermitFileSender::KermitFileSender(QSerialPort *serial, QObject *parent): QObject(parent)
{KermitSendFile* worker = new KermitSendFile(serial);serial->moveToThread(&workerThread);worker->moveToThread(&workerThread);connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);connect(this, &KermitFileSender::start_send, worker, &KermitSendFile::start);connect(this, &KermitFileSender::cancel_send, worker, &KermitSendFile::cancel);connect(worker, &KermitSendFile::gotFileSize, this, &KermitFileSender::gotFileSize);connect(worker, &KermitSendFile::progressInfo, this, &KermitFileSender::progressInfo);connect(worker, &KermitSendFile::finished, this, &KermitFileSender::finished);connect(worker, &KermitSendFile::error, this, &KermitFileSender::error);worker_ = worker;workerThread.start();
}KermitFileSender::~KermitFileSender()
{workerThread.quit();workerThread.wait();
}
函数说明:
- 构造函数
- 将serial和worker移动到workerThread线程
- 连接信号和槽函数
- 启动线程
- 析构函数
- 退出线程
- 等待线程退出
3.4.2.2 start/stop/cancel
KermitFileSender::~KermitFileSender()
{workerThread.quit();workerThread.wait();
}void KermitFileSender::start(QString const& fileName)
{emit start_send(fileName);
}void KermitFileSender::stop()
{worker_->stop();
}void KermitFileSender::cancel()
{emit cancel_send();
}
函数说明:
- start 发送信号start_send开始文件传输
- stop 调用work函数stop停止操作
- cancel 发送信号cancel_send取消文件传输操作
Qt实现Kermit协议(四) Qt实现Kermit协议(六)
这篇关于Qt实现Kermit协议(五)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!