ReconnectDemo.cpp

Reconnect a camera.

The sample code shows how to reconnect a camera the camera goes offline.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "MvCameraControl.h"
void* g_hHandle = NULL;
bool g_bConnect = false;
char g_strSerialNumber[64] = {0};
bool g_bExit = false;
// Wait for user to press enter to stop acquisition or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// Print current IP and user defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_GIGE_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_CAMERALINK_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_CXP_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_XOF_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chModelName);
}
else
{
printf("Not support.\n");
}
return true;
}
void __stdcall ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
if (pFrameInfo)
{
printf("GetOneFrame, Width[%d], Height[%d], nFrameNum[%d]\n",
pFrameInfo->nExtendWidth, pFrameInfo->nExtendHeight, pFrameInfo->nFrameNum);
}
}
void __stdcall ReconnectDevice(unsigned int nMsgType, void* pUser)
{
int nRet = MV_OK;
{
if(true == g_bConnect)
{
nRet = MV_CC_CloseDevice(g_hHandle);
nRet = MV_CC_DestroyHandle(g_hHandle);
g_hHandle = NULL;
MV_CC_DEVICE_INFO_LIST stDevTempList = { 0 };
memset(&stDevTempList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
unsigned int nIndex = -1;
printf("device diconnect, please wait...\n");
do
{
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
continue;
}
bool bFind = false;
for (int i = 0; i< stDevTempList.nDeviceNum; i++)
{
if (stDevTempList.pDeviceInfo[i]->nTLayerType == MV_USB_DEVICE)
{
if (0 == strcmp((char*)(stDevTempList.pDeviceInfo[i]->SpecialInfo.stUsb3VInfo.chSerialNumber), g_strSerialNumber))
{
nIndex = i;
bFind = true;
break;
}
}
else if ((stDevTempList.pDeviceInfo[i]->nTLayerType == MV_GIGE_DEVICE)||(stDevTempList.pDeviceInfo[i]->nTLayerType == MV_GENTL_GIGE_DEVICE))
{
if (0 == strcmp((char*)(stDevTempList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.chSerialNumber), g_strSerialNumber))
{
nIndex = i;
bFind = true;
break;
}
}
else if (stDevTempList.pDeviceInfo[i]->nTLayerType == MV_GENTL_CAMERALINK_DEVICE)
{
if (0 == strcmp((char*)(stDevTempList.pDeviceInfo[i]->SpecialInfo.stCMLInfo.chSerialNumber), g_strSerialNumber))
{
nIndex = i;
bFind = true;
break;
}
}
else if (stDevTempList.pDeviceInfo[i]->nTLayerType == MV_GENTL_CXP_DEVICE)
{
if (0 == strcmp((char*)(stDevTempList.pDeviceInfo[i]->SpecialInfo.stCXPInfo.chSerialNumber), g_strSerialNumber))
{
nIndex = i;
bFind = true;
break;
}
}
else if (stDevTempList.pDeviceInfo[i]->nTLayerType == MV_GENTL_XOF_DEVICE)
{
if (0 == strcmp((char*)(stDevTempList.pDeviceInfo[i]->SpecialInfo.stXoFInfo.chSerialNumber), g_strSerialNumber))
{
nIndex = i;
bFind = true;
break;
}
}
}
if ((-1 == nIndex) || (false == bFind))
{
continue;
}
// Select device and create handle
nRet = MV_CC_CreateHandle(&g_hHandle, stDevTempList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
continue;
}
// Open device
nRet = MV_CC_OpenDevice(g_hHandle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
MV_CC_DestroyHandle(g_hHandle);
g_hHandle = NULL;
continue;
}
else
{
break; // After the device is turned on, the cycle is ended
}
}while(g_bExit== false);
MV_CC_RegisterExceptionCallBack(g_hHandle,ReconnectDevice, g_hHandle);
// Register for exception callback
MV_CC_RegisterImageCallBackEx(g_hHandle, ImageCallBackEx, g_hHandle);
nRet = MV_CC_StartGrabbing(g_hHandle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
return;
}
}
}
}
int main()
{
int nRet = MV_OK;
MV_CC_DEVICE_INFO_LIST stDeviceList = {0};
unsigned int nSelectNum = 0;
// Initialize SDK.
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
return nRet;
}
// Enumerate devices
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
return nRet;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
return nRet;
}
printf("Please Intput camera index: ");
scanf("%d", &nSelectNum);
if (nSelectNum >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
return nRet;
}
if (stDeviceList.pDeviceInfo[nSelectNum]->nTLayerType == MV_GIGE_DEVICE)
{
memcpy(g_strSerialNumber, stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stGigEInfo.chSerialNumber,
sizeof(stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stGigEInfo.chSerialNumber));
}
else if (stDeviceList.pDeviceInfo[nSelectNum]->nTLayerType == MV_USB_DEVICE)
{
memcpy(g_strSerialNumber, stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stUsb3VInfo.chSerialNumber,
sizeof(stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stUsb3VInfo.chSerialNumber));
}
else if (stDeviceList.pDeviceInfo[nSelectNum]->nTLayerType == MV_GENTL_GIGE_DEVICE)
{
memcpy(g_strSerialNumber, stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stGigEInfo.chSerialNumber,
sizeof(stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stGigEInfo.chSerialNumber));
}
else if (stDeviceList.pDeviceInfo[nSelectNum]->nTLayerType == MV_GENTL_CAMERALINK_DEVICE)
{
memcpy(g_strSerialNumber, stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stCMLInfo.chSerialNumber,
sizeof(stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stCMLInfo.chSerialNumber));
}
else if (stDeviceList.pDeviceInfo[nSelectNum]->nTLayerType == MV_GENTL_CXP_DEVICE)
{
memcpy(g_strSerialNumber, stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stCXPInfo.chSerialNumber,
sizeof(stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stCXPInfo.chSerialNumber));
}
else if (stDeviceList.pDeviceInfo[nSelectNum]->nTLayerType == MV_GENTL_XOF_DEVICE)
{
memcpy(g_strSerialNumber, stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stXoFInfo.chSerialNumber,
sizeof(stDeviceList.pDeviceInfo[nSelectNum]->SpecialInfo.stXoFInfo.chSerialNumber));
}
else
{
printf("not support!\n");
}
// select device and create handle
nRet = MV_CC_CreateHandle(&g_hHandle, stDeviceList.pDeviceInfo[nSelectNum]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
return nRet;
}
// open device
nRet = MV_CC_OpenDevice(g_hHandle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
return nRet;
}
g_bConnect = true;
if (stDeviceList.pDeviceInfo[nSelectNum]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(g_hHandle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValueEx(g_hHandle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
// set trigger mode to off
nRet = MV_CC_SetEnumValue(g_hHandle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
return nRet;
}
// register an exception callback
nRet = MV_CC_RegisterExceptionCallBack(g_hHandle, ReconnectDevice, g_hHandle);
if (MV_OK != nRet)
{
printf("Register ExceptionCallBack fail! nRet [%x]\n", nRet);
}
// register an image callback
nRet = MV_CC_RegisterImageCallBackEx(g_hHandle, ImageCallBackEx, g_hHandle);
if (MV_OK != nRet)
{
printf("MV_CC_RegisterImageCallBackEx fail! nRet [%x]\n", nRet);
return nRet;
}
// start grabbing image
nRet = MV_CC_StartGrabbing(g_hHandle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
return nRet;
}
PressEnterToExit();
g_bConnect = false;
g_bExit = true;
// Close device
nRet = MV_CC_CloseDevice(g_hHandle);
if (MV_OK == nRet)
{
printf("Close Device success! \n");
}
// Destroy handle
nRet = MV_CC_DestroyHandle(g_hHandle);
if (MV_OK == nRet)
{
printf("Destroy Handle success! \n");
}
g_hHandle = NULL;
// Finalize SDK
printf("exit\n");
return 0;
}