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") #对应的总cmakeList.txt的target
|
||||
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,238 @@
|
||||
#include "Hd_SocketModule_MES.h"
|
||||
#include "qcoreapplication.h"
|
||||
#include <qtimer.h>
|
||||
|
||||
static QQueue<QByteArray> WritePLCPack;
|
||||
//static QQueue<PLCPack> ReadPack;
|
||||
static QQueue<QByteArray> ReadPLCPack;
|
||||
|
||||
short* v_uint16 = nullptr;
|
||||
unsigned char* v_uint8 = nullptr;
|
||||
char* v_char8 = nullptr;
|
||||
|
||||
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;
|
||||
|
||||
static bool ifrun = false;
|
||||
|
||||
Hd_SocketModule_MES* create()
|
||||
{
|
||||
qDebug() << "[INFO] " << "Hd_SocketModule_MES create success!";
|
||||
return new Hd_SocketModule_MES;
|
||||
}
|
||||
|
||||
void destory(Hd_SocketModule_MES* ptr)
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
delete ptr;
|
||||
ptr = nullptr;
|
||||
}
|
||||
qDebug() << "[INFO] " << " Hd_SocketModule_MES destory success!";
|
||||
}
|
||||
|
||||
Hd_SocketModule_MES::Hd_SocketModule_MES()
|
||||
{
|
||||
m_Mutex = new QMutex;
|
||||
m_Thread = new MyThread(this);
|
||||
}
|
||||
|
||||
Hd_SocketModule_MES::~Hd_SocketModule_MES()
|
||||
{
|
||||
ifrun = false;
|
||||
m_Thread->wait();
|
||||
m_Thread->quit();
|
||||
if (m_Thread)
|
||||
{
|
||||
qDebug() << "[INFO] "<< "~m_Thread()";
|
||||
delete m_Thread;
|
||||
m_Thread = nullptr;
|
||||
}
|
||||
|
||||
qDebug() << "[INFO] " << "~Hd_SocketModule_MES success!";
|
||||
}
|
||||
//从类中读数据到实例对象
|
||||
bool Hd_SocketModule_MES::readData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
||||
{
|
||||
Q_UNUSED(vec_mat);
|
||||
|
||||
/*if (ReadPack.isEmpty())
|
||||
return true;*/
|
||||
|
||||
// 发送数据包
|
||||
/*QMutexLocker locker(m_Mutex);
|
||||
QByteArray m_byte = ReadPack.dequeue();*/
|
||||
QByteArray m_byte = "";
|
||||
|
||||
int times = 0;
|
||||
while (m_byte == "")
|
||||
{
|
||||
times++;
|
||||
if (times > 2000)
|
||||
{
|
||||
qDebug() << "[Hd_SocketModule_MES] " << "Hd_SocketModule_MES readCmd fail";
|
||||
return false;
|
||||
}
|
||||
read_Mutex->lock();
|
||||
m_byte = readResult;
|
||||
if (m_byte != "")
|
||||
readResult = "";
|
||||
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_MES::writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
|
||||
{
|
||||
Q_UNUSED(vec_mat);
|
||||
write_Mutex->lock();
|
||||
//QString str = QString(data);
|
||||
if (data.isEmpty())
|
||||
{
|
||||
qCritical() << "[Hd_SocketModule_MES] " << " str is null";
|
||||
write_Mutex->unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
//QList<QByteArray> m_list = data.split("MESURL_");
|
||||
|
||||
//QString m_value;
|
||||
//QString m_ip_lastpos;
|
||||
if (data.contains("http:"))
|
||||
{
|
||||
//"http://ot-dbvwmeqapi.mflex.com.cn:8081/api/Equipments/saveresultpanel",
|
||||
//QString commond = m_list.at(1);
|
||||
//myUrl = QUrl(commond);
|
||||
m_Thread->setUrl(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
WritePLCPack.enqueue(data);
|
||||
}
|
||||
write_Mutex->unlock();
|
||||
return true;
|
||||
}
|
||||
//初始化
|
||||
bool Hd_SocketModule_MES::initParas(QByteArray& data)
|
||||
{
|
||||
qDebug() << "[Hd_SocketModule_MES] " << "DATA: " << data;
|
||||
m_Thread->start();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Hd_SocketModule_MES::run()
|
||||
{
|
||||
//qDebug() << "[info] " << __FUNCTION__ << " line:" << __LINE__ << " success!";
|
||||
if (connectStatus == false)
|
||||
connectStatus = true;
|
||||
return connectStatus;
|
||||
}
|
||||
//检查异常标志
|
||||
bool Hd_SocketModule_MES::checkStatus()
|
||||
{
|
||||
read_Mutex->lock();
|
||||
bool connectStatus1 = connectStatus;
|
||||
read_Mutex->unlock();
|
||||
return connectStatus1;
|
||||
}
|
||||
|
||||
MyThread::~MyThread()
|
||||
{
|
||||
//if (tcpsocket) {
|
||||
// tcpsocket->abort();
|
||||
//} // tcp通信的socket
|
||||
//if (m_mutex_lock) {
|
||||
// delete m_mutex_lock;
|
||||
// m_mutex_lock = nullptr;
|
||||
//}
|
||||
if (net_mgr) {
|
||||
delete net_mgr;
|
||||
net_mgr = nullptr;
|
||||
}
|
||||
ifrun = false;
|
||||
}
|
||||
|
||||
void MyThread::setUrl(QString qurl)
|
||||
{
|
||||
net_request.setUrl(QUrl(qurl));
|
||||
//要发送json格式的数据必须要在header里设置,不然不会成功的
|
||||
net_request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
|
||||
|
||||
////百度测试地址
|
||||
//net_request.setHeader(QNetworkRequest::UserAgentHeader,
|
||||
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38");
|
||||
//net_request.setUrl(QUrl("http://www.baidu.com:80"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
//线程 run 中初始化 socket
|
||||
void MyThread::run()
|
||||
{
|
||||
qDebug() << "[Hd_SocketModule_MES] " << " run start";
|
||||
ifrun = true;
|
||||
net_mgr = new QNetworkAccessManager(this);
|
||||
net_mgr->setNetworkAccessible(QNetworkAccessManager::Accessible); //设置状态,必须要
|
||||
connect(net_mgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
|
||||
|
||||
static bool ifmytimerStart = false;
|
||||
mytimer = new QTimer();
|
||||
connect(mytimer, &QTimer::timeout, [=]() {
|
||||
connectStatus = false;
|
||||
ifmytimerStart = false;
|
||||
});
|
||||
|
||||
while (ifrun)
|
||||
{
|
||||
write_Mutex->lock();
|
||||
if (WritePLCPack.size() > 0)
|
||||
{
|
||||
for (int i = 0; i < WritePLCPack.size(); i++)
|
||||
{
|
||||
QByteArray Writedata = WritePLCPack.dequeue();//json数据
|
||||
//net_mgr->post(net_request, "");
|
||||
net_mgr->post(net_request, Writedata);
|
||||
if (ifmytimerStart == false)
|
||||
{
|
||||
mytimer->stop();
|
||||
mytimer->start(30000);
|
||||
ifmytimerStart = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
qApp->processEvents();
|
||||
write_Mutex->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
//mes收到回复信号
|
||||
void MyThread::finishedSlot(QNetworkReply* reply)
|
||||
{
|
||||
if (reply->error() == QNetworkReply::NoError)
|
||||
{
|
||||
connectStatus = true;
|
||||
QByteArray bytes = reply->readAll();
|
||||
//qDebug() << "----------------------------" + bytes;
|
||||
|
||||
// 下边我们将收到的body部分解析为json格式,其中有个key是status
|
||||
/*QJsonDocument jsonDocument = QJsonDocument::fromJson(bytes);
|
||||
QJsonObject jsonObject = jsonDocument.object();
|
||||
QString value = jsonObject.value("data").toString();*/
|
||||
//qDebug() << "----------------------------" + value;
|
||||
|
||||
read_Mutex->lock();
|
||||
readResult = bytes;
|
||||
read_Mutex->unlock();
|
||||
}
|
||||
reply->deleteLater();
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#ifndef Hd_SocketModule_MES_H
|
||||
#define Hd_SocketModule_MES_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
#include <QByteArray>
|
||||
#include <QThread>
|
||||
#include <iostream>
|
||||
#include <QFile>
|
||||
#include <QUrl>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QMutex>
|
||||
#include <QObject>
|
||||
#include <QQueue>
|
||||
#include <QDebug>
|
||||
#include <Windows.h>
|
||||
|
||||
#include "base.h"
|
||||
class MyThread;
|
||||
class Hd_SocketModule_MES :public Base
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
//创建,这里new一下锁
|
||||
Hd_SocketModule_MES();
|
||||
//释放,删除连接,释放锁
|
||||
virtual ~Hd_SocketModule_MES();
|
||||
//向主线程发送数据,通信Mat为空
|
||||
//主线程根据通信流程解析数据段。
|
||||
//先writeData,再readData;
|
||||
//从类中读数据到实例对象。通信类返回的内容,只包含数据段,不含包头。
|
||||
bool readData(std::vector<cv::Mat>& vec_mat, QByteArray& data);//readData将PLC读取或者发送的返回值发回给主线程,通过全局变量的方式逐条发送
|
||||
//data为json数据;包含http时,为设置
|
||||
bool writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data); //writeData调用PLC的收发,将需要的数据发给寄存器,返回OK/NG
|
||||
//初始化,未使用
|
||||
bool initParas(QByteArray&);
|
||||
//运行;先initParas,再run;
|
||||
bool run();
|
||||
//检查异常标志
|
||||
bool checkStatus();
|
||||
private:
|
||||
|
||||
QMutex* m_Mutex = nullptr;
|
||||
MyThread* m_Thread = nullptr;
|
||||
|
||||
};
|
||||
|
||||
//自己创建线程
|
||||
class MyThread : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MyThread(QObject* parent = nullptr) :QThread(parent) {}
|
||||
~MyThread();
|
||||
void setUrl(QString qurl);
|
||||
void run();
|
||||
private:
|
||||
QNetworkAccessManager* net_mgr;
|
||||
QNetworkRequest net_request;
|
||||
QTimer* mytimer = nullptr;
|
||||
|
||||
public slots:
|
||||
void finishedSlot(QNetworkReply*);
|
||||
};
|
||||
extern "C"
|
||||
{
|
||||
Q_DECL_EXPORT Hd_SocketModule_MES * create();
|
||||
Q_DECL_EXPORT void destory(Hd_SocketModule_MES * ptr);
|
||||
}
|
||||
|
||||
#endif // Hd_SocketModule_MES_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