增加烧录器升级功能
This commit is contained in:
parent
aba79f8209
commit
2a4f4317c3
202
mainwindow.cpp
202
mainwindow.cpp
@ -18,6 +18,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
ui->setupUi(this);
|
||||
ui->groupBox->setMaximumWidth(300);
|
||||
ui->groupBox_2->setMaximumWidth(300);
|
||||
ui->progressBar->setValue(0);
|
||||
// 连接串口的 readyRead 信号到槽函数
|
||||
connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
|
||||
#ifdef Q_OS_WIN
|
||||
@ -413,115 +414,6 @@ 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;
|
||||
|
||||
// 提取文件名(不包含路径)
|
||||
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()//删除文件
|
||||
@ -638,7 +530,7 @@ void MainWindow::on_pushButton_7_clicked()//加载生成的镜像
|
||||
QString fileName = QFileDialog::getOpenFileName(
|
||||
this,
|
||||
"选择文件",
|
||||
"D:/", // 默认打开 D 盘根目录
|
||||
"D:/espressif/WorkSpace/esp32-off-Line-tools/build", // 默认打开 D 盘根目录
|
||||
"All Files (*.bin)"
|
||||
);
|
||||
|
||||
@ -668,23 +560,97 @@ void MainWindow::on_pushButton_8_clicked()
|
||||
qDebug() << "串口未打开";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::on_pushButton_9_clicked()//在线烧录
|
||||
void MainWindow::sendFile(uint8_t cmd1, uint8_t cmd2)
|
||||
{
|
||||
uint8_t Protocol_header[8] = {0xAA, 0x55, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00}; // 固定部分
|
||||
QString filePath = ui->lineEdit->text().trimmed();
|
||||
if (filePath.isEmpty()) {
|
||||
QMessageBox::warning(this, "提示", "请输入文件路径!");
|
||||
return;
|
||||
}
|
||||
qDebug() << "filePath:" << filePath;
|
||||
|
||||
// 构造数据包
|
||||
QFileInfo fileInfo(filePath);
|
||||
QString baseFileName = fileInfo.fileName();
|
||||
qint64 fileSize = fileInfo.size();
|
||||
qDebug() << "文件:" << baseFileName << " 大小:" << fileSize;
|
||||
|
||||
QByteArray fileNameBytes = baseFileName.toUtf8();
|
||||
QByteArray data;
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 8); // 添加固定数据
|
||||
|
||||
// 发送数据
|
||||
uint8_t Protocol_header[8] = {0xAA, 0x55, cmd1, cmd2, 0x00, 0x00, 0x00, 0x00};
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 6);
|
||||
|
||||
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);
|
||||
data.append(fileNameBytes.constData(), fileNameLength);
|
||||
|
||||
uint16_t dataLength = data.size() - 4;
|
||||
data[5] = dataLength & 0xFF;
|
||||
data[4] = (dataLength >> 8) & 0xFF;
|
||||
|
||||
uint16_t crcValue = crc16_modbus(reinterpret_cast<const uint8_t*>(data.constData() + 6), dataLength - 2);
|
||||
data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue));
|
||||
|
||||
if (serial->isOpen()) {
|
||||
ui->textBrowser->clear();
|
||||
serial->write(data);
|
||||
qDebug() << "数据发送成功";
|
||||
} else {
|
||||
qDebug() << "串口未打开";
|
||||
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, cmd1](const QByteArray &chunk, uint16_t packet) {
|
||||
uint8_t Protocol_header[8] = {0xAA, 0x55, cmd1, 0x02, 0x00, 0x00, 0x00, 0x00};
|
||||
QByteArray data;
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 8);
|
||||
if (serial->isOpen()) {
|
||||
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());
|
||||
data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue));
|
||||
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_3_clicked() // 传输文件
|
||||
{
|
||||
sendFile(0x02, 0x01);
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_9_clicked() // 在线烧录
|
||||
{
|
||||
sendFile(0x03, 0x01);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -38,6 +38,7 @@ private slots:
|
||||
void Mirror_directory_init(QTableView *tableView, const QList<File_directoryData> &dataList);
|
||||
void parseDeviceInfo(const QByteArray &data);
|
||||
void parseTransferProgress(const QByteArray &data);
|
||||
void sendFile(uint8_t cmd1, uint8_t cmd2);
|
||||
void on_pushButton_4_clicked();
|
||||
|
||||
void on_pushButton_3_clicked();
|
||||
|
||||
@ -97,7 +97,7 @@
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_9">
|
||||
<property name="text">
|
||||
<string>在线烧录</string>
|
||||
<string>更新烧录器</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user