From 2b3f8d699ac10f389dace2128e8328105fc61791 Mon Sep 17 00:00:00 2001 From: yanju1219 Date: Sun, 4 Feb 2024 13:42:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ModifyDatTime.pro | 29 ++++ ModifyDatTime.pro.user | 267 ++++++++++++++++++++++++++++++ main.cpp | 11 ++ mainwindow.cpp | 362 +++++++++++++++++++++++++++++++++++++++++ mainwindow.h | 78 +++++++++ mainwindow.ui | 131 +++++++++++++++ 6 files changed, 878 insertions(+) create mode 100644 ModifyDatTime.pro create mode 100644 ModifyDatTime.pro.user create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 mainwindow.ui diff --git a/ModifyDatTime.pro b/ModifyDatTime.pro new file mode 100644 index 0000000..d46119e --- /dev/null +++ b/ModifyDatTime.pro @@ -0,0 +1,29 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +msvc { + QMAKE_CFLAGS += /utf-8 + QMAKE_CXXFLAGS += /utf-8 +} + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/ModifyDatTime.pro.user b/ModifyDatTime.pro.user new file mode 100644 index 0000000..1db82cc --- /dev/null +++ b/ModifyDatTime.pro.user @@ -0,0 +1,267 @@ + + + + + + EnvironmentId + {8cfa9d78-659c-4111-ba1f-cc573f4fb26a} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + 0 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + true + true + *.md, *.MD, Makefile + false + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 8 + true + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop Qt 5.15.2 MSVC2019 64bit + Desktop Qt 5.15.2 MSVC2019 64bit + qt.qt5.5152.win64_msvc2019_64_kit + 1 + 0 + 0 + + 0 + E:\MyCode\Qt\build-ModifyDatTime-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug + E:/MyCode/Qt/build-ModifyDatTime-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + E:\MyCode\Qt\build-ModifyDatTime-Desktop_Qt_5_15_2_MSVC2019_64bit-Release + E:/MyCode/Qt/build-ModifyDatTime-Desktop_Qt_5_15_2_MSVC2019_64bit-Release + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + + + 0 + E:\MyCode\Qt\build-ModifyDatTime-Desktop_Qt_5_15_2_MSVC2019_64bit-Profile + E:/MyCode/Qt/build-ModifyDatTime-Desktop_Qt_5_15_2_MSVC2019_64bit-Profile + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + 0 + + 3 + + + 0 + Deploy + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + 0 + true + + 2 + + false + + Qt4ProjectManager.Qt4RunConfiguration:E:/MyCode/Qt/ModifyDatTime/ModifyDatTime.pro + E:/MyCode/Qt/ModifyDatTime/ModifyDatTime.pro + false + true + true + true + E:/MyCode/Qt/build-ModifyDatTime-Desktop_Qt_5_15_2_MSVC2019_64bit-Release + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..5831735 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,362 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include +#include +#include +#include +#include +#include +#include +#include + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); + folderPath = ""; + +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::ReadDat(QString fileName) +{ + int year = ui->yearSpinBox->value(); + int month = ui->monthSpinBox->value(); + int day = ui->daySpinBox->value(); + //转化为16进制 + uint8_t hexYear = IntToHex(year); + uint8_t hexMonth = IntToHex(month); + uint8_t hexDay = IntToHex(day); + + //QString filename = outputDir+"/"+fileName; + // // 一个示例日期,年月日分别为2023年、12月、3日 + // QDate date(year+2000, month, day); + // // 将日期格式化为yymmdd + // QString formattedDate = date.toString("yyyyMMdd"); + + // //修改文件名的时间信息 + // QString f1 = fileName; + // f1.remove(11,f1.length()-11); + // QString f2 = fileName; + // f2.remove(0,11+8); + // QString str = f1+formattedDate+f2; + + QString outputFileName = GetOutputFileName(fileName,year,month,day); + //创建修改后的dat文件 + QFile writeFile(outputDir+"/"+outputFileName); + QDataStream outStream; + if (writeFile.open(QIODevice::WriteOnly)) { + // 创建数据流,用于写入二进制数据 + // outStream = QDataStream(&writeFile); + outStream.setDevice(&writeFile); + outStream.setByteOrder(QDataStream::LittleEndian); + } + + int len = 265; + quint8 *data = new quint8[len]; + QFile file1 = QFile(folderPath+"/"+fileName); + //QFile file1 = QFile("E:\\dat\\output\\下行遥测_0_1_0_20230619230109.dat"); + file1.open(QIODevice::ReadOnly); + QDataStream readStream = QDataStream(&file1); + readStream.setVersion(QDataStream::Qt_5_15); //设置数据流的版本 + readStream.setByteOrder(QDataStream::LittleEndian); + int i =0; + quint8* content = new quint8[28]; //创建一个28字节的数据缓冲区 + // int sub1 = 0; + // int sub2 = 0; + while(!readStream.atEnd()) + { + readStream.readRawData((char *)data, len); //读取一帧 + // i++; + //四个子帧解析 + // int id1 = 16; + // int id2 = id1+32; + // int id3 = id2+32; + // int id4 = id3+32; + int startID = 16; + for (int var = 0; var < 4; ++var) { + startID = 16 + 32*var; + // if(data[startID]!=0x23) + // qDebug()<<"************Zhen1:"<0) + data[startID+18] = hexDay; + if(hexMonth>0) + data[startID+19] = hexMonth; + if(hexYear>0) + data[startID+20] = hexYear; + //计算CRC16校验位 + for(int i=0;i<28;i++)//提取数据帧内容 + { + content[i] = data[startID+2+i]; + } + quint16 crc = GetCrcSum(content,28);//计算校验码 + // 提取高位和低位 + quint8 highByte = static_cast((crc >> 8) & 0xFF); + quint8 lowByte = static_cast(crc & 0xFF); + //修改CRC校验位 + data[startID+30] = lowByte; + data[startID+31] = highByte; + } + } + } + // bool b = data[16]==0x23; + // bool b1 = data[17]==0x23; + // bool b2 = data[18]==0x85; + //修改系统时间戳 + if(hexYear>0) + data[2] = hexYear; + if(hexMonth>0) + data[3] = hexMonth; + if(hexDay>0) + data[4] = hexDay; + + //修改最后校验位 + data[len-1] = GetDataCheckCRC(data,len); + + //写入文件 + outStream.writeRawData((char *)data,len); + } + file1.close(); + writeFile.close(); + + qDebug()<<"*********************i:"<= 1 && day <= 29) { + return true; + } + } else { + if (day >= 1 && day <= 28) { + return true; + } + } + } else if (month == 4 || month == 6 || month == 9 || month == 11) { + if (day >= 1 && day <= 30) { + return true; + } + } else { + if (day >= 1 && day <= 31) { + return true; + } + } + return false; +} + + +QString MainWindow::GetOutputFileName(QString fileName,int year, int month, int day) +{ + //QString str = "下行遥测_0_17_0_20221114024453.dat"; + QStringList strList = fileName.split("_"); + QString endstr = strList.constLast(); + QString lastStr = endstr.mid(8,-1); + if(year==0) + year = endstr.mid(0,4).toInt()-2000; + //QString year0 = endstr.mid(0,4); + if(month==0) + month = endstr.mid(4,2).toInt(); + if(day==0) + day = endstr.mid(6,2).toInt(); + // QString month = endstr.mid(4,2); + // QString day = endstr.mid(6,2); + + // 一个示例日期,年月日分别为2023年、12月、3日 + QDate date(year+2000, month, day); + // 将日期格式化为yymmdd + QString formattedDate = date.toString("yyyyMMdd"); + + QString outputFileName = ""; + strList.removeLast(); + strList.append(formattedDate); + outputFileName = strList.join("_") + lastStr; + // for (int var = 0; var < strList.length()-2; ++var) { + // outputFileName += strList.at(var); + // } + return outputFileName; +} + +void MainWindow::on_openDirBtn_clicked() +{ + // 创建一个QFileDialog对象 + QFileDialog dialog; + + // 设置对话框的标题 + dialog.setWindowTitle("Select Folder"); + + // 设置对话框模式为选择文件夹 + dialog.setFileMode(QFileDialog::Directory); + // 打开对话框 + if (dialog.exec()) { + // 获取用户选择的文件夹路径 + folderPath = dialog.selectedFiles().at(0); + qDebug() << "Selected Folder: " << folderPath; + } else { + qDebug() << "User canceled the operation."; + } + +} + +void MainWindow::CreateDir() +{ + // 创建一个QDir对象 + QDir dir; + outputDir = folderPath+"/output"; + // 设置目录路径 + dir.setPath(outputDir); + + // 创建文件夹 + if (dir.mkpath(outputDir)) { + qDebug() << "Folder created successfully."; + } else { + qDebug() << "Failed to create folder."; + } + //QDir directory(folderPath+"/output"); +} + + +void MainWindow::on_startModifyBtn_clicked() +{ + int year = ui->yearSpinBox->value(); + int month = ui->monthSpinBox->value(); + int day = ui->daySpinBox->value(); + + if(year<=0&&month<=0&&day<=0) + { + QMessageBox::information(NULL,"提示","请设置要修改的年月日!!!"); + return; + } + if(folderPath=="") + { + QMessageBox::information(NULL,"提示","请先设置文件夹!!!"); + return; + } + + // 检查Day是否正确 + if(day>0 && !isValidDay(year+2000,month,day)) + { + QMessageBox::information(NULL,"提示","日期有误,请重新设置!!!"); + return; + } + + // 创建一个 QDir 对象 + QDir directory(folderPath); + // 获取文件夹中的所有文件 + QStringList files = directory.entryList(QDir::Files); + int numFiles = files.length(); + if(numFiles==0) + { + QMessageBox::information(NULL,"提示","该文件夹没有dat文件!!!"); + return; + } + + //新建output文件夹 + CreateDir(); + //开始处理 + for (int n = 0; n < numFiles; ++n) + { + ReadDat(files.at(n)); + } + + QMessageBox::information(NULL,"提示","处理结束!!!"); +} + diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..ede94b1 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,78 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +QT_BEGIN_NAMESPACE +namespace Ui { +class MainWindow; +} +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private slots: + void on_openDirBtn_clicked(); + + void on_startModifyBtn_clicked(); + +private: + void CreateDir(); + void ReadDat(QString fileName); + uint8_t IntToHex(int data); + quint16 GetCrcSum(quint8 *dataptr, quint16 len); + quint8 GetDataCheckCRC(quint8 *dataptr, quint16 len); + + bool isLeapYear(int year); + bool isValidDay(int year, int month, int day); + + QString GetOutputFileName(QString fileName,int year,int month,int day); + +private: + Ui::MainWindow *ui; + QString folderPath; + QString outputDir; + const quint8 CRCLIST[512]= + { + 0x0,0x0,0x89,0x11,0x12,0x23,0x9b,0x32,0x24,0x46,0xad,0x57,0x36,0x65,0xbf,0x74, + 0x48,0x8c,0xc1,0x9d,0x5a,0xaf,0xd3,0xbe,0x6c,0xca,0xe5,0xdb,0x7e,0xe9,0xf7,0xf8, + 0x81,0x10,0x8,0x1,0x93,0x33,0x1a,0x22,0xa5,0x56,0x2c,0x47,0xb7,0x75,0x3e,0x64, + 0xc9,0x9c,0x40,0x8d,0xdb,0xbf,0x52,0xae,0xed,0xda,0x64,0xcb,0xff,0xf9,0x76,0xe8, + 0x2,0x21,0x8b,0x30,0x10,0x2,0x99,0x13,0x26,0x67,0xaf,0x76,0x34,0x44,0xbd,0x55, + 0x4a,0xad,0xc3,0xbc,0x58,0x8e,0xd1,0x9f,0x6e,0xeb,0xe7,0xfa,0x7c,0xc8,0xf5,0xd9, + 0x83,0x31,0xa,0x20,0x91,0x12,0x18,0x3,0xa7,0x77,0x2e,0x66,0xb5,0x54,0x3c,0x45, + 0xcb,0xbd,0x42,0xac,0xd9,0x9e,0x50,0x8f,0xef,0xfb,0x66,0xea,0xfd,0xd8,0x74,0xc9, + 0x4,0x42,0x8d,0x53,0x16,0x61,0x9f,0x70,0x20,0x4,0xa9,0x15,0x32,0x27,0xbb,0x36, + 0x4c,0xce,0xc5,0xdf,0x5e,0xed,0xd7,0xfc,0x68,0x88,0xe1,0x99,0x7a,0xab,0xf3,0xba, + 0x85,0x52,0xc,0x43,0x97,0x71,0x1e,0x60,0xa1,0x14,0x28,0x5,0xb3,0x37,0x3a,0x26, + 0xcd,0xde,0x44,0xcf,0xdf,0xfd,0x56,0xec,0xe9,0x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x5b,0x56,0x69,0xdf,0x78,0x60,0xc,0xe9,0x1d,0x72,0x2f,0xfb,0x3e, + 0x8d,0xd6,0x4,0xc7,0x9f,0xf5,0x16,0xe4,0xa9,0x90,0x20,0x81,0xbb,0xb3,0x32,0xa2, + 0xc5,0x5a,0x4c,0x4b,0xd7,0x79,0x5e,0x68,0xe1,0x1c,0x68,0xd,0xf3,0x3f,0x7a,0x2e, + 0xe,0xe7,0x87,0xf6,0x1c,0xc4,0x95,0xd5,0x2a,0xa1,0xa3,0xb0,0x38,0x82,0xb1,0x93, + 0x46,0x6b,0xcf,0x7a,0x54,0x48,0xdd,0x59,0x62,0x2d,0xeb,0x3c,0x70,0xe,0xf9,0x1f, + 0x8f,0xf7,0x6,0xe6,0x9d,0xd4,0x14,0xc5,0xab,0xb1,0x22,0xa0,0xb9,0x92,0x30,0x83, + 0xc7,0x7b,0x4e,0x6a,0xd5,0x58,0x5c,0x49,0xe3,0x3d,0x6a,0x2c,0xf1,0x1e,0x78,0xf + }; +}; +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..c5ee6b9 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,131 @@ + + + MainWindow + + + + 0 + 0 + 404 + 181 + + + + MainWindow + + + + + + 80 + 20 + 92 + 28 + + + + 打开文件夹 + + + + + + 20 + 70 + 31 + 31 + + + + 年: + + + + + + 50 + 70 + 50 + 28 + + + + QAbstractSpinBox::NoButtons + + + + + + 120 + 70 + 31 + 31 + + + + 月: + + + + + + 150 + 70 + 50 + 28 + + + + QAbstractSpinBox::NoButtons + + + 12 + + + + + + 250 + 70 + 50 + 28 + + + + QAbstractSpinBox::NoButtons + + + 31 + + + + + + 220 + 70 + 31 + 31 + + + + 日: + + + + + + 200 + 20 + 92 + 28 + + + + 开始处理 + + + + + + +