init files

This commit is contained in:
rci
2024-09-27 14:31:22 +08:00
parent d04db633b4
commit a2ce7826dc
407 changed files with 123645 additions and 93 deletions
@@ -0,0 +1,70 @@
# »ñÈ¡»ñÈ¡µ±Ç°Ä¿Â¼Ãû
get_filename_component(CURRENT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(TIMESTAMP RUN_TIME)
message( " model start : " ${CURRENT_FOLDER}:${RUN_TIME})
STRING(REPLACE "/EHsc" "/EHa" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
STRING(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /AI${CMAKE_CURRENT_SOURCE_DIR} /clr")
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
${opencv_target_link}
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Network
)
string(TIMESTAMP RUN_TIME)
message( " model end : " ${CURRENT_FOLDER}:${RUN_TIME})
@@ -0,0 +1,476 @@
#include "Hd_SocketModule_ModbusTcp.h"
#include "qprocess.h"
static QQueue<QByteArray> ReadPack;
//static QVector<System::String^> Failed_address;
//static QVector<System::String^> Failed_address;
static QQueue<QByteArray> Failed_data;
QMutex* sendData_Mutex = new QMutex();
QMutex* recive_Mutex = new QMutex();
Hd_SocketModule_ModbusTcp* create()
{
qDebug() << "Hd_SocketModule_ModbusTcp create success!";
return new Hd_SocketModule_ModbusTcp;
}
void destory(Hd_SocketModule_ModbusTcp* ptr)
{
if (ptr)
{
delete ptr;
ptr = nullptr;
}
qDebug() << " Hd_SocketModule_ModbusTcp destory success!";
}
bool ifRun = true;
Hd_SocketModule_ModbusTcp::Hd_SocketModule_ModbusTcp()
{
ifRun = true;
*m_pModBusNet = gcnew HslCommunication::ModBus::ModbusTcpNet();
}
bool m_IsFinsTcp = false;
Hd_SocketModule_ModbusTcp::~Hd_SocketModule_ModbusTcp()
{
ifRun = false;
Sleep(100);
if (m_IsFinsTcp)
{
if ((*m_pModBusNet)->ConnectClose()->IsSuccess)
{
qDebug() << " (*m_pModBusNet)->ConnectClose() success!";
}
else {
qCritical() << " (*m_pModBusNet)->ConnectClose() failed!";
}
m_IsFinsTcp = false;
}
qDebug() << " ~Hd_SocketModule_ModbusTcp success!";
}
// 从类中读数据到实例对象
bool Hd_SocketModule_ModbusTcp::readData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
{
Q_UNUSED(vec_mat);
QMutexLocker locker(sendData_Mutex);
QByteArray statusVaule;
/*if (tcp_Status)
{
statusVaule = "truee";
}
else
{
statusVaule = "false";
}*/
if (ReadPack.isEmpty())
{
data = statusVaule;
return true;
}
else
{
data.append(statusVaule);
data.append(ReadPack.dequeue());
//qDebug() << " ReadPack len: " << ReadPack.length() << " data: " << data;
}
return true;
}
bool ifRetryCMD = false;
// 实例对象把数据写入到类
bool Hd_SocketModule_ModbusTcp::writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data)
{
Q_UNUSED(vec_mat);
if (ifRetryCMD == false)
QMutexLocker locker(recive_Mutex);
Sleep(sleepvalue);
QString str = QString(data);
if (data.isEmpty())
{
qCritical() << " str is null";
return false;
}
QList<QByteArray> m_list = data.split('_');
if (m_list.size() < 4 && m_list[0] != "init")
{
qCritical() << data + " m_list.size() != 4;command err";
return false;
}
if (m_list[2].contains("M") || m_list[2].contains("D"))
m_list[2] = m_list[2].mid(1, m_list[2].size() - 1);
QString commond = m_list.at(0);
if (commond == "init") {
System::String^ Code = gcnew System::String("b5f8f9f7-c075-4913-ae6e-d7d2cc6f2de1");
if (HslCommunication::Authorization::SetAuthorizationCode(Code))
{
std::cout << "Register HSLCode Success" << std::endl;
}
else
{
std::cout << "Register HSLCode Fail" << std::endl;
}
if (m_list.length() < 3)
{
qCritical() << " m_list failed! " << m_list;
return false;
}
m_IP = m_list.at(1);
m_port = m_list.at(2);
}
else if (commond == "read") {
//"read_byte_D3000_300",
while (ifRun)
{
if (sleepvalue < 2)
Sleep(2); // 读取速度过快,添加10ms延时
else
Sleep(sleepvalue);
int length = m_list[3].toInt();
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
HslCommunication::OperateResult<cli::array<unsigned char>^>^ result = (*m_pModBusNet)->Read(address, length);
if (!result->IsSuccess)
{
tcp_Status = false;
qCritical() << " (*FinsTcp)->ReadInt16 failed! check DA1: " << DA1;
QProcess pingProcess;
QString strArg = "ping " + m_IP + " -n 1 -i 2"; //strPingIP 为设备IP地址
pingProcess.start(strArg, QIODevice::ReadOnly);
pingProcess.waitForFinished(-1);
QString p_stdout = QString::fromLocal8Bit(pingProcess.readAllStandardOutput());
//qDebug() << p_stdout;
bool bPingSuccess = false;
if (p_stdout.contains("TTL=")) //通过特殊字符串进行判断ping是否成功
{
//printf("Ping true\n");
bPingSuccess = true;
//QMessageBox::information(this, "ping PLC:", "success!");
//qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ping PLC success! ";
}
else
{
/*printf("Ping false\n");
bPingSuccess = false;
QMessageBox::information(this, "ping PLC:", "failed!");*/
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ping PLC failed! ";
}
//
Failed_data.enqueue(data);
//if (m_IsFinsTcp)
{
//(*m_pModBusNet)->Dispose();
//qDebug() << " (*m_pModBusNet)->Dispose() success!";
if ((*m_pModBusNet)->ConnectClose()->IsSuccess)
{
qDebug() << " (*m_pModBusNet)->ConnectClose() success!";
}
else {
qCritical() << " (*m_pModBusNet)->ConnectClose() failed!";
}
//m_IsFinsTcp = false;
delete* m_pModBusNet;
Sleep(50);
*m_pModBusNet = gcnew HslCommunication::ModBus::ModbusTcpNet();
(*m_pModBusNet)->IpAddress = gcnew System::String(m_IP.toStdString().c_str());
(*m_pModBusNet)->Port = stoi(m_port.toStdString());
Sleep(10);
}
//if ((*m_pModBusNet)->ConnectClose()->IsSuccess)
//{
// qDebug() << " (*m_pModBusNet)->ConnectClose() success!";
//}
//else {
// qCritical() << " (*m_pModBusNet)->ConnectClose() failed!";
//}
int xunhuan_num = 20;
while (xunhuan_num) {
qDebug() << " TRY xunhuan_num: " << xunhuan_num;
(*m_pModBusNet)->ConnectTimeOut = 1000; // 连接超时2秒
(*m_pModBusNet)->ReceiveTimeOut = 500; // 收发超时0.5秒
if ((*m_pModBusNet)->ConnectServer()->IsSuccess)
{
m_IsFinsTcp = true;
tcp_Status = true;
/*QVector<short> Writefailpack;
for (QVector<short>::iterator ii = WriteFailPack.begin(); ii != WriteFailPack.end(); ii++)
{
for (int val = 0; val < 5; val++)
{
if (!(*m_pModBusNet)->Write(address, ii)->IsSuccess)
{
if (val == 4) Writefailpack.push_back(*ii);
continue;
}
else
{
break;
}
}
}
WriteFailPack.clear();
WriteFailPack = Writefailpack;
Writefailpack.clear();*/
qDebug() << "ModBusNet Connect Success! ";
ifRetryCMD = true;
for (int i = 0; i < Failed_data.size(); i++)
{
QByteArray try_data = Failed_data.dequeue();
std::vector<cv::Mat> my_vec_mat;
qDebug() << " try_data:: " << try_data;
writeData(my_vec_mat, try_data);
Sleep(10);
}
ifRetryCMD = false;
return tcp_Status;
}
else
{
xunhuan_num--;
tcp_Status = false;
qCritical() << "ModBusNet Connect failed! ";
Sleep(100);
}
}
//return false;
continue;
}
unsigned char* valuePtr = new unsigned char[m_list[3].toInt() * 2];
QByteArray byte;
for (int i = 0; i < length * 2; i++)
{
valuePtr[i] = result->Content[i];
char v = result->Content[i];
byte.append(v);
}
ReadPack.append(byte);
if (valuePtr) {
delete[] valuePtr;
valuePtr = nullptr;
}
tcp_Status = true;
break;
}
}
else if (commond == "write")
{
//例"write_short_D1003_1","write_short_D1003_1,1,1,1",
if (m_list[1] == "short")
{
QString vecValue = m_list.at(3);
QStringList valueList = vecValue.split(",");
cli::array<short>^ write_list = gcnew cli::array<short>(valueList.length());
for (int i = 0; i < valueList.length(); i++)
{
write_list[i] = valueList[i].toShort();
}
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
for (auto val = 0; val < 5; val++)
{
//Sleep(sleepvalue);
if (!(*m_pModBusNet)->HslCommunication::Core::Net::NetworkDeviceBase::Write(address, write_list)->IsSuccess)
{
qCritical() << " val: " << val++ << " writeAll failed! m_list: " << m_list;
Failed_data.enqueue(data);
Sleep(sleepvalue);
}
else {
qDebug() << "[TCP] " << str << endl;
return true;
}
}
tcp_Status = false;
return false;
}
if (m_list[1] == "float")
{
QString vecValue = m_list.at(3);
QStringList valueList = vecValue.split(",");
cli::array<float>^ write_list = gcnew cli::array<float>(valueList.length());
for (int i = 0; i < valueList.length(); i++)
{
write_list[i] = valueList[i].toFloat();
}
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
for (auto val = 0; val < 5; val++)
{
//Sleep(sleepvalue);
if (!(*m_pModBusNet)->HslCommunication::Core::Net::NetworkDeviceBase::Write(address, write_list)->IsSuccess)
{
qCritical() << " val: " << val++ << " writeAll failed! m_list: " << m_list;
Failed_data.enqueue(data);
Sleep(sleepvalue);
}
else {
qDebug() << "[TCP] " << str << endl;
return true;
}
}
tcp_Status = false;
return false;
}
if (m_list[1] == "double")
{
QString vecValue = m_list.at(3);
QStringList valueList = vecValue.split(",");
cli::array<double>^ write_list = gcnew cli::array<double>(valueList.length());
for (int i = 0; i < valueList.length(); i++)
{
write_list[i] = valueList[i].toDouble();
}
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
for (auto val = 0; val < 5; val++)
{
//Sleep(sleepvalue);
if (!(*m_pModBusNet)->HslCommunication::Core::Net::NetworkDeviceBase::Write(address, write_list)->IsSuccess)
{
qCritical() << " val: " << val++ << " writeAll failed! m_list: " << m_list;
Failed_data.enqueue(data);
Sleep(sleepvalue);
}
else {
qDebug() << "[TCP] " << str << endl;
return true;
}
}
tcp_Status = false;
return false;
}
if (m_list[1] == "string")
{
System::String^ address = gcnew System::String(m_list[2].toStdString().c_str());
System::String^ write_Value = gcnew System::String(m_list.at(3).toStdString().c_str());
for (auto val = 0; val < 5; val++)
{
//Sleep(sleepvalue);
if (!(*m_pModBusNet)->Write(address, write_Value)->IsSuccess) {
qCritical() << " val: " << val++ << " writeString failed! m_list: " << m_list;
Failed_data.enqueue(data);
Sleep(sleepvalue);
}
else {
qDebug() << "[TCP] " << str << endl;
return true;
}
}
tcp_Status = false;
return false;
}
}
return true;
}
bool Hd_SocketModule_ModbusTcp::initParas(QByteArray& data)
{
if (data.isEmpty())
{
qWarning() << "[TCP] " << " data is null!";
}
else
{
QString initDat = QString::fromUtf8(data);
QStringList list = initDat.split("_");
if (list.size() < 3)
{
qCritical() << "[TCP] " << " list size error :" << list.size();
return false;
}
QString sleepname = list.at(0);
sleepvalue = list.at(1).toInt();
if (sleepvalue == 0)sleepvalue = 1;
/*if (sleepvalue <= 5)
sleepvalue = 30;*/
QString readname = list.at(2);
QString readvalue = list.at(3);
QString write_type = list.at(4);
QString write_value = list.at(5);
}
// 连接通讯
//*m_pModBusNet = gcnew HslCommunication::ModBus::ModbusTcpNet();
//(*m_pModBusNet)->IpAddress = gcnew System::String(m_IP.toStdString().c_str());
//(*m_pModBusNet)->Port = stoi(m_port.toStdString());
byte s = '1';
System::String^ ss= gcnew System::String(m_IP.toStdString().c_str());
*m_pModBusNet = gcnew HslCommunication::ModBus::ModbusTcpNet(ss, m_port.toInt(), s);
int xunhuan_num = 20;
while (xunhuan_num) {
//(*m_pModBusNet)->ConnectTimeOut = 1000; // 连接超时0.5秒
//(*m_pModBusNet)->ReceiveTimeOut = 1000; // 收发超时0.5秒
if ((*m_pModBusNet)->ConnectServer()->IsSuccess)
{
m_IsFinsTcp = true;
tcp_Status = true;
qDebug() << "ModBusNet Connect Success! ";
return tcp_Status;
}
else
{
xunhuan_num--;
tcp_Status = false;
qCritical() << "ModBusNet Connect failed! ";
}
Sleep(100);
}
return tcp_Status;
}
bool Hd_SocketModule_ModbusTcp::run()
{
// 没有用到
//qDebug() << "[info] " << __FUNCTION__ << " line:" << __LINE__ << " success!";
return true;
}
bool Hd_SocketModule_ModbusTcp::decodeWriteResult(QByteArray byte, QVector<QString>& vec_ret)
{
try
{
if (byte.isEmpty())
{
qCritical() << __FUNCTION__ << "line:" << __LINE__ << " byte is null!";
return false;
}
while (!byte.isEmpty())
{
int len = 0;
QByteArray m_len = byte.mid(0, 4);
memcpy((void*)&len, (void*)m_len.data(), 4);
byte.remove(0, 4);
QByteArray mm_data = byte.mid(0, len);
vec_ret.append(QString::fromUtf8(mm_data));
byte.remove(0, len);
}
}
catch (QString ev) {
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
return false;
}
return true;
}
@@ -0,0 +1,49 @@
#ifndef Hd_SocketModule_ModbusTcp_H
#define Hd_SocketModule_ModbusTcp_H
#include <QtCore/qglobal.h>
#include <QByteArray>
#include <iostream>
#include <QQueue>
#include <QDebug>
#include "base.h"
#include <QMutex>
#include <Windows.h>
#include <msclr\marshal_cppstd.h>
#using "../../bin/HslCommunication.dll"
using namespace HslCommunication;
using namespace std;
using namespace cli;
class Hd_SocketModule_ModbusTcp :public Base
{
Q_OBJECT
public:
Hd_SocketModule_ModbusTcp();
virtual ~Hd_SocketModule_ModbusTcp();
bool readData(std::vector<cv::Mat>& vec_mat, QByteArray& data); // readData将PLC读取或者发送的返回值发回给主线程,通过全局变量的方式逐条发送
bool writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data); // writeData调用PLC的收发,将需要的数据发给寄存器,返回OK/NG
bool initParas(QByteArray&);
bool run();
bool decodeWriteResult(QByteArray byte, QVector<QString>& vec_ret);
private:
gcroot<HslCommunication::ModBus::ModbusTcpNet^>* m_pModBusNet = new gcroot<HslCommunication::ModBus::ModbusTcpNet^>;
bool tcp_Status = true; // 检查tcp状态
QString m_IP;
QString SA1;
QString DA1;
QString DA2;
QString m_port; // 端口
int sleepvalue = 1; // 刷新间隔
};
extern "C"
{
Q_DECL_EXPORT Hd_SocketModule_ModbusTcp* create();
Q_DECL_EXPORT void destory(Hd_SocketModule_ModbusTcp* ptr);
}
#endif // Hd_SocketModule_ModbusTcp_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
@@ -0,0 +1,69 @@
{
"taskName": "task1",
"save_image": "C:\\930SaveStation3\\",
"savePath": "C:\\930TotalResult\\",
"tablelist": [
{
"table_key": "",
"cName": "机台名:",
"eName": "NG number:",
"value": "外观检测"
},
{
"table_key": "",
"cName": "班次:",
"eName": "Number :",
"value": "白班"
},
{
"table_key": "",
"cName": "工位名:",
"eName": "yield :",
"value": "工位三"
}
],
"showlist": [
{
"show_key": "2D-J93413657-J93413638-K39998967-00K19936732",
"cName": "2D",
"eName": "view1"
},
{
"show_key": "3D-133872-132192-123490-124229",
"cName": "3D",
"eName": "view2"
}
],
"Communicationlist": [
{
"stations": [
"station3"
],
"Communication_key": "Communication",
"Communication_type": "PLC",
"writetype": "ushort",
"Communication_sleeptime": "30",
"Communication_init": "init_127.0.0.1_502_1", //init初始化指令,"127.0.0.1"IP,"502"端口,"1"从站ID
"Communication_readAllCmd": "read_byte_1400_100", //read读指令,"byte"字节寄存器,"1400"寄存器位置,Modbus的地址没有前缀D或M,"100",连续读100个,如果是"1",则只读1个
"heartCmd": "write_short_1003_1", //write写指令,"short"16位寄存器,"M1003"寄存器位置,"1"数值,如果是"1,2,3,4,5"则变为连续写5个16位寄存器
"heartTime": "250",
"moduleName": "Hd_SocketModule_ModbusTcp" //数据类型可选[byte,short,ushort,float,double,string]
}
],
"stationlist": [
{
"station_key": "offlineStation1",
"task_thread_num": "0",
"plcread_area": {
"Communication_readAllCmd": "read_byte_1300_100"
},
"plcwrite_area": {
"snOkCmd": "write_short_1300_9",
"snErrorCmd": "write_short_1300_1",
"overOkCmd": "write_short_1310_9",
"overErrorCmd": "write_short_1310_1",
"dateCmd": "write_string_1340_12"
}
}
]
}