From b5038030c6aff2239b300251371f39ff8f03db4c Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Sat, 1 Mar 2025 13:40:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=BA=E4=BB=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TSI_Config.pro | 8 +++- data_config.h | 22 ++++++++++ mainwindow.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++++-- mainwindow.h | 4 +- 4 files changed, 134 insertions(+), 6 deletions(-) diff --git a/TSI_Config.pro b/TSI_Config.pro index 209e965..af548a8 100644 --- a/TSI_Config.pro +++ b/TSI_Config.pro @@ -1,4 +1,4 @@ -QT += core gui +QT += core gui network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -10,6 +10,8 @@ CONFIG += c++11 SOURCES += \ acceleration.cpp \ + common.cpp \ + ftpclient.cpp \ keyphase.cpp \ main.cpp \ mainwindow.cpp \ @@ -18,11 +20,14 @@ SOURCES += \ setpoint.cpp \ singlerelay.cpp \ tachometer.cpp \ + tcpclient.cpp \ velocity.cpp HEADERS += \ acceleration.h \ + common.h \ data_config.h \ + ftpclient.h \ keyphase.h \ mainwindow.h \ radial_vibration.h \ @@ -30,6 +35,7 @@ HEADERS += \ setpoint.h \ singlerelay.h \ tachometer.h \ + tcpclient.h \ velocity.h FORMS += \ diff --git a/data_config.h b/data_config.h index 5da692a..09f01a7 100644 --- a/data_config.h +++ b/data_config.h @@ -4,6 +4,9 @@ #include #include +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; + #define CHANNLE_COUNT 4 typedef struct { int slot; @@ -72,4 +75,23 @@ typedef struct{ int comparision_percentage; } Alert_Variables; +#pragma pack(1) +typedef struct { + uint8_t head[3]; // 固定值:0xAA55AA + uint8_t cmd; // 命令 + int len; // 数据长度 + uint8_t crc; // 数据 CRC 校验和 + char data[0]; // 文件内容 +} PackageHead; +typedef struct { + uint8_t card_id; // 0xff是本机,其它子卡是1~15 + char data[0]; +} UpgradeCardReq; + +typedef struct { + uint8_t code; // 0: 上传成功 +} UpgradeRsp; + +#pragma pack() + #endif // DATA_CONFIG_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 3de62d2..7a092e8 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -11,6 +11,11 @@ #include "seismic_monitor.h" #include "setpoint.h" #include +#include +#include +#include +#include +#include "ftpclient.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -40,7 +45,9 @@ MainWindow::MainWindow(QWidget *parent) createMenu(); connect(btnGroup_slot, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(OnButtonGroup(QAbstractButton *))); + QSettings settingsread(QCoreApplication::applicationDirPath() + "\\config\\config.ini",QSettings::IniFormat); + m_strServerIp = settingsread.value("Server/IP").toString(); } MainWindow::~MainWindow() @@ -133,6 +140,7 @@ void MainWindow::createMenu(const QString& rootTitle, QPushButton* parent ) mainMenu->addMenu(relays); mainMenu->addMenu(keyphasor); QAction *reset = mainMenu->addAction("重置模块"); + QAction *upgrade = mainMenu->addAction("升级固件"); parent->setContextMenuPolicy(Qt::CustomContextMenu); connect(parent,&QPushButton::customContextMenuRequested,[=](const QPoint &pos) { @@ -148,6 +156,7 @@ void MainWindow::createMenu(const QString& rootTitle, QPushButton* parent ) QObject::connect(keyphasor_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); QObject::connect(keyphasor_2, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); QObject::connect(reset, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); + QObject::connect(upgrade, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); } @@ -244,9 +253,7 @@ void MainWindow::onMenuActionTriggered() button->setText(action->text()); } - QString reset_slot = action->text(); - qDebug() << "action->text():" << action->text() << reset_slot; - if(reset_slot == "重置模块"){ + if(action->text() == "重置模块"){ if(map_slot_config[button_id].rack_type == "TMR1"){ map_slot_config[button_id].slot_type = 0; map_slot_config[button_id].rack_type = "0"; @@ -314,6 +321,8 @@ void MainWindow::onMenuActionTriggered() map_slot_config[button_id].slot_btn->setText(""); map_slot_config[button_id].chan_display = ""; } + }else if(action->text() == "升级固件"){ + sendUpgradePackage(button_id-3); } // QMenu* menu = button->menu(); // if (!menu) { @@ -433,4 +442,95 @@ void MainWindow::on_pushButton_open_clicked() { } +uint8_t calculate_crc(uint8_t c,const QByteArray &data) { + uint8_t crc = c; // 初始化 CRC 为 0 + for (int i = 0; i < data.size(); ++i) { + crc += static_cast(data[i]); // 累加每个字节 + } + return crc; +} +uint32_t myHtonl(uint32_t value) { + return ((value >> 24) & 0x000000FF) | // 提取最高的8位 + ((value >> 8) & 0x0000FF00) | // 提取中间的8位 + ((value << 8) & 0x00FF0000) | // 提取次高的8位 + ((value << 24) & 0xFF000000); // 提取最低的8位 +} +void MainWindow::sendUpgradePackage(int slot) +{ + QString filepath = QFileDialog::getOpenFileName(this, tr("选择文件"), tr(""), tr("*")); + qDebug() << filepath << slot << endl; + QFileInfo fileinfo; + fileinfo = QFileInfo(filepath); + QString file_suffix = fileinfo.suffix(); + QString FileName = fileinfo.fileName(); + if(FileName.isEmpty()) + return; + QFile file(filepath); + if (!file.open(QIODevice::ReadOnly)) { + qWarning() << "Failed to open update file."; + return; + } + QTcpSocket socket; + + // 连接到服务器 + socket.connectToHost(m_strServerIp, 10000); + if (!socket.waitForConnected()) { + qDebug() << "Connection failed!"; + return; + } + + // 读取文件内容 + QByteArray fileData = file.readAll(); + int fileSize = fileData.size(); + qDebug() << "fileSize1" << fileSize; + fileSize = myHtonl(fileSize); + qDebug() << "fileSize2" << fileSize; + + // 创建 PackageHead 结构体 + PackageHead header = { {0xAA, 0x55, 0xAA}, 3, fileSize,0,{} }; + // 计算文件的 CRC 校验和 + + qDebug() << "filheader.file_md5" << slot <(&header), sizeof(PackageHead)); + + // 发送文件内容 + socket.write(packet); // 发送头部 + socket.waitForBytesWritten(); + + QByteArray packet2(reinterpret_cast(&upgrade_car_req), sizeof(UpgradeCardReq)); + socket.write(packet2); // 发送头部 + socket.waitForBytesWritten(); + + socket.write(fileData); // 发送文件数据 + socket.waitForBytesWritten(); + + qDebug() << "File sent successfully"; + // 等待服务器响应(根据需要处理响应) + + if (socket.waitForReadyRead()) { + QByteArray response = socket.readAll(); + UpgradeRsp resp; + QByteArray byteArray = response.mid(sizeof(PackageHead)); + QDataStream stream(&byteArray, QIODevice::ReadOnly); + stream >> resp.code ; + if(resp.code == 1){ + QMessageBox::information(this, QStringLiteral("提示"), "上传成功!"); + } + qDebug() << "Server response: " << resp.code; + } + // 关闭文件和连接 + file.close(); + socket.disconnectFromHost(); +} diff --git a/mainwindow.h b/mainwindow.h index 21150af..a965ffc 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -33,14 +33,14 @@ private: QMap map_slot_config; QButtonGroup * btnGroup_slot = nullptr; - + QString m_strServerIp; // 服务端IP void createMenu(); void createMenu(const QString& rootTitle, QPushButton* button = nullptr); void createMenuSet(const QString& rootTitle, QPushButton* button = nullptr); void clearMenuProperties(QMenu* menu); void readJsonFile(const QString &filePath); - + void sendUpgradePackage(int slot); private slots: void OnButtonGroup(QAbstractButton *);