增加连接成功后读取下位机的文件目录
This commit is contained in:
parent
cfe2734e42
commit
99929bb48c
@ -42,7 +42,7 @@ public slots:
|
||||
packet += 1;
|
||||
|
||||
// 延迟,防止过快发送
|
||||
QThread::msleep(1);
|
||||
QThread::msleep(20);
|
||||
}
|
||||
emit finished();
|
||||
}
|
||||
|
||||
141
mainwindow.cpp
141
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<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;
|
||||
@ -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<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);
|
||||
// 移除已处理部分
|
||||
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<const uint8_t*>(chunk.constData()), chunk.size());
|
||||
// qDebug() << "packet:" << packet << " crcValue:" << crcValue;
|
||||
qDebug() << "packet:" << packet << " crcValue:" << crcValue;
|
||||
data.append(reinterpret_cast<char*>(&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<char*>(Protocol_header), 8); // 添加固定数据
|
||||
|
||||
// 发送数据
|
||||
if (serial->isOpen()) {
|
||||
ui->textBrowser->clear();
|
||||
serial->write(data);
|
||||
qDebug() << "数据发送成功";
|
||||
} else {
|
||||
qDebug() << "串口未打开";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
12
mainwindow.h
12
mainwindow.h
@ -4,8 +4,16 @@
|
||||
#include <QMainWindow>
|
||||
#include <QSerialPort>
|
||||
#include <QFileDialog>
|
||||
#include <QTableView>
|
||||
|
||||
|
||||
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<File_directoryData> &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 定义为成员对象
|
||||
|
||||
112
mainwindow.ui
112
mainwindow.ui
@ -7,7 +7,7 @@
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1167</width>
|
||||
<height>648</height>
|
||||
<height>827</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@ -17,10 +17,10 @@
|
||||
<widget class="QGroupBox" name="groupBox_3">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>50</x>
|
||||
<y>450</y>
|
||||
<width>431</width>
|
||||
<height>101</height>
|
||||
<x>40</x>
|
||||
<y>430</y>
|
||||
<width>521</width>
|
||||
<height>341</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
@ -31,30 +31,84 @@
|
||||
<property name="title">
|
||||
<string>镜像管理</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_4">
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<item row="0" column="0">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||
<layout class="QVBoxLayout" name="verticalLayout_5">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>文件:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>文件:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEdit"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_7">
|
||||
<property name="text">
|
||||
<string>加载</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_3">
|
||||
<property name="text">
|
||||
<string>写入</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEdit"/>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_6">
|
||||
<item>
|
||||
<widget class="QTableView" name="tableView_2"/>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_4">
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_8">
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>13</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>格式化目录</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Orientation::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_9">
|
||||
<property name="text">
|
||||
<string>在线烧录</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_7">
|
||||
<property name="text">
|
||||
<string>加载</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_3">
|
||||
<property name="text">
|
||||
<string>写入</string>
|
||||
<widget class="QProgressBar" name="progressBar">
|
||||
<property name="value">
|
||||
<number>24</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@ -65,8 +119,8 @@
|
||||
<widget class="QGroupBox" name="groupBox_4">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>50</x>
|
||||
<y>10</y>
|
||||
<x>40</x>
|
||||
<y>0</y>
|
||||
<width>621</width>
|
||||
<height>431</height>
|
||||
</rect>
|
||||
@ -94,7 +148,7 @@
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
<enum>Qt::Orientation::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
@ -257,7 +311,7 @@
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<property name="sizeConstraint">
|
||||
<enum>QLayout::SetDefaultConstraint</enum>
|
||||
<enum>QLayout::SizeConstraint::SetDefaultConstraint</enum>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
@ -275,10 +329,10 @@
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
<enum>Qt::Orientation::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeType">
|
||||
<enum>QSizePolicy::MinimumExpanding</enum>
|
||||
<enum>QSizePolicy::Policy::MinimumExpanding</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user