完成串口配置窗口的文件上传处理

main
HwangKC 2024-11-15 18:23:33 +08:00
parent 440137cac2
commit 10b154e341
5 changed files with 126 additions and 38 deletions

View File

@ -5,8 +5,16 @@
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QPushButton> #include <QPushButton>
#include <QMessageBox> #include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QByteArray>
#include <QDebug>
#include <hv/hlog.h>
#include "frame_define.h"
#include "globalparameters.h" #include "globalparameters.h"
#include "kutilities.h"
FormSerialPortSettingDialog::FormSerialPortSettingDialog(QWidget *parent) FormSerialPortSettingDialog::FormSerialPortSettingDialog(QWidget *parent)
: QDialog(parent) : QDialog(parent)
@ -197,5 +205,77 @@ void FormSerialPortSettingDialog::onCancelClicked()
void FormSerialPortSettingDialog::onToolButtonClicked() 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;
} }

View File

@ -449,7 +449,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>640</x> <x>640</x>
<y>391</y> <y>390</y>
<width>41</width> <width>41</width>
<height>25</height> <height>25</height>
</rect> </rect>

View File

@ -1,4 +1,4 @@
#pragma once #pragma once
#pragma pack(1) #pragma pack(1)
#include "kdefine.h" #include "kdefine.h"
@ -16,27 +16,33 @@ typedef enum tagErrorCode : unsigned char
ERR_INVALID_BUF_LEN = 0X03, ERR_INVALID_BUF_LEN = 0X03,
ERR_INVALID_JSON_FMT = 0X04, ERR_INVALID_JSON_FMT = 0X04,
ERR_INVALID_FSUCODE = 0X05, ERR_INVALID_FSUCODE = 0X05,
ERR_INVALID_CFG_CONTENT = 0X06, // 文件内容错误
ERR_RETRANS_CONTENT = 0X07, // 重传文件内容
ERR_PERMISSION_DENIED = 0X08, //写文件权限不够
ERR_UNKOWN = 0XFF ERR_UNKOWN = 0XFF
}ErrorCode; } ErrorCode;
typedef enum tagFrameType : unsigned char typedef enum tagFrameType : unsigned char
{ {
Frame_Response = 0x00, //返回帧 Frame_Response = 0x00, //返回帧
Frame_Request = 0x01, //请求帧 Frame_Request = 0x01, //请求帧
Frame_Echo_Request = 0x02, //测试请求帧 Frame_Echo_Request = 0x02, //测试请求帧
Frame_DeviceData_Request = 0x03, //来自采集程序的数据请求包,将数据保存到数据库中 Frame_DeviceData_Request = 0x03, //来自采集程序的数据请求包,将数据保存到数据库中
}FrameType; Frame_Configure_DB_Request = 0x04, //上传配置文件
} FrameType;
typedef struct tagFrameTail typedef struct tagFrameTail
{ {
unsigned char frame_delimiter[4] = { 0xEE,0xFF,0xEE,0xFF }; unsigned char frame_delimiter[4] = { 0xEE,0xFF,0xEE,0xFF };
}FrameTail; } FrameTail;
#define FRAME_HEADER_LENGTH (5) // frame_type(1) + frame_len(4)
typedef struct tagFrame typedef struct tagFrame
{ {
FrameType frame_type; //帧类型 FrameType frame_type; //帧类型
unsigned int frame_len; //帧数据长度 unsigned int frame_len; //帧数据长度
unsigned char frame_content[1]; //帧的内容实际应为json字符串由json内容自解释 unsigned char frame_content[1]; //帧的内容实际应为json字符串由json内容自解释
tagFrame() tagFrame()
{ {
frame_type = Frame_Response; frame_type = Frame_Response;
@ -48,7 +54,7 @@ typedef struct tagFrame
frame_len = 1; frame_len = 1;
frame_content[0] = err; frame_content[0] = err;
} }
}MessageFrame; } MessageFrame;
__NAMESPACE_END__(HJ) __NAMESPACE_END__(HJ)

View File

@ -42,6 +42,7 @@ MainDialog::MainDialog(QWidget *parent) :
MainDialog::~MainDialog() MainDialog::~MainDialog()
{ {
delete m_pStackedWidgetLayout;
delete ui; delete ui;
} }
@ -160,11 +161,10 @@ void MainDialog::CreateTablePage()
{ {
m_pDevicestackedWidget = new QStackedWidget(this); m_pDevicestackedWidget = new QStackedWidget(this);
QVBoxLayout *stackedWidgetLayout = new QVBoxLayout(m_pDevicestackedWidget); m_pStackedWidgetLayout = new QVBoxLayout(m_pDevicestackedWidget);
#if 1 #if 1
//m_pDevicestackedWidget->setLayout(stackedWidgetLayout); m_pStackedWidgetLayout->addWidget(m_pDevicestackedWidget);
stackedWidgetLayout->addWidget(m_pDevicestackedWidget);
DevicePropertyPage *page1 = new DevicePropertyPage(m_pDevicestackedWidget); DevicePropertyPage *page1 = new DevicePropertyPage(m_pDevicestackedWidget);
DevicePropertyPage *page2 = 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 5"));
m_pDevicestackedWidget->addWidget(new QPushButton("Page 6")); m_pDevicestackedWidget->addWidget(new QPushButton("Page 6"));
#endif #endif
// setLayout(stackedWidgetLayout);
} }
void MainDialog::CreateIcon(const QIcon& icon,QString text) void MainDialog::CreateIcon(const QIcon& icon,QString text)

View File

@ -8,6 +8,8 @@
#include <QIcon> #include <QIcon>
#include <QStackedWidget> #include <QStackedWidget>
#include <QToolBar> #include <QToolBar>
#include <QVBoxLayout>
#include <hv_tcpclient.h> #include <hv_tcpclient.h>
namespace Ui namespace Ui
@ -44,6 +46,7 @@ private:
QListWidget *m_pDeviceListWidget; QListWidget *m_pDeviceListWidget;
QStackedWidget *m_pDevicestackedWidget; QStackedWidget *m_pDevicestackedWidget;
QToolBar *m_pMainToolBar; QToolBar *m_pMainToolBar;
QVBoxLayout* m_pStackedWidgetLayout;
private: private: