添加代码

This commit is contained in:
zhangsheng 2025-05-06 10:17:16 +08:00
parent 43a6bfff3c
commit ee2d142182
7 changed files with 84 additions and 68 deletions

View File

@ -95,7 +95,6 @@ void MyTcpClient::onReadyRead() {
} else {
// 2. 根据头部的 len 读取剩余数据
qint64 remainingBytes = m_currentHead.len - (m_buffer.size() - sizeof(PackageHead));
if (remainingBytes <= 0) {
// 数据已经完整,触发信号
emit dataReceived(m_buffer);

View File

@ -26,5 +26,6 @@ void Connect::on_pushButton_connect_clicked()
m_tcpClient = MyTcpClient::instance();
// 连接服务器
m_tcpClient->connectToServer(g_strServerIp, 10000);
this->close();
}

View File

@ -12,7 +12,7 @@ extern QString g_strServerIp; // 服务端IP
#define SLOT_NUM 15
#define CHANNEL_COUNT 4
#define RELAY_COUNT 8
#define RELAY_COUNT 16
typedef enum {
kCardNone = 0,
@ -285,7 +285,7 @@ typedef struct TMRRelay_{
typedef struct {
uint8_t head[3]; // 固定值0xAA55AA
uint8_t cmd; // 命令
int len; // 数据长度
int len; // 数据长度
uint8_t crc; // 数据 CRC 校验和
char data[0]; // 文件内容
} PackageHead;

View File

@ -37,6 +37,7 @@ MainWindow::MainWindow(QWidget *parent)
// this->setMenuBar(menuBar); //添加到对象树
// menuBar->addMenu(ui->menu_start);
current_slot = -1;
tsi_config_file = "";
ui->widget_body->setProperty("flag", "title");
ui->menuBar->setProperty("flag", "menuBar");
//关联事件过滤器用于双击放大
@ -99,7 +100,7 @@ MainWindow::MainWindow(QWidget *parent)
QSettings settingsread(QCoreApplication::applicationDirPath() + "\\config\\config.ini", QSettings::IniFormat);
g_strServerIp = settingsread.value("Server/IP").toString();
//connectServer();
connectServer();
// 设置自定义日志处理函数
#ifndef QT_DEBUG
//qInstallMessageHandler(messageHandler);
@ -127,8 +128,6 @@ void MainWindow::connectServer() {
connect(m_tcpClient, SIGNAL(disconnected()), this, SLOT(onDisConnected()));
connect(m_tcpClient, SIGNAL(connected()), this, SLOT(onConnected()));
connect(m_tcpClient, SIGNAL(errorOccurred(const QString &)), this, SLOT(onerrorOccurred(const QString &)));
// 连接服务器
//m_tcpClient->connectToServer(g_strServerIp, 10000);
}
void MainWindow::initStyle() {
@ -573,73 +572,25 @@ void MainWindow::on_pushButton_save_clicked() {
QString file_name = filepath + "\\tsi_config_file.json";
ConfigMgr::Instance()->Save(file_name);
return;
// 读取文件内容
QFile file(file_name);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open update file.";
return;
}
QByteArray fileData = file.readAll();
int fileSize = fileData.size();
if (fileSize > 10 * 1024 * 1024) {
QMessageBox::information(this, QStringLiteral("提示"), "文件大小超过10M请重新选择");
file.close();
return;
}
qDebug() << "fileSize" << fileSize ;
// 创建 PackageHead 结构体
PackageHead header = { {0xAA, 0x55, 0xAA}, kUploadConfigFile, fileSize, 0, {} };
// 计算文件的 CRC 校验和
header.crc = calculate_crc(0, fileData);
header.len = fileSize;
char *send_buf = NULL;
send_buf = (char *)malloc(sizeof(PackageHead) + fileData.size() + 1);
memset(send_buf, 0, sizeof(PackageHead) + fileData.size() + 1);
memcpy(send_buf, &header, sizeof(PackageHead));
memcpy(send_buf + sizeof(PackageHead), fileData.data(), fileData.size());
int length = sizeof(PackageHead) + fileData.size();
const int MAX_CHUNK_SIZE = 50 * 1024; // 50 KB
qint64 bytesSent = 0;
qint64 totalBytes = length;
qDebug() << "totalBytes" << totalBytes ;
while (bytesSent < totalBytes) {
qint64 chunkSize = 0;
if (MAX_CHUNK_SIZE < totalBytes - bytesSent) {
chunkSize = MAX_CHUNK_SIZE;
} else {
chunkSize = totalBytes - bytesSent;
}
qint64 bytesWritten = m_tcpClient->sendData(send_buf + bytesSent, chunkSize);
qDebug() << "bytesWritten" << bytesWritten << "bytesSent" << bytesSent << "chunkSize" << chunkSize ;
if (bytesWritten == -1) {
break;
}
bytesSent += bytesWritten;
}
qDebug() << "bytesSent" << bytesSent;
m_tcpClient->waitForRead();
file.close();
if (send_buf != NULL) {
free(send_buf);
}
}
void MainWindow::on_pushButton_open_clicked() {
QString filepath = QFileDialog::getOpenFileName(this, tr("选择文件"), tr(""), tr("*.json"));
tsi_config_file = QFileDialog::getOpenFileName(this, tr("选择文件"), tr(""), tr("*.json"));
QFileInfo fileinfo;
fileinfo = QFileInfo(filepath);
fileinfo = QFileInfo(tsi_config_file);
QString file_suffix = fileinfo.suffix();
QString FileName = fileinfo.fileName();
if (FileName.isEmpty()) {
return;
}
QFile file(filepath);
QFile file(tsi_config_file);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open update file.";
return;
}
ConfigMgr::Instance()->Load(filepath);
ConfigMgr::Instance()->Load(tsi_config_file);
QList<QAbstractButton *> buttonList = btnGroup_slot->buttons();
for (int i = 0; i < buttonList.count(); i++) {
std::shared_ptr<CardBase> base_ptr = ConfigMgr::Instance()->GetSlotPtr(i + 1);
@ -813,7 +764,7 @@ void MainWindow::readData(const QByteArray &data) {
config_file = (char*)malloc(header.len + 1);
memset(config_file,0,header.len + 1);
memcpy(config_file, data.data() + sizeof(PackageHead), header.len);
QString filename_ = QCoreApplication::applicationDirPath() + "\\config\\tsi_config_file2.json";
QString filename_ = QCoreApplication::applicationDirPath() + "\\config\\tsi_config_file_download.json";
//qDebug() << config_file ;
QFile file(filename_);
file.open(QIODevice::WriteOnly);
@ -854,3 +805,68 @@ void MainWindow::onMACConfig(){
mac_config->setWindowModality(Qt::ApplicationModal);
mac_config->show();
}
void MainWindow::on_pushButton_upload_clicked()
{
QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "确认上传配置文件?", QMessageBox::Yes | QMessageBox::No, this);
box->button(QMessageBox::Yes)->setText("确认");
box->button(QMessageBox::No)->setText("取消");
int res = box->exec();
if(QMessageBox::No == res){
return;
}
// 读取文件内容
if(tsi_config_file == ""){
QMessageBox::information(this, QStringLiteral("提示"), "请先打开配置文件!");
return;
}
QFile file(tsi_config_file);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open config file.";
return;
}
QByteArray fileData = file.readAll();
int fileSize = fileData.size();
if (fileSize > 10 * 1024 * 1024) {
QMessageBox::information(this, QStringLiteral("提示"), "文件大小超过10M请重新选择");
file.close();
return;
}
qDebug() << "fileSize" << fileSize ;
// 创建 PackageHead 结构体
PackageHead header = { {0xAA, 0x55, 0xAA}, kUploadConfigFile, fileSize, 0, {} };
// 计算文件的 CRC 校验和
header.crc = calculate_crc(0, fileData);
header.len = fileSize;
char *send_buf = NULL;
send_buf = (char *)malloc(sizeof(PackageHead) + fileData.size() + 1);
memset(send_buf, 0, sizeof(PackageHead) + fileData.size() + 1);
memcpy(send_buf, &header, sizeof(PackageHead));
memcpy(send_buf + sizeof(PackageHead), fileData.data(), fileData.size());
int length = sizeof(PackageHead) + fileData.size();
const int MAX_CHUNK_SIZE = 50 * 1024; // 50 KB
qint64 bytesSent = 0;
qint64 totalBytes = length;
qDebug() << "totalBytes" << totalBytes ;
while (bytesSent < totalBytes) {
qint64 chunkSize = 0;
if (MAX_CHUNK_SIZE < totalBytes - bytesSent) {
chunkSize = MAX_CHUNK_SIZE;
} else {
chunkSize = totalBytes - bytesSent;
}
qint64 bytesWritten = m_tcpClient->sendData(send_buf + bytesSent, chunkSize);
qDebug() << "bytesWritten" << bytesWritten << "bytesSent" << bytesSent << "chunkSize" << chunkSize ;
if (bytesWritten == -1) {
break;
}
bytesSent += bytesWritten;
}
qDebug() << "bytesSent" << bytesSent;
m_tcpClient->waitForRead();
file.close();
if (send_buf != NULL) {
free(send_buf);
}
}

View File

@ -47,6 +47,7 @@ private:
QProgressBar *progressBar;
CardType card_type;
int current_slot;
QString tsi_config_file;
void createMenu();
void createMenu(const QString& rootTitle, QPushButton* button = nullptr);
@ -58,12 +59,13 @@ private:
void getVersion(int slot);
void initStyle();
void connectServer();
void onerrorOccurred(const QString &errorMsg);
private slots:
void onDisConnected();
void onConnected();
void readData(const QByteArray&);
void onerrorOccurred(const QString &errorMsg);
void onMenuAction_relay();
void OnButtonGroup(QAbstractButton *);
void onConnect();
@ -79,5 +81,6 @@ private slots:
void on_pushButton_save_clicked();
void on_pushButton_open_clicked();
void on_pushButton_download_clicked();
void on_pushButton_upload_clicked();
};
#endif // MAINWINDOW_H

View File

@ -97,10 +97,10 @@
<string>打开</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_22">
<widget class="QPushButton" name="pushButton_upload">
<property name="geometry">
<rect>
<x>200</x>
<x>140</x>
<y>20</y>
<width>45</width>
<height>45</height>
@ -119,13 +119,13 @@
</size>
</property>
<property name="text">
<string/>
<string>上传</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_download">
<property name="geometry">
<rect>
<x>140</x>
<x>190</x>
<y>20</y>
<width>45</width>
<height>45</height>

View File

@ -22,9 +22,6 @@ class Radial : public QWidget {
void on_checkBox_1x_ampl_toggled(bool checked);
void on_checkBox_2x_ampl_toggled(bool checked);
void on_checkBox_not_1x_ampl_toggled(bool checked);
void on_checkBox_smax_ampl_toggled(bool checked);
private:
Ui::Radial_vibration *ui;