彻底改版2.0
This commit is contained in:
11
tool/comtool/api/api.pri
Normal file
11
tool/comtool/api/api.pri
Normal file
@@ -0,0 +1,11 @@
|
||||
HEADERS += \
|
||||
$$PWD/appconfig.h \
|
||||
$$PWD/appdata.h \
|
||||
$$PWD/quihelper.h \
|
||||
$$PWD/quihelperdata.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/appconfig.cpp \
|
||||
$$PWD/appdata.cpp \
|
||||
$$PWD/quihelper.cpp \
|
||||
$$PWD/quihelperdata.cpp
|
||||
99
tool/comtool/api/appconfig.cpp
Normal file
99
tool/comtool/api/appconfig.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "appconfig.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
QString AppConfig::ConfigFile = "config.ini";
|
||||
QString AppConfig::SendFileName = "send.txt";
|
||||
QString AppConfig::DeviceFileName = "device.txt";
|
||||
|
||||
QString AppConfig::PortName = "COM1";
|
||||
int AppConfig::BaudRate = 9600;
|
||||
int AppConfig::DataBit = 8;
|
||||
QString AppConfig::Parity = QString::fromUtf8("无");
|
||||
double AppConfig::StopBit = 1;
|
||||
|
||||
bool AppConfig::HexSend = false;
|
||||
bool AppConfig::HexReceive = false;
|
||||
bool AppConfig::Debug = false;
|
||||
bool AppConfig::AutoClear = false;
|
||||
|
||||
bool AppConfig::AutoSend = false;
|
||||
int AppConfig::SendInterval = 1000;
|
||||
bool AppConfig::AutoSave = false;
|
||||
int AppConfig::SaveInterval = 5000;
|
||||
|
||||
QString AppConfig::Mode = "Tcp_Client";
|
||||
QString AppConfig::ServerIP = "127.0.0.1";
|
||||
int AppConfig::ServerPort = 6000;
|
||||
int AppConfig::ListenPort = 6000;
|
||||
int AppConfig::SleepTime = 100;
|
||||
bool AppConfig::AutoConnect = false;
|
||||
|
||||
void AppConfig::readConfig()
|
||||
{
|
||||
QSettings set(AppConfig::ConfigFile, QSettings::IniFormat);
|
||||
|
||||
set.beginGroup("ComConfig");
|
||||
AppConfig::PortName = set.value("PortName", AppConfig::PortName).toString();
|
||||
AppConfig::BaudRate = set.value("BaudRate", AppConfig::BaudRate).toInt();
|
||||
AppConfig::DataBit = set.value("DataBit", AppConfig::DataBit).toInt();
|
||||
AppConfig::Parity = set.value("Parity", AppConfig::Parity).toString();
|
||||
AppConfig::StopBit = set.value("StopBit", AppConfig::StopBit).toInt();
|
||||
|
||||
AppConfig::HexSend = set.value("HexSend", AppConfig::HexSend).toBool();
|
||||
AppConfig::HexReceive = set.value("HexReceive", AppConfig::HexReceive).toBool();
|
||||
AppConfig::Debug = set.value("Debug", AppConfig::Debug).toBool();
|
||||
AppConfig::AutoClear = set.value("AutoClear", AppConfig::AutoClear).toBool();
|
||||
|
||||
AppConfig::AutoSend = set.value("AutoSend", AppConfig::AutoSend).toBool();
|
||||
AppConfig::SendInterval = set.value("SendInterval", AppConfig::SendInterval).toInt();
|
||||
AppConfig::AutoSave = set.value("AutoSave", AppConfig::AutoSave).toBool();
|
||||
AppConfig::SaveInterval = set.value("SaveInterval", AppConfig::SaveInterval).toInt();
|
||||
set.endGroup();
|
||||
|
||||
set.beginGroup("NetConfig");
|
||||
AppConfig::Mode = set.value("Mode", AppConfig::Mode).toString();
|
||||
AppConfig::ServerIP = set.value("ServerIP", AppConfig::ServerIP).toString();
|
||||
AppConfig::ServerPort = set.value("ServerPort", AppConfig::ServerPort).toInt();
|
||||
AppConfig::ListenPort = set.value("ListenPort", AppConfig::ListenPort).toInt();
|
||||
AppConfig::SleepTime = set.value("SleepTime", AppConfig::SleepTime).toInt();
|
||||
AppConfig::AutoConnect = set.value("AutoConnect", AppConfig::AutoConnect).toBool();
|
||||
set.endGroup();
|
||||
|
||||
//配置文件不存在或者不全则重新生成
|
||||
if (!QUIHelper::checkIniFile(AppConfig::ConfigFile)) {
|
||||
writeConfig();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void AppConfig::writeConfig()
|
||||
{
|
||||
QSettings set(AppConfig::ConfigFile, QSettings::IniFormat);
|
||||
|
||||
set.beginGroup("ComConfig");
|
||||
set.setValue("PortName", AppConfig::PortName);
|
||||
set.setValue("BaudRate", AppConfig::BaudRate);
|
||||
set.setValue("DataBit", AppConfig::DataBit);
|
||||
set.setValue("Parity", AppConfig::Parity);
|
||||
set.setValue("StopBit", AppConfig::StopBit);
|
||||
|
||||
set.setValue("HexSend", AppConfig::HexSend);
|
||||
set.setValue("HexReceive", AppConfig::HexReceive);
|
||||
set.setValue("Debug", AppConfig::Debug);
|
||||
set.setValue("AutoClear", AppConfig::AutoClear);
|
||||
|
||||
set.setValue("AutoSend", AppConfig::AutoSend);
|
||||
set.setValue("SendInterval", AppConfig::SendInterval);
|
||||
set.setValue("AutoSave", AppConfig::AutoSave);
|
||||
set.setValue("SaveInterval", AppConfig::SaveInterval);
|
||||
set.endGroup();
|
||||
|
||||
set.beginGroup("NetConfig");
|
||||
set.setValue("Mode", AppConfig::Mode);
|
||||
set.setValue("ServerIP", AppConfig::ServerIP);
|
||||
set.setValue("ServerPort", AppConfig::ServerPort);
|
||||
set.setValue("ListenPort", AppConfig::ListenPort);
|
||||
set.setValue("SleepTime", AppConfig::SleepTime);
|
||||
set.setValue("AutoConnect", AppConfig::AutoConnect);
|
||||
set.endGroup();
|
||||
}
|
||||
41
tool/comtool/api/appconfig.h
Normal file
41
tool/comtool/api/appconfig.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef APPCONFIG_H
|
||||
#define APPCONFIG_H
|
||||
|
||||
#include "head.h"
|
||||
|
||||
class AppConfig
|
||||
{
|
||||
public:
|
||||
static QString ConfigFile; //配置文件路径
|
||||
static QString SendFileName; //发送配置文件名
|
||||
static QString DeviceFileName; //模拟设备数据文件名
|
||||
|
||||
static QString PortName; //串口号
|
||||
static int BaudRate; //波特率
|
||||
static int DataBit; //数据位
|
||||
static QString Parity; //校验位
|
||||
static double StopBit; //停止位
|
||||
|
||||
static bool HexSend; //16进制发送
|
||||
static bool HexReceive; //16进制接收
|
||||
static bool Debug; //模拟设备
|
||||
static bool AutoClear; //自动清空
|
||||
|
||||
static bool AutoSend; //自动发送
|
||||
static int SendInterval; //自动发送间隔
|
||||
static bool AutoSave; //自动保存
|
||||
static int SaveInterval; //自动保存间隔
|
||||
|
||||
static QString Mode; //转换模式
|
||||
static QString ServerIP; //服务器IP
|
||||
static int ServerPort; //服务器端口
|
||||
static int ListenPort; //监听端口
|
||||
static int SleepTime; //延时时间
|
||||
static bool AutoConnect; //自动重连
|
||||
|
||||
//读写配置参数
|
||||
static void readConfig(); //读取配置参数
|
||||
static void writeConfig(); //写入配置参数
|
||||
};
|
||||
|
||||
#endif // APPCONFIG_H
|
||||
122
tool/comtool/api/appdata.cpp
Normal file
122
tool/comtool/api/appdata.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
#include "appdata.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
QStringList AppData::Intervals = QStringList();
|
||||
QStringList AppData::Datas = QStringList();
|
||||
QStringList AppData::Keys = QStringList();
|
||||
QStringList AppData::Values = QStringList();
|
||||
|
||||
QString AppData::SendFileName = "send.txt";
|
||||
void AppData::readSendData()
|
||||
{
|
||||
//读取发送数据列表
|
||||
AppData::Datas.clear();
|
||||
QString fileName = QString("%1/%2").arg(QUIHelper::appPath()).arg(AppData::SendFileName);
|
||||
QFile file(fileName);
|
||||
if (file.size() > 0 && file.open(QFile::ReadOnly | QIODevice::Text)) {
|
||||
while (!file.atEnd()) {
|
||||
QString line = file.readLine();
|
||||
line = line.trimmed();
|
||||
line = line.replace("\r", "");
|
||||
line = line.replace("\n", "");
|
||||
if (!line.isEmpty()) {
|
||||
AppData::Datas.append(line);
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
//没有的时候主动添加点免得太空
|
||||
if (AppData::Datas.count() == 0) {
|
||||
AppData::Datas << "16 FF 01 01 E0 E1" << "16 FF 01 01 E1 E2";
|
||||
}
|
||||
}
|
||||
|
||||
QString AppData::DeviceFileName = "device.txt";
|
||||
void AppData::readDeviceData()
|
||||
{
|
||||
//读取转发数据列表
|
||||
AppData::Keys.clear();
|
||||
AppData::Values.clear();
|
||||
QString fileName = QString("%1/%2").arg(QUIHelper::appPath()).arg(AppData::DeviceFileName);
|
||||
QFile file(fileName);
|
||||
if (file.size() > 0 && file.open(QFile::ReadOnly | QIODevice::Text)) {
|
||||
while (!file.atEnd()) {
|
||||
QString line = file.readLine();
|
||||
line = line.trimmed();
|
||||
line = line.replace("\r", "");
|
||||
line = line.replace("\n", "");
|
||||
if (!line.isEmpty()) {
|
||||
QStringList list = line.split(";");
|
||||
QString key = list.at(0);
|
||||
QString value;
|
||||
for (int i = 1; i < list.count(); i++) {
|
||||
value += QString("%1;").arg(list.at(i));
|
||||
}
|
||||
|
||||
//去掉末尾分号
|
||||
value = value.mid(0, value.length() - 1);
|
||||
AppData::Keys.append(key);
|
||||
AppData::Values.append(value);
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
void AppData::saveData(const QString &data)
|
||||
{
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString fileName = QString("%1/%2.txt").arg(QUIHelper::appPath()).arg(STRDATETIME);
|
||||
QFile file(fileName);
|
||||
if (file.open(QFile::WriteOnly | QFile::Text)) {
|
||||
file.write(data.toUtf8());
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
void AppData::loadIP(QComboBox *cbox)
|
||||
{
|
||||
//获取本机所有IP
|
||||
static QStringList ips;
|
||||
if (ips.count() == 0) {
|
||||
#ifdef emsdk
|
||||
ips << "127.0.0.1";
|
||||
#else
|
||||
QList<QNetworkInterface> netInterfaces = QNetworkInterface::allInterfaces();
|
||||
foreach (const QNetworkInterface &netInterface, netInterfaces) {
|
||||
//移除虚拟机和抓包工具的虚拟网卡
|
||||
QString humanReadableName = netInterface.humanReadableName().toLower();
|
||||
if (humanReadableName.startsWith("vmware network adapter") || humanReadableName.startsWith("npcap loopback adapter")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//过滤当前网络接口
|
||||
bool flag = (netInterface.flags() == (QNetworkInterface::IsUp | QNetworkInterface::IsRunning | QNetworkInterface::CanBroadcast | QNetworkInterface::CanMulticast));
|
||||
if (flag) {
|
||||
QList<QNetworkAddressEntry> addrs = netInterface.addressEntries();
|
||||
foreach (QNetworkAddressEntry addr, addrs) {
|
||||
//只取出IPV4的地址
|
||||
if (addr.ip().protocol() == QAbstractSocket::IPv4Protocol) {
|
||||
QString ip4 = addr.ip().toString();
|
||||
if (ip4 != "127.0.0.1") {
|
||||
ips << ip4;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
cbox->clear();
|
||||
cbox->addItems(ips);
|
||||
if (!ips.contains("127.0.0.1")) {
|
||||
cbox->addItem("127.0.0.1");
|
||||
}
|
||||
}
|
||||
30
tool/comtool/api/appdata.h
Normal file
30
tool/comtool/api/appdata.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef APPDATA_H
|
||||
#define APPDATA_H
|
||||
|
||||
#include "head.h"
|
||||
|
||||
class AppData
|
||||
{
|
||||
public:
|
||||
//全局变量
|
||||
static QStringList Intervals;
|
||||
static QStringList Datas;
|
||||
static QStringList Keys;
|
||||
static QStringList Values;
|
||||
|
||||
//读取发送数据列表
|
||||
static QString SendFileName;
|
||||
static void readSendData();
|
||||
|
||||
//读取转发数据列表
|
||||
static QString DeviceFileName;
|
||||
static void readDeviceData();
|
||||
|
||||
//保存数据到文件
|
||||
static void saveData(const QString &data);
|
||||
|
||||
//添加网卡IP地址到下拉框
|
||||
static void loadIP(QComboBox *cbox);
|
||||
};
|
||||
|
||||
#endif // APPDATA_H
|
||||
420
tool/comtool/api/quihelper.cpp
Normal file
420
tool/comtool/api/quihelper.cpp
Normal file
@@ -0,0 +1,420 @@
|
||||
#include "quihelper.h"
|
||||
|
||||
int QUIHelper::getScreenIndex()
|
||||
{
|
||||
//需要对多个屏幕进行处理
|
||||
int screenIndex = 0;
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
int screenCount = qApp->screens().count();
|
||||
#else
|
||||
int screenCount = qApp->desktop()->screenCount();
|
||||
#endif
|
||||
|
||||
if (screenCount > 1) {
|
||||
//找到当前鼠标所在屏幕
|
||||
QPoint pos = QCursor::pos();
|
||||
for (int i = 0; i < screenCount; ++i) {
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
if (qApp->screens().at(i)->geometry().contains(pos)) {
|
||||
#else
|
||||
if (qApp->desktop()->screenGeometry(i).contains(pos)) {
|
||||
#endif
|
||||
screenIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return screenIndex;
|
||||
}
|
||||
|
||||
QRect QUIHelper::getScreenRect(bool available)
|
||||
{
|
||||
QRect rect;
|
||||
int screenIndex = QUIHelper::getScreenIndex();
|
||||
if (available) {
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
rect = qApp->screens().at(screenIndex)->availableGeometry();
|
||||
#else
|
||||
rect = qApp->desktop()->availableGeometry(screenIndex);
|
||||
#endif
|
||||
} else {
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
rect = qApp->screens().at(screenIndex)->geometry();
|
||||
#else
|
||||
rect = qApp->desktop()->screenGeometry(screenIndex);
|
||||
#endif
|
||||
}
|
||||
return rect;
|
||||
}
|
||||
|
||||
int QUIHelper::deskWidth()
|
||||
{
|
||||
return getScreenRect().width();
|
||||
}
|
||||
|
||||
int QUIHelper::deskHeight()
|
||||
{
|
||||
return getScreenRect().height();
|
||||
}
|
||||
|
||||
QWidget *QUIHelper::centerBaseForm = 0;
|
||||
void QUIHelper::setFormInCenter(QWidget *form)
|
||||
{
|
||||
int formWidth = form->width();
|
||||
int formHeight = form->height();
|
||||
|
||||
//如果=0表示采用系统桌面屏幕为参照
|
||||
QRect rect;
|
||||
if (centerBaseForm == 0) {
|
||||
rect = getScreenRect();
|
||||
} else {
|
||||
rect = centerBaseForm->geometry();
|
||||
}
|
||||
|
||||
int deskWidth = rect.width();
|
||||
int deskHeight = rect.height();
|
||||
QPoint movePoint(deskWidth / 2 - formWidth / 2 + rect.x(), deskHeight / 2 - formHeight / 2 + rect.y());
|
||||
form->move(movePoint);
|
||||
}
|
||||
|
||||
QString QUIHelper::appName()
|
||||
{
|
||||
//没有必要每次都获取,只有当变量为空时才去获取一次
|
||||
static QString name;
|
||||
if (name.isEmpty()) {
|
||||
name = qApp->applicationFilePath();
|
||||
//下面的方法主要为了过滤安卓的路径 lib程序名_armeabi-v7a
|
||||
QStringList list = name.split("/");
|
||||
name = list.at(list.count() - 1).split(".").at(0);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
QString QUIHelper::appPath()
|
||||
{
|
||||
#ifdef Q_OS_ANDROID
|
||||
//return QString("/sdcard/Android/%1").arg(appName());
|
||||
return QString("/storage/emulated/0/%1").arg(appName());
|
||||
#else
|
||||
return qApp->applicationDirPath();
|
||||
#endif
|
||||
}
|
||||
|
||||
QString QUIHelper::getUuid()
|
||||
{
|
||||
QString uuid = QUuid::createUuid().toString();
|
||||
uuid.replace("{", "");
|
||||
uuid.replace("}", "");
|
||||
return uuid;
|
||||
}
|
||||
|
||||
void QUIHelper::initRand()
|
||||
{
|
||||
//初始化随机数种子
|
||||
QTime t = QTime::currentTime();
|
||||
srand(t.msec() + t.second() * 1000);
|
||||
}
|
||||
|
||||
void QUIHelper::newDir(const QString &dirName)
|
||||
{
|
||||
QString strDir = dirName;
|
||||
|
||||
//如果路径中包含斜杠字符则说明是绝对路径
|
||||
//linux系统路径字符带有 / windows系统 路径字符带有 :/
|
||||
if (!strDir.startsWith("/") && !strDir.contains(":/")) {
|
||||
strDir = QString("%1/%2").arg(QUIHelper::appPath()).arg(strDir);
|
||||
}
|
||||
|
||||
QDir dir(strDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkpath(strDir);
|
||||
}
|
||||
}
|
||||
|
||||
void QUIHelper::sleep(int msec)
|
||||
{
|
||||
if (msec <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
QThread::msleep(msec);
|
||||
#else
|
||||
QTime endTime = QTime::currentTime().addMSecs(msec);
|
||||
while (QTime::currentTime() < endTime) {
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void QUIHelper::setStyle()
|
||||
{
|
||||
//打印下所有内置风格的名字
|
||||
qDebug() << "Qt内置的样式" << QStyleFactory::keys();
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
qApp->setStyle(QStyleFactory::create("Fusion"));
|
||||
#else
|
||||
qApp->setStyle(QStyleFactory::create("Cleanlooks"));
|
||||
#endif
|
||||
//qApp->setPalette(QPalette("#FFFFFF"));
|
||||
}
|
||||
|
||||
void QUIHelper::setFont(int fontSize)
|
||||
{
|
||||
QFont font;
|
||||
font.setFamily("MicroSoft Yahei");
|
||||
#ifdef Q_OS_ANDROID
|
||||
font.setPixelSize(15);
|
||||
#elif __arm__
|
||||
font.setPixelSize(25);
|
||||
#else
|
||||
font.setPixelSize(fontSize);
|
||||
#endif
|
||||
|
||||
#ifndef rk3399
|
||||
qApp->setFont(font);
|
||||
#endif
|
||||
}
|
||||
|
||||
void QUIHelper::setCode(bool utf8)
|
||||
{
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
//如果想要控制台打印信息中文正常就注释掉这个设置
|
||||
if (utf8) {
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
}
|
||||
#else
|
||||
#if _MSC_VER
|
||||
QTextCodec *codec = QTextCodec::codecForName("gbk");
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
#endif
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
QTextCodec::setCodecForCStrings(codec);
|
||||
QTextCodec::setCodecForTr(codec);
|
||||
#endif
|
||||
}
|
||||
|
||||
void QUIHelper::setTranslator(const QString &qmFile)
|
||||
{
|
||||
//过滤下不存在的就不用设置了
|
||||
if (!QFile(qmFile).exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QTranslator *translator = new QTranslator(qApp);
|
||||
translator->load(qmFile);
|
||||
qApp->installTranslator(translator);
|
||||
}
|
||||
|
||||
void QUIHelper::initAll()
|
||||
{
|
||||
//初始化随机数种子
|
||||
QUIHelper::initRand();
|
||||
//设置样式风格
|
||||
QUIHelper::setStyle();
|
||||
//设置字体
|
||||
QUIHelper::setFont(13);
|
||||
//设置编码
|
||||
QUIHelper::setCode();
|
||||
//设置翻译文件支持多个
|
||||
QUIHelper::setTranslator(":/qm/widgets.qm");
|
||||
QUIHelper::setTranslator(":/qm/qt_zh_CN.qm");
|
||||
QUIHelper::setTranslator(":/qm/designer_zh_CN.qm");
|
||||
}
|
||||
|
||||
void QUIHelper::setFramelessForm(QWidget *widgetMain, bool tool, bool top, bool menu)
|
||||
{
|
||||
widgetMain->setProperty("form", true);
|
||||
widgetMain->setProperty("canMove", true);
|
||||
|
||||
//根据设定逐个追加属性
|
||||
#ifdef __arm__
|
||||
widgetMain->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
|
||||
#else
|
||||
widgetMain->setWindowFlags(Qt::FramelessWindowHint);
|
||||
#endif
|
||||
if (tool) {
|
||||
widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::Tool);
|
||||
}
|
||||
if (top) {
|
||||
widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::WindowStaysOnTopHint);
|
||||
}
|
||||
if (menu) {
|
||||
//如果是其他系统比如neokylin会产生系统边框
|
||||
#ifdef Q_OS_WIN
|
||||
widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
int QUIHelper::showMessageBox(const QString &info, int type, int closeSec, bool exec)
|
||||
{
|
||||
int result = 0;
|
||||
if (type == 0) {
|
||||
showMessageBoxInfo(info, closeSec, exec);
|
||||
} else if (type == 1) {
|
||||
showMessageBoxError(info, closeSec, exec);
|
||||
} else if (type == 2) {
|
||||
result = showMessageBoxQuestion(info);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void QUIHelper::showMessageBoxInfo(const QString &info, int closeSec, bool exec)
|
||||
{
|
||||
QMessageBox box(QMessageBox::Information, "提示", info);
|
||||
box.setStandardButtons(QMessageBox::Yes);
|
||||
box.setButtonText(QMessageBox::Yes, QString("确 定"));
|
||||
box.exec();
|
||||
//QMessageBox::information(0, "提示", info, QMessageBox::Yes);
|
||||
}
|
||||
|
||||
void QUIHelper::showMessageBoxError(const QString &info, int closeSec, bool exec)
|
||||
{
|
||||
QMessageBox box(QMessageBox::Critical, "错误", info);
|
||||
box.setStandardButtons(QMessageBox::Yes);
|
||||
box.setButtonText(QMessageBox::Yes, QString("确 定"));
|
||||
box.exec();
|
||||
//QMessageBox::critical(0, "错误", info, QMessageBox::Yes);
|
||||
}
|
||||
|
||||
int QUIHelper::showMessageBoxQuestion(const QString &info)
|
||||
{
|
||||
QMessageBox box(QMessageBox::Question, "询问", info);
|
||||
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||
box.setButtonText(QMessageBox::Yes, QString("确 定"));
|
||||
box.setButtonText(QMessageBox::No, QString("取 消"));
|
||||
return box.exec();
|
||||
//return QMessageBox::question(0, "询问", info, QMessageBox::Yes | QMessageBox::No);
|
||||
}
|
||||
|
||||
QString QUIHelper::getXorEncryptDecrypt(const QString &value, char key)
|
||||
{
|
||||
//矫正范围外的数据
|
||||
if (key < 0 || key >= 127) {
|
||||
key = 127;
|
||||
}
|
||||
|
||||
QString result = value;
|
||||
int count = result.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
result[i] = QChar(result.at(i).toLatin1() ^ key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
uchar QUIHelper::getOrCode(const QByteArray &data)
|
||||
{
|
||||
int len = data.length();
|
||||
uchar result = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
result ^= data.at(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uchar QUIHelper::getCheckCode(const QByteArray &data)
|
||||
{
|
||||
int len = data.length();
|
||||
uchar temp = 0;
|
||||
for (uchar i = 0; i < len; i++) {
|
||||
temp += data.at(i);
|
||||
}
|
||||
|
||||
return temp % 256;
|
||||
}
|
||||
|
||||
void QUIHelper::initTableView(QTableView *tableView, int rowHeight, bool headVisible, bool edit, bool stretchLast)
|
||||
{
|
||||
//取消自动换行
|
||||
tableView->setWordWrap(false);
|
||||
//超出文本不显示省略号
|
||||
tableView->setTextElideMode(Qt::ElideNone);
|
||||
//奇数偶数行颜色交替
|
||||
tableView->setAlternatingRowColors(false);
|
||||
//垂直表头是否可见
|
||||
tableView->verticalHeader()->setVisible(headVisible);
|
||||
//选中一行表头是否加粗
|
||||
tableView->horizontalHeader()->setHighlightSections(false);
|
||||
//最后一行拉伸填充
|
||||
tableView->horizontalHeader()->setStretchLastSection(stretchLast);
|
||||
//行标题最小宽度尺寸
|
||||
tableView->horizontalHeader()->setMinimumSectionSize(0);
|
||||
//行标题最小高度,等同于和默认行高一致
|
||||
tableView->horizontalHeader()->setFixedHeight(rowHeight);
|
||||
//默认行高
|
||||
tableView->verticalHeader()->setDefaultSectionSize(rowHeight);
|
||||
//选中时一行整体选中
|
||||
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
//只允许选择单个
|
||||
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
|
||||
//表头不可单击
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
tableView->horizontalHeader()->setSectionsClickable(false);
|
||||
#else
|
||||
tableView->horizontalHeader()->setClickable(false);
|
||||
#endif
|
||||
|
||||
//鼠标按下即进入编辑模式
|
||||
if (edit) {
|
||||
tableView->setEditTriggers(QAbstractItemView::CurrentChanged | QAbstractItemView::DoubleClicked);
|
||||
} else {
|
||||
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
}
|
||||
}
|
||||
|
||||
void QUIHelper::openFile(const QString &fileName, const QString &msg)
|
||||
{
|
||||
#ifdef __arm__
|
||||
return;
|
||||
#endif
|
||||
if (fileName.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (QUIHelper::showMessageBoxQuestion(msg + "成功!确定现在就打开吗?") == QMessageBox::Yes) {
|
||||
QString url = QString("file:///%1").arg(fileName);
|
||||
QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode));
|
||||
}
|
||||
}
|
||||
|
||||
bool QUIHelper::checkIniFile(const QString &iniFile)
|
||||
{
|
||||
//如果配置文件大小为0,则以初始值继续运行,并生成配置文件
|
||||
QFile file(iniFile);
|
||||
if (file.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//如果配置文件不完整,则以初始值继续运行,并生成配置文件
|
||||
if (file.open(QFile::ReadOnly)) {
|
||||
bool ok = true;
|
||||
while (!file.atEnd()) {
|
||||
QString line = file.readLine();
|
||||
line.replace("\r", "");
|
||||
line.replace("\n", "");
|
||||
QStringList list = line.split("=");
|
||||
|
||||
if (list.count() == 2) {
|
||||
if (list.at(1) == "") {
|
||||
qDebug() << "ini node no value" << list.at(0);
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
70
tool/comtool/api/quihelper.h
Normal file
70
tool/comtool/api/quihelper.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef QUIHELPER2_H
|
||||
#define QUIHELPER2_H
|
||||
|
||||
#include "head.h"
|
||||
|
||||
class QUIHelper
|
||||
{
|
||||
public:
|
||||
//获取当前鼠标所在屏幕索引+尺寸
|
||||
static int getScreenIndex();
|
||||
static QRect getScreenRect(bool available = true);
|
||||
|
||||
//获取桌面宽度高度+居中显示
|
||||
static int deskWidth();
|
||||
static int deskHeight();
|
||||
|
||||
//居中显示窗体
|
||||
//定义标志位指定是以桌面为参照还是主程序界面为参照
|
||||
static QWidget *centerBaseForm;
|
||||
static void setFormInCenter(QWidget *form);
|
||||
|
||||
//程序文件名称+当前所在路径
|
||||
static QString appName();
|
||||
static QString appPath();
|
||||
|
||||
//获取uuid+初始化随机数种子+新建目录+延时
|
||||
static QString getUuid();
|
||||
static void initRand();
|
||||
static void newDir(const QString &dirName);
|
||||
static void sleep(int msec);
|
||||
|
||||
//设置样式+字体+编码+居中+翻译
|
||||
static void setStyle();
|
||||
static void setFont(int fontSize = 12);
|
||||
static void setCode(bool utf8 = true);
|
||||
static void setTranslator(const QString &qmFile);
|
||||
//一次性设置所有
|
||||
static void initAll();
|
||||
|
||||
//设置无边框
|
||||
static void setFramelessForm(QWidget *widgetMain, bool tool = false, bool top = false, bool menu = true);
|
||||
|
||||
//弹出框
|
||||
static int showMessageBox(const QString &info, int type = 0, int closeSec = 0, bool exec = false);
|
||||
//弹出消息框
|
||||
static void showMessageBoxInfo(const QString &info, int closeSec = 0, bool exec = false);
|
||||
//弹出错误框
|
||||
static void showMessageBoxError(const QString &info, int closeSec = 0, bool exec = false);
|
||||
//弹出询问框
|
||||
static int showMessageBoxQuestion(const QString &info);
|
||||
|
||||
//异或加密-只支持字符,如果是中文需要将其转换base64编码
|
||||
static QString getXorEncryptDecrypt(const QString &value, char key);
|
||||
//异或校验
|
||||
static uchar getOrCode(const QByteArray &data);
|
||||
//计算校验码
|
||||
static uchar getCheckCode(const QByteArray &data);
|
||||
|
||||
//初始化表格
|
||||
static void initTableView(QTableView *tableView, int rowHeight = 25,
|
||||
bool headVisible = false, bool edit = false,
|
||||
bool stretchLast = true);
|
||||
//打开文件带提示框
|
||||
static void openFile(const QString &fileName, const QString &msg);
|
||||
|
||||
//检查ini配置文件
|
||||
static bool checkIniFile(const QString &iniFile);
|
||||
};
|
||||
|
||||
#endif // QUIHELPER2_H
|
||||
450
tool/comtool/api/quihelperdata.cpp
Normal file
450
tool/comtool/api/quihelperdata.cpp
Normal file
@@ -0,0 +1,450 @@
|
||||
#include "quihelperdata.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
int QUIHelperData::strHexToDecimal(const QString &strHex)
|
||||
{
|
||||
bool ok;
|
||||
return strHex.toInt(&ok, 16);
|
||||
}
|
||||
|
||||
int QUIHelperData::strDecimalToDecimal(const QString &strDecimal)
|
||||
{
|
||||
bool ok;
|
||||
return strDecimal.toInt(&ok, 10);
|
||||
}
|
||||
|
||||
int QUIHelperData::strBinToDecimal(const QString &strBin)
|
||||
{
|
||||
bool ok;
|
||||
return strBin.toInt(&ok, 2);
|
||||
}
|
||||
|
||||
QString QUIHelperData::strHexToStrBin(const QString &strHex)
|
||||
{
|
||||
uchar decimal = strHexToDecimal(strHex);
|
||||
QString bin = QString::number(decimal, 2);
|
||||
uchar len = bin.length();
|
||||
|
||||
if (len < 8) {
|
||||
for (int i = 0; i < 8 - len; i++) {
|
||||
bin = "0" + bin;
|
||||
}
|
||||
}
|
||||
|
||||
return bin;
|
||||
}
|
||||
|
||||
QString QUIHelperData::decimalToStrBin1(int decimal)
|
||||
{
|
||||
QString bin = QString::number(decimal, 2);
|
||||
uchar len = bin.length();
|
||||
if (len <= 8) {
|
||||
for (int i = 0; i < 8 - len; i++) {
|
||||
bin = "0" + bin;
|
||||
}
|
||||
}
|
||||
|
||||
return bin;
|
||||
}
|
||||
|
||||
QString QUIHelperData::decimalToStrBin2(int decimal)
|
||||
{
|
||||
QString bin = QString::number(decimal, 2);
|
||||
uchar len = bin.length();
|
||||
if (len <= 16) {
|
||||
for (int i = 0; i < 16 - len; i++) {
|
||||
bin = "0" + bin;
|
||||
}
|
||||
}
|
||||
|
||||
return bin;
|
||||
}
|
||||
|
||||
QString QUIHelperData::decimalToStrHex(int decimal)
|
||||
{
|
||||
QString temp = QString::number(decimal, 16);
|
||||
if (temp.length() == 1) {
|
||||
temp = "0" + temp;
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::intToByte(int data)
|
||||
{
|
||||
QByteArray result;
|
||||
result.resize(4);
|
||||
result[3] = (uchar)(0x000000ff & data);
|
||||
result[2] = (uchar)((0x0000ff00 & data) >> 8);
|
||||
result[1] = (uchar)((0x00ff0000 & data) >> 16);
|
||||
result[0] = (uchar)((0xff000000 & data) >> 24);
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::intToByteRec(int data)
|
||||
{
|
||||
QByteArray result;
|
||||
result.resize(4);
|
||||
result[0] = (uchar)(0x000000ff & data);
|
||||
result[1] = (uchar)((0x0000ff00 & data) >> 8);
|
||||
result[2] = (uchar)((0x00ff0000 & data) >> 16);
|
||||
result[3] = (uchar)((0xff000000 & data) >> 24);
|
||||
return result;
|
||||
}
|
||||
|
||||
int QUIHelperData::byteToInt(const QByteArray &data)
|
||||
{
|
||||
int i = data.at(3) & 0x000000ff;
|
||||
i |= ((data.at(2) << 8) & 0x0000ff00);
|
||||
i |= ((data.at(1) << 16) & 0x00ff0000);
|
||||
i |= ((data.at(0) << 24) & 0xff000000);
|
||||
return i;
|
||||
}
|
||||
|
||||
int QUIHelperData::byteToIntRec(const QByteArray &data)
|
||||
{
|
||||
int i = data.at(0) & 0x000000ff;
|
||||
i |= ((data.at(1) << 8) & 0x0000ff00);
|
||||
i |= ((data.at(2) << 16) & 0x00ff0000);
|
||||
i |= ((data.at(3) << 24) & 0xff000000);
|
||||
return i;
|
||||
}
|
||||
|
||||
quint32 QUIHelperData::byteToUInt(const QByteArray &data)
|
||||
{
|
||||
quint32 i = data.at(3) & 0x000000ff;
|
||||
i |= ((data.at(2) << 8) & 0x0000ff00);
|
||||
i |= ((data.at(1) << 16) & 0x00ff0000);
|
||||
i |= ((data.at(0) << 24) & 0xff000000);
|
||||
return i;
|
||||
}
|
||||
|
||||
quint32 QUIHelperData::byteToUIntRec(const QByteArray &data)
|
||||
{
|
||||
quint32 i = data.at(0) & 0x000000ff;
|
||||
i |= ((data.at(1) << 8) & 0x0000ff00);
|
||||
i |= ((data.at(2) << 16) & 0x00ff0000);
|
||||
i |= ((data.at(3) << 24) & 0xff000000);
|
||||
return i;
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::ushortToByte(ushort data)
|
||||
{
|
||||
QByteArray result;
|
||||
result.resize(2);
|
||||
result[1] = (uchar)(0x000000ff & data);
|
||||
result[0] = (uchar)((0x0000ff00 & data) >> 8);
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::ushortToByteRec(ushort data)
|
||||
{
|
||||
QByteArray result;
|
||||
result.resize(2);
|
||||
result[0] = (uchar)(0x000000ff & data);
|
||||
result[1] = (uchar)((0x0000ff00 & data) >> 8);
|
||||
return result;
|
||||
}
|
||||
|
||||
int QUIHelperData::byteToUShort(const QByteArray &data)
|
||||
{
|
||||
int i = data.at(1) & 0x000000FF;
|
||||
i |= ((data.at(0) << 8) & 0x0000FF00);
|
||||
if (i >= 32768) {
|
||||
i = i - 65536;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
int QUIHelperData::byteToUShortRec(const QByteArray &data)
|
||||
{
|
||||
int i = data.at(0) & 0x000000FF;
|
||||
i |= ((data.at(1) << 8) & 0x0000FF00);
|
||||
if (i >= 32768) {
|
||||
i = i - 65536;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
QString QUIHelperData::getValue(quint8 value)
|
||||
{
|
||||
QString result = QString::number(value);
|
||||
if (result.length() <= 1) {
|
||||
result = QString("0%1").arg(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QString QUIHelperData::getXorEncryptDecrypt(const QString &value, char key)
|
||||
{
|
||||
//矫正范围外的数据
|
||||
if (key < 0 || key >= 127) {
|
||||
key = 127;
|
||||
}
|
||||
|
||||
QString result = value;
|
||||
int count = result.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
result[i] = QChar(result.at(i).toLatin1() ^ key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
uchar QUIHelperData::getOrCode(const QByteArray &data)
|
||||
{
|
||||
int len = data.length();
|
||||
uchar result = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
result ^= data.at(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uchar QUIHelperData::getCheckCode(const QByteArray &data)
|
||||
{
|
||||
int len = data.length();
|
||||
uchar temp = 0;
|
||||
for (uchar i = 0; i < len; i++) {
|
||||
temp += data.at(i);
|
||||
}
|
||||
|
||||
return temp % 256;
|
||||
}
|
||||
|
||||
//函数功能:计算CRC16
|
||||
//参数1:*data 16位CRC校验数据,
|
||||
//参数2:len 数据流长度
|
||||
//参数3:init 初始化值
|
||||
//参数4:table 16位CRC查找表
|
||||
|
||||
//正序CRC计算
|
||||
quint16 QUIHelperData::getCrc16(quint8 *data, int len, quint16 init, const quint16 *table)
|
||||
{
|
||||
quint16 crc_16 = init;
|
||||
quint8 temp;
|
||||
while (len-- > 0) {
|
||||
temp = crc_16 & 0xff;
|
||||
crc_16 = (crc_16 >> 8) ^ table[(temp ^ *data++) & 0xff];
|
||||
}
|
||||
|
||||
return crc_16;
|
||||
}
|
||||
|
||||
//逆序CRC计算
|
||||
quint16 QUIHelperData::getCrc16Rec(quint8 *data, int len, quint16 init, const quint16 *table)
|
||||
{
|
||||
quint16 crc_16 = init;
|
||||
quint8 temp;
|
||||
while (len-- > 0) {
|
||||
temp = crc_16 >> 8;
|
||||
crc_16 = (crc_16 << 8) ^ table[(temp ^ *data++) & 0xff];
|
||||
}
|
||||
|
||||
return crc_16;
|
||||
}
|
||||
|
||||
//Modbus CRC16校验
|
||||
quint16 QUIHelperData::getModbus16(quint8 *data, int len)
|
||||
{
|
||||
//MODBUS CRC-16表 8005 逆序
|
||||
const quint16 table_16[256] = {
|
||||
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
|
||||
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
|
||||
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
|
||||
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
|
||||
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
|
||||
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
|
||||
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
|
||||
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
|
||||
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
|
||||
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
|
||||
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
|
||||
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
|
||||
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
|
||||
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
|
||||
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
|
||||
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
|
||||
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
|
||||
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
|
||||
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
|
||||
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
|
||||
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
|
||||
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
|
||||
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
|
||||
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
|
||||
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
|
||||
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
|
||||
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
|
||||
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
|
||||
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
|
||||
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
|
||||
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
|
||||
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
|
||||
};
|
||||
|
||||
return getCrc16(data, len, 0xFFFF, table_16);
|
||||
}
|
||||
|
||||
//CRC16校验
|
||||
QByteArray QUIHelperData::getCrcCode(const QByteArray &data)
|
||||
{
|
||||
quint16 result = getModbus16((quint8 *)data.data(), data.length());
|
||||
return QUIHelperData::ushortToByteRec(result);
|
||||
}
|
||||
|
||||
static QMap<char, QString> listChar;
|
||||
void QUIHelperData::initAscii()
|
||||
{
|
||||
//0x20为空格,空格以下都是不可见字符
|
||||
if (listChar.count() == 0) {
|
||||
listChar.insert(0, "\\NUL");
|
||||
listChar.insert(1, "\\SOH");
|
||||
listChar.insert(2, "\\STX");
|
||||
listChar.insert(3, "\\ETX");
|
||||
listChar.insert(4, "\\EOT");
|
||||
listChar.insert(5, "\\ENQ");
|
||||
listChar.insert(6, "\\ACK");
|
||||
listChar.insert(7, "\\BEL");
|
||||
listChar.insert(8, "\\BS");
|
||||
listChar.insert(9, "\\HT");
|
||||
listChar.insert(10, "\\LF");
|
||||
listChar.insert(11, "\\VT");
|
||||
listChar.insert(12, "\\FF");
|
||||
listChar.insert(13, "\\CR");
|
||||
listChar.insert(14, "\\SO");
|
||||
listChar.insert(15, "\\SI");
|
||||
listChar.insert(16, "\\DLE");
|
||||
listChar.insert(17, "\\DC1");
|
||||
listChar.insert(18, "\\DC2");
|
||||
listChar.insert(19, "\\DC3");
|
||||
listChar.insert(20, "\\DC4");
|
||||
listChar.insert(21, "\\NAK");
|
||||
listChar.insert(22, "\\SYN");
|
||||
listChar.insert(23, "\\ETB");
|
||||
listChar.insert(24, "\\CAN");
|
||||
listChar.insert(25, "\\EM");
|
||||
listChar.insert(26, "\\SUB");
|
||||
listChar.insert(27, "\\ESC");
|
||||
listChar.insert(28, "\\FS");
|
||||
listChar.insert(29, "\\GS");
|
||||
listChar.insert(30, "\\RS");
|
||||
listChar.insert(31, "\\US");
|
||||
listChar.insert(0x5C, "\\");
|
||||
listChar.insert(0x7F, "\\DEL");
|
||||
}
|
||||
}
|
||||
|
||||
QString QUIHelperData::byteArrayToAsciiStr(const QByteArray &data)
|
||||
{
|
||||
//先初始化字符表
|
||||
initAscii();
|
||||
|
||||
QString temp;
|
||||
int len = data.size();
|
||||
for (int i = 0; i < len; i++) {
|
||||
char byte = data.at(i);
|
||||
QString value = listChar.value(byte);
|
||||
if (!value.isEmpty()) {
|
||||
} else if (byte >= 0 && byte <= 0x7F) {
|
||||
value = QString("%1").arg(byte);
|
||||
} else {
|
||||
value = decimalToStrHex((quint8)byte);
|
||||
value = QString("\\x%1").arg(value.toUpper());
|
||||
}
|
||||
|
||||
temp += value;
|
||||
}
|
||||
|
||||
return temp.trimmed();
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::asciiStrToByteArray(const QString &data)
|
||||
{
|
||||
//先初始化字符表
|
||||
initAscii();
|
||||
|
||||
QByteArray buffer;
|
||||
QStringList list = data.split("\\");
|
||||
|
||||
int count = list.count();
|
||||
for (int i = 1; i < count; i++) {
|
||||
QString str = list.at(i);
|
||||
int key = 0;
|
||||
if (str.contains("x")) {
|
||||
key = strHexToDecimal(str.mid(1, 2));
|
||||
} else {
|
||||
key = listChar.key("\\" + str);
|
||||
}
|
||||
|
||||
buffer.append(key);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
char QUIHelperData::hexStrToChar(char data)
|
||||
{
|
||||
if ((data >= '0') && (data <= '9')) {
|
||||
return data - 0x30;
|
||||
} else if ((data >= 'A') && (data <= 'F')) {
|
||||
return data - 'A' + 10;
|
||||
} else if ((data >= 'a') && (data <= 'f')) {
|
||||
return data - 'a' + 10;
|
||||
} else {
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::hexStrToByteArray(const QString &data)
|
||||
{
|
||||
QByteArray senddata;
|
||||
int hexdata, lowhexdata;
|
||||
int hexdatalen = 0;
|
||||
int len = data.length();
|
||||
senddata.resize(len / 2);
|
||||
char lstr, hstr;
|
||||
|
||||
for (int i = 0; i < len;) {
|
||||
hstr = data.at(i).toLatin1();
|
||||
if (hstr == ' ') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
if (i >= len) {
|
||||
break;
|
||||
}
|
||||
|
||||
lstr = data.at(i).toLatin1();
|
||||
hexdata = hexStrToChar(hstr);
|
||||
lowhexdata = hexStrToChar(lstr);
|
||||
|
||||
if ((hexdata == 16) || (lowhexdata == 16)) {
|
||||
break;
|
||||
} else {
|
||||
hexdata = hexdata * 16 + lowhexdata;
|
||||
}
|
||||
|
||||
i++;
|
||||
senddata[hexdatalen] = (char)hexdata;
|
||||
hexdatalen++;
|
||||
}
|
||||
|
||||
senddata.resize(hexdatalen);
|
||||
return senddata;
|
||||
}
|
||||
|
||||
QString QUIHelperData::byteArrayToHexStr(const QByteArray &data)
|
||||
{
|
||||
QString temp = "";
|
||||
QString hex = data.toHex();
|
||||
for (int i = 0; i < hex.length(); i = i + 2) {
|
||||
temp += hex.mid(i, 2) + " ";
|
||||
}
|
||||
|
||||
return temp.trimmed().toUpper();
|
||||
}
|
||||
70
tool/comtool/api/quihelperdata.h
Normal file
70
tool/comtool/api/quihelperdata.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef QUIHELPERDATA_H
|
||||
#define QUIHELPERDATA_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class QUIHelperData
|
||||
{
|
||||
public:
|
||||
//16进制字符串转10进制
|
||||
static int strHexToDecimal(const QString &strHex);
|
||||
//10进制字符串转10进制
|
||||
static int strDecimalToDecimal(const QString &strDecimal);
|
||||
//2进制字符串转10进制
|
||||
static int strBinToDecimal(const QString &strBin);
|
||||
|
||||
//16进制字符串转2进制字符串
|
||||
static QString strHexToStrBin(const QString &strHex);
|
||||
//10进制转2进制字符串一个字节
|
||||
static QString decimalToStrBin1(int decimal);
|
||||
//10进制转2进制字符串两个字节
|
||||
static QString decimalToStrBin2(int decimal);
|
||||
//10进制转16进制字符串,补零.
|
||||
static QString decimalToStrHex(int decimal);
|
||||
|
||||
//int转字节数组
|
||||
static QByteArray intToByte(int data);
|
||||
static QByteArray intToByteRec(int data);
|
||||
|
||||
//字节数组转int
|
||||
static int byteToInt(const QByteArray &data);
|
||||
static int byteToIntRec(const QByteArray &data);
|
||||
static quint32 byteToUInt(const QByteArray &data);
|
||||
static quint32 byteToUIntRec(const QByteArray &data);
|
||||
|
||||
//ushort转字节数组
|
||||
static QByteArray ushortToByte(ushort data);
|
||||
static QByteArray ushortToByteRec(ushort data);
|
||||
|
||||
//字节数组转ushort
|
||||
static int byteToUShort(const QByteArray &data);
|
||||
static int byteToUShortRec(const QByteArray &data);
|
||||
|
||||
//字符串补全
|
||||
static QString getValue(quint8 value);
|
||||
|
||||
//异或加密-只支持字符,如果是中文需要将其转换base64编码
|
||||
static QString getXorEncryptDecrypt(const QString &value, char key);
|
||||
//异或校验
|
||||
static uchar getOrCode(const QByteArray &data);
|
||||
//计算校验码
|
||||
static uchar getCheckCode(const QByteArray &data);
|
||||
|
||||
//CRC校验
|
||||
static quint16 getCrc16Rec(quint8 *data, int len, quint16 init, const quint16 *table);
|
||||
static quint16 getCrc16(quint8 *data, int len, quint16 init, const quint16 *table);
|
||||
static quint16 getModbus16(quint8 *data, int len);
|
||||
static QByteArray getCrcCode(const QByteArray &data);
|
||||
|
||||
//字节数组与Ascii字符串互转
|
||||
static void initAscii();
|
||||
static QString byteArrayToAsciiStr(const QByteArray &data);
|
||||
static QByteArray asciiStrToByteArray(const QString &data);
|
||||
|
||||
//16进制字符串与字节数组互转
|
||||
static char hexStrToChar(char data);
|
||||
static QByteArray hexStrToByteArray(const QString &data);
|
||||
static QString byteArrayToHexStr(const QByteArray &data);
|
||||
};
|
||||
|
||||
#endif // QUIHELPERDATA_H
|
||||
Reference in New Issue
Block a user