From 10b154e341db29a39f9e6f14f8bd24d6d7473827 Mon Sep 17 00:00:00 2001 From: HwangKC Date: Fri, 15 Nov 2024 18:23:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=B2=E5=8F=A3=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=AA=97=E5=8F=A3=E7=9A=84=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../formserialportsettingdialog.cpp | 82 ++++++++++++++++++- .../formserialportsettingdialog.ui | 2 +- applications/emsConfigurer/frame_define.h | 70 ++++++++-------- applications/emsConfigurer/maindialog.cpp | 7 +- applications/emsConfigurer/maindialog.h | 3 + 5 files changed, 126 insertions(+), 38 deletions(-) diff --git a/applications/emsConfigurer/formserialportsettingdialog.cpp b/applications/emsConfigurer/formserialportsettingdialog.cpp index 12aea25..76bc73f 100644 --- a/applications/emsConfigurer/formserialportsettingdialog.cpp +++ b/applications/emsConfigurer/formserialportsettingdialog.cpp @@ -5,8 +5,16 @@ #include #include #include +#include +#include +#include +#include +#include + +#include "frame_define.h" #include "globalparameters.h" +#include "kutilities.h" FormSerialPortSettingDialog::FormSerialPortSettingDialog(QWidget *parent) : QDialog(parent) @@ -197,5 +205,77 @@ void FormSerialPortSettingDialog::onCancelClicked() void FormSerialPortSettingDialog::onToolButtonClicked() { - QMessageBox::information(this, "onToolButtonClicked", "Refresh"); + QString filePath("d:\\test_base641.zip"); + + // 打开文件进行读取 + QFile file(filePath); + if (!file.open(QIODevice::ReadOnly)) + { + QString msg = QString(tr("Failed to open file: %1")).arg(filePath); + QMessageBox::critical(this, msg, tr("Critical")); + return; + } + + // 读取文件内容 + QByteArray fileData = file.readAll(); + int fileLen = fileData.length(); + + // 关闭文件 + file.close(); + + //构筑消息包 + int msgLen = FRAME_HEADER_LENGTH + fileLen + 4; + unsigned char* pMsg = new unsigned char[msgLen]; + memset(pMsg, 0, msgLen); + + HJ::MessageFrame* pFrame = (HJ::MessageFrame*)pMsg; + pFrame->frame_type = HJ::Frame_Configure_DB_Request; + pFrame->frame_len = fileLen; + memcpy(pMsg+FRAME_HEADER_LENGTH,(void*)fileData.data(),fileLen); + pMsg[FRAME_HEADER_LENGTH+fileLen] = 0xEE; + pMsg[FRAME_HEADER_LENGTH+fileLen+1] = 0xFF; + pMsg[FRAME_HEADER_LENGTH+fileLen+2] = 0xEE; + pMsg[FRAME_HEADER_LENGTH+fileLen+3] = 0xFF; + + int ret = -1; + + AppTcpClient::getInstance()->m_tcpclient->onMessage = [&ret](const hv::SocketChannelPtr& channel, hv::Buffer* buf) + { + hlogd("FormSerialportSettingDialog <==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str()); + HJ::MessageFrame* pRespMsg = (HJ::MessageFrame*)buf->data(); + ret = (int)(*pRespMsg->frame_content); + }; + + while(1) + { + int cnt = AppTcpClient::getInstance()->m_tcpclient->send((void*)pMsg, msgLen); + hlogd("send %d bytes configure database ...", cnt); + + { + CWaitorCursor waits; + + hv_sleep(2); + } + + hlogd("FormSerialportSettingDialog <== response from service %d ", ret); + + if (ret == HJ::ErrorCode::ERR_OK) + { + break; + } + else if (ret == HJ::ErrorCode::ERR_PERMISSION_DENIED) + { + QMessageBox::critical(this, "Critical", tr("Service responsed: Permission denyed\r\nContact Developement for supporting!")); + break; + } + else if (ret == HJ::ErrorCode::ERR_INVALID_CFG_CONTENT || ret == HJ::ErrorCode::ERR_RETRANS_CONTENT) + { + if (QMessageBox::No == QMessageBox::critical(this, "Alert",tr("Upload file failed, Press Yes to upload again!"), QMessageBox::Yes | QMessageBox::No)) + break; + } + } + + delete[] pMsg; } + + diff --git a/applications/emsConfigurer/formserialportsettingdialog.ui b/applications/emsConfigurer/formserialportsettingdialog.ui index da76240..355a45e 100644 --- a/applications/emsConfigurer/formserialportsettingdialog.ui +++ b/applications/emsConfigurer/formserialportsettingdialog.ui @@ -449,7 +449,7 @@ 640 - 391 + 390 41 25 diff --git a/applications/emsConfigurer/frame_define.h b/applications/emsConfigurer/frame_define.h index bf26eed..3dcdcc3 100644 --- a/applications/emsConfigurer/frame_define.h +++ b/applications/emsConfigurer/frame_define.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #pragma pack(1) #include "kdefine.h" @@ -10,46 +10,52 @@ __NAMESPACE_BEGIN__(HJ) typedef enum tagErrorCode : unsigned char { - ERR_OK = 0X00, - ERR_INVALID_LEN = 0X01, - ERR_INVALID_UTF8 = 0X02, - ERR_INVALID_BUF_LEN = 0X03, - ERR_INVALID_JSON_FMT = 0X04, - ERR_INVALID_FSUCODE = 0X05, - ERR_UNKOWN = 0XFF -}ErrorCode; + ERR_OK = 0X00, + ERR_INVALID_LEN = 0X01, + ERR_INVALID_UTF8 = 0X02, + ERR_INVALID_BUF_LEN = 0X03, + ERR_INVALID_JSON_FMT = 0X04, + ERR_INVALID_FSUCODE = 0X05, + ERR_INVALID_CFG_CONTENT = 0X06, // 文件内容错误 + ERR_RETRANS_CONTENT = 0X07, // 重传文件内容 + ERR_PERMISSION_DENIED = 0X08, //写文件权限不够 + ERR_UNKOWN = 0XFF +} ErrorCode; typedef enum tagFrameType : unsigned char { - Frame_Response = 0x00, //֡ - Frame_Request = 0x01, //֡ - Frame_Echo_Request = 0x02, //֡ - Frame_DeviceData_Request = 0x03, //Բɼݱ浽ݿ -}FrameType; + Frame_Response = 0x00, //返回帧 + Frame_Request = 0x01, //请求帧 + Frame_Echo_Request = 0x02, //测试请求帧 + Frame_DeviceData_Request = 0x03, //来自采集程序的数据请求包,将数据保存到数据库中 + Frame_Configure_DB_Request = 0x04, //上传配置文件 +} FrameType; typedef struct tagFrameTail { - unsigned char frame_delimiter[4] = { 0xEE,0xFF,0xEE,0xFF }; -}FrameTail; + unsigned char frame_delimiter[4] = { 0xEE,0xFF,0xEE,0xFF }; +} FrameTail; + +#define FRAME_HEADER_LENGTH (5) // frame_type(1) + frame_len(4) typedef struct tagFrame { - FrameType frame_type; //֡ - unsigned int frame_len; //֡ݳ - unsigned char frame_content[1]; //֡ݣʵӦΪjsonַjsonԽ - tagFrame() - { - frame_type = Frame_Response; - frame_len = 1; - frame_content[0] = ERR_OK; - } - void setErrorFrame(ErrorCode err = ERR_OK) - { - frame_len = 1; - frame_content[0] = err; - } -}MessageFrame; + FrameType frame_type; //帧类型 + unsigned int frame_len; //帧数据长度 + unsigned char frame_content[1]; //帧的内容,实际应为json字符串,由json内容自解释 + tagFrame() + { + frame_type = Frame_Response; + frame_len = 1; + frame_content[0] = ERR_OK; + } + void setErrorFrame(ErrorCode err = ERR_OK) + { + frame_len = 1; + frame_content[0] = err; + } +} MessageFrame; __NAMESPACE_END__(HJ) -#pragma pack() \ No newline at end of file +#pragma pack() diff --git a/applications/emsConfigurer/maindialog.cpp b/applications/emsConfigurer/maindialog.cpp index 085a5bf..9276325 100644 --- a/applications/emsConfigurer/maindialog.cpp +++ b/applications/emsConfigurer/maindialog.cpp @@ -42,6 +42,7 @@ MainDialog::MainDialog(QWidget *parent) : MainDialog::~MainDialog() { + delete m_pStackedWidgetLayout; delete ui; } @@ -160,11 +161,10 @@ void MainDialog::CreateTablePage() { m_pDevicestackedWidget = new QStackedWidget(this); - QVBoxLayout *stackedWidgetLayout = new QVBoxLayout(m_pDevicestackedWidget); + m_pStackedWidgetLayout = new QVBoxLayout(m_pDevicestackedWidget); #if 1 - //m_pDevicestackedWidget->setLayout(stackedWidgetLayout); - stackedWidgetLayout->addWidget(m_pDevicestackedWidget); + m_pStackedWidgetLayout->addWidget(m_pDevicestackedWidget); DevicePropertyPage *page1 = new DevicePropertyPage(m_pDevicestackedWidget); DevicePropertyPage *page2 = new DevicePropertyPage(m_pDevicestackedWidget); @@ -177,7 +177,6 @@ void MainDialog::CreateTablePage() m_pDevicestackedWidget->addWidget(new QPushButton("Page 5")); m_pDevicestackedWidget->addWidget(new QPushButton("Page 6")); #endif - // setLayout(stackedWidgetLayout); } void MainDialog::CreateIcon(const QIcon& icon,QString text) diff --git a/applications/emsConfigurer/maindialog.h b/applications/emsConfigurer/maindialog.h index 0708ccf..ceff044 100644 --- a/applications/emsConfigurer/maindialog.h +++ b/applications/emsConfigurer/maindialog.h @@ -8,6 +8,8 @@ #include #include #include +#include + #include namespace Ui @@ -44,6 +46,7 @@ private: QListWidget *m_pDeviceListWidget; QStackedWidget *m_pDevicestackedWidget; QToolBar *m_pMainToolBar; + QVBoxLayout* m_pStackedWidgetLayout; private: