From fc8f54c7d3251b8fc71514245aeaf2266c0e0c2e Mon Sep 17 00:00:00 2001 From: seiyu Date: Sat, 2 Mar 2024 17:37:47 +0800 Subject: [PATCH] =?UTF-8?q?tcp=E4=BC=A0=E5=9B=BE=E7=89=87=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cigarette/CaptureThreadHIK.cpp | 2 +- Cigarette/Cigarette.vcxproj | 8 ++-- Cigarette/Cigarette.vcxproj.filters | 6 +++ Cigarette/License.dat | 1 + Cigarette/cigarette.cpp | 33 +++++++++++++--- Cigarette/cigarette.h | 5 +++ Cigarette/common.h | 5 ++- Cigarette/savethread.h | 2 +- Cigarette/threadSend.cpp | 1 - Cigarette/threadSendTCP.cpp | 61 +++++++++++++++++++++++++++++ Cigarette/threadSendTCP.h | 55 ++++++++++++++++++++++++++ Cigarette/workthread.cpp | 22 ++++++++++- 12 files changed, 185 insertions(+), 16 deletions(-) create mode 100644 Cigarette/License.dat create mode 100644 Cigarette/threadSendTCP.cpp create mode 100644 Cigarette/threadSendTCP.h diff --git a/Cigarette/CaptureThreadHIK.cpp b/Cigarette/CaptureThreadHIK.cpp index c2148ce..8879dfb 100644 --- a/Cigarette/CaptureThreadHIK.cpp +++ b/Cigarette/CaptureThreadHIK.cpp @@ -7,7 +7,7 @@ extern bool g_debug_mode; //相机调试模式,工作模式必须停止状态才能打开 extern SingleCamInfoStruct SingleCamInfo[NumberOfSupportedCameras]; extern SyncQueue<_XMLExportDataInfo>* export_XMLData_Info_queue; -extern PLCDevice * m_PLCDevice; +extern PLCDevice* m_PLCDevice; inline void LossCallBackfunction(unsigned int pData, void* pUser){ try{ diff --git a/Cigarette/Cigarette.vcxproj b/Cigarette/Cigarette.vcxproj index 0a177e7..31f9c98 100644 --- a/Cigarette/Cigarette.vcxproj +++ b/Cigarette/Cigarette.vcxproj @@ -133,15 +133,15 @@ true true stdcpp14 - Disabled + MaxSpeed false 4819;%(DisableSpecificWarnings) Console $(OutDir)\$(ProjectName).exe - $(QTDIR)\lib;$(ProjectDir)OpenCV455Simple\win64\vc15\lib;$(ProjectDir)Pylon6.2\lib\Win64;$(ProjectDir)MvIMPACT\lib\win64;$(ProjectDir)MVS3.2.1\lib\win64;$(ProjectDir)modbus;%(AdditionalLibraryDirectories) - true + $(QTDIR)\lib;$(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 qtmain.lib;Qt5Core.lib;Qt5Widgets.lib;Qt5Gui.lib;opencv_world455.lib;modbus.lib;mvDeviceManager.lib;MvCameraControl.lib;Qt5Network.lib;%(AdditionalDependencies) @@ -349,6 +349,7 @@ + @@ -394,6 +395,7 @@ + $(QTDIR)\bin\moc.exe;%(FullPath) diff --git a/Cigarette/Cigarette.vcxproj.filters b/Cigarette/Cigarette.vcxproj.filters index 833d2c3..d77a18f 100644 --- a/Cigarette/Cigarette.vcxproj.filters +++ b/Cigarette/Cigarette.vcxproj.filters @@ -257,6 +257,9 @@ Source Files + + Source Files + @@ -420,5 +423,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Cigarette/License.dat b/Cigarette/License.dat new file mode 100644 index 0000000..64489a7 --- /dev/null +++ b/Cigarette/License.dat @@ -0,0 +1 @@ +8瀺姄讀;秈嘊2@%^/|讁勣5 *晞)*3H俫k绒葙糶 Ed螿う+SBt跼R羱驧觯XT2禾O'E萐1,呐l 鷊Z轮=w祮xIK葎績,Rb?斩襥%憼:YCm騿湰n} \ No newline at end of file diff --git a/Cigarette/cigarette.cpp b/Cigarette/cigarette.cpp index 9a5a484..0df79eb 100644 --- a/Cigarette/cigarette.cpp +++ b/Cigarette/cigarette.cpp @@ -59,10 +59,12 @@ SyncQueue *g_debug_queue[NumberOfSupportedCameras]; // #ifdef __UDPSend SyncQueue<_UDPSendInfo>* UDP_Info_queue; #endif +#ifdef __TCPSend +SyncQueue<_TCPSendInfo>* TCP_Info_queue; +#endif bool flag = false; - -#ifdef __ExportData SyncQueue<_XMLExportDataInfo>* export_XMLData_Info_queue; +#ifdef __ExportData ExportDataThread exportDataThread; #endif @@ -138,12 +140,16 @@ Cigarette::Cigarette(QWidget *parent) emit sengMsgToClog("Application Start."); - g_save_queue = new SyncQueue >(Queue_Size); + g_save_queue = new SyncQueue>(Queue_Size); g_save_queue->name = "save queue"; #ifdef __UDPSend UDP_Info_queue = new SyncQueue<_UDPSendInfo>(Queue_Size); UDP_Info_queue->name = "UDP Info queue"; #endif +#ifdef __TCPSend + TCP_Info_queue = new SyncQueue<_TCPSendInfo>(Queue_Size); + TCP_Info_queue->name = "TCP Info queue"; +#endif #ifdef __ExportData export_XMLData_Info_queue = new SyncQueue<_XMLExportDataInfo>(Queue_Size); export_XMLData_Info_queue->name = "Export Data Info queue"; @@ -262,6 +268,13 @@ Cigarette::Cigarette(QWidget *parent) rThread->start_work(); #endif +#ifdef __TCPSend + std::string serverIp = "192.168.1.120"; + int serverPort = 8888; + tcpSendThread.init(TCP_Info_queue, serverIp, serverPort); + tcpSendThread.start_work(); +#endif + #ifdef __ExportData exportDataThread.init(); connect(this, &Cigarette::sendMsgToExportData, &exportDataThread, &ExportDataThread::EDrecMsgFromCigarette); @@ -359,7 +372,7 @@ Cigarette::Cigarette(QWidget *parent) } else if (alarm_info.alarm_code == 0) { ui.label_alarm->setStyleSheet(tr("background-color: rgb(0, 170, 0);")); - ui.label_alarm->setText(QStringLiteral("无报警")); + ui.label_alarm->setText(QStringLiteral("无报警")); } #ifdef __UDPSend QString str; @@ -406,8 +419,10 @@ Cigarette::Cigarette(QWidget *parent) m_delay = new QTimer(this);//换班防连击 connect(m_delay, SIGNAL(timeout()), this, SLOT(enable_shift())); +#ifdef __UDPSend m_sendMsg_delay = new QTimer(this); connect(m_sendMsg_delay, SIGNAL(timeout()), this, SLOT(sendLatestData())); +#endif m_op_delay = new QTimer(this); connect(m_op_delay, SIGNAL(timeout()), this, SLOT(op_timeout())); @@ -629,8 +644,10 @@ void Cigarette::pause_work() label_reslut_mat[i]->setVisible(false); } +#ifdef __UDPSend m_sendMsg_delay->setSingleShot(true); m_sendMsg_delay->start(1000); +#endif } void Cigarette::Exit() @@ -663,6 +680,10 @@ void Cigarette::Exit() delete rThread; #endif +#ifdef __TCPSend + tcpSendThread.stop(); +#endif + #ifdef __ExportData exportDataThread.stop(); // delete export_Data_Info_queue; @@ -897,7 +918,7 @@ void Cigarette::OnRotateReleasedHub(int Num)// { QString str = "Start to rotate picture " + QString::number(90 * ((rotationAngle[Num] + 1) % 4)) + " degrees of camera " + QString::number(Num) + "."; emit sengMsgToClog(str); - if(isNeedRotate[Num] == true) + if( isNeedRotate[Num] == true) { rotationAngle[Num] = (rotationAngle[Num] + 1) % 4; save_rotate_message(); @@ -1074,7 +1095,7 @@ void Cigarette::OnDBClickHub(int Num_Cnt) display_lable_info[Num][Cnt].height ); rotate_mat[Num]->setGeometry( - rotate_info[Num].x, + rotate_info[Num].x, rotate_info[Num].y, rotate_info[Num].width, rotate_info[Num].height diff --git a/Cigarette/cigarette.h b/Cigarette/cigarette.h index 07603c4..a6b50a7 100644 --- a/Cigarette/cigarette.h +++ b/Cigarette/cigarette.h @@ -29,6 +29,7 @@ #include "Logthread.h" #include "threadSend.h" #include "threadReceive.h" +#include "threadSendTCP.h" #include "Cleanthread.h" #include @@ -206,6 +207,10 @@ public: threadSend sThread; threadReceive *rThread; #endif +#ifdef __TCPSend + private: + threadSendTCP tcpSendThread; +#endif public slots: void CleanThreadStartAuto(); #ifdef __UDPSend diff --git a/Cigarette/common.h b/Cigarette/common.h index ff08da8..4465bf4 100644 --- a/Cigarette/common.h +++ b/Cigarette/common.h @@ -7,7 +7,8 @@ #include "QtCore\qdatetime.h" //#define __DEBUG //debug信息输出功能 -#define __UDPSend //网络发送功能 +//#define __UDPSend //网络发送功能 +#define __TCPSend // TCP发送 #define USB_BASLER_NEW_FW //使用basler定制固件 //#define IMM_PROCESS //拍照后立马处理,不等校验信号 //#define IMM_FEED_BACK //处理完后立马反馈,不等校验信号 @@ -20,7 +21,7 @@ //#define DOUBLE_FEED_BACK //一次ng,两次反馈ng信号 #endif //#define identify_Hik_YSXID//识别海康相机YSXID -#define __ExportData // 输出检测数据到XML文档 +//#define __ExportData // 输出检测数据到XML文档 #define DRAW_RECT // 鼠标画框功能 #define Queue_Size 15 diff --git a/Cigarette/savethread.h b/Cigarette/savethread.h index 057f2fb..1ced71d 100644 --- a/Cigarette/savethread.h +++ b/Cigarette/savethread.h @@ -12,7 +12,7 @@ #include #include -extern SyncQueue > *g_save_queue; //图像保存队列 +extern SyncQueue>* g_save_queue; //图像保存队列 class SaveThread : public QThread { diff --git a/Cigarette/threadSend.cpp b/Cigarette/threadSend.cpp index 2550e66..4ea86e6 100644 --- a/Cigarette/threadSend.cpp +++ b/Cigarette/threadSend.cpp @@ -64,7 +64,6 @@ void threadSend::run() void threadSend::sendData(_UDPSendInfo *UDPSendInfo,int port) { - QString res = QString("%1_%2_%3") .arg(QString::number(UDPSendInfo->FrameID)) .arg(UDPSendInfo->JD) diff --git a/Cigarette/threadSendTCP.cpp b/Cigarette/threadSendTCP.cpp new file mode 100644 index 0000000..69f5805 --- /dev/null +++ b/Cigarette/threadSendTCP.cpp @@ -0,0 +1,61 @@ +#include "threadSendTCP.h" +#include +#include +#include +#include +#pragma comment(lib, "ws2_32.lib") + +void threadSendTCP::init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_) { + + ip = QString::fromStdString(ip_); + port = port_; + qDebug() << "tcp ip:" << ip << "| tcp port:" << port; + Local_TCP_Info_queue = p_TCP_Info_queue; +} +void threadSendTCP::start_work() +{ + //start(HighestPriority); + start(); +} + +void threadSendTCP::stop() +{ + isLoop = false; + // wait(); + delete mySocket; +} + +bool threadSendTCP::connectTCP() { + mySocket = new QTcpSocket(); + // 取消已有的连接 + mySocket->abort(); + // 连接服务器 + mySocket->connectToHost(ip, port); + if (!mySocket->waitForConnected(30000)) { + qDebug() << "connect failed!"; + return false; + } + qDebug() << "connect successfully!"; + return true; +} + +void threadSendTCP::run() +{ + if (!connectTCP()) + qDebug() << "TCP connect error!"; + while (isLoop) { + _TCPSendInfo TCPSendInfo; + Local_TCP_Info_queue->take(TCPSendInfo); + num++; + sendData(&TCPSendInfo, num); + //mySocket->write("Hello! here is tcp client!\n"); + //mySocket->flush(); + } +} + +void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) { + std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString(); + mySocket->write(fileName.c_str()); + mySocket->write("\n"); + mySocket->flush(); +} \ No newline at end of file diff --git a/Cigarette/threadSendTCP.h b/Cigarette/threadSendTCP.h new file mode 100644 index 0000000..760e9fb --- /dev/null +++ b/Cigarette/threadSendTCP.h @@ -0,0 +1,55 @@ +#pragma once +#include +#include +#include +#include +#include +#include "common.h" +#include "SyncQueue.h" + +class _TCPSendInfo +{ +public: + std::string pics_name; + + _TCPSendInfo() + { + pics_name = ""; + } +}; + +class threadSendTCP : public QThread { +public: + QString ip; + int port; + int num = 0; + + threadSendTCP(QObject* o = nullptr) :QThread(o) + { + isLoop = true; + } + + ~threadSendTCP() + { + stop(); + _TCPSendInfo TCPSendInfo; + Local_TCP_Info_queue->put(TCPSendInfo); + quit(); + wait(); + } + void stop(); + bool connectTCP(); + +protected: + void run(); + +public: + void init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_); + void start_work(); + void sendData(_TCPSendInfo* TCPSendInfo, int Num); + +public: + SyncQueue<_TCPSendInfo>* Local_TCP_Info_queue; + std::atomic_bool isLoop = { 0 }; + QTcpSocket *mySocket; +}; \ No newline at end of file diff --git a/Cigarette/workthread.cpp b/Cigarette/workthread.cpp index 87b88f3..f04927c 100644 --- a/Cigarette/workthread.cpp +++ b/Cigarette/workthread.cpp @@ -21,6 +21,9 @@ extern SyncQueue>* g_image_queue[NumberOfSupportedCamera #ifdef __UDPSend extern SyncQueue<_UDPSendInfo>* UDP_Info_queue; #endif +#ifdef __TCPSend +extern SyncQueue<_TCPSendInfo>* TCP_Info_queue; +#endif #ifdef __ExportData extern ExportDataThread exportDataThread; #endif @@ -78,7 +81,6 @@ void WorkThread::run() #endif } - QDateTime now_ts = QDateTime::currentDateTime(); std::pair element; local_g_image_queue->take(element); @@ -90,6 +92,10 @@ void WorkThread::run() UDPSendInfo.FrameID = info_frame; UDPSendInfo.index = local_camera_number; #endif + +#ifdef __TCPSend + _TCPSendInfo TCPSendInfo; +#endif if (!image.data) { continue; //图像为空,跳过 @@ -176,6 +182,10 @@ void WorkThread::run() "#" + "_" + QString::number(index + 1) + ".jpg"; std::string filename = file_name.toLocal8Bit().constData(); g_save_queue->put(std::make_pair(filename, m)); +#ifdef __TCPSend + TCPSendInfo.pics_name = filename; + TCP_Info_queue->put(TCPSendInfo); +#endif } } @@ -244,7 +254,10 @@ void WorkThread::run() "#" + "_" + QString::number(index + 1) + "_" + ng_reason_maps[ngReason] + ".jpg"; g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); - +#ifdef __TCPSend + TCPSendInfo.pics_name = file_name.toLocal8Bit().constData(); + TCP_Info_queue->put(TCPSendInfo); +#endif m = vec_out[index].clone(); file_name = g_conf_path.save_pics_path + "/ng_result/" + now_ts.toString("yyyy-MM-dd") + "/" @@ -258,6 +271,11 @@ void WorkThread::run() ".jpg"; //g_save_queue->put(std::make_pair(file_name.toStdString(), m)); g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); + +#ifdef __TCPSend + TCPSendInfo.pics_name = file_name.toLocal8Bit().constData(); + TCP_Info_queue->put(TCPSendInfo); +#endif } } }