diff --git a/Cigarette/common.h b/Cigarette/common.h index 0b85862..82918fa 100644 --- a/Cigarette/common.h +++ b/Cigarette/common.h @@ -8,6 +8,7 @@ //#define __DEBUG //debug信息输出功能 //#define __UDPSend //网络发送功能 //#define __TCPSend // TCP发送 +//#define __TCPServer // TCP服务器 #define __ExportData // FTP发送 #define USB_BASLER_NEW_FW //使用basler定制固件 //#define IMM_PROCESS //拍照后立马处理,不等校验信号 diff --git a/Cigarette/main.cpp b/Cigarette/main.cpp index 72dd525..fd8d15f 100644 --- a/Cigarette/main.cpp +++ b/Cigarette/main.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #if defined LICENSE_VERIFY #pragma comment(lib,"CryptoToolLib.lib") #include "CryptoToolLib.h" @@ -9,6 +11,18 @@ int main(int argc, char* argv[]) { + QApplication a(argc, argv); + HANDLE hMutex = NULL; + + hMutex = CreateMutex(nullptr, TRUE, L"CigratteShanghai"); + if ((GetLastError() == ERROR_ALREADY_EXISTS) || (hMutex == NULL)) { + QMessageBox::warning(nullptr, "Error", "An instance of the application is already running."); + CloseHandle(hMutex); + hMutex = NULL; + a.closeAllWindows(); + return 0; + } + #if defined LICENSE_VERIFY if (!VerifyLicense()) { @@ -16,7 +30,6 @@ int main(int argc, char* argv[]) } #endif qRegisterMetaType("cv::Mat"); - QApplication a(argc, argv); QPixmap pixmap("D:/Release/splash.jpg"); QSplashScreen splash(pixmap); splash.show(); @@ -25,5 +38,12 @@ int main(int argc, char* argv[]) w.show(); //w.showFullScreen(); splash.finish(&w); - return a.exec(); + a.exec(); + + if (hMutex != NULL) { + CloseHandle (hMutex); + hMutex = NULL; + } + + return 0; } diff --git a/Cigarette/threadSendTCP.cpp b/Cigarette/threadSendTCP.cpp index 6d26f8a..26130e1 100644 --- a/Cigarette/threadSendTCP.cpp +++ b/Cigarette/threadSendTCP.cpp @@ -11,6 +11,13 @@ void threadSendTCP::init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string qDebug() << "tcp ip:" << ip << "| tcp port:" << port; Local_TCP_Info_queue = p_TCP_Info_queue; +#ifdef __TCPServer + tcpServer = new QTcpServer(this); + tcpServer->listen(QHostAddress::Any, port);// Equivalent to QHostAddress("127.0.0.1"). + connect(tcpServer,SIGNAL(newConnection()),this,SLOT(onNewConnection())); + connect(mySocket, SIGNAL(connected()), this, SLOT(onClientConnected())); + connect(mySocket, SIGNAL(disconnected()), this, SLOT(onClientDisconnected())); +#endif } void threadSendTCP::start_work() @@ -21,13 +28,21 @@ void threadSendTCP::start_work() void threadSendTCP::stop() { +#ifdef __TCPServer + if (tcpServer->isListening()) + tcpServer->close(); + if(tcpServer) delete tcpServer; +#endif isLoop = false; // wait(); - delete mySocket; + mySocket->deleteLater(); + if (mySocket) delete mySocket; } +#ifndef __TCPServer bool threadSendTCP::connectTCP() { mySocket = new QTcpSocket(); + //mySocket = new QTcpServer(); // 取消已有的连接 mySocket->abort(); // 连接服务器 @@ -39,26 +54,61 @@ bool threadSendTCP::connectTCP() { qDebug() << "connect successfully!"; return true; } +#endif + void threadSendTCP::run() { +#ifndef __TCPServer 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) + { + sendData(&TCPSendInfo, num); + } +#else + sendData(&TCPSendInfo, num); +#endif //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"); + //std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString(); + //mySocket->write(fileName.c_str()); + char temp = num % 10; + mySocket->write((char*)&temp,sizeof(char)); + //mySocket->write("\n"); mySocket->flush(); } +#ifdef __TCPServer +void threadSendTCP::onNewConnection() +{ + mySocket = tcpServer->nextPendingConnection(); //创建socket + qDebug() << "NewConnectionConnected"; + ClientStatus = QAbstractSocket::ConnectedState; + +} + +void threadSendTCP::onClientConnected() +{ + + qDebug() << "ClientConnected"; +} + +void threadSendTCP::onClientDisconnected() +{ + ClientStatus = QAbstractSocket::UnconnectedState; + qDebug() << "ClientDisconnected"; + mySocket->deleteLater(); +} +#endif \ No newline at end of file diff --git a/Cigarette/threadSendTCP.h b/Cigarette/threadSendTCP.h index 559689e..adcbf17 100644 --- a/Cigarette/threadSendTCP.h +++ b/Cigarette/threadSendTCP.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -6,6 +7,7 @@ #include #include "common.h" #include "SyncQueue.h" +#include class _TCPSendInfo { @@ -43,8 +45,9 @@ public: wait(); } void stop(); +#ifndef __TCPServer bool connectTCP(); - +#endif protected: void run(); @@ -52,10 +55,18 @@ public: void init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_); void start_work(); void sendData(_TCPSendInfo* TCPSendInfo, int Num); - +#ifdef __TCPServer +private slots: + void onNewConnection(); + void onClientConnected(); + void onClientDisconnected(); +#endif public: SyncQueue<_TCPSendInfo>* Local_TCP_Info_queue; std::atomic_bool isLoop = { 0 }; - QTcpSocket* mySocket; - + QTcpSocket* mySocket = NULL; +#ifdef __TCPServer + QTcpServer *tcpServer = NULL; + QAbstractSocket::SocketState ClientStatus; +#endif };