From 0665bf3d75e7beb9c99c9bc9ed0f0d991cdf1132 Mon Sep 17 00:00:00 2001 From: Jinhuan <412100639@qq.com> Date: Mon, 1 Sep 2025 21:06:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E5=85=B7=E6=95=B0=E9=87=87=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E6=9B=B4=E6=96=B0send=20TCP=20=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cigarette/Thread/threadSendTCP.cpp | 189 +++++++++++++++++++++++++++-- Cigarette/Thread/threadSendTCP.h | 25 +++- 2 files changed, 198 insertions(+), 16 deletions(-) diff --git a/Cigarette/Thread/threadSendTCP.cpp b/Cigarette/Thread/threadSendTCP.cpp index 88723aab..816d20da 100644 --- a/Cigarette/Thread/threadSendTCP.cpp +++ b/Cigarette/Thread/threadSendTCP.cpp @@ -3,13 +3,25 @@ #include #include #include +#include #pragma comment(lib, "ws2_32.lib") +extern PLCDevice* m_PLCTCPDevice; +extern PLCDevice* m_PLCDevice; 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; + +#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() { @@ -19,42 +31,195 @@ void threadSendTCP::start_work() void threadSendTCP::stop() { +#ifdef __TCPServer + if (tcpServer->isListening()) + tcpServer->close(); + if(tcpServer) delete tcpServer; +#endif +#ifdef __TCPClient + mySocket->deleteLater(); + if (mySocket) delete mySocket; +#endif isLoop = false; // wait(); - delete mySocket; + } +#ifdef __TCPClient bool threadSendTCP::connectTCP() { mySocket = new QTcpSocket(); // 取消已有的连接 mySocket->abort(); // 连接服务器 mySocket->connectToHost(ip, port); - if (!mySocket->waitForConnected(30000)) { - qDebug() << "connect failed!"; - return false; + if (!mySocket->waitForConnected(100)) { + qDebug() << "connect failed!"; + return false; } qDebug() << "connect successfully!"; return true; } +#endif void threadSendTCP::run() { - if (!connectTCP()) - qDebug() << "TCP connect error!"; - while (isLoop) { - _TCPSendInfo TCPSendInfo; - Local_TCP_Info_queue->take(TCPSendInfo); - num++; - sendData(&TCPSendInfo, num); + 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) + { + sendData(&TCPSendInfo, num); + } +#endif +#ifdef __TCPClient + sendData(&TCPSendInfo, num); +#endif //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; } } + void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) { - std::string fileName = TCPSendInfo->pics_name + ", " + QString::number(Num).toStdString(); + + std::string fileName = TCPSendInfo->pics_name; + QDateTime ts_start = QDateTime::currentDateTime(); +#if defined(__TCPServer) || defined(__TCPClient) mySocket->write(fileName.c_str()); + char temp = num % 10; + mySocket->write((char*)&temp, sizeof(char)); mySocket->write("\n"); mySocket->flush(); +#endif + +#if defined(__ModebusServer) || defined(__ModebusClient) + std::vector asciiVals = stringToAscii(fileName); + //mySocket->write(fileName.c_str()); + /*int temp = num % 2; + if (temp == 1) + { + m_PLCDevice->write_bit_2_plc(10006, 1); + + } + else + { + m_PLCDevice->write_bit_2_plc(10006, 0); + }*/ + + //mySocket->write((char*)&temp,sizeof(char)); + //mySocket->write("\n"); + //mySocket->flush(); + // + //遍历发送 + int val_[32] = {}; + uint8_t high[17] = {}; + uint8_t low[17] = {}; + uint16_t val[17] = {}; + int h = 0; + int l = 0; + for (int i = 0; i < asciiVals.size(); i++) + { + if (i % 2 == 0) + { + high[h] = asciiVals[i]; + h++; + } + else + { + low[l] = asciiVals[i]; + l++; + } + } + +#endif + +#ifdef __ModebusServer + //m_PLCTCPDevice->write_2_plc(40000, 70, (uint16_t*)&val_);//小盒 + m_PLCTCPDevice->write_2_plc(40070, 70, (uint16_t*)&val_);//条盒 +#endif + +#ifdef __ModebusClient + + for (int i = 0; i < sizeof(val) / sizeof(short); i++) + { + val[i] = (static_cast(high[i]) << 8) | low[i];//组合高低字节 + m_PLCTCPDevice->mapping->tab_registers[i] = val[i]; + } + + /*uint16_t da, dat, data, data_, data__; + da = m_PLCTCPDevice->mapping->tab_registers[13]; + dat = m_PLCTCPDevice->mapping->tab_registers[14]; + data = m_PLCTCPDevice->mapping->tab_registers[15]; + data_ = m_PLCTCPDevice->mapping->tab_registers[16]; + data__ = m_PLCTCPDevice->mapping->tab_registers[17]; + uint8_t da8[2], dat8[2], data8[2], data8_[2], data8__[2]; + Ui16ToUin8_P(da, da8); + Ui16ToUin8_P(dat, dat8); + Ui16ToUin8_P(data, data8); + Ui16ToUin8_P(data_, data8_); + Ui16ToUin8_P(data__, data8__); + string str[8]; + str[0] = std::to_string(data8[0]); + str[1] = std::to_string(data8[1]); + str[2] = std::to_string(data8_[0]); + str[3] = std::to_string(data8__[0]); + str[4] = std::to_string(dat8[0]); + str[7] = std::to_string(da8[1]); + cout << "tcp_send_times:" << num << endl; + cout << "data:" << str[7] << str[4] << "." << str[0] << str[1] << str[2] << "_" << str[3] << endl;*/ + +#endif + +} +//字符串转ASCII +std::vector threadSendTCP::stringToAscii(const std::string& str) +{ + std::vector asciiValues; + for (char c : str) { + asciiValues.push_back(static_cast(c)); + } + return asciiValues; +} + +#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/Thread/threadSendTCP.h b/Cigarette/Thread/threadSendTCP.h index ee2e49a5..a51bd00c 100644 --- a/Cigarette/Thread/threadSendTCP.h +++ b/Cigarette/Thread/threadSendTCP.h @@ -1,16 +1,20 @@ #pragma once +#include "common.h" +#include #include #include #include #include #include -#include "common.h" #include "SyncQueue.h" +#include class _TCPSendInfo { public: std::string pics_name; + std::string result; + std::string num; _TCPSendInfo() { @@ -38,8 +42,9 @@ public: wait(); } void stop(); +#ifdef __TCPClient bool connectTCP(); - +#endif protected: void run(); @@ -47,9 +52,21 @@ public: void init(SyncQueue<_TCPSendInfo>* p_TCP_Info_queue, std::string ip_, int port_); void start_work(); void sendData(_TCPSendInfo* TCPSendInfo, int Num); - + std::vector stringToAscii(const std::string& str); +#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; +#if defined(__TCPServer) || defined(__TCPClient) + QTcpSocket* mySocket = NULL; +#endif +#ifdef __TCPServer + QTcpServer *tcpServer = NULL; + QAbstractSocket::SocketState ClientStatus; +#endif };