From 5b93c1e05511a3951c572a5cc34821b91cc355c4 Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Thu, 13 Mar 2025 15:10:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96scoket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TSI_Config.pro | 2 + data_config.h | 3 +- mainwindow.cpp | 161 ++++++++++++++++++++++++++++--------------------- mainwindow.h | 13 +++- 4 files changed, 107 insertions(+), 72 deletions(-) diff --git a/TSI_Config.pro b/TSI_Config.pro index 0e65fdb..cf821d8 100644 --- a/TSI_Config.pro +++ b/TSI_Config.pro @@ -9,6 +9,7 @@ CONFIG += c++11 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + MyTcpClient.cpp \ acceleration.cpp \ common.cpp \ keyphase.cpp \ @@ -23,6 +24,7 @@ SOURCES += \ velocity.cpp HEADERS += \ + MyTcpClient.h \ acceleration.h \ common.h \ data_config.h \ diff --git a/data_config.h b/data_config.h index dd0bdd9..d7be396 100644 --- a/data_config.h +++ b/data_config.h @@ -26,7 +26,8 @@ enum CMTCommand { kUpgradeCard = 3, kGetVersionInfo = 4, kRelaySetting = 5, - kRelayStatus = 6 + kRelayStatus = 6, + kUpgradeProgress = 7 }; enum SlotType{ POWER = 10, diff --git a/mainwindow.cpp b/mainwindow.cpp index 6b40d38..ec3127e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -5,7 +5,7 @@ #include #include #include -#include + #include "keyphase.h" #include "singlerelay.h" #include "tachometer.h" @@ -18,7 +18,7 @@ #include #include #include "relaysetting.h" -#include "common.h" + QString g_strServerIp; MainWindow::MainWindow(QWidget *parent) @@ -35,10 +35,22 @@ MainWindow::MainWindow(QWidget *parent) ui->widget_body->installEventFilter(this); ui->widget_body->setProperty("flag", "body"); ui->statusBar->setProperty("flag","status"); + + // 创建进度条 + progressBar = new QProgressBar(this); + + // 设置进度条的范围(0到100) + progressBar->setRange(0, 100); + + // 将进度条添加到状态栏 + statusBar()->addWidget(progressBar); + progressBar->setVisible(false); // 初始隐藏 + progressBar->setFixedWidth(300); + //this->initStyle(); //添加信号槽 QObject::connect(ui->action_realy, &QAction::triggered, this, &MainWindow::onMenuAction_relay); - slot = -1; + slot_no = -1; btnGroup_slot = new QButtonGroup(this); btnGroup_slot->addButton(ui->pushButton_slot0); btnGroup_slot->addButton(ui->pushButton_slot1); @@ -84,7 +96,7 @@ MainWindow::MainWindow(QWidget *parent) QSettings settingsread(QCoreApplication::applicationDirPath() + "\\config\\config.ini",QSettings::IniFormat); g_strServerIp = settingsread.value("Server/IP").toString(); - + connectServer(); // 设置自定义日志处理函数 #ifndef QT_DEBUG qInstallMessageHandler(messageHandler); @@ -96,6 +108,34 @@ MainWindow::~MainWindow() { delete ui; } + +void MainWindow::connectServer() +{ + + + // 连接服务器 + client.connectToServer(g_strServerIp, 10000); + //client.sendData("send_buf", 1); + // // 监听信号 + // QObject::connect(&client, &MyTcpClient::connected, []() { + // qDebug() << "Client connected!"; + // }); + + // // QObject::connect(&client, &MyTcpClient::dataReceived, [](const QByteArray &data) { + // // qDebug() << "Received from server:" << data; + // // }); + connect(&client, SIGNAL(dataReceived(const QByteArray&)), this, SLOT(readData(const QByteArray&))); + connect(&client, SIGNAL(disconnected()), this, SLOT(onDisConnected())); + // // QObject::connect(&client, &MyTcpClient::errorOccurred, [](const QString &error) { + // // qDebug() << "Error:" << error; + // // }); + // socket = new QTcpSocket(this); + // connect(socket, &QTcpSocket::connected, this, &MainWindow::onConnected); + // connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readData); + // socket->connectToHost(g_strServerIp, 10000); + +} + void MainWindow::initStyle() { //加载样式表 @@ -542,22 +582,10 @@ void MainWindow::sendUpgradePackage(int slot) qWarning() << "Failed to open update file."; return; } - QTcpSocket socket; - - // 连接到服务器 - QNetworkProxy proxy; - proxy.setType(QNetworkProxy::NoProxy); // 不使用代理 - socket.connectToHost(g_strServerIp, 10000); - socket.setProxy(proxy); - if (!socket.waitForConnected()) { - qDebug() << "Connection failed!"; - return; - } // 读取文件内容 QByteArray fileData = file.readAll(); int fileSize = fileData.size(); - //fileSize = myHtonl(fileSize); qDebug() << "fileSize" << fileSize <(&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(); - //QByteArray send_buf; char *send_buf = NULL; send_buf = (char *)malloc(sizeof(PackageHead) + sizeof(UpgradeCardReq) + fileData.size() + 1); memset(send_buf,0,sizeof(PackageHead) + sizeof(UpgradeCardReq) + fileData.size() + 1); @@ -609,45 +624,27 @@ void MainWindow::sendUpgradePackage(int slot) }else{ chunkSize = totalBytes - bytesSent; } - qint64 bytesWritten = socket.write(send_buf + bytesSent, chunkSize); - socket.waitForBytesWritten(); + qint64 bytesWritten = client.sendData(send_buf + bytesSent, chunkSize); qDebug() << "bytesWritten" << bytesWritten << "bytesSent" << bytesSent << endl; if (bytesWritten == -1) { - // Handle error break; } bytesSent += bytesWritten; } - 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; - } - // 关闭文件和连接 + qDebug() << "bytesSent" << bytesSent << endl; + client.waitForRead(); + progressBar->reset(); + progressBar->setVisible(true); + QString upgrade_text = QStringLiteral("正在上传板卡 [ %1 ] …… %p%").arg(slot); + progressBar->setTextVisible(true); + progressBar->setFormat(upgrade_text); file.close(); - socket.disconnectFromHost(); if(send_buf != NULL) free(send_buf); } void MainWindow::getVersion(int slot) { - // 连接到服务器 - QTcpSocket socket; - // 连接到服务器 - QNetworkProxy proxy; - proxy.setType(QNetworkProxy::NoProxy); // 不使用代理 - socket.connectToHost(g_strServerIp, 10000); - socket.setProxy(proxy); - if (!socket.waitForConnected()) { - qDebug() << "Connection failed!"; - return; - } + slot_no = slot; PackageHead header = { {0xAA, 0x55, 0xAA}, kGetVersionInfo, 1,0,{} }; qDebug() << "slot" << slot <showMessage("连接失败!", 10000); // 显示3秒 } +void MainWindow::readData(const QByteArray& data) +{ + qDebug() << "Received from server:" << data; + PackageHead header; + memcpy(&header,data.data(),sizeof(PackageHead)); + if(header.cmd == kGetVersionInfo){ + VersionRsp version_rsp; + memcpy(&version_rsp,data.data() + sizeof(PackageHead),sizeof(VersionRsp)); + QString strVerion = QString("第 %1 板卡\nFPGA 版本:%2\n软件版本:%3\nFPGA版本日期:%4").arg(slot_no).arg(version_rsp.fpga).arg(version_rsp.sw).arg(version_rsp.fpga_data); + QMessageBox::information(this, QStringLiteral("提示"), strVerion); + }else if(header.cmd == kUpgradeCard){ + UpgradeRsp resp; + QByteArray byteArray = data.mid(sizeof(PackageHead)); + QDataStream stream(&byteArray, QIODevice::ReadOnly); + stream >> resp.code ; + // if(resp.code == 1){ + // QMessageBox::information(this, QStringLiteral("提示"), "上传成功!"); + // } + }else if(header.cmd == kUpgradeProgress){ + QByteArray byteArray = data.mid(sizeof(PackageHead)); + UpgradeRsp upgrade_resp; + QDataStream stream(&byteArray, QIODevice::ReadOnly); + stream >> upgrade_resp.code ; + progressBar->setValue(upgrade_resp.code); + if(upgrade_resp.code == 100){ + progressBar->setVisible(false); + statusBar()->showMessage("升级完成!", 3000); // 显示3秒 + } + //qDebug() << "Server response: " << upgrade_resp.code; + } +} diff --git a/mainwindow.h b/mainwindow.h index a71c01b..77da011 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -5,7 +5,10 @@ #include #include #include +#include +#include "MyTcpClient.h" #include "data_config.h" +#include QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -37,7 +40,11 @@ private: QButtonGroup * btnGroup_slot = nullptr; - int slot; + int slot_no = 0; + QTcpSocket *socket; + MyTcpClient client; + + QProgressBar *progressBar; void createMenu(); void createMenu(const QString& rootTitle, QPushButton* button = nullptr); @@ -48,7 +55,11 @@ private: void sendUpgradePackage(int slot); void getVersion(int slot); void initStyle(); + void connectServer(); private slots: + + void onDisConnected(); + void readData(const QByteArray&); void onMenuAction_relay(); void OnButtonGroup(QAbstractButton *);