ConnectSpecCamera.cpp

Connect a camera via IP address.

The sample code below shows how to connect to cameras via its IP address and the related NIC's IP address. The input format is xx.xx.xx.xx

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include "MvCameraControl.h"
bool g_bExit = false;
// Wait for user to press "enter" key 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');
g_bExit = true;
sleep(1);
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
MV_FRAME_OUT stImageInfo = {0};
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT));
while(1)
{
if(g_bExit)
{
break;
}
nRet = MV_CC_GetImageBuffer(pUser, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.stFrameInfo.nExtendWidth, stImageInfo.stFrameInfo.nExtendHeight, stImageInfo.stFrameInfo.nFrameNum);
MV_CC_FreeImageBuffer(pUser, &stImageInfo);
}
else
{
printf("No data[0x%x]\n", nRet);
break;
}
}
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
MV_CC_DEVICE_INFO stDevInfo = {0};
MV_GIGE_DEVICE_INFO stGigEDev = {0};
// The camera IP that needs to be connected (based on actual demand)
printf("Please input Current Camera Ip : ");
char nCurrentIp[128];
scanf("%s", (char*)&nCurrentIp);
// The PC IP that needs to be connected (based on actual demand)
printf("Please input Net Export Ip : ");
char nNetExport[128];
scanf("%s", (char*)&nNetExport);
unsigned int nIp1, nIp2, nIp3, nIp4, nIp;
sscanf(nCurrentIp, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
stGigEDev.nCurrentIp = nIp;
sscanf(nNetExport, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
stGigEDev.nNetExport = nIp;
stDevInfo.nTLayerType = MV_GIGE_DEVICE;// Only support GigE cameras
stDevInfo.SpecialInfo.stGigEInfo = stGigEDev;
do
{
// Initialize the SDK
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
// Select device, and create handle
nRet = MV_CC_CreateHandle(&handle, &stDevInfo);
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
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 trigger mode to off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
if (MV_OK != nRet)
{
printf("Set Trigger Mode 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;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
PressEnterToExit();
// End grabbing image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing 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 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
printf("exit.\n");
return 0;
}