该示例程序说明了如何通过ip地址连接网口相机。通过输入需要连接的相机ip(Camera Ip)和相机对应的网卡ip(Export Ip)来连接相机,输入的格式为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;
unsigned int g_nPayloadSize = 0;
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)
{
unsigned char *
pData = (
unsigned char *)malloc(
sizeof(
unsigned char) * (g_nPayloadSize));
if (NULL == pData)
{
return NULL;
}
unsigned int nDataSize = g_nPayloadSize;
while(1)
{
if(g_bExit)
{
break;
}
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
}
else
{
printf("No data[0x%x]\n", nRet);
break;
}
}
free(pData);
return 0;
}
int main()
{
void* handle = NULL;
printf("Please input Current Camera Ip : ");
scanf("%s", &nCurrentIp);
printf("Please input Net Export Ip : ");
scanf("%s", &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;
sscanf(nNetExport, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
do
{
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet[0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
if (nPacketSize > 0)
{
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);
}
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
g_nPayloadSize =
stParam.nCurValue;
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;
}
printf("Press a key to stop grabbing.\n");
PressEnterToExit();
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Close Device fail! nRet [0x%x]\n", nRet);
break;
}
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
handle = NULL;
}
}
printf("exit.\n");
return 0;
}