备份 更新整理后的稳定版本
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
# CMakeList.txt: 遍历文件夹 添加子项目
|
||||
|
||||
set(OUR_LIBS "")
|
||||
SUBDIRENAMES(OUR_LIBS ${CMAKE_CURRENT_SOURCE_DIR} "" OFF)
|
||||
message("OUR_LIBS :" ${OUR_LIBS})
|
||||
|
||||
|
||||
# 遍历子库
|
||||
foreach(dll ${OUR_LIBS})
|
||||
add_subdirectory(${dll})
|
||||
endforeach(dll ${})
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
# 获取获取当前目录名
|
||||
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" "keyence2_5D" "2_5Ddll")
|
||||
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 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
|
||||
${qt_target_link}
|
||||
${opencv_target_link}
|
||||
${keyence2_5D_target_link}
|
||||
Qt${QT_VERSION_MAJOR}::Core
|
||||
)
|
||||
|
||||
|
||||
# target_compile_definitions(${CURRENT_FOLDER} PRIVATE HD_AIDEMO_MODULE_LIBRARY)
|
||||
|
||||
string(TIMESTAMP RUN_TIME)
|
||||
message( " model end : " ${CURRENT_FOLDER}:${RUN_TIME})
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,56 @@
|
||||
#ifndef Hd_25DCameraVJ_module_H
|
||||
#define Hd_25DCameraVJ_module_H
|
||||
#include <QtCore/qglobal.h>
|
||||
#include <Windows.h>
|
||||
#include <time.h>
|
||||
#include <QThread>
|
||||
#include <QQueue>
|
||||
#include <QMutex>
|
||||
|
||||
#include <subModuleBase.h>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
#pragma execution_character_set("utf-8")
|
||||
static bool m_flag = true;
|
||||
class MyThread : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MyThread(QObject* parent = nullptr) :m_obj(parent) {}
|
||||
void run();
|
||||
void setFlag(bool r) { m_runFlag = r; };
|
||||
private:
|
||||
bool m_runFlag = true;
|
||||
QObject* m_obj;
|
||||
signals:
|
||||
|
||||
};
|
||||
|
||||
|
||||
class Hd_25DCameraVJ_module :public subModuleBase
|
||||
{
|
||||
public:
|
||||
Hd_25DCameraVJ_module();
|
||||
virtual ~Hd_25DCameraVJ_module();
|
||||
bool readData(std::vector<cv::Mat>&,QByteArray&, nlohmann::json& json);
|
||||
bool writeData(std::vector<cv::Mat>&,QByteArray&, nlohmann::json& json);
|
||||
bool initParas(QByteArray&, nlohmann::json& json = nlohmann::json());
|
||||
bool run();
|
||||
|
||||
std::string sTriggerSource;
|
||||
private:
|
||||
bool softwareTriggerStatus = 0;
|
||||
int cameraIndex = 0;
|
||||
MyThread* workThread = nullptr;
|
||||
|
||||
};
|
||||
|
||||
extern "C"
|
||||
{
|
||||
Q_DECL_EXPORT Hd_25DCameraVJ_module * create();
|
||||
Q_DECL_EXPORT void destory(Hd_25DCameraVJ_module * ptr);
|
||||
}
|
||||
|
||||
#endif // Hd_25DCameraVJ_module_H
|
||||
//Hd_25DCameraVJ_module
|
||||
@@ -0,0 +1,69 @@
|
||||
# 获取获取当前目录名
|
||||
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" "keyence2_5D" "2_5Ddll")
|
||||
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 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
|
||||
${qt_target_link}
|
||||
${opencv_target_link}
|
||||
${keyence2_5D_target_link}
|
||||
Qt${QT_VERSION_MAJOR}::Core
|
||||
)
|
||||
|
||||
|
||||
# target_compile_definitions(${CURRENT_FOLDER} PRIVATE HD_AIDEMO_MODULE_LIBRARY)
|
||||
|
||||
string(TIMESTAMP RUN_TIME)
|
||||
message( " model end : " ${CURRENT_FOLDER}:${RUN_TIME})
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,56 @@
|
||||
#ifndef Hd_25DCameraVJ_module_H
|
||||
#define Hd_25DCameraVJ_module_H
|
||||
#include <QtCore/qglobal.h>
|
||||
#include <Windows.h>
|
||||
#include <time.h>
|
||||
#include <QThread>
|
||||
#include <QQueue>
|
||||
#include <QMutex>
|
||||
|
||||
#include <subModuleBase.h>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
#pragma execution_character_set("utf-8")
|
||||
static bool m_flag = true;
|
||||
class MyThread : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MyThread(QObject* parent = nullptr) :m_obj(parent) {}
|
||||
void run();
|
||||
void setFlag(bool r) { m_runFlag = r; };
|
||||
private:
|
||||
bool m_runFlag = true;
|
||||
QObject* m_obj;
|
||||
signals:
|
||||
|
||||
};
|
||||
|
||||
|
||||
class Hd_25DCameraVJ_module :public subModuleBase
|
||||
{
|
||||
public:
|
||||
Hd_25DCameraVJ_module();
|
||||
virtual ~Hd_25DCameraVJ_module();
|
||||
bool readData(std::vector<cv::Mat>&,QByteArray&, nlohmann::json& json);
|
||||
bool writeData(std::vector<cv::Mat>&,QByteArray&, nlohmann::json& json);
|
||||
bool initParas(QByteArray&, nlohmann::json& json = nlohmann::json());
|
||||
bool run();
|
||||
|
||||
std::string sTriggerSource;
|
||||
private:
|
||||
bool softwareTriggerStatus = 0;
|
||||
int cameraIndex = 0;
|
||||
MyThread* workThread = nullptr;
|
||||
|
||||
};
|
||||
|
||||
extern "C"
|
||||
{
|
||||
Q_DECL_EXPORT Hd_25DCameraVJ_module * create();
|
||||
Q_DECL_EXPORT void destory(Hd_25DCameraVJ_module * ptr);
|
||||
}
|
||||
|
||||
#endif // Hd_25DCameraVJ_module_H
|
||||
//Hd_25DCameraVJ_module
|
||||
@@ -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,603 @@
|
||||
#include "Hd_SocketModule_FinsTcp.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* readData_Mutex = new QMutex();
|
||||
QMutex* recive_Mutex = new QMutex();
|
||||
Hd_SocketModule_FinsTcp* create()
|
||||
{
|
||||
qDebug() << "Hd_SocketModule_FinsTcp create success!";
|
||||
return new Hd_SocketModule_FinsTcp;
|
||||
}
|
||||
|
||||
void destory(Hd_SocketModule_FinsTcp* ptr)
|
||||
{
|
||||
|
||||
if (ptr)
|
||||
{
|
||||
delete ptr;
|
||||
ptr = nullptr;
|
||||
}
|
||||
qDebug() << " Hd_SocketModule_FinsTcp destory success!";
|
||||
}
|
||||
bool ifRun = true;
|
||||
Hd_SocketModule_FinsTcp::Hd_SocketModule_FinsTcp()
|
||||
{
|
||||
ifRun = true;
|
||||
*m_pFinsTcpNet = gcnew HslCommunication::Profinet::Omron::OmronFinsNet();
|
||||
}
|
||||
bool m_IsFinsTcp = false;
|
||||
Hd_SocketModule_FinsTcp::~Hd_SocketModule_FinsTcp()
|
||||
{
|
||||
ifRun = false;
|
||||
Sleep(100);
|
||||
if (status == false)
|
||||
Sleep(3000);
|
||||
if (m_IsFinsTcp)
|
||||
{
|
||||
if ((*m_pFinsTcpNet)->ConnectClose()->IsSuccess)
|
||||
{
|
||||
qDebug() << " (*m_pFinsTcpNet)->ConnectClose() success!";
|
||||
}else {
|
||||
qCritical() << " (*m_pFinsTcpNet)->ConnectClose() failed!";
|
||||
}
|
||||
m_IsFinsTcp = false;
|
||||
}
|
||||
qDebug() << " ~Hd_SocketModule_FinsTcp success!";
|
||||
}
|
||||
|
||||
// 从类中读数据到实例对象
|
||||
bool Hd_SocketModule_FinsTcp::readData(std::vector<cv::Mat>& vec_mat, QByteArray& data, nlohmann::json&)
|
||||
{
|
||||
Q_UNUSED(vec_mat);
|
||||
QMutexLocker locker(readData_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_FinsTcp::writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data, nlohmann::json&)
|
||||
{
|
||||
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)
|
||||
{
|
||||
qCritical() << data+" m_list.size() != 4;command err";
|
||||
return false;
|
||||
}
|
||||
QString m_value= m_list.at(3);
|
||||
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() < 6)
|
||||
{
|
||||
qCritical() << " m_list failed! " << m_list;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_IP = m_list.at(1);
|
||||
m_port = m_list.at(2);
|
||||
SA1 = m_list.at(3);
|
||||
DA1 = m_list.at(4);
|
||||
DA2 = m_list.at(5);
|
||||
}
|
||||
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_pFinsTcpNet)->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! ";
|
||||
}
|
||||
//
|
||||
if (!Failed_data.contains(data))
|
||||
Failed_data.enqueue(data);
|
||||
//if (m_IsFinsTcp)
|
||||
{
|
||||
//(*m_pFinsTcpNet)->Dispose();
|
||||
//qDebug() << " (*m_pFinsTcpNet)->Dispose() success!";
|
||||
|
||||
if ((*m_pFinsTcpNet)->ConnectClose()->IsSuccess)
|
||||
{
|
||||
qDebug() << " (*m_pFinsTcpNet)->ConnectClose() success!";
|
||||
}
|
||||
else {
|
||||
qCritical() << " (*m_pFinsTcpNet)->ConnectClose() failed!";
|
||||
}
|
||||
//m_IsFinsTcp = false;
|
||||
delete* m_pFinsTcpNet;
|
||||
Sleep(50);
|
||||
*m_pFinsTcpNet = gcnew HslCommunication::Profinet::Omron::OmronFinsNet();
|
||||
(*m_pFinsTcpNet)->IpAddress = gcnew System::String(m_IP.toStdString().c_str());
|
||||
(*m_pFinsTcpNet)->Port = stoi(m_port.toStdString());
|
||||
Sleep(10);
|
||||
}
|
||||
|
||||
//if ((*m_pFinsTcpNet)->ConnectClose()->IsSuccess)
|
||||
//{
|
||||
// qDebug() << " (*m_pFinsTcpNet)->ConnectClose() success!";
|
||||
//}
|
||||
//else {
|
||||
// qCritical() << " (*m_pFinsTcpNet)->ConnectClose() failed!";
|
||||
//}
|
||||
int xunhuan_num = 20;
|
||||
while (xunhuan_num && ifRun) {
|
||||
qDebug() << " TRY xunhuan_num: "<< xunhuan_num;
|
||||
(*m_pFinsTcpNet)->SA1 = (unsigned char)stoi(SA1.toStdString());
|
||||
(*m_pFinsTcpNet)->DA1 = (unsigned char)stoi(DA1.toStdString());//DA1.toStdString().c_str();//可以测试DA1累加1,不超过255
|
||||
(*m_pFinsTcpNet)->DA2 = (unsigned char)stoi(DA2.toStdString());
|
||||
(*m_pFinsTcpNet)->ConnectTimeOut = 1000; // 连接超时2秒
|
||||
(*m_pFinsTcpNet)->ReceiveTimeOut = 500; // 收发超时0.5秒
|
||||
if ((*m_pFinsTcpNet)->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_pFinsTcpNet)->Write(address, ii)->IsSuccess)
|
||||
{
|
||||
if (val == 4) Writefailpack.push_back(*ii);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
WriteFailPack.clear();
|
||||
WriteFailPack = Writefailpack;
|
||||
Writefailpack.clear();*/
|
||||
qDebug() << " Connect Success! SA1: " << SA1 << "DA1 :" << DA1;
|
||||
|
||||
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--;
|
||||
if (SA1 > 255)
|
||||
SA1 = 50;
|
||||
SA1 = QString::number(SA1.toInt() + 1);
|
||||
if (DA1 > 255)
|
||||
DA1 = 50;
|
||||
DA1 = QString::number(DA1.toInt() + 1);
|
||||
tcp_Status = false;
|
||||
qCritical() << " Connect failed! SA1: " << SA1 << "DA1 : " << DA1;
|
||||
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_pFinsTcpNet)->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_pFinsTcpNet)->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_pFinsTcpNet)->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_pFinsTcpNet)->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;
|
||||
}
|
||||
}
|
||||
else if (commond == "writeHeart")
|
||||
{
|
||||
//例"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_pFinsTcpNet)->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 {
|
||||
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_pFinsTcpNet)->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_pFinsTcpNet)->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_pFinsTcpNet)->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_FinsTcp::initParas(QByteArray& data, nlohmann::json& json)
|
||||
{
|
||||
|
||||
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_pFinsTcpNet = gcnew HslCommunication::Profinet::Omron::OmronFinsNet();
|
||||
(*m_pFinsTcpNet)->IpAddress = gcnew System::String(m_IP.toStdString().c_str());
|
||||
(*m_pFinsTcpNet)->Port = stoi(m_port.toStdString());
|
||||
int xunhuan_num = 20;
|
||||
while (xunhuan_num && ifRun) {
|
||||
(*m_pFinsTcpNet)->SA1 = (unsigned char)stoi(SA1.toStdString());
|
||||
(*m_pFinsTcpNet)->DA1 = (unsigned char)stoi(DA1.toStdString());//DA1.toStdString().c_str();
|
||||
(*m_pFinsTcpNet)->DA2 = (unsigned char)stoi(DA2.toStdString());
|
||||
(*m_pFinsTcpNet)->ConnectTimeOut = 500; // 连接超时0.5秒
|
||||
(*m_pFinsTcpNet)->ReceiveTimeOut = 500; // 收发超时0.5秒
|
||||
if ((*m_pFinsTcpNet)->ConnectServer()->IsSuccess)
|
||||
{
|
||||
m_IsFinsTcp = true;
|
||||
tcp_Status = true;
|
||||
qDebug() << " Connect Success! SA1: " << SA1;
|
||||
return tcp_Status;
|
||||
}
|
||||
else
|
||||
{
|
||||
xunhuan_num--;
|
||||
SA1 = QString::number(SA1.toInt() + 1);
|
||||
tcp_Status = false;
|
||||
qCritical() <<" Connect failed! SA1: " << SA1;
|
||||
|
||||
}
|
||||
Sleep(100);
|
||||
}
|
||||
return tcp_Status;
|
||||
|
||||
}
|
||||
|
||||
bool Hd_SocketModule_FinsTcp::run()
|
||||
{
|
||||
// 没有用到
|
||||
//qDebug() << "[info] " << __FUNCTION__ << " line:" << __LINE__ << " success!";
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Hd_SocketModule_FinsTcp::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;
|
||||
}
|
||||
bool Hd_SocketModule_FinsTcp::checkStatus()
|
||||
{
|
||||
return tcp_Status;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#ifndef Hd_SocketModule_FinsTcp_H
|
||||
#define Hd_SocketModule_FinsTcp_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
#include <QByteArray>
|
||||
#include <iostream>
|
||||
#include <QQueue>
|
||||
#include <QDebug>
|
||||
#include <subModuleBase.h>
|
||||
#include <QMutex>
|
||||
|
||||
#include <Windows.h>
|
||||
#include <msclr\marshal_cppstd.h>
|
||||
|
||||
#using "../../bin/HslCommunication.dll"
|
||||
|
||||
using namespace HslCommunication;
|
||||
using namespace cli;
|
||||
|
||||
class Hd_SocketModule_FinsTcp :public subModuleBase
|
||||
{
|
||||
public:
|
||||
Hd_SocketModule_FinsTcp();
|
||||
virtual ~Hd_SocketModule_FinsTcp();
|
||||
//bool readData(std::vector<cv::Mat>& vec_mat, QByteArray& data, nlohmann::json&); // readData将PLC读取或者发送的返回值发回给主线程,通过全局变量的方式逐条发送
|
||||
//bool writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data, nlohmann::json&); // writeData调用PLC的收发,将需要的数据发给寄存器,返回OK/NG
|
||||
bool readData(std::vector<cv::Mat>&, QByteArray&, nlohmann::json& json = nlohmann::json());
|
||||
bool writeData(std::vector<cv::Mat>&, QByteArray&, nlohmann::json& json = nlohmann::json());
|
||||
bool initParas(QByteArray&, nlohmann::json& json = nlohmann::json());
|
||||
bool run();
|
||||
bool decodeWriteResult(QByteArray byteArray, QVector<QString>& vec_ret);
|
||||
bool checkStatus();
|
||||
private:
|
||||
gcroot<HslCommunication::Profinet::Omron::OmronFinsNet^>* m_pFinsTcpNet = new gcroot<HslCommunication::Profinet::Omron::OmronFinsNet^>;
|
||||
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_FinsTcp * create();
|
||||
Q_DECL_EXPORT void destory(Hd_SocketModule_FinsTcp * ptr);
|
||||
}
|
||||
|
||||
#endif // Hd_SocketModule_FinsTcp_H
|
||||
Binary file not shown.
Reference in New Issue
Block a user