ParametrizeCamera_AreaScanIOSettings.cpp

Configure trigger node IO settings of area scan cameras.

It shows how to configure trigger node IO settings of area scan camera.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include "MvCameraControl.h"
bool g_bExit = false;
// Wait for a pressed key
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 address 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;
}
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);
}
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// Initialize the SDK
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
// Enumerate devices
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned 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");
break;
}
printf("Please Input camera index(0-%d):", stDeviceList.nDeviceNum-1);
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Input error!\n");
break;
}
// Select a device, and create a handle
nRet = MV_CC_CreateHandle(&handle, 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(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// Get optimal packet size, and set it for GigE cameras
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValueEx(handle,"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 IO input of Line0 (Line2 with lower delay is optional)
printf("Now set IO input...\n");
{
// Set trigger mode to on
nRet = MV_CC_SetEnumValueByString(handle, "TriggerMode", "On");
if (MV_OK != nRet)
{
printf("Set trigger mode fail! nRet [0x%x]\n", nRet);
break;
}
// Set trigger source to Line0
nRet = MV_CC_SetEnumValueByString(handle, "TriggerSource", "Line0");
if (MV_OK != nRet)
{
printf("Set trigger source fail! nRet [0x%x]\n", nRet);
break;
}
// Set trigger edge (TriggerActivation) to RisingEdge
nRet = MV_CC_SetEnumValueByString(handle, "TriggerActivation", "RisingEdge");
if (MV_OK != nRet)
{
printf("Set trigger activation RisingEdge fail! nRet [0x%x]\n", nRet);
break;
}
// Set trigger delay time
nRet = MV_CC_SetFloatValue(handle, "TriggerDelay", 0);
if (MV_OK != nRet)
{
printf("Set trigger delay fail! nRet [0x%x]\n", nRet);
break;
}
// Turn off TriggerCacheEnable, and you can turn on it according to the actual demand
nRet = MV_CC_SetBoolValue(handle, "TriggerCacheEnable", false);
if (MV_OK != nRet)
{
printf("Set trigger cache enable fail! nRet [0x%x]\n", nRet);
break;
}
// Set LineSelector to Line0
nRet = MV_CC_SetEnumValueByString(handle, "LineSelector", "Line0");
if (MV_OK != nRet)
{
printf("Set line selector fail! nRet [0x%x]\n", nRet);
break;
}
// Set Line0 debounce time (¦Ěs), and a larger value can be set to avoid mistaken trigger
nRet = MV_CC_SetIntValueEx(handle, "LineDebouncerTime", 50);
if (MV_OK != nRet)
{
printf("Set line debouncer time fail! nRet [0x%x]\n", nRet);
break;
}
}
// Set IO output of Line1 (Line2 with lower delay is optional) to control devices such as external light source
printf("Now set IO output...\n");
{
// Set LineSelector to Line1
nRet = MV_CC_SetEnumValueByString(handle, "LineSelector", "Line1");
if (MV_OK != nRet)
{
printf("Set line selector fail! nRet [0x%x]\n", nRet);
break;
}
// Set LineSource to ExposureStartActive
nRet = MV_CC_SetEnumValueByString(handle, "LineSource", "ExposureStartActive");
if (MV_OK != nRet)
{
printf("Set LineSource fail! nRet [0x%x]\n", nRet);
break;
}
// Turn on StrobeEnable
nRet = MV_CC_SetBoolValue(handle, "StrobeEnable", true);
if (MV_OK != nRet)
{
printf("Set strobe enable fail! nRet [0x%x]\n", nRet);
break;
}
// Set duration of output channel (StrobeLineDuration) in microsecond
nRet = MV_CC_SetIntValueEx(handle, "StrobeLineDuration", 0);
if (MV_OK != nRet)
{
printf("Set line strobe line duration fail! nRet [0x%x]\n", nRet);
break;
}
// Set delay time of output channel (StrobeLineDelay) in microsecond
nRet = MV_CC_SetIntValueEx(handle, "StrobeLineDelay", 0);
if (MV_OK != nRet)
{
printf("Set line strobe line delay fail! nRet [0x%x]\n", nRet);
break;
}
// Set pre-delay time of output channel (StrobeLinePreDelay) in microsecond
nRet = MV_CC_SetIntValueEx(handle, "StrobeLinePreDelay", 0);
if (MV_OK != nRet)
{
printf("Set line strobe line pre-delay fail! nRet [0x%x]\n", nRet);
break;
}
}
// Register an image callback
nRet = MV_CC_RegisterImageCallBackEx(handle, ImageCallBackEx, handle);
if (MV_OK != nRet)
{
printf("Register Image CallBack fail! nRet [0x%x]\n", nRet);
break;
}
// Start grabbing image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start grabbing fail! nRet [0x%x]\n", nRet);
break;
}
else
{
printf("Start grabbing success!\n");
}
printf("Press a key to stop.\n");
PressEnterToExit();
// End grabbing image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// Deregister the image callback
nRet = MV_CC_RegisterImageCallBackEx(handle, NULL, NULL);
if (MV_OK != nRet)
{
printf("Unregister image callBack fail! nRet [0x%x]\n", nRet);
break;
}
// Turn off the device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("Close device fail! nRet [0x%x]\n", nRet);
break;
}
// Destroy the handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
if (handle != NULL)
{
handle = NULL;
}
// Deinitialize the SDK
return 0;
}