优化文件发送速度,增加读取下位机配置
This commit is contained in:
parent
d873f0bf30
commit
cfe2734e42
@ -24,8 +24,9 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
||||
qt_add_executable(Off-line-Tools
|
||||
MANUAL_FINALIZATION
|
||||
${PROJECT_SOURCES}
|
||||
FileSenderThread.h
|
||||
|
||||
FileEncryptor.h
|
||||
FileSenderWorker.h
|
||||
|
||||
)
|
||||
# Define target properties for Android with Qt 6 as:
|
||||
|
||||
@ -42,10 +42,13 @@ private:
|
||||
void xorEncryptDecrypt(QByteArray& data) const {
|
||||
int keyLength = m_key.size();
|
||||
for (int i = 0; i < data.size(); ++i) {
|
||||
data[i] ^= m_key[i % keyLength]; // 使用密钥与数据进行XOR操作
|
||||
// 每4KB块重新开始密钥循环
|
||||
int blockOffset = i % 4096;
|
||||
data[i] ^= m_key[blockOffset % keyLength];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QByteArray m_key; // 用于加密/解密的密钥
|
||||
};
|
||||
|
||||
|
||||
@ -1,55 +0,0 @@
|
||||
#ifndef FILESENDERTHREAD_H
|
||||
#define FILESENDERTHREAD_H
|
||||
|
||||
#include <QThread>
|
||||
#include <QSerialPort>
|
||||
#include <QByteArray>
|
||||
#include <QFileDialog>
|
||||
#include <QDebug>
|
||||
|
||||
class FileSenderThread : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit FileSenderThread(QSerialPort *serial, const QString &filePath, QObject *parent = nullptr)
|
||||
: QThread(parent), m_serial(serial), m_filePath(filePath) {}
|
||||
|
||||
void run() override {
|
||||
// 打开文件
|
||||
QFile file(m_filePath);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
emit errorOccurred("无法打开文件");
|
||||
return;
|
||||
}
|
||||
|
||||
// 读取文件并发送
|
||||
QByteArray data = file.readAll();
|
||||
file.close();
|
||||
|
||||
// 将数据发送出去
|
||||
// int offset = 0;
|
||||
// const int chunkSize = 256; // 每次发送的最大数据量
|
||||
// while (offset < data.size()) {
|
||||
// QByteArray chunk = data.mid(offset, chunkSize);
|
||||
// m_serial->write(chunk);
|
||||
// offset += chunkSize;
|
||||
|
||||
// // 等待数据发送完成
|
||||
// m_serial->waitForBytesWritten(100);
|
||||
// QThread::msleep(10); // 可适当延时,以防止数据发送过快
|
||||
// }
|
||||
qDebug() << "文件已发送";
|
||||
emit finished();
|
||||
}
|
||||
|
||||
signals:
|
||||
void errorOccurred(const QString &errorMessage);
|
||||
void finished();
|
||||
|
||||
private:
|
||||
QSerialPort *m_serial;
|
||||
QString m_filePath;
|
||||
};
|
||||
|
||||
#endif // FILESENDERTHREAD_H
|
||||
59
FileSenderWorker.h
Normal file
59
FileSenderWorker.h
Normal file
@ -0,0 +1,59 @@
|
||||
#ifndef FILESENDERWORKER_H
|
||||
#define FILESENDERWORKER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QByteArray>
|
||||
#include <QThread>
|
||||
|
||||
class FileSenderWorker : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit FileSenderWorker(const QString &filePath, QObject *parent = nullptr)
|
||||
: QObject(parent), m_filePath(filePath) {}
|
||||
|
||||
public slots:
|
||||
void startSend() {
|
||||
QFile file(m_filePath);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
emit errorOccurred("无法打开文件: " + m_filePath);
|
||||
return;
|
||||
}
|
||||
QByteArray data = file.readAll();
|
||||
file.close();
|
||||
|
||||
const int chunkSize = 1024;
|
||||
int offset = 0;
|
||||
uint16_t packet = 0;
|
||||
while (offset < data.size()) {
|
||||
// 获取当前块数据
|
||||
QByteArray chunk = data.mid(offset, chunkSize);
|
||||
|
||||
// 判断是否是最后一个数据块
|
||||
uint16_t packetNumber = (offset + chunk.size() >= data.size()) ? 0xffff : packet;
|
||||
|
||||
// 发送数据块
|
||||
emit sendChunk(chunk, packetNumber);
|
||||
|
||||
// 更新偏移量和包号
|
||||
offset += chunk.size();
|
||||
packet += 1;
|
||||
|
||||
// 延迟,防止过快发送
|
||||
QThread::msleep(1);
|
||||
}
|
||||
emit finished();
|
||||
}
|
||||
|
||||
signals:
|
||||
void sendChunk(const QByteArray &chunk,uint16_t packet);
|
||||
void errorOccurred(const QString &errorMessage);
|
||||
void finished();
|
||||
|
||||
private:
|
||||
QString m_filePath;
|
||||
};
|
||||
|
||||
#endif // FILESENDERWORKER_H
|
||||
424
mainwindow.cpp
424
mainwindow.cpp
@ -6,7 +6,7 @@
|
||||
#include <QDebug>
|
||||
#include <QStandardItemModel>
|
||||
#include <QStandardItem>
|
||||
#include "FileSenderThread.h"
|
||||
#include "FileSenderWorker.h"
|
||||
#include "FileEncryptor.h"
|
||||
|
||||
|
||||
@ -61,42 +61,37 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
QStandardItemModel *model = new QStandardItemModel(3, 4); // 3行4列(包括序号列)
|
||||
|
||||
// 设置表头标签
|
||||
model->setHorizontalHeaderLabels({"序号", "文件名", "烧录地址", "文件大小"});
|
||||
model->setHorizontalHeaderLabels({"序号", "文件", "烧录地址", "文件大小"});
|
||||
|
||||
// 填充表格数据(假设我们填充一些假数据)
|
||||
for (int row = 0; row < 3; ++row) {
|
||||
for (int row = 0; row < 10; ++row) {
|
||||
// 创建序号列(根据行号动态填充)
|
||||
QStandardItem *serialNumberItem = new QStandardItem(QString::number(row + 1));
|
||||
serialNumberItem->setTextAlignment(Qt::AlignCenter); // 设置序号列内容居中
|
||||
model->setItem(row, 0, serialNumberItem);
|
||||
|
||||
// 创建其他数据列
|
||||
model->setItem(row, 1, new QStandardItem("example.txt"));
|
||||
model->setItem(row, 2, new QStandardItem("0x1000"));
|
||||
model->setItem(row, 3, new QStandardItem("1024"));
|
||||
// model->setItem(row, 1, new QStandardItem("example.txt"));
|
||||
// model->setItem(row, 2, new QStandardItem("0x1000"));
|
||||
// model->setItem(row, 3, new QStandardItem("1024"));
|
||||
}
|
||||
|
||||
// 插入数据
|
||||
model->setItem(0, 1, new QStandardItem("/Users/yu/Qt/Work_Space/Off-line-Tools"));
|
||||
model->setItem(0, 2, new QStandardItem("0xffffffff"));
|
||||
model->setItem(0, 3, new QStandardItem("0xffffffff"));
|
||||
|
||||
|
||||
// 将模型与视图关联
|
||||
ui->tableView->setModel(model);
|
||||
|
||||
// 隐藏默认的行号列
|
||||
ui->tableView->verticalHeader()->setVisible(false);
|
||||
// 设置默认行号列宽度
|
||||
// ui->tableView->verticalHeader()->setFixedWidth(30); // 设定行号列的宽度为 像素
|
||||
//设置列宽
|
||||
ui->tableView->setColumnWidth(0,60);
|
||||
ui->tableView->setColumnWidth(1,280);
|
||||
ui->tableView->setColumnWidth(2,100);
|
||||
ui->tableView->setColumnWidth(3,100);
|
||||
|
||||
// 设置列宽
|
||||
ui->tableView->setColumnWidth(0, 60);
|
||||
ui->tableView->setColumnWidth(1, 280);
|
||||
ui->tableView->setColumnWidth(2, 100);
|
||||
ui->tableView->setColumnWidth(3, 100);
|
||||
|
||||
// 连接 QTableView 的 clicked 信号到槽函数
|
||||
connect(ui->tableView, &QTableView::clicked, this, &MainWindow::onCellClicked);
|
||||
|
||||
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
@ -121,6 +116,23 @@ void MainWindow::onCellClicked(const QModelIndex &index) {
|
||||
|
||||
}
|
||||
|
||||
void MainWindow::Get_Device_info()
|
||||
{
|
||||
uint8_t Protocol_header[8] = {0xAA, 0x55, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00}; // 固定部分
|
||||
|
||||
// 构造数据包
|
||||
QByteArray data;
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 8); // 添加固定数据
|
||||
|
||||
// 发送数据
|
||||
if (serial->isOpen()) {
|
||||
serial->write(data);
|
||||
qDebug() << "数据发送成功";
|
||||
} else {
|
||||
qDebug() << "串口未打开";
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_clicked()
|
||||
{
|
||||
// 获取用户在 comboBox 中选择的串口设备
|
||||
@ -153,119 +165,104 @@ void MainWindow::on_pushButton_clicked()
|
||||
if (serial->open(QIODevice::ReadWrite)) {
|
||||
QMessageBox::information(this, "成功", "串口已成功打开");
|
||||
|
||||
// 这里可以执行更多的操作,比如发送数据、读取数据等
|
||||
// 连接 readyRead 信号到槽函数
|
||||
// connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
|
||||
// 示例:发送简单数据
|
||||
QByteArray dataToSend = "Hello, Serial Port!\r\n";
|
||||
serial->write(dataToSend);
|
||||
// QByteArray dataToSend = "Hello, Serial Port!\r\n";
|
||||
// serial->write(dataToSend);
|
||||
Get_Device_info();//获取脱机烧录器的信息
|
||||
|
||||
} else {
|
||||
QMessageBox::critical(this, "错误", "无法打开串口 " + selectedPort);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
typedef struct Device_info_
|
||||
{
|
||||
uint8_t Device_SN[12];
|
||||
uint8_t Device_MAC_WIFI[6];
|
||||
uint8_t Device_MAC_BLE[6];
|
||||
uint32_t flash_id;
|
||||
}Device_info_t;
|
||||
|
||||
|
||||
QByteArray rxBuffer;
|
||||
|
||||
|
||||
void MainWindow::onReadyRead()
|
||||
{
|
||||
while (serial->canReadLine()) {
|
||||
QByteArray line = serial->readLine(); // 读取一行数据
|
||||
qDebug() << "接收到一行数据: " << line;
|
||||
rxBuffer.append(serial->readAll()); // 追加读取到的数据
|
||||
|
||||
// 处理数据
|
||||
// 按帧头查找
|
||||
while (true) {
|
||||
int headIndex = rxBuffer.indexOf(QByteArray::fromHex("55AA"));
|
||||
if (headIndex == -1 || rxBuffer.size() < headIndex + 6 + sizeof(Device_info_t) + 4) {
|
||||
// 找不到头 或者数据不足一整帧
|
||||
break;
|
||||
}
|
||||
|
||||
// 如果找到帧头并且数据够
|
||||
Device_info_t device_tool;
|
||||
memcpy(&device_tool, rxBuffer.constData() + headIndex + 6, sizeof(Device_info_t));
|
||||
|
||||
// 打印 MAC_WIFI
|
||||
qDebug() << "Device_MAC_WIFI:"
|
||||
<< QByteArray((const char*)device_tool.Device_MAC_WIFI, 6).toHex(' ').toUpper();
|
||||
|
||||
// 移除已处理部分
|
||||
rxBuffer.remove(0, headIndex + 6 + sizeof(Device_info_t));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_4_clicked()
|
||||
{
|
||||
// 获取选择模型
|
||||
QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
|
||||
if (!selectionModel->hasSelection()) {
|
||||
QMessageBox::warning(this, "警告", "未选择任何行");
|
||||
return;
|
||||
}
|
||||
QString fileName = QFileDialog::getOpenFileName(
|
||||
this,
|
||||
"选择文件",
|
||||
QDir::homePath(),
|
||||
"All Files (*.*)"
|
||||
"D:/esp/WorkSpace/esp32c3-usb_serial/build/", // 默认打开 D 盘根目录
|
||||
"All Files (*.bin)"
|
||||
);
|
||||
|
||||
if (fileName.isEmpty()) {
|
||||
qDebug() << "未选择文件";
|
||||
return;
|
||||
}
|
||||
qDebug() << "fileName:" << fileName;
|
||||
|
||||
// 创建一个新的发送线程
|
||||
FileSenderThread *fileSenderThread = new FileSenderThread(serial, fileName, this);
|
||||
|
||||
// 连接信号和槽
|
||||
connect(fileSenderThread, &FileSenderThread::finished, this, [this, fileSenderThread]() {
|
||||
// 文件发送完成
|
||||
qDebug() << "文件发送完成";
|
||||
fileSenderThread->deleteLater();
|
||||
});
|
||||
connect(fileSenderThread, &FileSenderThread::errorOccurred, this, [](const QString &errorMessage) {
|
||||
// 文件发送出错
|
||||
qDebug() << "错误:" << errorMessage;
|
||||
});
|
||||
|
||||
// 启动线程
|
||||
fileSenderThread->start();
|
||||
#if 0
|
||||
// 提取文件名(不包含路径)
|
||||
// 获取文件信息
|
||||
QFileInfo fileInfo(fileName);
|
||||
QString baseFileName = fileInfo.fileName();
|
||||
qDebug() << "文件:" << baseFileName;
|
||||
QString fileNameOnly = fileInfo.fileName(); // 获取文件名
|
||||
QString fileSize = QString::number(fileInfo.size()); // 获取文件大小(字节)
|
||||
QString filePath = fileInfo.absoluteFilePath(); // 获取文件路径
|
||||
QString burnAddress = "0x0"; // 假设烧录地址固定为0x0,实际可根据需要修改
|
||||
|
||||
// 将文件名转换为字节数组
|
||||
QByteArray fileNameBytes = baseFileName.toUtf8();
|
||||
|
||||
// 只取前 18 个字节,确保不会超出数组的大小限制
|
||||
uint8_t Protocol_header[6] = {0xAA, 0x55, 0x02, 0x01, 0x00, 0x00}; // 固定部分
|
||||
// 判断是否有选择的单元格
|
||||
if (selectionModel->hasSelection()) {
|
||||
QModelIndex selectedIndex = selectionModel->currentIndex(); // 获取当前选中的单元格索引
|
||||
|
||||
// 构造数据包
|
||||
QByteArray data;
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 6); // 添加固定数据
|
||||
int row = selectedIndex.row(); // 获取选中行
|
||||
|
||||
// 将文件名字节拷贝到指定位置
|
||||
int fileNameLength = qMin(fileNameBytes.size(), 48); // 文件名最大长度为18
|
||||
data.append(fileNameBytes.constData(), fileNameLength); //添加文件名
|
||||
// 获取模型
|
||||
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
|
||||
|
||||
// 计算数据包的长度(去掉开头的0xAA, 0x55, 0x02, 0x01等)
|
||||
uint16_t dataLength = data.size() - 4; // 减去开头的4个字节(0xAA, 0x55, 0x02, 0x01)
|
||||
data[5] = dataLength & 0xFF; // 设置低字节
|
||||
data[4] = (dataLength >> 8) & 0xFF; // 设置高字节
|
||||
qDebug() << "dataLength:" << dataLength;
|
||||
// 计算CRC16 Modbus值
|
||||
uint16_t crcValue = crc16_modbus(reinterpret_cast<const uint8_t*>(fileNameBytes.constData()), fileNameLength);
|
||||
qDebug() << "fileNameLength:" << fileNameLength << " crcValue:" << crcValue;
|
||||
// 将 CRC 添加到数据包
|
||||
data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue)); // 添加 CRC16
|
||||
|
||||
// 发送数据
|
||||
if (serial->isOpen()) {
|
||||
serial->write(data);
|
||||
qDebug() << "数据发送成功";
|
||||
if (model) {
|
||||
// 将文件信息填充到表格中
|
||||
model->setItem(row, 1, new QStandardItem(filePath)); // 文件名
|
||||
model->setItem(row, 2, new QStandardItem(burnAddress)); // 烧录地址
|
||||
model->setItem(row, 3, new QStandardItem(fileSize)); // 文件大小
|
||||
}
|
||||
} else {
|
||||
qDebug() << "串口未打开";
|
||||
qDebug() << "未选择表格单元格";
|
||||
QMessageBox::warning(this, "警告", "未选择任何行");
|
||||
}
|
||||
|
||||
data.clear();
|
||||
Protocol_header[3] = 0x02;
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 6); // 添加固定数据
|
||||
uint8_t buffer[128] = {0x01,0x7f,0xff};
|
||||
data.append(reinterpret_cast<char*>(buffer),128);
|
||||
dataLength = data.size() - 4;
|
||||
data[5] = dataLength & 0xFF; // 设置低字节
|
||||
data[4] = (dataLength >> 8) & 0xFF; // 设置高字节
|
||||
crcValue = crc16_modbus(reinterpret_cast<const uint8_t*>(buffer), 128);
|
||||
qDebug() << "fileNameLength:" << fileNameLength << " crcValue:" << crcValue;
|
||||
data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue)); // 添加 CRC16
|
||||
// 发送数据
|
||||
if (serial->isOpen()) {
|
||||
serial->write(data);
|
||||
qDebug() << "数据发送成功";
|
||||
} else {
|
||||
qDebug() << "串口未打开";
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// CRC16 (Modbus) 计算函数
|
||||
uint16_t MainWindow::crc16_modbus(const uint8_t *data, uint16_t len) {
|
||||
uint16_t crc = 0xFFFF; // 初始化 CRC 值
|
||||
@ -280,3 +277,236 @@ uint16_t MainWindow::crc16_modbus(const uint8_t *data, uint16_t len) {
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_3_clicked()
|
||||
{
|
||||
QString filePath = ui->lineEdit->text().trimmed();
|
||||
if (filePath.isEmpty()) {
|
||||
QMessageBox::warning(this, "提示", "请输入文件路径!");
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
QString baseFileName = fileInfo.fileName();
|
||||
qDebug() << "文件:" << baseFileName;
|
||||
|
||||
// 将文件名转换为字节数组
|
||||
QByteArray fileNameBytes = baseFileName.toUtf8();
|
||||
|
||||
// 只取前 18 个字节,确保不会超出数组的大小限制
|
||||
uint8_t Protocol_header[6] = {0xAA, 0x55, 0x02, 0x01, 0x00, 0x00}; // 固定部分
|
||||
|
||||
// 构造数据包
|
||||
QByteArray data;
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 6); // 添加固定数据
|
||||
|
||||
// 将文件名字节拷贝到指定位置
|
||||
int fileNameLength = qMin(fileNameBytes.size(), 48); // 文件名最大长度为48
|
||||
data.append(fileNameBytes.constData(), fileNameLength); //添加文件名
|
||||
|
||||
// 计算数据包的长度(去掉开头的0xAA, 0x55, 0x02, 0x01等)
|
||||
uint16_t dataLength = data.size() - 4; // 减去开头的4个字节(0xAA, 0x55, 0x02, 0x01)
|
||||
data[5] = dataLength & 0xFF; // 设置低字节
|
||||
data[4] = (dataLength >> 8) & 0xFF; // 设置高字节
|
||||
qDebug() << "dataLength:" << dataLength;
|
||||
// 计算CRC16 Modbus值
|
||||
uint16_t crcValue = crc16_modbus(reinterpret_cast<const uint8_t*>(fileNameBytes.constData()), fileNameLength);
|
||||
qDebug() << "fileNameLength:" << fileNameLength << " crcValue:" << crcValue;
|
||||
// 将 CRC 添加到数据包
|
||||
data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue)); // 添加 CRC16
|
||||
// 发送数据
|
||||
if (serial->isOpen()) {
|
||||
serial->write(data);
|
||||
qDebug() << "数据发送成功";
|
||||
} else {
|
||||
qDebug() << "串口未打开";
|
||||
}
|
||||
data.clear();
|
||||
// 创建线程和工作对象
|
||||
QThread *thread = new QThread;
|
||||
FileSenderWorker *worker = new FileSenderWorker(outputFilePath);
|
||||
|
||||
worker->moveToThread(thread);
|
||||
|
||||
connect(thread, &QThread::started, worker, &FileSenderWorker::startSend);
|
||||
|
||||
// 在主线程接收信号并用串口写
|
||||
connect(worker, &FileSenderWorker::sendChunk, this, [this](const QByteArray &chunk,uint16_t packet){
|
||||
uint8_t Protocol_header[8] = {0xAA, 0x55, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00}; // 固定部分
|
||||
// 构造数据包
|
||||
QByteArray data;
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 8); // 添加固定数据
|
||||
if (serial->isOpen()) {
|
||||
// qDebug() << "chunk size:" << chunk.size();
|
||||
data.append(chunk);
|
||||
uint16_t dataLength = data.size() - 4;
|
||||
data[5] = dataLength & 0xFF; // 设置低字节
|
||||
data[4] = (dataLength >> 8) & 0xFF; // 设置高字节
|
||||
data[7] = packet & 0xFF;
|
||||
data[6] = (packet >> 8) & 0xFF;
|
||||
uint16_t crcValue = crc16_modbus(reinterpret_cast<const uint8_t*>(chunk.constData()), chunk.size());
|
||||
// qDebug() << "packet:" << packet << " crcValue:" << crcValue;
|
||||
data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue)); // 添加 CRC16
|
||||
serial->write(data);
|
||||
serial->waitForBytesWritten(100);
|
||||
} else {
|
||||
qDebug() << "串口未打开";
|
||||
}
|
||||
});
|
||||
|
||||
connect(worker, &FileSenderWorker::errorOccurred, this, [](const QString &err){
|
||||
qDebug() << "发生错误:" << err;
|
||||
});
|
||||
|
||||
connect(worker, &FileSenderWorker::finished, this, [=](){
|
||||
qDebug() << "文件发送完成";
|
||||
thread->quit();
|
||||
thread->wait();
|
||||
worker->deleteLater();
|
||||
thread->deleteLater();
|
||||
});
|
||||
|
||||
thread->start();
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::on_pushButton_5_clicked()//删除文件
|
||||
{
|
||||
// 获取选择模型
|
||||
QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
|
||||
|
||||
// 判断是否有选择的单元格
|
||||
if (selectionModel->hasSelection()) {
|
||||
// 获取当前选中的行
|
||||
QModelIndex selectedIndex = selectionModel->currentIndex();
|
||||
int row = selectedIndex.row();
|
||||
|
||||
// 获取模型
|
||||
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
|
||||
|
||||
if (model) {
|
||||
// 清空选中行的数据(但不清空序号列)
|
||||
for (int column = 1; column < model->columnCount(); ++column) {
|
||||
model->setItem(row, column, new QStandardItem(""));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 未选中任何行时弹出提示
|
||||
QMessageBox::warning(this, "警告", "未选择任何行");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QDataStream>
|
||||
#include <QByteArray>
|
||||
#include <QTextStream>
|
||||
|
||||
void MainWindow::on_pushButton_6_clicked() // 生成镜像
|
||||
{
|
||||
// 获取模型
|
||||
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
|
||||
|
||||
if (model) {
|
||||
QByteArray finalBinData; // 用来存储最终拼接后的数据
|
||||
|
||||
// 遍历所有行
|
||||
for (int row = 0; row < model->rowCount(); ++row) {
|
||||
// 获取烧录地址列的内容
|
||||
QStandardItem *burnAddressItem = model->item(row, 2); // 烧录地址列
|
||||
|
||||
// 确保烧录地址列有内容
|
||||
if (burnAddressItem && !burnAddressItem->text().isEmpty()) {
|
||||
// 获取其他列的内容
|
||||
QString serialNumber = model->item(row, 0)->text(); // 序号列
|
||||
QString filePath = model->item(row, 1)->text(); // 文件名列
|
||||
QString burnAddress = burnAddressItem->text(); // 烧录地址列
|
||||
QString fileSize = model->item(row, 3)->text(); // 文件大小列
|
||||
|
||||
// 打开文件
|
||||
QFile file(filePath);
|
||||
if (file.exists() && file.open(QIODevice::ReadOnly)) {
|
||||
QByteArray fileData = file.readAll(); // 读取文件内容
|
||||
|
||||
// 将文件内容按照烧录地址拼接到最终的镜像数据中
|
||||
// 烧录地址转换为整数,计算偏移量
|
||||
bool ok;
|
||||
quint64 address = burnAddress.toULongLong(&ok, 16); // 假设烧录地址是16进制格式
|
||||
if (ok) {
|
||||
// 填充到最终的二进制数据中(使用烧录地址进行偏移)
|
||||
quint64 oldSize = finalBinData.size();
|
||||
quint64 newSize = address + fileData.size();
|
||||
if (oldSize < newSize) {
|
||||
// 先扩展
|
||||
finalBinData.resize(newSize);
|
||||
// 将新扩展出来的部分初始化为 0xFF
|
||||
// std::fill(finalBinData.begin() + oldSize, finalBinData.end(), static_cast<char>(0xFF));
|
||||
}
|
||||
std::copy(fileData.begin(), fileData.end(), finalBinData.begin() + address);
|
||||
}
|
||||
|
||||
file.close();
|
||||
} else {
|
||||
qDebug() << "无法打开文件: " << filePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 将拼接后的数据保存到一个 .bin 文件
|
||||
QFile outputFile("D:/output.bin");
|
||||
if (outputFile.open(QIODevice::WriteOnly)) {
|
||||
outputFile.write(finalBinData); // 写入拼接后的数据
|
||||
outputFile.close();
|
||||
|
||||
QByteArray macAddress = QByteArray::fromHex("A1B2C3D4E5F6");
|
||||
FileEncryptor encryptor(macAddress);
|
||||
|
||||
if (encryptor.encryptDecryptFile("D:/output.bin", "D:/encipher.bin")) {
|
||||
qDebug() << "文件加密成功!";
|
||||
ui->lineEdit->setText("D:/encipher.bin");
|
||||
} else {
|
||||
qDebug() << "文件加密失败!";
|
||||
return;
|
||||
}
|
||||
|
||||
qDebug() << "镜像文件生成成功!";
|
||||
} else {
|
||||
qDebug() << "无法创建镜像文件!";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::on_pushButton_7_clicked()//加载生成的镜像
|
||||
{
|
||||
QString fileName = QFileDialog::getOpenFileName(
|
||||
this,
|
||||
"选择文件",
|
||||
"D:/", // 默认打开 D 盘根目录
|
||||
"All Files (*.bin)"
|
||||
);
|
||||
|
||||
if (fileName.isEmpty()) {
|
||||
qDebug() << "未选择文件";
|
||||
return;
|
||||
}
|
||||
qDebug() << "fileName:" << fileName;
|
||||
ui->lineEdit->setText(fileName);
|
||||
}
|
||||
|
||||
|
||||
@ -25,8 +25,17 @@ private slots:
|
||||
void on_pushButton_clicked();
|
||||
void onReadyRead(); // 串口数据接收的槽
|
||||
void onCellClicked(const QModelIndex &index);
|
||||
void Get_Device_info();
|
||||
void on_pushButton_4_clicked();
|
||||
|
||||
void on_pushButton_3_clicked();
|
||||
|
||||
void on_pushButton_5_clicked();
|
||||
|
||||
void on_pushButton_6_clicked();
|
||||
|
||||
void on_pushButton_7_clicked();
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
QSerialPort *serial; // 将 serial 定义为成员对象
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>838</width>
|
||||
<width>1167</width>
|
||||
<height>648</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -67,7 +67,7 @@
|
||||
<rect>
|
||||
<x>50</x>
|
||||
<y>10</y>
|
||||
<width>431</width>
|
||||
<width>621</width>
|
||||
<height>431</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -79,12 +79,12 @@
|
||||
<property name="title">
|
||||
<string>镜像生成</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="">
|
||||
<widget class="QWidget" name="layoutWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>10</x>
|
||||
<y>39</y>
|
||||
<width>411</width>
|
||||
<width>571</width>
|
||||
<height>381</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -94,7 +94,7 @@
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Orientation::Horizontal</enum>
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
@ -161,10 +161,10 @@
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
<widget class="QWidget" name="">
|
||||
<widget class="QWidget" name="layoutWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>530</x>
|
||||
<x>840</x>
|
||||
<y>20</y>
|
||||
<width>301</width>
|
||||
<height>566</height>
|
||||
@ -257,7 +257,7 @@
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<property name="sizeConstraint">
|
||||
<enum>QLayout::SizeConstraint::SetDefaultConstraint</enum>
|
||||
<enum>QLayout::SetDefaultConstraint</enum>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
@ -275,10 +275,10 @@
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Orientation::Horizontal</enum>
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeType">
|
||||
<enum>QSizePolicy::Policy::MinimumExpanding</enum>
|
||||
<enum>QSizePolicy::MinimumExpanding</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
@ -349,8 +349,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>838</width>
|
||||
<height>24</height>
|
||||
<width>1167</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user