增加进度条显示功能

This commit is contained in:
Yu-886 2025-03-27 17:52:24 +08:00
parent 99929bb48c
commit aba79f8209
2 changed files with 96 additions and 41 deletions

View File

@ -281,31 +281,20 @@ typedef struct Device_info_
QByteArray rxBuffer; QByteArray rxBuffer;
void MainWindow::onReadyRead() void MainWindow::parseDeviceInfo(const QByteArray &data)
{ {
rxBuffer.append(serial->readAll()); // 追加读取到的数据 if (data.size() < static_cast<int>(sizeof(Device_info_t))) return;
// 按帧头查找
while (true) {
int headIndex = rxBuffer.indexOf(QByteArray::fromHex("55AA"));
if (headIndex == -1 || rxBuffer.size() < headIndex + 6 + sizeof(Device_info_t) + 2) {
// 找不到头 或者数据不足一整帧
break;
}
// 如果找到帧头并且数据够
Device_info_t device_tool; Device_info_t device_tool;
memcpy(&device_tool, rxBuffer.constData() + headIndex + 6, sizeof(Device_info_t)); memcpy(&device_tool, data.constData(), sizeof(Device_info_t));
// 打印 MAC_WIFI qDebug() << "Device_MAC_WIFI:" << QByteArray((const char*)device_tool.Device_MAC_WIFI, 6).toHex(' ').toUpper();
qDebug() << "Device_MAC_WIFI:"
<< QByteArray((const char*)device_tool.Device_MAC_WIFI, 6).toHex(' ').toUpper();
ui->textBrowser->append("SN:" + QByteArray((const char*)device_tool.Device_SN, 10).toUpper()); ui->textBrowser->append("SN:" + QByteArray((const char*)device_tool.Device_SN, 10).toUpper());
ui->textBrowser->append("Flash ID: 0x" + QString::number(device_tool.flash_id, 16).toUpper()); ui->textBrowser->append("Flash ID: 0x" + QString::number(device_tool.flash_id, 16).toUpper());
ui->textBrowser->append("MAC:" + QByteArray((const char*)device_tool.Device_MAC_WIFI, 6).toHex('-').toUpper()); ui->textBrowser->append("MAC:" + QByteArray((const char*)device_tool.Device_MAC_WIFI, 6).toHex('-').toUpper());
// 打印文件列表 // 解析文件列表
QList<File_directoryData> dataList; QList<File_directoryData> dataList;
ui->textBrowser->append("Files:"); ui->textBrowser->append("Files:");
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
@ -314,12 +303,50 @@ void MainWindow::onReadyRead()
} }
} }
Mirror_directory_init(ui->tableView_2, dataList); Mirror_directory_init(ui->tableView_2, dataList);
}
void MainWindow::parseTransferProgress(const QByteArray &data)
{
if (data.size() < 1) return;
int progress = static_cast<uint8_t>(data[0]); // 避免显示成字符
qDebug() << "传输进度:" << progress;
ui->progressBar->setValue(qMin(progress, 100)); // 限制最大值 100
}
void MainWindow::onReadyRead()
{
rxBuffer.append(serial->readAll()); // 追加读取到的数据
while (!rxBuffer.isEmpty()) {
// 解析设备信息
int headIndex = rxBuffer.indexOf(QByteArray::fromHex("55AA0101"));
if (headIndex != -1 && rxBuffer.size() >= headIndex + 6 + sizeof(Device_info_t) + 2) {
parseDeviceInfo(rxBuffer.mid(headIndex + 6, sizeof(Device_info_t)));
// 移除已处理部分 // 移除已处理部分
rxBuffer.remove(0, headIndex + 6 + sizeof(Device_info_t) + 2); rxBuffer.remove(0, headIndex + 6 + sizeof(Device_info_t) + 2);
continue; // 继续处理下一个数据包
}
// 解析传输进度
headIndex = rxBuffer.indexOf(QByteArray::fromHex("55AA0204"));
if (headIndex != -1 && rxBuffer.size() >= headIndex + 9) {
parseTransferProgress(rxBuffer.mid(headIndex + 6, 1));
// 移除已处理部分
rxBuffer.remove(0, headIndex + 9);
continue;
}
// 如果没有可处理的数据,退出循环
break;
} }
} }
void MainWindow::on_pushButton_4_clicked() void MainWindow::on_pushButton_4_clicked()
{ {
// 获取选择模型 // 获取选择模型
@ -393,25 +420,17 @@ void MainWindow::on_pushButton_3_clicked()
QMessageBox::warning(this, "提示", "请输入文件路径!"); QMessageBox::warning(this, "提示", "请输入文件路径!");
return; return;
} }
qDebug() << "filePath:" << filePath; qDebug() << "filePath:" << filePath;
QString outputFilePath = "D:/decrypt.bin";
QByteArray macAddress = QByteArray::fromHex("A1B2C3D4E5F6");
FileEncryptor encryptor(macAddress);
if (encryptor.encryptDecryptFile(filePath, outputFilePath)) {
qDebug() << "文件解密成功!";
} else {
qDebug() << "文件加密失败!";
return;
}
// 提取文件名(不包含路径) // 提取文件名(不包含路径)
QFileInfo fileInfo(filePath); QFileInfo fileInfo(filePath);
QString baseFileName = fileInfo.fileName(); QString baseFileName = fileInfo.fileName();
qDebug() << "文件:" << baseFileName; qDebug() << "文件:" << baseFileName;
// 获取文件大小
qint64 fileSize = fileInfo.size();
qDebug() << "文件大小:" << fileSize;
// 将文件名转换为字节数组 // 将文件名转换为字节数组
QByteArray fileNameBytes = baseFileName.toUtf8(); QByteArray fileNameBytes = baseFileName.toUtf8();
@ -422,17 +441,27 @@ void MainWindow::on_pushButton_3_clicked()
QByteArray data; QByteArray data;
data.append(reinterpret_cast<char*>(Protocol_header), 6); // 添加固定数据 data.append(reinterpret_cast<char*>(Protocol_header), 6); // 添加固定数据
// 追加文件大小(4 字节,小端序)
data.append(static_cast<char>(fileSize & 0xFF));
data.append(static_cast<char>((fileSize >> 8) & 0xFF));
data.append(static_cast<char>((fileSize >> 16) & 0xFF));
data.append(static_cast<char>((fileSize >> 24) & 0xFF));
// 将文件名字节拷贝到指定位置 // 将文件名字节拷贝到指定位置
int fileNameLength = qMin(fileNameBytes.size(), 48); // 文件名最大长度为48 int fileNameLength = qMin(fileNameBytes.size(), 48); // 文件名最大长度为48
data.append(fileNameBytes.constData(), fileNameLength); //添加文件名 data.append(fileNameBytes.constData(), fileNameLength); //添加文件名
qDebug() << "最终数据包长度:" << data.size();
// 计算数据包的长度(去掉开头的0xAA, 0x55, 0x02, 0x01等) // 计算数据包的长度(去掉开头的0xAA, 0x55, 0x02, 0x01等)
uint16_t dataLength = data.size() - 4; // 减去开头的4个字节(0xAA, 0x55, 0x02, 0x01) uint16_t dataLength = data.size() - 4; // 减去开头的4个字节(0xAA, 0x55, 0x02, 0x01)
data[5] = dataLength & 0xFF; // 设置低字节 data[5] = dataLength & 0xFF; // 设置低字节
data[4] = (dataLength >> 8) & 0xFF; // 设置高字节 data[4] = (dataLength >> 8) & 0xFF; // 设置高字节
qDebug() << "dataLength:" << dataLength; qDebug() << "dataLength:" << dataLength;
// 计算CRC16 Modbus值 // 计算CRC16 Modbus值
uint16_t crcValue = crc16_modbus(reinterpret_cast<const uint8_t*>(fileNameBytes.constData()), fileNameLength); uint16_t crcValue = crc16_modbus(reinterpret_cast<const uint8_t*>(data.constData() + 6), dataLength - 2);
qDebug() << "fileNameLength:" << fileNameLength << " crcValue:" << crcValue; qDebug() << "fileNameLength:" << fileNameLength << " crcValue:" << crcValue;
// 将 CRC 添加到数据包 // 将 CRC 添加到数据包
data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue)); // 添加 CRC16 data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue)); // 添加 CRC16
@ -444,10 +473,13 @@ void MainWindow::on_pushButton_3_clicked()
qDebug() << "串口未打开"; qDebug() << "串口未打开";
} }
data.clear(); data.clear();
// return;
// 创建线程和工作对象 // 创建线程和工作对象
QThread *thread = new QThread; QThread *thread = new QThread;
FileSenderWorker *worker = new FileSenderWorker(outputFilePath);
FileSenderWorker *worker = new FileSenderWorker(filePath);//未加密文件
worker->moveToThread(thread); worker->moveToThread(thread);
connect(thread, &QThread::started, worker, &FileSenderWorker::startSend); connect(thread, &QThread::started, worker, &FileSenderWorker::startSend);
@ -637,3 +669,22 @@ void MainWindow::on_pushButton_8_clicked()
} }
} }
void MainWindow::on_pushButton_9_clicked()//在线烧录
{
uint8_t Protocol_header[8] = {0xAA, 0x55, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00}; // 固定部分
// 构造数据包
QByteArray data;
data.append(reinterpret_cast<char*>(Protocol_header), 8); // 添加固定数据
// 发送数据
if (serial->isOpen()) {
ui->textBrowser->clear();
serial->write(data);
qDebug() << "数据发送成功";
} else {
qDebug() << "串口未打开";
}
}

View File

@ -36,6 +36,8 @@ private slots:
void Get_Device_info(); void Get_Device_info();
void setButtonColor(QPushButton *button, const QString &color); void setButtonColor(QPushButton *button, const QString &color);
void Mirror_directory_init(QTableView *tableView, const QList<File_directoryData> &dataList); void Mirror_directory_init(QTableView *tableView, const QList<File_directoryData> &dataList);
void parseDeviceInfo(const QByteArray &data);
void parseTransferProgress(const QByteArray &data);
void on_pushButton_4_clicked(); void on_pushButton_4_clicked();
void on_pushButton_3_clicked(); void on_pushButton_3_clicked();
@ -48,6 +50,8 @@ private slots:
void on_pushButton_8_clicked(); void on_pushButton_8_clicked();
void on_pushButton_9_clicked();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QSerialPort *serial; // 将 serial 定义为成员对象 QSerialPort *serial; // 将 serial 定义为成员对象