V20251127-2.2.7
修复CLS切图函数。 同步最新的相机模块。
This commit is contained in:
@@ -15,7 +15,9 @@ enum triggerModeEnum {
|
||||
HARDWARE,
|
||||
EMPTY
|
||||
};
|
||||
statusEnum cameraStatus = DISCONNECTED;
|
||||
std::atomic<statusEnum> cameraStatus(DISCONNECTED);
|
||||
std::atomic<bool> reconnecting(false);
|
||||
std::mutex handleMutex;
|
||||
triggerModeEnum triggerMode = HARDWARE;
|
||||
int getImageTimeOut = 3000;
|
||||
bool runFlag = true;
|
||||
@@ -77,11 +79,14 @@ bool connectDevice()
|
||||
lastError = "找不到相机" + QString::fromStdString(Username);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(handleMutex);
|
||||
if (handle != NULL)
|
||||
{
|
||||
MV_CC_StopGrabbing(handle);
|
||||
MV_CC_CloseDevice(handle);
|
||||
MV_CC_DestroyHandle(handle);
|
||||
handle = NULL;
|
||||
}
|
||||
|
||||
//打开相机
|
||||
@@ -111,7 +116,7 @@ bool connectDevice()
|
||||
if (MV_OK != nRet)return false;
|
||||
|
||||
MV_CC_StartGrabbing(handle);
|
||||
cameraStatus = CONNECTED;
|
||||
cameraStatus.store(CONNECTED);
|
||||
return true;
|
||||
}
|
||||
void SetExposureTime(float exposureValue)
|
||||
@@ -133,27 +138,42 @@ void __stdcall ReconnectDevice(unsigned int nMsgType, void* pUser){
|
||||
|
||||
qDebug() << "[Error] " << " MV camera disconnects!";
|
||||
|
||||
if (nMsgType == MV_EXCEPTION_DEV_DISCONNECT && cameraStatus == CONNECTED){
|
||||
|
||||
cameraStatus = RECONNECTING;
|
||||
if (nMsgType == MV_EXCEPTION_DEV_DISCONNECT && cameraStatus.load() == CONNECTED){
|
||||
|
||||
// 防止重复启动重连线程
|
||||
bool expected = false;
|
||||
if (!reconnecting.compare_exchange_strong(expected, true)) {
|
||||
qDebug() << "[Warning] Reconnect already in progress, skipping duplicate request";
|
||||
return;
|
||||
}
|
||||
|
||||
cameraStatus.store(RECONNECTING);
|
||||
// 启动后台线程负责重连,避免在回调线程中长时间阻塞
|
||||
std::thread([]() {
|
||||
qDebug() << "[Info] start reconnect thread";
|
||||
qDebug() << QString::fromStdString(Username) << " start reconnect thread";
|
||||
|
||||
// 确保设备资源关闭
|
||||
CloseDevice();
|
||||
int times = 0;
|
||||
while (runFlag)
|
||||
{
|
||||
qDebug() << "[Info] reconnect times:" << times++;
|
||||
qDebug() << QString::fromStdString(Username) << " reconnect attempt:" << times++;
|
||||
if (connectDevice())
|
||||
{
|
||||
qWarning() << "[Hd_CameraModule_HIK] reconnect success!";
|
||||
cameraStatus = CONNECTED;
|
||||
qWarning() << QString::fromStdString(Username) << " reconnect success!";
|
||||
cameraStatus.store(CONNECTED);
|
||||
reconnecting.store(false);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
qDebug() << QString::fromStdString(Username) << " reconnect failed, waiting 1 seconds before retry...";
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
// 如果runFlag为false,线程退出
|
||||
qDebug() << "[Info] reconnect thread stopped (runFlag=false)";
|
||||
cameraStatus.store(DISCONNECTED);
|
||||
reconnecting.store(false);
|
||||
}).detach();
|
||||
}
|
||||
}
|
||||
@@ -234,21 +254,43 @@ void __stdcall ImageCallBackEx(unsigned char* pData0, MV_FRAME_OUT_INFO_EX* pFra
|
||||
//关闭设备
|
||||
void CloseDevice()
|
||||
{
|
||||
|
||||
std::lock_guard<std::mutex> lock(handleMutex);
|
||||
|
||||
// 如果 handle 已经为空,直接返回
|
||||
if (handle == nullptr)
|
||||
return;
|
||||
|
||||
// 使用 SDK 正确的停止/关闭/销毁流程,避免使用 CloseHandle 等不匹配的 API
|
||||
MV_CC_StopGrabbing(handle);
|
||||
MV_CC_CloseDevice(handle);
|
||||
MV_CC_DestroyHandle(handle);
|
||||
int nRet = MV_CC_StopGrabbing(handle);
|
||||
if (MV_OK != nRet) {
|
||||
qDebug() << "[Warning] MV_CC_StopGrabbing failed, nRet=" << nRet;
|
||||
}
|
||||
|
||||
nRet = MV_CC_CloseDevice(handle);
|
||||
if (MV_OK != nRet) {
|
||||
qDebug() << "[Warning] MV_CC_CloseDevice failed, nRet=" << nRet;
|
||||
}
|
||||
|
||||
nRet = MV_CC_DestroyHandle(handle);
|
||||
if (MV_OK != nRet) {
|
||||
qDebug() << "[Warning] MV_CC_DestroyHandle failed, nRet=" << nRet;
|
||||
}
|
||||
|
||||
handle = nullptr;
|
||||
}
|
||||
|
||||
Hd_CameraModule_HIK_General::~Hd_CameraModule_HIK_General()
|
||||
{
|
||||
runFlag = false;
|
||||
|
||||
// 等待重连线程结束
|
||||
int waitCount = 0;
|
||||
while (reconnecting.load() && waitCount < 50) { // 最多等待5秒
|
||||
qDebug() << "[Info] Waiting for reconnect thread to finish...";
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
waitCount++;
|
||||
}
|
||||
|
||||
CloseDevice();
|
||||
if (n_mutex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user