InterfaceDemo.cpp

Frame grabber enumeration and control.

The sample code shows how to perform operations (e.g., enumerating, opening, and attribute configuring) on self-developed frame grabber via enumeration APIs.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "MvCameraControl.h"
void* g_hInterface = NULL;
// Wait for key press
bool g_bExit = false;
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
// Print frame grabber information
bool PrintInterfaceInfo(MV_INTERFACE_INFO* pstInterfaceInfo)
{
if (NULL == pstInterfaceInfo)
{
printf("The Pointer of pstInterfaceInfo is NULL!\n");
return false;
}
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;
}
// Set/get enumeration value
void Set_Get_Enum(const char* str)
{
MVCC_ENUMVALUE stEnumValue = {0};
MVCC_ENUMENTRY stEnumentryInfo = {0};
int nRet = MV_CC_GetEnumValue(g_hInterface,str, &stEnumValue);
if (MV_OK != nRet)
{
printf("Get %s Fail! nRet [0x%x]\n", str, nRet);
return;
}
stEnumentryInfo.nValue = stEnumValue.nCurValue;
nRet = MV_CC_GetEnumEntrySymbolic(g_hInterface,str, &stEnumentryInfo);
if (MV_OK != nRet)
{
printf("Get %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Get %s = [%s] Success!\n",str,stEnumentryInfo.chSymbolic);
}
MV_XML_AccessMode enAccessMode = AM_NI;
nRet = MV_XML_GetNodeAccessMode(g_hInterface, str, &enAccessMode);
if(MV_OK == nRet && AM_RW == enAccessMode)
{
nRet = MV_CC_SetEnumValue(g_hInterface,str,stEnumValue.nCurValue);
if (MV_OK != nRet)
{
printf("Set %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Set %s = [%s] Success!\n",str,stEnumentryInfo.chSymbolic);
}
}
}
// Set/get boolean value
void Set_Get_Bool(const char* str)
{
bool bValue = false;
int nRet = MV_CC_GetBoolValue(g_hInterface,str, &bValue);
if (MV_OK != nRet)
{
printf("Get %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Get %s = [%d] Success!\n",str,bValue);
}
MV_XML_AccessMode enAccessMode = AM_NI;
nRet = MV_XML_GetNodeAccessMode(g_hInterface, str, &enAccessMode);
if(MV_OK == nRet && AM_RW == enAccessMode)
{
nRet = MV_CC_SetBoolValue(g_hInterface,str, bValue);
if (MV_OK != nRet)
{
printf("Set %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Set %s = [%d] Success!\n",str,bValue);
}
}
}
// Set/get integer value
void Set_Get_Int(const char* str)
{
MVCC_INTVALUE_EX stIntValue;
int nRet = MV_CC_GetIntValueEx(g_hInterface,str,&stIntValue);
if (MV_OK != nRet)
{
printf("Get %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Get %s = [%ld] Success!\n",str,stIntValue.nCurValue);
}
MV_XML_AccessMode enAccessMode = AM_NI;
nRet = MV_XML_GetNodeAccessMode(g_hInterface, str, &enAccessMode);
if(MV_OK == nRet && AM_RW == enAccessMode)
{
nRet = MV_CC_SetIntValueEx(g_hInterface,str,stIntValue.nCurValue);
if (MV_OK != nRet)
{
printf("Set %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Set %s = [%ld] Success!\n",str,stIntValue.nCurValue);
}
}
}
// Set/get string value
void Set_Get_String(const char* str)
{
MVCC_STRINGVALUE StringValue;
int nRet = MV_CC_GetStringValue(g_hInterface,str, &StringValue);
if (MV_OK != nRet)
{
printf("Get %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Get %s = [%s] Success!\n",str,StringValue.chCurValue);
}
MV_XML_AccessMode enAccessMode = AM_NI;
nRet = MV_XML_GetNodeAccessMode(g_hInterface, str, &enAccessMode);
if(MV_OK == nRet && AM_RW == enAccessMode)
{
nRet = MV_CC_SetStringValue(g_hInterface,str, StringValue.chCurValue);
if (MV_OK != nRet)
{
printf("Set %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Set %s = [%s] Success!\n",str,StringValue.chCurValue);
}
}
}
// Set/get float value
void Set_Get_Float(const char* str)
{
MVCC_FLOATVALUE FloatValue;
int nRet = MV_CC_GetFloatValue(g_hInterface,str, &FloatValue);
if (MV_OK != nRet)
{
printf("Get %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Get %s = [%f] Success!\n",str,FloatValue.fCurValue);
}
MV_XML_AccessMode enAccessMode = AM_NI;
nRet = MV_XML_GetNodeAccessMode(g_hInterface, str, &enAccessMode);
if(MV_OK == nRet && AM_RW == enAccessMode)
{
nRet = MV_CC_SetFloatValue(g_hInterface,str, FloatValue.fCurValue);
if (MV_OK != nRet)
{
printf("Set %s Fail! nRet [0x%x]\n", str,nRet);
return;
}
else
{
printf("Set %s = [%f] Success!\n",str,FloatValue.fCurValue);
}
}
}
// Convert user input to transport layer type
unsigned int UserSelect2TLayerType(unsigned int nSelect)
{
switch(nSelect)
{
case 0:
case 1:
case 2:
case 3:
default:
return -1;
}
}
int main()
{
int nRet = MV_OK;
printf("[0]: GIGE Interface\n");
printf("[1]: CAMERALINK Interface\n");
printf("[2]: CXP Interface\n");
printf("[3]: XOF Interface\n\n");
do
{
// Initialize SDK
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
MV_INTERFACE_INFO_LIST stInterfaceInfoList={0};
unsigned int nType = 0;
printf("Please Input Enum Interfaces Type(0-%d):", 3);
scanf("%d", &nType);
unsigned int nTLayerType = UserSelect2TLayerType(nType);
if(-1 == nTLayerType)
{
printf("Input error!\n");
break;
}
// Enumerate frame grabbers
nRet = MV_CC_EnumInterfaces(nTLayerType, &stInterfaceInfoList);
if (MV_OK != nRet)
{
printf("Enum Interfaces fail! nRet [0x%x]\n", nRet);
break;
}
if (stInterfaceInfoList.nInterfaceNum > 0)
{
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;
}
PrintInterfaceInfo(pstInterfaceInfo);
}
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 frame grabber handle
nRet = MV_CC_CreateInterface(&g_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;
}
// Turn on frame grabber
nRet = MV_CC_OpenInterface(g_hInterface, NULL);
if (MV_OK == nRet)
{
printf("Open Interface success!\n");
}
else
{
printf("Open Interface fail! nRet [0x%x]\n", nRet);
break;
}
// Operations on frame grabber attributes: get/set attributes of different frame grabber accordingly
switch(nType)
{
case 0:
{
// Get/set attributes of MV_GIGE_INTERFACE frame grabber
Set_Get_Enum("StreamSelector");
Set_Get_Enum("TimerSelector");
Set_Get_Enum("TimerTriggerSource");
Set_Get_Enum("TimerTriggerActivation");
Set_Get_Bool("HBDecompression");
Set_Get_Int("TimerDuration");
Set_Get_Int("TimerDelay");
Set_Get_Int("TimerFrequency");
break;
}
case 1:
{
// Attribute operation of MV_CAMERALINK_INTERFACE frame grabber
Set_Get_Enum("StreamSelector");
Set_Get_Enum("CameraType");
Set_Get_Enum("StreamPartialImageControl");
Set_Get_Int("ImageHeight");
Set_Get_Int("FrameTimeoutTime");
break;
}
case 2:
{
// Attribute operation of MV_CXP_INTERFACE frame grabber
Set_Get_Enum("StreamSelector");
Set_Get_Int("StreamEnableStatus");
Set_Get_Bool("BayerCFAEnable");
Set_Get_Bool("GammaEnable");
Set_Get_Float("Gamma");
break;
}
case 3:
{
// Attribute operation of MV_XOF_INTERFACE frame grabber
Set_Get_Enum("StreamSelector");
Set_Get_Int("FrameTimeoutTime");
Set_Get_Bool("MinFrameDelay");
Set_Get_Enum("LinkSelector");
break;
}
default:
{
printf("Input error!\n");
break;
}
}
// Turn off the frame grabber
nRet = MV_CC_CloseInterface(g_hInterface);
if (MV_OK == nRet)
{
printf("Close Interface success!\n");
}
else
{
printf("Close Interface Handle fail! nRet [0x%x]\n", nRet);
break;
}
// Destroy frame grabber handle
nRet = MV_CC_DestroyInterface(g_hInterface);
if (MV_OK == nRet)
{
printf("Destroy Interface success!\n");
}
else
{
printf("Destroy Interface Handle fail! nRet [0x%x]\n", nRet);
break;
}
g_hInterface = NULL;
} while (0);
if (g_hInterface != NULL)
{
MV_CC_DestroyInterface(g_hInterface);
g_hInterface = NULL;
}
// ch:·´³õʼ»¯SDK | en:Finalize SDK
printf("exit\n");
return 0;
}