QT 读取 bin 文件
书接上回,bootloader 升级都做了,自然要用 QT 实现一下啦
Bootloader开发流程
https://zhuanlan.zhihu.com/p/647128903
# 选择文件
拿到路径
QString updatafilepath;
void MainWindow::on_pushButton_updata_seletbin_clicked()
{
QString filename = QFileDialog::getOpenFileName(
this,
tr("请选择升级包的Bin文件"),
QDir::currentPath(),
"Bin files (*.bin);;All files(*.*)");
updatafilepath=getFileInfo(filename);
}
# 获取信息
获得文件基本信息
QString MainWindow::getFileInfo(const QString fileFullName)
{
QStringList slFileInfo;
if(!fileFullName.isEmpty())
{
QFileInfo fileInfo(fileFullName);
slFileInfo<< tr("File Path: ") + fileInfo.absolutePath();
slFileInfo<< tr("File Full Name: ") + fileInfo.fileName();
ui->textBrowser->append("File Path: "+fileInfo.absolutePath()+"/"+fileInfo.fileName()+"\n");
//---------------拿到文件的路径-----------------------------------
updatafilepath=fileInfo.absolutePath()+"/"+fileInfo.fileName();
ui->lineEdit_updata->setText(updatafilepath);
//---------------拿到文件的路径-----------------------------------
slFileInfo<< tr("File Base Name: ") + fileInfo.baseName();
slFileInfo<< tr("File Type: ") + fileInfo.suffix();
qint64 size = fileInfo.size();
size = size / 1024;
slFileInfo<< tr("File Size: ") + QString::number(size) + "KB";
ui->textBrowser->append("File Size: "+QString::number(size) + "KB"+"\n");
QDateTime timeInfo = fileInfo.created();
slFileInfo<< tr("Create Time: ") + timeInfo.toString("yyyy-MM-dd hh:mm:ss");
slFileInfo<< tr("Modifed Time: ") + fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss");
slFileInfo<< tr("Latest Read: ") + fileInfo.lastRead().toString("yyyy-MM-dd hh:mm:ss");
QString strInfo = slFileInfo.join('\n');
QMessageBox::information(this,tr("File Info"),strInfo);
}
//读取文件
OnReadBin(updatafilepath);
return updatafilepath;
}
# 读取数据
QFile updatafile;
long lastLen,fileLen,sendCnt;
volatile uint8_t myupdatafilepBuf[64*1024];
const int PACKET_SIZE = 768;//每次发送的字节数
void MainWindow::OnReadBin(QString filepathstr)
{
if(filepathstr==""){
QMessageBox::information(this, tr("提示消息"), tr("请先选择正确的Bin文件!"), QMessageBox::Ok);
return;
}
QString filePath(filepathstr);
updatafile.setFileName(filePath);
QFileInfo fileInfo(filePath);
//数据信息计算
fileLen = fileInfo.size();//待发送文件大小
sendCnt = (int)ceil(fileLen / (PACKET_SIZE * 1.0));//发送次数
lastLen = fileLen % PACKET_SIZE;//最后一次发送的大小
if (0 == lastLen) {//恰巧是PACKET_SIZE的整数倍
lastLen = PACKET_SIZE;
}
qDebug() << "fileLen,lastLen,sendCnt:" << fileLen << lastLen << sendCnt;
//----------------------------读取---------------------------------
if (updatafile.open(QIODevice::ReadOnly)){
QDataStream dataStream(&updatafile);
dataStream.readRawData((char *)myupdatafilepBuf, fileLen);
}
updatafile.close();
}
读出来之后就靠大家的发挥啦,我的业务场景是:读出来后拆包,每 768 字节是一包,分包发送给下位机。下位机接收完成后进行升级。