Events_Interface.cpp

Open and receive a frame grabber event.

The sample code below shows how to open and receive a frame grabber event.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "MvCameraControl.h"
// Print the information of devices
bool PrintDeviceInfo(MV_CC_DEVICE_INFO_LIST& stDeviceList);
// Print the information of frame grabbers
bool PrintInterfaceInfo(MV_INTERFACE_INFO_LIST& stInterfaceInfoList);
// Event callback
void __stdcall EventCallBack(MV_EVENT_OUT_INFO * pEventInfo, void* pUser);
// Image grabbing callback
void __stdcall ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);
// Wait for a pressed key
void WaitForKeyPress(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
}
int main()
{
int nRet = MV_OK;
// The interface type of frame grabber
void* hInterface = NULL; // Frame grabber handle
void* hDevHandle = NULL; // Device handle
do
{
// Initialize the SDK
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
// Enumerate frame grabbers
MV_INTERFACE_INFO_LIST stInterfaceInfoList={0};
nRet = MV_CC_EnumInterfaces(nInterfaceType, &stInterfaceInfoList);
if (MV_OK != nRet)
{
printf("Enum interfaces fail! nRet [0x%x]\n", nRet);
break;
}
if (stInterfaceInfoList.nInterfaceNum > 0)
{
PrintInterfaceInfo(stInterfaceInfoList);
printf("Enum interfaces success!\n\n");
}
else
{
printf("Find no interface!\n");
break;
}
printf("Please input interfaces index(0-%d):", stInterfaceInfoList.nInterfaceNum-1);
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stInterfaceInfoList.nInterfaceNum)
{
printf("Input error!\n");
break;
}
// Create the handle for frame grabber
nRet = MV_CC_CreateInterface(&hInterface, stInterfaceInfoList.pInterfaceInfos[nIndex]);
if (MV_OK == nRet)
{
printf("Create interface success!\n");
}
else
{
printf("Create interface Handle fail! nRet [0x%x]\n", nRet);
break;
}
// Open the frame grabber
nRet = MV_CC_OpenInterface(hInterface, NULL);
if (MV_OK == nRet)
{
printf("Open interface success!\n");
}
else
{
printf("Open interface fail! nRet [0x%x]\n", nRet);
break;
}
// Enable the ReceiveImageFrameStart0 event of frame grabber
nRet = MV_CC_EventNotificationOn(hInterface, "ReceiveImageFrameStart0");
if (nRet != MV_OK)
{
printf("EventNotificationOn fail! nRet [0x%x]\n", nRet);
break;
}
// Register an event callback of frame grabber
nRet = MV_CC_RegisterEventCallBackEx(hInterface, "ReceiveImageFrameStart0", EventCallBack, NULL);
if (MV_OK != nRet)
{
printf("Register Event CallBack fail! nRet [0x%x]\n", nRet);
break;
}
// Enumerate the devices that are connected to the frame grabber
MV_CC_DEVICE_INFO_LIST stDeviceList = {0};
nRet = MV_CC_EnumDevicesByInterface(hInterface, &stDeviceList);
if (nRet != MV_OK)
{
printf("MV_CC_EnumDevicesByInterface fail! nRet [0x%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum == 0)
{
printf("no device! fail!\n");
break;
}
PrintDeviceInfo(stDeviceList);
// Select device 0, and create a handle
nIndex = 0;
nRet = MV_CC_CreateHandle(&hDevHandle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create handle fail! nRet [0x%x]\n", nRet);
break;
}
// Turn on the device
nRet = MV_CC_OpenDevice(hDevHandle);
if (MV_OK != nRet)
{
printf("Open device fail! nRet [0x%x]\n", nRet);
break;
}
printf("Open device success!\n");
// Register an image grabbing callback
nRet = MV_CC_RegisterImageCallBackEx(hDevHandle, ImageCallBackEx, NULL);
if (MV_OK != nRet)
{
printf("Register Image CallBack fail! nRet [0x%x]\n", nRet);
break;
}
// Start grabbing image
nRet = MV_CC_StartGrabbing(hDevHandle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
WaitForKeyPress();
// Stop grabbing image
nRet = MV_CC_StopGrabbing(hDevHandle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// Turn off the device
nRet = MV_CC_CloseDevice(hDevHandle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
printf("Close device success!\n");
// Destroy the handle of device
nRet = MV_CC_DestroyHandle(hDevHandle);
if (MV_OK != nRet)
{
printf("Destroy handle fail! nRet [0x%x]\n", nRet);
break;
}
printf("Destroy device handle success!\n");
hDevHandle = NULL;
// Turn off the frame grabber
nRet = MV_CC_CloseInterface(hInterface);
if (MV_OK == nRet)
{
printf("Close interface success!\n");
}
else
{
printf("Close interface Handle fail! nRet [0x%x]\n", nRet);
break;
}
// Destroy the handle of frame grabber
nRet = MV_CC_DestroyInterface(hInterface);
if (MV_OK == nRet)
{
printf("Destroy interface success!\n");
}
else
{
printf("Destroy interface Handle fail! nRet [0x%x]\n", nRet);
break;
}
hInterface = NULL;
} while (0);
if (hDevHandle != NULL)
{
MV_CC_CloseDevice(hDevHandle);
MV_CC_DestroyHandle(hDevHandle);
hDevHandle = NULL;
}
if (hInterface != NULL)
{
MV_CC_CloseInterface(hInterface);
hInterface = NULL;
}
// Deinitialize the SDK
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO_LIST& stDeviceList)
{
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pstMVDevInfo = stDeviceList.pDeviceInfo[i];
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 the current IP and user-defined name
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("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
}
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;
}
bool PrintInterfaceInfo(MV_INTERFACE_INFO_LIST& stInterfaceInfoList)
{
for (unsigned int i = 0; i < stInterfaceInfoList.nInterfaceNum; i++)
{
printf("[Interface %d]:\n", i);
MV_INTERFACE_INFO* pstInterfaceInfo = stInterfaceInfoList.pInterfaceInfos[i];
if (NULL == pstInterfaceInfo)
{
break;
}
printf("Display name: %s\n",pstInterfaceInfo->chDisplayName);
printf("Serial number: %s\n",pstInterfaceInfo->chSerialNumber);
printf("model name: %s\n",pstInterfaceInfo->chModelName);
printf("\n");
}
return true;
}
void __stdcall EventCallBack(MV_EVENT_OUT_INFO * pEventInfo, void* pUser)
{
if (pEventInfo)
{
uint64_t nTimestamp = pEventInfo->nTimestampHigh;
nTimestamp = (nTimestamp << 32) + pEventInfo->nTimestampLow;
printf("EventName[%s], EventID[%u], Timestamp[%lu]\n",
pEventInfo->EventName, pEventInfo->nEventID, nTimestamp);
}
}
void __stdcall ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
/*if (pFrameInfo)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
pFrameInfo->nExtendWidth, pFrameInfo->nExtendHeight, pFrameInfo->nFrameNum);
}*/
}