From bec1a3e4d0f92a7900ec5b520797b9aaa0e7118e Mon Sep 17 00:00:00 2001 From: Jinhuan <412100639@qq.com> Date: Wed, 6 Nov 2024 23:24:18 +0800 Subject: [PATCH] =?UTF-8?q?1.=E9=80=80=E5=9B=9E=E4=B8=8A=E4=B8=80=E7=AC=94?= =?UTF-8?q?=E7=9A=84=E6=94=B9=E5=8A=A8=202.=E6=B7=BB=E5=8A=A0try=20catch?= =?UTF-8?q?=E5=9D=97=203.=E8=AE=BE=E7=BD=AE=E7=94=9F=E6=88=90pdb=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=B9=B6=E4=B8=94=E5=85=B3=E9=97=AD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cigarette/Cigarette.vcxproj | 6 +- Cigarette/exportData.cpp | 228 +++++++++++++++++++---------------- Cigarette/syncworkthread.cpp | 12 +- Cigarette/threadSend.cpp | 77 +++++++----- Cigarette/threadSendTCP.cpp | 45 ++++--- Cigarette/workthread.cpp | 12 +- 6 files changed, 211 insertions(+), 169 deletions(-) diff --git a/Cigarette/Cigarette.vcxproj b/Cigarette/Cigarette.vcxproj index b391327..69e1752 100644 --- a/Cigarette/Cigarette.vcxproj +++ b/Cigarette/Cigarette.vcxproj @@ -1,4 +1,4 @@ - + @@ -215,7 +215,7 @@ true true stdcpp14 - MaxSpeed + Disabled false 4819;%(DisableSpecificWarnings) @@ -223,7 +223,7 @@ Console $(OutDir)\$(ProjectName).exe $(ProjectDir)OpenCV455Simple\win64\vc15\lib;$(ProjectDir)Pylon6.2\lib\Win64;$(ProjectDir)MvIMPACT\lib\win64;$(ProjectDir)MVS3.2.1\lib\win64;$(ProjectDir)modbus;Ws2_32.lib;%(AdditionalLibraryDirectories) - false + true opencv_world455.lib;modbus.lib;mvDeviceManager.lib;MvCameraControl.lib;%(AdditionalDependencies) diff --git a/Cigarette/exportData.cpp b/Cigarette/exportData.cpp index 7e829e4..f78a5ae 100644 --- a/Cigarette/exportData.cpp +++ b/Cigarette/exportData.cpp @@ -310,124 +310,138 @@ int ExportDataThread::insertXMLNode(const char* xmlPath, _XMLExportDataInfo& dat void ExportDataThread::run() { - while (!b_quit || (b_quit && (!export_XMLData_Info_queue->isEmpty() || readyToSendZip || !tmpListToZip.isEmpty()))) { - _XMLExportDataInfo element; - if (!export_XMLData_Info_queue->isEmpty()) { - export_XMLData_Info_queue->take(element); - - if (element.cameraId != -1 && flag) { - char buf[256]; - memset(buf, 0, 256); - QString xmlPath = QString(EXPORTDATA_FILE).arg(element.cameraId); - QString filePath = g_conf_path.config_path + "/" + xmlPath; - sprintf(buf, filePath.toLocal8Bit().constData(), element.cameraId); - insertXMLNode(buf, element); - QString remotePath = "/tmp/" + xmlPath; - ConnectServer(filePath, remotePath); + try { + while (!b_quit || (b_quit && (!export_XMLData_Info_queue->isEmpty() || readyToSendZip || !tmpListToZip.isEmpty()))) { + _XMLExportDataInfo element; + if (!export_XMLData_Info_queue->isEmpty()) { + export_XMLData_Info_queue->take(element); + + if (element.cameraId != -1 && flag) { + char buf[256]; + memset(buf, 0, 256); + QString xmlPath = QString(EXPORTDATA_FILE).arg(element.cameraId); + QString filePath = g_conf_path.config_path + "/" + xmlPath; + sprintf(buf, filePath.toLocal8Bit().constData(), element.cameraId); + insertXMLNode(buf, element); + QString remotePath = "/tmp/" + xmlPath; + ConnectServer(filePath, remotePath); + } } - } - if ((b_quit || (!timer->isActive() && !readyToSendZip)) && (!tmpListToZip.isEmpty())) { - listToZip.unite(tmpListToZip); - tmpListToZip.clear(); - zipTimeStamp.append(lastTimeStamp[0]); - lastTimeStamp.clear(); - readyToSendZip = true; - } + if ((b_quit || (!timer->isActive() && !readyToSendZip)) && (!tmpListToZip.isEmpty())) { + listToZip.unite(tmpListToZip); + tmpListToZip.clear(); + zipTimeStamp.append(lastTimeStamp[0]); + lastTimeStamp.clear(); + readyToSendZip = true; + } - if (readyToSendZip && !listToZip.isEmpty()) { - QString remotePath; - QStringList zipFilePaths; - QStringList fileList; - QString okStr = "1"; - QProcess process(0); - //QDateTime now_ts = QDateTime::currentDateTime(); - QStringList argsNg; - QStringList argsOk; - - //条盒TJ 小盒BJ - /*zipFilePaths.append(g_conf_path.save_pics_path + "/" + "416_BJ_" + zipTimeStamp[0] + "-00_" + zipTimeStamp[0] + "-59_0.zip"); - zipFilePaths.append(g_conf_path.save_pics_path + "/" + "416_BJ_" + zipTimeStamp[0] + "-00_" + zipTimeStamp[0] + "-59_1.zip"); - fileList.append(g_conf_path.save_pics_path + "/" + zipTimeStamp[0] + "ng.txt"); - fileList.append(g_conf_path.save_pics_path + "/" + zipTimeStamp[0] + "ok.txt");*/ - zipFilePaths.append("416_TJ_" + zipTimeStamp[0] + "-00_" + zipTimeStamp[0] + "-59_0.zip"); - zipFilePaths.append("416_TJ_" + zipTimeStamp[0] + "-00_" + zipTimeStamp[0] + "-59_1.zip"); - fileList.append(zipTimeStamp[0] + "ng.txt"); - fileList.append(zipTimeStamp[0] + "ok.txt"); - - zipTimeStamp.clear(); - - QFile* file[2] = { NULL }; - - QFile fileNg(fileList[0]); - QFile fileOk(fileList[1]); - - fileNg.open(QIODevice::WriteOnly); - fileOk.open(QIODevice::WriteOnly); - - QTextStream ngStream(fileNg); - QTextStream okStream(fileOk); - - argsNg.append("a"); - argsNg.append(zipFilePaths[0]); - - argsOk.append("a"); - argsOk.append(zipFilePaths[1]); - - // create zip file - QMapIterator iterator(listToZip); - while (iterator.hasNext()) { - iterator.next(); - if (iterator.value()) { - ngStream << iterator.key() << endl; + if (readyToSendZip && !listToZip.isEmpty()) { + QString remotePath; + QStringList zipFilePaths; + QStringList fileList; + QString okStr = "1"; + QProcess process(0); + //QDateTime now_ts = QDateTime::currentDateTime(); + QStringList argsNg; + QStringList argsOk; + + //条盒TJ 小盒BJ + /*zipFilePaths.append(g_conf_path.save_pics_path + "/" + "416_BJ_" + zipTimeStamp[0] + "-00_" + zipTimeStamp[0] + "-59_0.zip"); + zipFilePaths.append(g_conf_path.save_pics_path + "/" + "416_BJ_" + zipTimeStamp[0] + "-00_" + zipTimeStamp[0] + "-59_1.zip"); + fileList.append(g_conf_path.save_pics_path + "/" + zipTimeStamp[0] + "ng.txt"); + fileList.append(g_conf_path.save_pics_path + "/" + zipTimeStamp[0] + "ok.txt");*/ + zipFilePaths.append("416_TJ_" + zipTimeStamp[0] + "-00_" + zipTimeStamp[0] + "-59_0.zip"); + zipFilePaths.append("416_TJ_" + zipTimeStamp[0] + "-00_" + zipTimeStamp[0] + "-59_1.zip"); + fileList.append(zipTimeStamp[0] + "ng.txt"); + fileList.append(zipTimeStamp[0] + "ok.txt"); + + zipTimeStamp.clear(); + + QFile* file[2] = { NULL }; + + file[0] = new QFile(fileList[0]); + file[1] = new QFile(fileList[1]); + + // QFile fileNg(fileList[0]); + // QFile fileOk(fileList[1]); + + file[0]->open(QIODevice::WriteOnly); + file[1]->open(QIODevice::WriteOnly); + + QTextStream ngStream(file[0]); + QTextStream okStream(file[1]); + + argsNg.append("a"); + argsNg.append(zipFilePaths[0]); + + argsOk.append("a"); + argsOk.append(zipFilePaths[1]); + + // create zip file + QMapIterator iterator(listToZip); + while (iterator.hasNext()) { + iterator.next(); + if (iterator.value()) { + ngStream << iterator.key() << endl; + } + else { + okStream << iterator.key() << endl; + } } - else { - okStream << iterator.key() << endl; + argsNg.append("-i@" + fileList[0]); + argsNg.append("-mx=3"); + + argsOk.append("-i@" + fileList[1]); + argsOk.append("-mx=3"); + + file[0]->close(); + file[1]->close(); + + for (int i = 0; i < zipFilePaths.size(); i++) { + if (file[i]->size() == 0) { + continue; + } + process.start(QApplication::applicationDirPath() + "/7z.exe", i == 0 ? argsNg : argsOk); + process.waitForStarted(); + process.waitForFinished(); + //QString out = QString::fromLocal8Bit(process.readAllStandardOutput()); + //qDebug() << out; + remotePath = zipFilePaths[i].mid(zipFilePaths[i].indexOf("image") - 1); + qDebug() << remotePath; + ConnectServer(zipFilePaths[i], remotePath); + QFile file(zipFilePaths[i]); + file.remove(); } + file[0]->remove(); + file[1]->remove(); + listToZip.clear(); + readyToSendZip = false; } - argsNg.append("-i@" + fileList[0]); - argsNg.append("-mx=3"); - - argsOk.append("-i@" + fileList[1]); - argsOk.append("-mx=3"); - - fileNg.close(); - fileOk.close(); + } + flag = false; - for (int i = 0; i < zipFilePaths.size(); i++) { - if (file[i]->size() == 0) { - continue; - } - process.start(QApplication::applicationDirPath() + "/7z.exe", i == 0 ? argsNg : argsOk); - process.waitForStarted(); - process.waitForFinished(); - //QString out = QString::fromLocal8Bit(process.readAllStandardOutput()); - //qDebug() << out; - remotePath = zipFilePaths[i].mid(zipFilePaths[i].indexOf("image") - 1); - qDebug() << remotePath; - ConnectServer(zipFilePaths[i], remotePath); - QFile file(zipFilePaths[i]); - file.remove(); - } - fileNg.remove(); - fileOk.remove(); - listToZip.clear(); - readyToSendZip = false; + for (int index = 0; index < NumberOfSupportedCameras; index++) { + pDocument[index]->~XMLDocument(); } - } - flag = false; - for (int index = 0; index < NumberOfSupportedCameras; index++) { - pDocument[index]->~XMLDocument(); + if (hftp != NULL) { + InternetCloseHandle(hftp); + hftp = NULL; + } + if (hftp != NULL) { + InternetCloseHandle(hint); + hint = NULL; + } } - - if (hftp != NULL) { - InternetCloseHandle(hftp); - hftp = NULL; + catch (const std::exception& e) { + // 注意:std::exception通常不捕获由运行时错误引发的异常 + // 但这里是为了演示catch块的结构 + std::cerr << __FUNCTION__<<"捕获到异常: " << e.what() << std::endl; } - if (hftp != NULL) { - InternetCloseHandle(hint); - hint = NULL; + catch (...) { + // 使用省略号(...)可以捕获所有类型的异常,这是一种通用的捕获方式 + std::cerr << "捕获到未知类型的异常" << std::endl; } } diff --git a/Cigarette/syncworkthread.cpp b/Cigarette/syncworkthread.cpp index 333ede9..f84d548 100644 --- a/Cigarette/syncworkthread.cpp +++ b/Cigarette/syncworkthread.cpp @@ -389,9 +389,13 @@ void SyncWorkThread::run() // emit display_check_total(local_camera_number, ++(frame_total[local_camera_number])); } } - catch (cv::Exception& e) - { - const char* err_msg = e.what(); - std::cout << "exception caught: " << err_msg << std::endl; + catch (const std::exception& e) { + // 注意:std::exception通常不捕获由运行时错误引发的异常 + // 但这里是为了演示catch块的结构 + std::cerr << __FUNCTION__<<"捕获到异常: " << e.what() << std::endl; + } + catch (...) { + // 使用省略号(...)可以捕获所有类型的异常,这是一种通用的捕获方式 + std::cerr << "捕获到未知类型的异常" << std::endl; } } diff --git a/Cigarette/threadSend.cpp b/Cigarette/threadSend.cpp index 0ee3fdb..f087a34 100644 --- a/Cigarette/threadSend.cpp +++ b/Cigarette/threadSend.cpp @@ -25,41 +25,52 @@ void threadSend::stop() void threadSend::run() { - while (isLoop) - { - _UDPSendInfo UDPSendInfo; - Local_UDP_Info_queue->take(UDPSendInfo); - switch (UDPSendInfo.FrameID) { - case info_frame: { - sendData(&UDPSendInfo, dataport); - break; - } - case speed_frame: { - sendSpeed(&UDPSendInfo, dataport); - break; - } - case kick_frame: { - sendKick(&UDPSendInfo, dataport); - break; - } - case ok_frame: { - sendOK(&UDPSendInfo, dataport); - break; - } - case ng_frame: { - sendNG(&UDPSendInfo, dataport); - break; - } - case total_frame: { - sendTotal(&UDPSendInfo, dataport); - break; - } - case image_frame: { - sendImage(&UDPSendInfo, imageport); - break; - } + try { + while (isLoop) + { + _UDPSendInfo UDPSendInfo; + Local_UDP_Info_queue->take(UDPSendInfo); + switch (UDPSendInfo.FrameID) { + case info_frame: { + sendData(&UDPSendInfo, dataport); + break; + } + case speed_frame: { + sendSpeed(&UDPSendInfo, dataport); + break; + } + case kick_frame: { + sendKick(&UDPSendInfo, dataport); + break; + } + case ok_frame: { + sendOK(&UDPSendInfo, dataport); + break; + } + case ng_frame: { + sendNG(&UDPSendInfo, dataport); + break; + } + case total_frame: { + sendTotal(&UDPSendInfo, dataport); + break; + } + case image_frame: { + sendImage(&UDPSendInfo, imageport); + break; + } + } } } + catch (const std::exception& e) { + // 注意:std::exception通常不捕获由运行时错误引发的异常 + // 但这里是为了演示catch块的结构 + std::cerr << __FUNCTION__<<"捕获到异常: " << e.what() << std::endl; + } + catch (...) { + // 使用省略号(...)可以捕获所有类型的异常,这是一种通用的捕获方式 + std::cerr << "捕获到未知类型的异常" << std::endl; + } } void threadSend::sendData(_UDPSendInfo* UDPSendInfo, int port) diff --git a/Cigarette/threadSendTCP.cpp b/Cigarette/threadSendTCP.cpp index 8fbd5de..7bc3344 100644 --- a/Cigarette/threadSendTCP.cpp +++ b/Cigarette/threadSendTCP.cpp @@ -64,30 +64,39 @@ bool threadSendTCP::connectTCP() { void threadSendTCP::run() { - + try { #ifdef __TCPClient - if (!connectTCP()) - { - qDebug() << "TCP connect error!"; - } -#endif - while (isLoop) { - _TCPSendInfo TCPSendInfo; - Local_TCP_Info_queue->take(TCPSendInfo); - num++; - sendData(&TCPSendInfo, num); -#ifdef __TCPServer - if (ClientStatus == QAbstractSocket::ConnectedState) + if (!connectTCP()) { - sendData(&TCPSendInfo, num); + qDebug() << "TCP connect error!"; } +#endif + while (isLoop) { + _TCPSendInfo TCPSendInfo; + Local_TCP_Info_queue->take(TCPSendInfo); + num++; + sendData(&TCPSendInfo, num); +#ifdef __TCPServer + if (ClientStatus == QAbstractSocket::ConnectedState) + { + sendData(&TCPSendInfo, num); + } #endif #ifdef __TCPClient - sendData(&TCPSendInfo, num); + sendData(&TCPSendInfo, num); #endif - //mySocket->write("Hello! here is tcp client!\n"); - //mySocket->flush(); - + //mySocket->write("Hello! here is tcp client!\n"); + //mySocket->flush(); + } + } + catch (const std::exception& e) { + // 注意:std::exception通常不捕获由运行时错误引发的异常 + // 但这里是为了演示catch块的结构 + std::cerr << __FUNCTION__<<"捕获到异常: " << e.what() << std::endl; + } + catch (...) { + // 使用省略号(...)可以捕获所有类型的异常,这是一种通用的捕获方式 + std::cerr << "捕获到未知类型的异常" << std::endl; } } diff --git a/Cigarette/workthread.cpp b/Cigarette/workthread.cpp index cb6f166..d202c03 100644 --- a/Cigarette/workthread.cpp +++ b/Cigarette/workthread.cpp @@ -321,9 +321,13 @@ void WorkThread::run() #endif } } - catch (cv::Exception& e) - { - const char* err_msg = e.what(); - std::cout << "exception caught: " << err_msg << std::endl; + catch (const std::exception& e) { + // 注意:std::exception通常不捕获由运行时错误引发的异常 + // 但这里是为了演示catch块的结构 + std::cerr << __FUNCTION__<<"捕获到异常: " << e.what() << std::endl; + } + catch (...) { + // 使用省略号(...)可以捕获所有类型的异常,这是一种通用的捕获方式 + std::cerr << "捕获到未知类型的异常" << std::endl; } }