优化相机模块,优化rpc模块

This commit is contained in:
2025-09-03 15:37:40 +08:00
parent 22e6be7afb
commit 142c41da77
14 changed files with 459 additions and 394 deletions
@@ -2,6 +2,11 @@
#include <QDebug>
using namespace std;
#include <QTextCodec>
#include <thread>
#include <atomic>
#include <chrono>
#include <mutex>
QByteArray mv_Status = "connected";//0106 新增相机断联报警
long long index = 0; //相机序号
void* handle;
@@ -9,6 +14,8 @@ string Username = "";
bool m_flag = false;
//MV_CC_DEVICE_INFO_LIST m_stDevList;//相机设备
bool moduleStatus = true;
static std::atomic<bool> g_reconnecting(false);
static std::mutex g_reconnect_mutex;
QQueue<cv::Mat> queuepic;
//data == "rest_newProductIn"时要结束run;机台报警,取图超时,复位后重新拍的情况。
bool ifBreak = false;
@@ -168,29 +175,67 @@ void SetGain(float GainValue)
MV_CC_SetFloatValue(handle, "Gain", GainValue);
}
//关闭设备
void CloseDevice()
{
// 如果 handle 已经为空,直接返回
if (handle == nullptr)
return;
// 使用 SDK 正确的停止/关闭/销毁流程,避免在回调或其他线程中出错
MV_CC_StopGrabbing(handle);
MV_CC_CloseDevice(handle);
MV_CC_DestroyHandle(handle);
handle = nullptr; //确保后续可以重新创建 handle
}
//0518 断线重连
void __stdcall ReconnectDevice(unsigned int nMsgType, void* pUser)
{
mv_Status = "disconnect";
qDebug() << "[Error] " << " MV camera disconnects!";
moduleStatus = false;
if (nMsgType == MV_EXCEPTION_DEV_DISCONNECT)
{
//断开连接
CloseDevice();
handle = nullptr;
BOOL bConnected = FALSE;
while (1)
// 如果已有重连线程在运行,则直接返回
bool expected = false;
if (!g_reconnecting.compare_exchange_strong(expected, true))
{
Sleep(100);
if (connectDevice() == true)
{
qWarning() << "[Hd_CameraModule_HIK] " << " Hd_CameraModule_HIK create success again! ";
moduleStatus = true;
break;
}
qDebug() << "[Info] reconnect already in progress";
return;
}
// 启动后台线程负责重连,避免在回调线程中长时间阻塞
std::thread([]() {
qDebug() << "[Info] start reconnect thread";
// 确保设备资源关闭
CloseDevice();
int attempt = 0;
int delay_ms = 500; // 初始延迟
while (true)
{
attempt++;
qDebug() << "[Info] reconnect attempt:" << attempt;
if (connectDevice())
{
qWarning() << "[Hd_CameraModule_HIK] reconnect success!";
moduleStatus = true;
mv_Status = "connected";
g_reconnecting = false;
break;
}
// 指数退避,最大 5000ms
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
delay_ms = delay_ms * 2;
if (delay_ms > 5000) delay_ms = 5000;
// 在某些场景可能希望退出重连(例如程序关闭),如果未来需要可增加退出条件
}
}).detach();
}
}
@@ -308,14 +353,6 @@ Hd_CameraModule_HIK_hard_PCIE::Hd_CameraModule_HIK_hard_PCIE()
//m_thread = new MyThread;
//m_mutex = new QMutex();
}
//关闭设备
void CloseDevice()
{
MV_CC_StopGrabbing(handle);
MV_CC_CloseDevice(handle);
MV_CC_DestroyHandle(handle);
handle = nullptr; //此处注释掉的话,软件断开再连接,回调函数不出图
}
Hd_CameraModule_HIK_hard_PCIE::~Hd_CameraModule_HIK_hard_PCIE()
{
CloseDevice();
@@ -487,20 +524,8 @@ bool Hd_CameraModule_HIK_hard_PCIE::run()
{
m_flag = false;
int mytime = 0;
if (imgShouldGetIndex < imgShouldGetVec.size()) {
if (imgShouldGetVec[imgShouldGetIndex] == 0) {
n_mutex->lock();
queuepic.push_back(cv::Mat(200, 200, CV_8UC3, cv::Scalar(0, 0, 255))); //返回空图
std::cout << "red" << std::endl;
n_mutex->unlock();
imgShouldGetIndex++;
return true;
}
else
MV_CC_SetCommandValue(handle, "TriggerSoftware");
imgShouldGetIndex++;
}
int cnt = 0;
MV_CC_SetCommandValue(handle, "TriggerSoftware");
while (true)
{
/* if (moduleStatus == false || mytime++ > getImageTimeOut || ifBreak == true)
@@ -517,14 +542,14 @@ bool Hd_CameraModule_HIK_hard_PCIE::run()
std::cout << "\n gotImg" << std::endl;
break;
}
if (cnt > 25000) {
if (cnt > 120) {
queuepic.push_back(cv::Mat(250, 250, CV_8UC3, cv::Scalar(0, 255, 0))); //返回空图
std::cout << "timeout" << std::endl;
n_mutex->unlock();
return true;
}
n_mutex->unlock();
QThread::msleep(1);
QThread::msleep(10);
}
//考虑到误触发、漏触发的图,在这里进行设置。
//需要设置曝光时