diff --git a/FileSenderWorker.h b/FileSenderWorker.h index e2555f5..0efdd88 100644 --- a/FileSenderWorker.h +++ b/FileSenderWorker.h @@ -42,7 +42,7 @@ public slots: packet += 1; // 延迟,防止过快发送 - QThread::msleep(1); + QThread::msleep(20); } emit finished(); } diff --git a/mainwindow.cpp b/mainwindow.cpp index 018bc53..05321cb 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -16,8 +16,8 @@ MainWindow::MainWindow(QWidget *parent) , serial(new QSerialPort(this)) // 在构造函数中初始化 serial { ui->setupUi(this); - ui->groupBox->setMaximumWidth(250); - ui->groupBox_2->setMaximumWidth(250); + ui->groupBox->setMaximumWidth(300); + ui->groupBox_2->setMaximumWidth(300); // 连接串口的 readyRead 信号到槽函数 connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead); #ifdef Q_OS_WIN @@ -54,7 +54,7 @@ MainWindow::MainWindow(QWidget *parent) } } - + setButtonColor(ui->pushButton, "red"); ui->comboBox->addItems(portList); // 创建 QStandardItemModel(表格数据模型) @@ -91,9 +91,48 @@ MainWindow::MainWindow(QWidget *parent) // 连接 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 &dataList) +{ + QStandardItemModel *model = new QStandardItemModel(0, 3); + + model->setHorizontalHeaderLabels({ "文件名", "烧录地址", "文件大小" }); + + // 根据 dataList 填充 + for (const auto &data : dataList) { + QList 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; @@ -126,6 +165,7 @@ void MainWindow::Get_Device_info() // 发送数据 if (serial->isOpen()) { + ui->textBrowser->clear(); serial->write(data); qDebug() << "数据发送成功"; } else { @@ -133,6 +173,47 @@ void MainWindow::Get_Device_info() } } +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 中选择的串口设备 @@ -150,6 +231,9 @@ void MainWindow::on_pushButton_clicked() // 确保串口已打开才调用 close if (serial->isOpen()) { serial->close(); + setButtonColor(ui->pushButton, "red"); + ui->textBrowser->clear(); + return; } } @@ -163,7 +247,8 @@ void MainWindow::on_pushButton_clicked() // 打开串口 if (serial->open(QIODevice::ReadWrite)) { - QMessageBox::information(this, "成功", "串口已成功打开"); + QMessageBox::information(this, "成功", "设备成功连接"); + setButtonColor(ui->pushButton, "green"); // QByteArray dataToSend = "Hello, Serial Port!\r\n"; // serial->write(dataToSend); @@ -175,12 +260,21 @@ void MainWindow::on_pushButton_clicked() } +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; @@ -194,7 +288,7 @@ void MainWindow::onReadyRead() // 按帧头查找 while (true) { int headIndex = rxBuffer.indexOf(QByteArray::fromHex("55AA")); - if (headIndex == -1 || rxBuffer.size() < headIndex + 6 + sizeof(Device_info_t) + 4) { + if (headIndex == -1 || rxBuffer.size() < headIndex + 6 + sizeof(Device_info_t) + 2) { // 找不到头 或者数据不足一整帧 break; } @@ -207,11 +301,25 @@ void MainWindow::onReadyRead() 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 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); // 移除已处理部分 - rxBuffer.remove(0, headIndex + 6 + sizeof(Device_info_t)); + rxBuffer.remove(0, headIndex + 6 + sizeof(Device_info_t) + 2); } } + void MainWindow::on_pushButton_4_clicked() { // 获取选择模型 @@ -359,7 +467,7 @@ void MainWindow::on_pushButton_3_clicked() data[7] = packet & 0xFF; data[6] = (packet >> 8) & 0xFF; uint16_t crcValue = crc16_modbus(reinterpret_cast(chunk.constData()), chunk.size()); - // qDebug() << "packet:" << packet << " crcValue:" << crcValue; + qDebug() << "packet:" << packet << " crcValue:" << crcValue; data.append(reinterpret_cast(&crcValue), sizeof(crcValue)); // 添加 CRC16 serial->write(data); serial->waitForBytesWritten(100); @@ -510,3 +618,22 @@ void MainWindow::on_pushButton_7_clicked()//加载生成的镜像 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(Protocol_header), 8); // 添加固定数据 + + // 发送数据 + if (serial->isOpen()) { + ui->textBrowser->clear(); + serial->write(data); + qDebug() << "数据发送成功"; + } else { + qDebug() << "串口未打开"; + } +} + diff --git a/mainwindow.h b/mainwindow.h index de5db72..17a8f69 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,8 +4,16 @@ #include #include #include +#include +struct File_directoryData { + uint32_t id; + QString name; + uint32_t addr; + uint32_t size; +}; + QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; @@ -26,6 +34,8 @@ private slots: void onReadyRead(); // 串口数据接收的槽 void onCellClicked(const QModelIndex &index); void Get_Device_info(); + void setButtonColor(QPushButton *button, const QString &color); + void Mirror_directory_init(QTableView *tableView, const QList &dataList); void on_pushButton_4_clicked(); void on_pushButton_3_clicked(); @@ -36,6 +46,8 @@ private slots: void on_pushButton_7_clicked(); + void on_pushButton_8_clicked(); + private: Ui::MainWindow *ui; QSerialPort *serial; // 将 serial 定义为成员对象 diff --git a/mainwindow.ui b/mainwindow.ui index 7bd64b7..1311870 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -7,7 +7,7 @@ 0 0 1167 - 648 + 827 @@ -17,10 +17,10 @@ - 50 - 450 - 431 - 101 + 40 + 430 + 521 + 341 @@ -31,30 +31,84 @@ 镜像管理 - + - + - - - 文件: - - + + + + + 文件: + + + + + + + + + + 加载 + + + + + + + 写入 + + + + - + + + + + + + + + + + 13 + + + + 格式化目录 + + + + + + + Qt::Orientation::Vertical + + + + 20 + 40 + + + + + + + + 在线烧录 + + + + + + - - - 加载 - - - - - - - 写入 + + + 24 @@ -65,8 +119,8 @@ - 50 - 10 + 40 + 0 621 431 @@ -94,7 +148,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -257,7 +311,7 @@ - QLayout::SetDefaultConstraint + QLayout::SizeConstraint::SetDefaultConstraint @@ -275,10 +329,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSizePolicy::MinimumExpanding + QSizePolicy::Policy::MinimumExpanding