1 概述
XMODEM协议是一种使用拨号调制解调器的个人计算机通信中广泛使用的异步文件运输协议。这种协议以128字节块的形式传输数据,并且每个块都使用一个校验和过程来进行错误检测。使用循环冗余校验的与XMODEM相应的一种协议称为XMODEM-CRC。还有一种是XMODEM-1K,它以1024字节一块来传输数据。YMODEM也是一种XMODEM的实现。它包括XMODEM-1K的所有特征,另外在一次单一会话期间为发送一组文件,增加了批处理文件传输模式。
本文利用C++实现XYModem-1K协议,并利用Qt串口类QSerialPort实现数据读写。
3 实现
YModemFileSender_5">3.6 XYModemFileSender
该模块实现XYModem协议在线程中异步发送文件。
YModemFileSender_8">3.6.1 XYModemFileSender定义
class QSerialPort;
class XYModemSendFile;
class XYModemFileSender : public QObject
{Q_OBJECT
public:explicit XYModemFileSender(QSerialPort *serial, bool isYModem = true,QObject *parent = nullptr);~XYModemFileSender();signals:void start_send(QString const& fileName);void stop_send();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;XYModemSendFile* worker_;
};
公共接口:
- start 开始XYModem协议发送文件
- stop 停止发送
- cancel 取消发送
信号:
- gotFileSize 文件大小信号
- progressInfo 传输进度信号
- error 出错信号
- finished 传输结束信号
YModemFileSender_49">3.6.2 XYModemFileSender实现
3.6.2.1 构造函数/析构函数
XYModemFileSender::XYModemFileSender(QSerialPort *serial, bool isYModem, QObject *parent): QObject(parent)
{XYModemSendFile* worker = new XYModemSendFile(serial);serial->moveToThread(&workerThread);worker->moveToThread(&workerThread);connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);if(isYModem)connect(this, &XYModemFileSender::start_send, worker, &XYModemSendFile::startYModem);elseconnect(this, &XYModemFileSender::start_send, worker, &XYModemSendFile::startXModem);connect(this, &XYModemFileSender::stop_send, worker, &XYModemSendFile::stop);connect(this, &XYModemFileSender::cancel_send, worker, &XYModemSendFile::cancel);connect(worker, &XYModemSendFile::gotFileSize, this, &XYModemFileSender::gotFileSize);connect(worker, &XYModemSendFile::progressInfo, this, &XYModemFileSender::progressInfo);connect(worker, &XYModemSendFile::finished, this, &XYModemFileSender::finished);connect(worker, &XYModemSendFile::error, this, &XYModemFileSender::error);worker_ = worker;workerThread.start();
}XYModemFileSender::~XYModemFileSender()
{workerThread.quit();workerThread.wait();
}
函数说明:
- 构造函数
- 析构函数
- 退出线程
- 等待线程退出
3.6.2.2 start/stop/cancel
void XYModemFileSender::start(QString const& fileName)
{emit start_send(fileName);
}void XYModemFileSender::stop()
{worker_->stop();
}void XYModemFileSender::cancel()
{worker_->cancel();
}
函数说明: