改进部分代码

This commit is contained in:
feiyangqingyun
2021-04-13 09:36:48 +08:00
parent bc5acc612e
commit 788da958fb
40 changed files with 1181 additions and 289 deletions

View File

@@ -1,11 +1,11 @@
HEADERS += \
$$PWD/app.h \
$$PWD/appconfig.h \
$$PWD/quiwidget.h \
$$PWD/tcpserver1.h \
$$PWD/tcpserver2.h
SOURCES += \
$$PWD/app.cpp \
$$PWD/appconfig.cpp \
$$PWD/quiwidget.cpp \
$$PWD/tcpserver1.cpp \
$$PWD/tcpserver2.cpp

View File

@@ -0,0 +1,50 @@
#include "appconfig.h"
#include "quiwidget.h"
QString AppConfig::ConfigFile = "config.ini";
int AppConfig::ListenPort1 = 6907;
int AppConfig::CmdStart1 = 76;
int AppConfig::CmdLen1 = 12;
bool AppConfig::HexData1 = false;
int AppConfig::ListenPort2 = 6908;
int AppConfig::CmdStart2 = 76;
int AppConfig::CmdLen2 = 12;
bool AppConfig::HexData2 = false;
void AppConfig::readConfig()
{
if (!QUIHelper::checkIniFile(AppConfig::ConfigFile)) {
writeConfig();
return;
}
QSettings set(AppConfig::ConfigFile, QSettings::IniFormat);
set.beginGroup("AppConfig");
AppConfig::ListenPort1 = set.value("ListenPort1").toInt();
AppConfig::CmdStart1 = set.value("CmdStart1").toInt();
AppConfig::CmdLen1 = set.value("CmdLen1").toInt();
AppConfig::HexData1 = set.value("HexData1").toBool();
AppConfig::ListenPort2 = set.value("ListenPort2").toInt();
AppConfig::CmdStart2 = set.value("CmdStart2").toInt();
AppConfig::CmdLen2 = set.value("CmdLen2").toInt();
AppConfig::HexData2 = set.value("HexData2").toBool();
set.endGroup();
}
void AppConfig::writeConfig()
{
QSettings set(AppConfig::ConfigFile, QSettings::IniFormat);
set.beginGroup("AppConfig");
set.setValue("ListenPort1", AppConfig::ListenPort1);
set.setValue("CmdStart1", AppConfig::CmdStart1);
set.setValue("CmdLen1", AppConfig::CmdLen1);
set.setValue("HexData1", AppConfig::HexData1);
set.setValue("ListenPort2", AppConfig::ListenPort2);
set.setValue("CmdStart2", AppConfig::CmdStart2);
set.setValue("CmdLen2", AppConfig::CmdLen2);
set.setValue("HexData2", AppConfig::HexData2);
set.endGroup();
}

26
netserver/api/appconfig.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef APPCONFIG_H
#define APPCONFIG_H
#include "head.h"
class AppConfig
{
public:
static QString ConfigFile; //配置文件路径
static int ListenPort1; //监听端口1
static int CmdStart1; //标识符1开始
static int CmdLen1; //标识符1长度
static bool HexData1; //16进制显示
static int ListenPort2; //监听端口2
static int CmdStart2; //标识符2开始
static int CmdLen2; //标识符2长度
static bool HexData2; //16进制显示
//读写配置参数及其他操作
static void readConfig(); //读取配置参数
static void writeConfig(); //写入配置参数
};
#endif // APPCONFIG_H

View File

@@ -45,7 +45,7 @@ void TcpClient1::readData()
}
//取出唯一标识符,并过滤,可自行更改过滤条件
QByteArray cmd = data.mid(App::CmdStart1, App::CmdLen1);
QByteArray cmd = data.mid(AppConfig::CmdStart1, AppConfig::CmdLen1);
QString id = QString(cmd);
if (id.startsWith("S") && deviceID != id) {
deviceID = id;
@@ -54,7 +54,7 @@ void TcpClient1::readData()
}
QString buffer;
if (App::HexData1) {
if (AppConfig::HexData1) {
buffer = QUIHelper::byteArrayToHexStr(data);
} else {
buffer = QString(data);
@@ -66,7 +66,7 @@ void TcpClient1::readData()
void TcpClient1::sendData(const QString &data)
{
QByteArray buffer;
if (App::HexData1) {
if (AppConfig::HexData1) {
buffer = QUIHelper::hexStrToByteArray(data);
} else {
buffer = data.toLatin1();
@@ -121,9 +121,9 @@ void TcpServer1::disconnected()
bool TcpServer1::start()
{
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
bool ok = listen(QHostAddress::AnyIPv4, App::ListenPort1);
bool ok = listen(QHostAddress::AnyIPv4, AppConfig::ListenPort1);
#else
bool ok = listen(QHostAddress::Any, App::ListenPort1);
bool ok = listen(QHostAddress::Any, AppConfig::ListenPort1);
#endif
return ok;
}

View File

@@ -45,7 +45,7 @@ void TcpClient2::readData()
}
//取出唯一标识符,并过滤,可自行更改过滤条件
QByteArray cmd = data.mid(App::CmdStart2, App::CmdLen2);
QByteArray cmd = data.mid(AppConfig::CmdStart2, AppConfig::CmdLen2);
QString id = QString(cmd);
if (id.startsWith("S") && deviceID != id) {
deviceID = id;
@@ -54,7 +54,7 @@ void TcpClient2::readData()
}
QString buffer;
if (App::HexData2) {
if (AppConfig::HexData2) {
buffer = QUIHelper::byteArrayToHexStr(data);
} else {
buffer = QString(data);
@@ -66,7 +66,7 @@ void TcpClient2::readData()
void TcpClient2::sendData(const QString &data)
{
QByteArray buffer;
if (App::HexData2) {
if (AppConfig::HexData2) {
buffer = QUIHelper::hexStrToByteArray(data);
} else {
buffer = data.toLatin1();
@@ -121,9 +121,9 @@ void TcpServer2::disconnected()
bool TcpServer2::start()
{
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
bool ok = listen(QHostAddress::AnyIPv4, App::ListenPort2);
bool ok = listen(QHostAddress::AnyIPv4, AppConfig::ListenPort2);
#else
bool ok = listen(QHostAddress::Any, App::ListenPort2);
bool ok = listen(QHostAddress::Any, AppConfig::ListenPort2);
#endif
return ok;
}