691 lines
23 KiB
C++
691 lines
23 KiB
C++
#include "mainwindow.h"
|
||
#include "ui_mainwindow.h"
|
||
#include <QSerialPort>
|
||
#include <QSerialPortInfo>
|
||
#include <QMessageBox>
|
||
#include <QDebug>
|
||
#include <QStandardItemModel>
|
||
#include <QStandardItem>
|
||
#include "FileSenderWorker.h"
|
||
#include "FileEncryptor.h"
|
||
|
||
|
||
MainWindow::MainWindow(QWidget *parent)
|
||
: QMainWindow(parent)
|
||
, ui(new Ui::MainWindow)
|
||
, serial(new QSerialPort(this)) // 在构造函数中初始化 serial
|
||
{
|
||
ui->setupUi(this);
|
||
ui->groupBox->setMaximumWidth(300);
|
||
ui->groupBox_2->setMaximumWidth(300);
|
||
// 连接串口的 readyRead 信号到槽函数
|
||
connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
|
||
#ifdef Q_OS_WIN
|
||
qDebug() << "当前是 Windows 平台";
|
||
#elif defined(Q_OS_MAC)
|
||
qDebug() << "当前是 macOS 平台";
|
||
#elif defined(Q_OS_LINUX)
|
||
qDebug() << "当前是 Linux 平台";
|
||
#else
|
||
qDebug() << "未知平台";
|
||
#endif
|
||
QStringList portList;
|
||
const auto serialPortList = QSerialPortInfo::availablePorts();
|
||
for (const QSerialPortInfo &info : serialPortList) {
|
||
qDebug() << "Port Name:" << info.portName();
|
||
qDebug() << "Description:" << info.description();
|
||
qDebug() << "Manufacturer:" << info.manufacturer();
|
||
qDebug() << "Serial Number:" << info.serialNumber();
|
||
qDebug() << "System Location:" << info.systemLocation();
|
||
qDebug() << "-----------------------------";
|
||
if (info.manufacturer().compare("GenesysLogic") == 0 ||info.manufacturer().compare("Espressif") == 0 ||info.manufacturer().compare("Microsoft") == 0) {
|
||
// 完全匹配
|
||
#ifdef Q_OS_WIN
|
||
if(info.systemLocation().startsWith("\\\\.\\COM"))
|
||
{
|
||
portList << info.portName();
|
||
}
|
||
#elif defined(Q_OS_MAC)
|
||
if(info.systemLocation().startsWith("/dev/tty."))
|
||
{
|
||
portList << info.systemLocation();
|
||
}
|
||
#endif
|
||
}
|
||
|
||
}
|
||
setButtonColor(ui->pushButton, "red");
|
||
ui->comboBox->addItems(portList);
|
||
|
||
// 创建 QStandardItemModel(表格数据模型)
|
||
QStandardItemModel *model = new QStandardItemModel(3, 4); // 3行4列(包括序号列)
|
||
|
||
// 设置表头标签
|
||
model->setHorizontalHeaderLabels({"序号", "文件", "烧录地址", "文件大小"});
|
||
|
||
// 填充表格数据(假设我们填充一些假数据)
|
||
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"));
|
||
}
|
||
|
||
// 将模型与视图关联
|
||
ui->tableView->setModel(model);
|
||
|
||
// 隐藏默认的行号列
|
||
ui->tableView->verticalHeader()->setVisible(false);
|
||
|
||
// 设置列宽
|
||
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);
|
||
|
||
// ui->tableView_2
|
||
//Mirror_directory_init(ui->tableView_2);
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
void MainWindow::Mirror_directory_init(QTableView *tableView, const QList<File_directoryData> &dataList)
|
||
{
|
||
QStandardItemModel *model = new QStandardItemModel(0, 3);
|
||
|
||
model->setHorizontalHeaderLabels({ "文件名", "烧录地址", "文件大小" });
|
||
|
||
// 根据 dataList 填充
|
||
for (const auto &data : dataList) {
|
||
QList<QStandardItem*> rowItems;
|
||
|
||
QStandardItem *itemFileName = new QStandardItem(data.name);
|
||
QStandardItem *itemAddress = new QStandardItem(QString("0x%1").arg(data.addr, 0, 16).toUpper());
|
||
QStandardItem *itemSize = new QStandardItem(QString("%1 字节").arg(data.size));
|
||
|
||
// 设置不可编辑
|
||
itemFileName->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||
itemAddress->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||
itemSize->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||
|
||
rowItems << itemFileName << itemAddress << itemSize;
|
||
model->appendRow(rowItems);
|
||
}
|
||
|
||
tableView->setModel(model);
|
||
|
||
tableView->setColumnWidth(0, 180);
|
||
tableView->setColumnWidth(1, 100);
|
||
tableView->setColumnWidth(2, 100);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
MainWindow::~MainWindow()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void MainWindow::onCellClicked(const QModelIndex &index) {
|
||
// 检查是否是第一列
|
||
if (index.column() == 0) {
|
||
// 获取选中的行
|
||
int row = index.row();
|
||
|
||
// 获取选择模型
|
||
QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
|
||
|
||
// 选中整行
|
||
QItemSelection selection(ui->tableView->model()->index(row, 0), ui->tableView->model()->index(row, ui->tableView->model()->columnCount() - 1));
|
||
selectionModel->select(selection, QItemSelectionModel::Select);
|
||
}
|
||
qDebug() << "onCellClicked:" << index.column();
|
||
|
||
}
|
||
|
||
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()) {
|
||
ui->textBrowser->clear();
|
||
serial->write(data);
|
||
qDebug() << "数据发送成功";
|
||
} else {
|
||
qDebug() << "串口未打开";
|
||
}
|
||
}
|
||
|
||
void MainWindow::setButtonColor(QPushButton *button, const QString &color)
|
||
{
|
||
QString hoverColor;
|
||
QString pressColor;
|
||
|
||
// 根据基础颜色定义 hover 和 press 状态的颜色
|
||
if (color == "green") {
|
||
hoverColor = "#28a745"; // 浅绿色
|
||
pressColor = "#1e7e34"; // 深绿色
|
||
button->setText("断开");
|
||
} else if (color == "red") {
|
||
hoverColor = "#dc3545"; // 浅红色
|
||
pressColor = "#b21f2d"; // 深红色
|
||
button->setText("连接");
|
||
} else {
|
||
hoverColor = color;
|
||
pressColor = color;
|
||
}
|
||
|
||
button->setStyleSheet(QString(R"(
|
||
QPushButton {
|
||
background-color: %1;
|
||
color: white;
|
||
border: none;
|
||
border-radius: 3px;
|
||
font-size: 15px;
|
||
min-width: 75px;
|
||
min-height: 30px;
|
||
}
|
||
QPushButton:hover {
|
||
background-color: %2;
|
||
color: white;
|
||
}
|
||
QPushButton:pressed {
|
||
background-color: %3;
|
||
color: white;
|
||
}
|
||
)").arg(color, hoverColor, pressColor));
|
||
}
|
||
|
||
|
||
void MainWindow::on_pushButton_clicked()
|
||
{
|
||
// 获取用户在 comboBox 中选择的串口设备
|
||
QString selectedPort = ui->comboBox->currentText();
|
||
|
||
if (selectedPort.isEmpty()) {
|
||
QMessageBox::warning(this, "警告", "请选择一个串口设备");
|
||
return;
|
||
}
|
||
|
||
// 创建串口对象,如果已创建则先关闭
|
||
if (!serial) {
|
||
serial = new QSerialPort(this);
|
||
} else {
|
||
// 确保串口已打开才调用 close
|
||
if (serial->isOpen()) {
|
||
serial->close();
|
||
setButtonColor(ui->pushButton, "red");
|
||
ui->textBrowser->clear();
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 设置串口信息
|
||
serial->setPortName(selectedPort);
|
||
serial->setBaudRate(QSerialPort::Baud115200); // 设置波特率
|
||
serial->setDataBits(QSerialPort::Data8); // 数据位
|
||
serial->setParity(QSerialPort::NoParity); // 校验位
|
||
serial->setStopBits(QSerialPort::OneStop); // 停止位
|
||
serial->setFlowControl(QSerialPort::NoFlowControl); // 无流控
|
||
|
||
// 打开串口
|
||
if (serial->open(QIODevice::ReadWrite)) {
|
||
QMessageBox::information(this, "成功", "设备成功连接");
|
||
setButtonColor(ui->pushButton, "green");
|
||
|
||
// QByteArray dataToSend = "Hello, Serial Port!\r\n";
|
||
// serial->write(dataToSend);
|
||
Get_Device_info();//获取脱机烧录器的信息
|
||
|
||
} else {
|
||
QMessageBox::critical(this, "错误", "无法打开串口 " + selectedPort);
|
||
}
|
||
|
||
}
|
||
|
||
typedef struct spiffs_list_
|
||
{
|
||
uint32_t id;
|
||
char name[32];
|
||
uint32_t addr;
|
||
uint32_t size;
|
||
}spiffs_list_t;
|
||
|
||
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;
|
||
spiffs_list_t directory[10];
|
||
}Device_info_t;
|
||
|
||
|
||
QByteArray rxBuffer;
|
||
|
||
|
||
void MainWindow::parseDeviceInfo(const QByteArray &data)
|
||
{
|
||
if (data.size() < static_cast<int>(sizeof(Device_info_t))) return;
|
||
|
||
Device_info_t device_tool;
|
||
memcpy(&device_tool, data.constData(), sizeof(Device_info_t));
|
||
|
||
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("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());
|
||
|
||
// 解析文件列表
|
||
QList<File_directoryData> dataList;
|
||
ui->textBrowser->append("Files:");
|
||
for (int i = 0; i < 10; i++) {
|
||
if (device_tool.directory[i].size > 0) {
|
||
dataList.append({device_tool.directory[i].id, device_tool.directory[i].name, 0x1000, device_tool.directory[i].size});
|
||
}
|
||
}
|
||
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);
|
||
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()
|
||
{
|
||
// 获取选择模型
|
||
QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
|
||
if (!selectionModel->hasSelection()) {
|
||
QMessageBox::warning(this, "警告", "未选择任何行");
|
||
return;
|
||
}
|
||
QString fileName = QFileDialog::getOpenFileName(
|
||
this,
|
||
"选择文件",
|
||
"D:/esp/WorkSpace/esp32c3-usb_serial/build/", // 默认打开 D 盘根目录
|
||
"All Files (*.bin)"
|
||
);
|
||
|
||
if (fileName.isEmpty()) {
|
||
qDebug() << "未选择文件";
|
||
return;
|
||
}
|
||
qDebug() << "fileName:" << fileName;
|
||
|
||
// 获取文件信息
|
||
QFileInfo fileInfo(fileName);
|
||
QString fileNameOnly = fileInfo.fileName(); // 获取文件名
|
||
QString fileSize = QString::number(fileInfo.size()); // 获取文件大小(字节)
|
||
QString filePath = fileInfo.absoluteFilePath(); // 获取文件路径
|
||
QString burnAddress = "0x0"; // 假设烧录地址固定为0x0,实际可根据需要修改
|
||
|
||
|
||
// 判断是否有选择的单元格
|
||
if (selectionModel->hasSelection()) {
|
||
QModelIndex selectedIndex = selectionModel->currentIndex(); // 获取当前选中的单元格索引
|
||
|
||
int row = selectedIndex.row(); // 获取选中行
|
||
|
||
// 获取模型
|
||
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
|
||
|
||
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() << "未选择表格单元格";
|
||
QMessageBox::warning(this, "警告", "未选择任何行");
|
||
}
|
||
}
|
||
|
||
|
||
// CRC16 (Modbus) 计算函数
|
||
uint16_t MainWindow::crc16_modbus(const uint8_t *data, uint16_t len) {
|
||
uint16_t crc = 0xFFFF; // 初始化 CRC 值
|
||
for (int i = 0; i < len; i++) {
|
||
crc ^= data[i]; // 与数据进行异或
|
||
for (int j = 0; j < 8; j++) { // 遍历每一位
|
||
if (crc & 0x0001) // 如果最低位为 1
|
||
crc = (crc >> 1) ^ 0xA001; // 右移一位并异或
|
||
else
|
||
crc = crc >> 1; // 否则仅右移
|
||
}
|
||
}
|
||
return crc;
|
||
}
|
||
|
||
void MainWindow::on_pushButton_3_clicked()
|
||
{
|
||
QString filePath = ui->lineEdit->text().trimmed();
|
||
if (filePath.isEmpty()) {
|
||
QMessageBox::warning(this, "提示", "请输入文件路径!");
|
||
return;
|
||
}
|
||
qDebug() << "filePath:" << filePath;
|
||
|
||
// 提取文件名(不包含路径)
|
||
QFileInfo fileInfo(filePath);
|
||
QString baseFileName = fileInfo.fileName();
|
||
qDebug() << "文件:" << baseFileName;
|
||
|
||
// 获取文件大小
|
||
qint64 fileSize = fileInfo.size();
|
||
qDebug() << "文件大小:" << fileSize;
|
||
|
||
// 将文件名转换为字节数组
|
||
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); // 添加固定数据
|
||
|
||
// 追加文件大小(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
|
||
data.append(fileNameBytes.constData(), fileNameLength); //添加文件名
|
||
|
||
qDebug() << "最终数据包长度:" << data.size();
|
||
|
||
// 计算数据包的长度(去掉开头的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*>(data.constData() + 6), dataLength - 2);
|
||
|
||
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();
|
||
|
||
// return;
|
||
|
||
// 创建线程和工作对象
|
||
QThread *thread = new QThread;
|
||
|
||
FileSenderWorker *worker = new FileSenderWorker(filePath);//未加密文件
|
||
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);
|
||
}
|
||
|
||
|
||
void MainWindow::on_pushButton_8_clicked()
|
||
{
|
||
uint8_t Protocol_header[8] = {0xAA, 0x55, 0x02, 0x03, 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() << "串口未打开";
|
||
}
|
||
}
|
||
|
||
|
||
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() << "串口未打开";
|
||
}
|
||
}
|
||
|