添加固件更新

This commit is contained in:
zhangsheng 2025-03-01 13:40:11 +08:00
parent 88b9de608a
commit b5038030c6
4 changed files with 134 additions and 6 deletions

View File

@ -1,4 +1,4 @@
QT += core gui QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
@ -10,6 +10,8 @@ CONFIG += c++11
SOURCES += \ SOURCES += \
acceleration.cpp \ acceleration.cpp \
common.cpp \
ftpclient.cpp \
keyphase.cpp \ keyphase.cpp \
main.cpp \ main.cpp \
mainwindow.cpp \ mainwindow.cpp \
@ -18,11 +20,14 @@ SOURCES += \
setpoint.cpp \ setpoint.cpp \
singlerelay.cpp \ singlerelay.cpp \
tachometer.cpp \ tachometer.cpp \
tcpclient.cpp \
velocity.cpp velocity.cpp
HEADERS += \ HEADERS += \
acceleration.h \ acceleration.h \
common.h \
data_config.h \ data_config.h \
ftpclient.h \
keyphase.h \ keyphase.h \
mainwindow.h \ mainwindow.h \
radial_vibration.h \ radial_vibration.h \
@ -30,6 +35,7 @@ HEADERS += \
setpoint.h \ setpoint.h \
singlerelay.h \ singlerelay.h \
tachometer.h \ tachometer.h \
tcpclient.h \
velocity.h velocity.h
FORMS += \ FORMS += \

View File

@ -4,6 +4,9 @@
#include <QPushButton> #include <QPushButton>
#include <QList> #include <QList>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
#define CHANNLE_COUNT 4 #define CHANNLE_COUNT 4
typedef struct { typedef struct {
int slot; int slot;
@ -72,4 +75,23 @@ typedef struct{
int comparision_percentage; int comparision_percentage;
} Alert_Variables; } 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是本机其它子卡是115
char data[0];
} UpgradeCardReq;
typedef struct {
uint8_t code; // 0: 上传成功
} UpgradeRsp;
#pragma pack()
#endif // DATA_CONFIG_H #endif // DATA_CONFIG_H

View File

@ -11,6 +11,11 @@
#include "seismic_monitor.h" #include "seismic_monitor.h"
#include "setpoint.h" #include "setpoint.h"
#include <QMessageBox> #include <QMessageBox>
#include <QFileDialog>
#include <stdio.h>
#include <QSysInfo>
#include <qsettings.h>
#include "ftpclient.h"
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
@ -40,7 +45,9 @@ MainWindow::MainWindow(QWidget *parent)
createMenu(); createMenu();
connect(btnGroup_slot, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(OnButtonGroup(QAbstractButton *))); 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() MainWindow::~MainWindow()
@ -133,6 +140,7 @@ void MainWindow::createMenu(const QString& rootTitle, QPushButton* parent )
mainMenu->addMenu(relays); mainMenu->addMenu(relays);
mainMenu->addMenu(keyphasor); mainMenu->addMenu(keyphasor);
QAction *reset = mainMenu->addAction("重置模块"); QAction *reset = mainMenu->addAction("重置模块");
QAction *upgrade = mainMenu->addAction("升级固件");
parent->setContextMenuPolicy(Qt::CustomContextMenu); parent->setContextMenuPolicy(Qt::CustomContextMenu);
connect(parent,&QPushButton::customContextMenuRequested,[=](const QPoint &pos) 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_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(keyphasor_2, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); QObject::connect(keyphasor_2, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(reset, &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()); button->setText(action->text());
} }
QString reset_slot = action->text(); if(action->text() == "重置模块"){
qDebug() << "action->text()" << action->text() << reset_slot;
if(reset_slot == "重置模块"){
if(map_slot_config[button_id].rack_type == "TMR1"){ if(map_slot_config[button_id].rack_type == "TMR1"){
map_slot_config[button_id].slot_type = 0; map_slot_config[button_id].slot_type = 0;
map_slot_config[button_id].rack_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].slot_btn->setText("");
map_slot_config[button_id].chan_display = ""; map_slot_config[button_id].chan_display = "";
} }
}else if(action->text() == "升级固件"){
sendUpgradePackage(button_id-3);
} }
// QMenu* menu = button->menu(); // QMenu* menu = button->menu();
// if (!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<uint8_t>(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 <<endl;
UpgradeCardReq upgrade_car_req;
if(slot == 0)
upgrade_car_req.card_id = 0xFF;
else
upgrade_car_req.card_id = slot & 0xFF;
header.crc = calculate_crc(upgrade_car_req.card_id,fileData);
qDebug() << "filheader.crc" << header.crc;
// 发送头部数据
QByteArray packet(reinterpret_cast<char*>(&header), sizeof(PackageHead));
// 发送文件内容
socket.write(packet); // 发送头部
socket.waitForBytesWritten();
QByteArray packet2(reinterpret_cast<char*>(&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();
}

View File

@ -33,14 +33,14 @@ private:
QMap<int,SlotConfig> map_slot_config; QMap<int,SlotConfig> map_slot_config;
QButtonGroup * btnGroup_slot = nullptr; QButtonGroup * btnGroup_slot = nullptr;
QString m_strServerIp; // 服务端IP
void createMenu(); void createMenu();
void createMenu(const QString& rootTitle, QPushButton* button = nullptr); void createMenu(const QString& rootTitle, QPushButton* button = nullptr);
void createMenuSet(const QString& rootTitle, QPushButton* button = nullptr); void createMenuSet(const QString& rootTitle, QPushButton* button = nullptr);
void clearMenuProperties(QMenu* menu); void clearMenuProperties(QMenu* menu);
void readJsonFile(const QString &filePath); void readJsonFile(const QString &filePath);
void sendUpgradePackage(int slot);
private slots: private slots:
void OnButtonGroup(QAbstractButton *); void OnButtonGroup(QAbstractButton *);