init files
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
# »ñÈ¡»ñÈ¡µ±Ç°Ä¿Â¼Ãû
|
||||
get_filename_component(CURRENT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR} NAME)
|
||||
string(TIMESTAMP RUN_TIME)
|
||||
message( " model start : " ${CURRENT_FOLDER}:${RUN_TIME})
|
||||
|
||||
set(DLL_NAMES "opencv")
|
||||
foreach(child ${DLL_NAMES})
|
||||
LIST(APPEND ENV_INCLUDE ${${child}_include})
|
||||
LIST(APPEND ENV_LIB ${${child}_lib})
|
||||
LIST(APPEND ENV_TARGET ${${child}_target_link})
|
||||
endforeach()
|
||||
message(" ENV_INCLUDE : " ${ENV_INCLUDE} )
|
||||
message(" ENV_LIB : " ${ENV_LIB} )
|
||||
message(" ENV_TARGET : " ${ENV_TARGET} )
|
||||
include_directories(
|
||||
|
||||
${ENV_INCLUDE}
|
||||
)
|
||||
|
||||
link_directories (
|
||||
${ENV_LIB}
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
SET(CMAKE_CXX_STANDARD 11)
|
||||
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
SET(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
FILE(GLOB HEAD_FILES "./*.h" )
|
||||
FILE(GLOB SRC_FILES "./*.cpp")
|
||||
FILE(GLOB UI_FILES "./*.ui")
|
||||
|
||||
|
||||
#find_package(Qt5 COMPONENTS Core REQUIRED)
|
||||
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
|
||||
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Network REQUIRED)
|
||||
|
||||
add_library(${CURRENT_FOLDER} SHARED
|
||||
${HEAD_FILES}
|
||||
${SRC_FILES}
|
||||
${UI_FILES}
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
set_target_properties(${CURRENT_FOLDER} PROPERTIES LINKER_LANGUAGE C
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
|
||||
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
|
||||
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}
|
||||
)
|
||||
|
||||
target_link_libraries(${CURRENT_FOLDER} PRIVATE
|
||||
|
||||
${ENV_TARGET}
|
||||
Qt${QT_VERSION_MAJOR}::Core
|
||||
Qt${QT_VERSION_MAJOR}::Network
|
||||
)
|
||||
|
||||
|
||||
string(TIMESTAMP RUN_TIME)
|
||||
message( " model end : " ${CURRENT_FOLDER}:${RUN_TIME})
|
||||
@@ -0,0 +1,354 @@
|
||||
#include "Hd_SocketModule_TCP.h"
|
||||
#include "qcoreapplication.h"
|
||||
//读到的信息列表
|
||||
QQueue< QByteArray> readDataList;
|
||||
QByteArray readResult = "";
|
||||
QMutex* r_Mutex = new QMutex();
|
||||
QMutex* write_Mutex = new QMutex();
|
||||
QMutex* read_Mutex = new QMutex();
|
||||
QMutex* recive_Mutex = new QMutex();
|
||||
|
||||
bool connectStatus = true;
|
||||
|
||||
Hd_SocketModule_TCP* create()
|
||||
{
|
||||
qDebug() << "[INFO] " << "Hd_SocketModule_TCP create success!";
|
||||
return new Hd_SocketModule_TCP;
|
||||
}
|
||||
|
||||
void destory(Hd_SocketModule_TCP* ptr)
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
delete ptr;
|
||||
ptr = nullptr;
|
||||
}
|
||||
qDebug() << "[INFO] " << " Hd_SocketModule_TCP destory success!";
|
||||
}
|
||||
|
||||
Hd_SocketModule_TCP::Hd_SocketModule_TCP()
|
||||
{
|
||||
read_Mutex = new QMutex;
|
||||
recive_Mutex = new QMutex();
|
||||
m_Thread = new MyThread(this);
|
||||
m_Thread->setMutexLock(read_Mutex);
|
||||
}
|
||||
|
||||
Hd_SocketModule_TCP::~Hd_SocketModule_TCP()
|
||||
{
|
||||
m_Thread->setRunStop();
|
||||
m_Thread->wait();
|
||||
m_Thread->quit();
|
||||
if (m_Thread)
|
||||
{
|
||||
qDebug() << "[INFO] " << "~m_Thread()";
|
||||
delete m_Thread;
|
||||
m_Thread = nullptr;
|
||||
}
|
||||
|
||||
qDebug() << "[INFO] " << "~Hd_SocketModule_TCP success!";
|
||||
}
|
||||
//从类中读数据到实例对象
|
||||
bool Hd_SocketModule_TCP::readData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
||||
{
|
||||
Q_UNUSED(vec_mat);
|
||||
|
||||
/*if (ReadPack.isEmpty())
|
||||
return true;*/
|
||||
|
||||
// 发送数据包
|
||||
/*QMutexLocker locker(read_Mutex);
|
||||
QByteArray m_byte = ReadPack.dequeue();*/
|
||||
QByteArray m_byte = "";
|
||||
|
||||
int times = 0;
|
||||
while (m_byte == "")
|
||||
{
|
||||
//读超时,退出
|
||||
times++;
|
||||
if (times > 100)
|
||||
{
|
||||
//qDebug() << "[TCP] " << "fansTcp readCmd fail";
|
||||
return false;
|
||||
}
|
||||
read_Mutex->lock();
|
||||
if(readDataList.size()>0)
|
||||
m_byte = readDataList.dequeue();
|
||||
read_Mutex->unlock();
|
||||
Sleep(5);
|
||||
}
|
||||
|
||||
/*m_byte.remove(0, 30);*/
|
||||
data = m_byte;
|
||||
//qDebug() << __FUNCTION__ << "line:" << __LINE__ << "result:" << data.size();
|
||||
return true;
|
||||
}
|
||||
//实例对象把数据写入到类
|
||||
bool Hd_SocketModule_TCP::writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
||||
{
|
||||
Q_UNUSED(vec_mat);
|
||||
recive_Mutex->lock();
|
||||
if (data.isEmpty())
|
||||
{
|
||||
qCritical() << "[Hd_SocketModule_TCP] " << " str is null";
|
||||
recive_Mutex->unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
QList<QByteArray> m_list = data.split('_');
|
||||
|
||||
QString commond = m_list.at(0);
|
||||
if (commond == "init")
|
||||
{
|
||||
m_host = m_list.at(1);
|
||||
m_pot = m_list.at(2);
|
||||
|
||||
if (!m_Thread->setIpAndPort(m_host, m_pot))
|
||||
{
|
||||
qDebug() << "[Hd_SocketModule_TCP] " << "m_IP:" << m_host << " m_host:" << m_pot << " connectToHost failed!";
|
||||
recive_Mutex->unlock();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Thread->WritePLCPack.enqueue(data);
|
||||
}
|
||||
recive_Mutex->unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
//初始化
|
||||
bool Hd_SocketModule_TCP::initParas(QByteArray& data)
|
||||
{
|
||||
qDebug() << "[Hd_SocketModule_TCP] " << "DATA: " << data;
|
||||
if (data.isEmpty())
|
||||
{
|
||||
qCritical() << "[Hd_SocketModule_TCP] " << " str is null";
|
||||
recive_Mutex->unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
QList<QByteArray> m_list = data.split('_');
|
||||
|
||||
QString commond = m_list.at(0);
|
||||
if (commond == "init")
|
||||
{
|
||||
m_host = m_list.at(1);
|
||||
m_pot = m_list.at(2);
|
||||
|
||||
if (!m_Thread->setIpAndPort(m_host, m_pot))
|
||||
{
|
||||
qDebug() << "[Hd_SocketModule_TCP] " << "m_IP:" << m_host << " m_host:" << m_pot << " connectToHost failed!";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_Thread->start();
|
||||
Sleep(500);
|
||||
|
||||
if (!m_Thread->getInitSuccessFlag())
|
||||
{
|
||||
qCritical() << "[Hd_SocketModule_TCP] " << "TCP init failed!";
|
||||
return false;
|
||||
}
|
||||
// 开启线程循环
|
||||
m_Thread->start();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Hd_SocketModule_TCP::run()
|
||||
{
|
||||
//qDebug() << "[info] " << __FUNCTION__ << " line:" << __LINE__ << " success!";
|
||||
if (connectStatus == false)
|
||||
connectStatus = true;
|
||||
return connectStatus;
|
||||
}
|
||||
|
||||
|
||||
//检查异常标志
|
||||
bool Hd_SocketModule_TCP::checkStatus()
|
||||
{
|
||||
read_Mutex->lock();
|
||||
bool connectStatus1 = connectStatus;
|
||||
read_Mutex->unlock();
|
||||
return connectStatus1;
|
||||
}
|
||||
|
||||
MyThread::~MyThread()
|
||||
{
|
||||
if (tcpsocket) {
|
||||
tcpsocket->deleteLater();
|
||||
} // tcp通信的socket
|
||||
if (m_mutex_lock) {
|
||||
delete m_mutex_lock;
|
||||
m_mutex_lock = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void MyThread::closeSocket()
|
||||
{
|
||||
if (tcpsocket)
|
||||
{
|
||||
tcpsocket->abort();
|
||||
tcpsocket->disconnect();
|
||||
tcpsocket->close();
|
||||
|
||||
}
|
||||
qDebug() << "[Hd_SocketModule_TCP] " << " Hd_SocketModule_FinsTcp close tcpsocket success!";
|
||||
}
|
||||
//断线重连函数
|
||||
bool MyThread::disReconnects()
|
||||
{
|
||||
bool reResult = false;
|
||||
if (!(tcpsocket->state() == QAbstractSocket::ConnectedState))
|
||||
{
|
||||
read_Mutex->lock();
|
||||
connectStatus = false;
|
||||
read_Mutex->unlock();
|
||||
qCritical() << "[Hd_SocketModule_TCP] " << QString("tcp disconected");
|
||||
//QMessageBox::information(MyThreadWidget,"连接断开", "Hd_SocketModule_TCP连接断开,开始自动重连");
|
||||
while(1)
|
||||
{
|
||||
if (init_flag == 2)break;
|
||||
tcpsocket->bind(QHostAddress::Any);
|
||||
tcpsocket->connectToHost(m_IP, m_Port.toInt());
|
||||
if (tcpsocket->waitForConnected(5))
|
||||
{
|
||||
qWarning() << "[Hd_SocketModule_TCP] " << " You have connected to the server again";
|
||||
//QMessageBox::information(MyThreadWidget, "自动重连", "Hd_SocketModule_TCP自动重连成功");
|
||||
reResult = true;
|
||||
read_Mutex->lock();
|
||||
connectStatus = true;
|
||||
read_Mutex->unlock();
|
||||
break;
|
||||
}
|
||||
Sleep(200);
|
||||
qApp->processEvents();
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
reResult = true;
|
||||
return reResult;
|
||||
}
|
||||
|
||||
//线程 run 中初始化 socket
|
||||
void MyThread::run()
|
||||
{
|
||||
qDebug() << "[TCP] " << " run start";
|
||||
|
||||
if (init_flag == 0)
|
||||
{
|
||||
tcpsocket = new QTcpSocket(this);
|
||||
tcpsocket->bind(QHostAddress::Any);
|
||||
tcpsocket->abort();
|
||||
|
||||
if (tcpsocket->state() == QAbstractSocket::ConnectedState)
|
||||
{
|
||||
qCritical() << "[Hd_SocketModule_TCP] " << QString("[%1:%2] server has been linked").arg(m_IP).arg(m_Port);
|
||||
return;
|
||||
}
|
||||
|
||||
tcpsocket->connectToHost(m_IP, m_Port.toInt());
|
||||
if (tcpsocket->waitForConnected(5))
|
||||
{
|
||||
qWarning() << "[Hd_SocketModule_TCP] " << " You have connected to the server ";
|
||||
connectStatus = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
qWarning() << "[Hd_SocketModule_TCP] " << " Hd_SocketModule_TCP :: Server not connected";
|
||||
init_success = false;
|
||||
read_Mutex->lock();
|
||||
connectStatus = false;
|
||||
read_Mutex->unlock();
|
||||
return;
|
||||
}
|
||||
init_flag = 1;
|
||||
qDebug() << "[Hd_SocketModule_TCP] " << " run end ----------";
|
||||
}
|
||||
else if (init_flag == 1)
|
||||
{
|
||||
//开启wile循环来读写
|
||||
while (true)
|
||||
{
|
||||
if (init_flag == 2) {
|
||||
break;
|
||||
}
|
||||
writeSocketData();
|
||||
qApp->processEvents();
|
||||
Sleep(20);
|
||||
readSocketData();
|
||||
qApp->processEvents();
|
||||
Sleep(20);
|
||||
}
|
||||
}
|
||||
if (init_flag == 2) {
|
||||
|
||||
QThread::msleep(10);
|
||||
closeSocket();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool MyThread::setIpAndPort(QString ip, QString port)
|
||||
{
|
||||
m_IP = ip;
|
||||
m_Port = port;
|
||||
return true;
|
||||
}
|
||||
|
||||
//读取数据
|
||||
void MyThread::readSocketData()
|
||||
{
|
||||
qApp->processEvents();
|
||||
QByteArray readData = tcpsocket->readAll();
|
||||
|
||||
if (readData.isEmpty())
|
||||
return;
|
||||
read_Mutex->lock();
|
||||
readDataList.append(readData);
|
||||
read_Mutex->unlock();
|
||||
readData.clear();
|
||||
Sleep(5);
|
||||
}
|
||||
//往PLC发报文
|
||||
void MyThread::writeSocketData()
|
||||
{
|
||||
if (WritePLCPack.isEmpty())
|
||||
return;
|
||||
//写PLC
|
||||
if (!WritePLCPack.isEmpty())
|
||||
{
|
||||
|
||||
for (int i = 0; i < WritePLCPack.size(); i++)
|
||||
{
|
||||
recive_Mutex->lock();
|
||||
if (!(WritePLCPack.size() > 0))
|
||||
{
|
||||
recive_Mutex->unlock();
|
||||
break;
|
||||
}
|
||||
QByteArray byte = WritePLCPack.dequeue(); // 取出包
|
||||
recive_Mutex->unlock();
|
||||
if (!disReconnects())
|
||||
{
|
||||
qDebug() << "[Hd_SocketModule_TCP] " << QString("The server cannot be connected. Check the network port");
|
||||
|
||||
//init_flag = 2;
|
||||
return;
|
||||
}
|
||||
tcpsocket->write(byte);
|
||||
tcpsocket->waitForBytesWritten(3000);
|
||||
tcpsocket->flush();
|
||||
qApp->processEvents();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
#ifndef Hd_SocketModule_TCP_H
|
||||
#define Hd_SocketModule_TCP_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
#include <QByteArray>
|
||||
#include <QThread>
|
||||
#include <iostream>
|
||||
#include <QTcpSocket>
|
||||
#include <QHostAddress>
|
||||
#include <QtWidgets/qmessagebox.h>
|
||||
#include <QMutex>
|
||||
#include <QObject>
|
||||
#include <QQueue>
|
||||
#include <QDebug>
|
||||
#include <Windows.h>
|
||||
|
||||
#include "base.h"
|
||||
|
||||
class MyThread;
|
||||
class Hd_SocketModule_TCP :public Base
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
//创建,这里new一下锁
|
||||
Hd_SocketModule_TCP();
|
||||
//释放,删除连接,释放锁
|
||||
virtual ~Hd_SocketModule_TCP();
|
||||
//向主线程发送数据,通信Mat为空
|
||||
//主线程调用sendData函数,把读报文的数据段发送回来。 //注意,只要数据段。
|
||||
//主线程根据通信流程解析数据段。
|
||||
//先writeData,再readData;
|
||||
//从类中读数据到实例对象。通信类返回的内容,只包含数据段,不含包头。
|
||||
bool readData(std::vector<cv::Mat>& vec_mat, QByteArray& data);//readData将PLC读取或者发送的返回值发回给主线程,通过全局变量的方式逐条发送
|
||||
//接收主线程发来的数据,通信Mat为空
|
||||
//例:write_DM1300_9,写寄存器DM1300为9;
|
||||
//例:writeAll_DM1700_14_1,1,1,1,1,1,1,1,1,1,1,1,1,1,写寄存器DM1700-DM1713的14个寄存器,均为1
|
||||
//例:readAll_DM1400_100,读DM1400-DM1499的100个寄存器;
|
||||
//例:readAll_DM1001_1,读DM1001
|
||||
//例:writeString_DM1340_12_2023-0217-1650-03-0899,写寄存器DM1340-DM1351时间戳,一个寄存器里面含2个字符;
|
||||
//其它通信方式可参考,并添加解析协议。如:writeFloat_DM10000_-1.1,指令函数增加bool writeFloat(QString DM,QString value);
|
||||
//实例对象把数据写入到类。通信类包含读、写指令。
|
||||
bool writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data); //writeData调用PLC的收发,将需要的数据发给寄存器,返回OK/NG
|
||||
//把通信创建需要的参数传进去,不同协议不同,例:init_192.168.1.50_9600_24;
|
||||
//连接通信,可以在run函数里进行
|
||||
bool initParas(QByteArray&);
|
||||
//运行
|
||||
bool run();
|
||||
//检查异常标志
|
||||
bool checkStatus();
|
||||
//private:
|
||||
// QString UShortToHex(int value); // 只转 ushort 类型
|
||||
// QString ShortToHex(int value); // 只转 short 类型
|
||||
// void setSleepTimeAndReadNum(int time,int num);
|
||||
// bool writeCommond(QString DM,QString m_value); // 写入单个值; //例:write_DM1300_9
|
||||
// bool readCommond(QString DM,QString Num);
|
||||
// bool readAllCommond(QString DM,QString Num); // 写入连续的值
|
||||
// bool readStringCommond(QString DM, QString Num);
|
||||
// bool writeAllCommond(QString DM,QString write_Num,QVector<short> vec_value);
|
||||
// bool writeResultCommond(QString DM,QString write_Num,QVector<QString> vec_value); // 写入起始位结果值,要做解析再写入
|
||||
// bool writeStringCommond(QString DM, QString write_num, QString vec_value);
|
||||
// bool decodeWriteResult(QByteArray resultByte,QVector<QString> &vec_ret);
|
||||
// bool tcpServer(QByteArray cmd, QByteArray& resultPack);
|
||||
// void decodePack(QByteArray resultArray);
|
||||
private:
|
||||
int m_sleepTime = 100; // 延迟时间
|
||||
QString m_host;
|
||||
QString m_pot;
|
||||
MyThread *m_Thread = nullptr;
|
||||
|
||||
};
|
||||
|
||||
//自己创建线程
|
||||
class MyThread : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MyThread(QObject* parent = nullptr) :QThread(parent) {}
|
||||
~MyThread();
|
||||
bool setIpAndPort(QString ip, QString port);
|
||||
void run();
|
||||
void readSocketData(); //读取数据
|
||||
void writeSocketData(); //写入数据
|
||||
void decodePack();
|
||||
void setMutexLock(QMutex* lock) { m_mutex_lock = lock; }
|
||||
void setRunStop() { init_flag = 2; }
|
||||
void setReadPackValue(int value);
|
||||
void closeSocket();
|
||||
bool getInitSuccessFlag() { return init_success; }
|
||||
bool disReconnects();
|
||||
void setSocketIp(QString m_socketIp) { socketIp = m_socketIp; }
|
||||
|
||||
QQueue<QByteArray> WritePLCPack;
|
||||
QQueue<QByteArray> ReadPack;
|
||||
private:
|
||||
QString m_IP;
|
||||
QString m_Port;
|
||||
QMutex m_Mutex;
|
||||
QByteArray reciveData;
|
||||
QString socketIp;
|
||||
bool init_success = true; // 初始化成功 返回true;失败返回false
|
||||
int init_flag = 0; // false为完整数据包,true为非完整数据包
|
||||
int m_readPackValue = 122; // 接受50个数据包的长度 为 122 ,加包头一起为 130, 如果是 100的话就是 222 加包头为230
|
||||
QMutex* m_mutex_lock = nullptr;
|
||||
QTcpSocket* tcpsocket = nullptr; // tcp通信的socket
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
extern "C"
|
||||
{
|
||||
Q_DECL_EXPORT Hd_SocketModule_TCP * create();
|
||||
Q_DECL_EXPORT void destory(Hd_SocketModule_TCP * ptr);
|
||||
}
|
||||
|
||||
#endif // Hd_SocketModule_FinsTcp_H
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef __BASECLASS_H__
|
||||
#define __BASECLASS_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
|
||||
class Base :public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
bool init_flag; //是否初始化,false,true
|
||||
bool status = true; //相机状态(判断相机是否断开连接)
|
||||
std::string paras; //传输数据
|
||||
std::vector<cv::Mat> m_mat; //传输图片
|
||||
std::string moduleName; //模块名称
|
||||
public:
|
||||
Base(){}
|
||||
virtual ~Base(){}
|
||||
virtual bool readData(std::vector<cv::Mat>&,QByteArray&){return true;}
|
||||
virtual bool writeData(std::vector<cv::Mat>&,QByteArray&){return true;}
|
||||
virtual bool initParas(QByteArray&){return true;}
|
||||
virtual bool run() {return true;}
|
||||
virtual bool checkStatus() { return true; }
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user