QuickSoftwareTrigger.cpp

Achieve quick software trigger of frame grabbers.

The sample code shows how to achieve quick software trigger of some frame grabbers. For supported frame grabber models, refer to actual conditions.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "MvCameraControl.h"
void* g_hInterface = NULL;
bool g_bExit = false;
// Wait for user to press "enter" key to end grabbing image 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');
g_bExit = true;
sleep(1);
}
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;
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
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;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
MV_FRAME_OUT stOutFrame = {0};
while(true)
{
// Software trigger once by frame grabber
nRet = MV_CC_SetCommandValue(g_hInterface, "QuickSoftwareTrigger0");
if (MV_OK != nRet)
{
printf("Quick Software Trigger once failed! %#x\n", nRet);
}
else
{
printf("Quick Software Trigger once success!\n");
}
nRet = MV_CC_GetImageBuffer(pUser, &stOutFrame, 1000);
if (nRet == MV_OK)
{
printf("Get Image Buffer: Width[%d], Height[%d], FrameNum[%d]\n",
nRet = MV_CC_FreeImageBuffer(pUser, &stOutFrame);
if(nRet != MV_OK)
{
printf("Free Image Buffer fail! nRet [0x%x]\n", nRet);
}
}
else
{
printf("Get Image fail! nRet [0x%x]\n", nRet);
}
if(g_bExit)
{
break;
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* hDevice = NULL;
do
{
// Initialize the 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};
nRet = MV_CC_EnumInterfaces(MV_CXP_INTERFACE | MV_XOF_INTERFACE, &stInterfaceInfoList);
// Enumerate frame grabbers
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 handles
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 the 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;
}
// Set stream trigger source to QuickSoftwareTrigger0
nRet = MV_CC_SetEnumValueByString(g_hInterface, "StreamTriggerSource", "QuickSoftwareTrigger0");
if (MV_OK != nRet)
{
printf("Set StreamTriggerSource fail! nRet [0x%x], maybe firmware not support Quick Software Trigger\n", nRet);
break;
}
else
{
printf("Set StreamTriggerSource = QuickSoftwareTrigger0 Success!\n");
}
// Set stream trigger activation to RisingEdge
nRet = MV_CC_SetEnumValueByString(g_hInterface, "StreamTriggerActivation", "RisingEdge");
if (MV_OK != nRet)
{
printf("Set StreamTriggerActivation Fail! nRet [0x%x]\n", nRet);
break;
}
else
{
printf("Set StreamTriggerActivation = RisingEdge Success!\n");
}
MV_CC_DEVICE_INFO_LIST stDeviceList = { 0 };
// Enumerate frame grabber devices
if (MV_OK != nRet)
{
printf("Enum Interfaces Devices fail! nRet [0x%x]\n", nRet);
break;
}
int nDeviceNum = 0;
int nDeviceIndex[MV_MAX_DEVICE_NUM] = { 0 };
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
{
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
if (MV_CXP_INTERFACE == stInterfaceInfoList.pInterfaceInfos[nIndex]->nTLayerType)
{
if (0 == strcmp((char*)stInterfaceInfoList.pInterfaceInfos[nIndex]->chInterfaceID,
(char*)pDeviceInfo->SpecialInfo.stCXPInfo.chInterfaceID))
{
printf("[device %d]:\n", nDeviceNum);
nDeviceIndex[nDeviceNum] = i;
PrintDeviceInfo(pDeviceInfo);
nDeviceNum++;
}
}
else
{
if (0 == strcmp((char*)stInterfaceInfoList.pInterfaceInfos[nIndex]->chInterfaceID,
(char*)pDeviceInfo->SpecialInfo.stXoFInfo.chInterfaceID))
{
printf("[device %d]:\n", nDeviceNum);
nDeviceIndex[nDeviceNum] = i;
PrintDeviceInfo(pDeviceInfo);
nDeviceNum++;
}
}
}
}
else
{
printf("Find No Devices!\n");
break;
}
if (0 == nDeviceNum)
{
printf("Find No Devices!\n");
break;
}
printf("Please Input camera index(0-%d):", nDeviceNum - 1);
nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= nDeviceNum)
{
printf("Input error!\n");
break;
}
// Select device and create handle
nRet = MV_CC_CreateHandle(&hDevice, stDeviceList.pDeviceInfo[nDeviceIndex[nIndex]]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
// Turn on the device
nRet = MV_CC_OpenDevice(hDevice);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// Set trigger mode to off
nRet = MV_CC_SetEnumValue(hDevice, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
// Start grabbing image
nRet = MV_CC_StartGrabbing(hDevice);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , hDevice);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
printf("Press a key to stop grabbing.\n");
PressEnterToExit();
g_bExit = true;
// End grabbing image
nRet = MV_CC_StopGrabbing(hDevice);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// Turn off the device
nRet = MV_CC_CloseDevice(hDevice);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
// Destroy the handle
nRet = MV_CC_DestroyHandle(hDevice);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
hDevice = NULL;
// 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;
}
// Destory 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 (hDevice != NULL)
{
hDevice = NULL;
}
if (g_hInterface != NULL)
{
MV_CC_CloseInterface(g_hInterface);
MV_CC_DestroyInterface(g_hInterface);
g_hInterface = NULL;
}
// Deinitialize the SDK
printf("Press a key to exit.\n");
PressEnterToExit();
return 0;
}