Force reset camera IP address.
The sample code below shows how to force reset the port camera IP address, that is, reconnecting and re-enumerating after power-off.。
#include <stdio.h>
#include <Windows.h>
#include <process.h>
#include <conio.h>
#include "MvCameraControl.h"
#pragma comment(lib, "wsock32.lib")
#define PERSISTENT_CONFIG_REG 0x0014
#define PERSISTENT_IPADDR_REG 0x064C
#define PERSISTENT_SUBNETMASK_REG 0x065C
#define PERSISTENT_DEFAULTGATEWAY_REG 0x066C
bool WritesToRegisters(char* chSerialNumber, unsigned int nIpAddr, unsigned int nNetWorkMask, unsigned int nDefaultGateway);
void WaitForKeyPress(void)
{
while(!_kbhit())
{
Sleep(10);
}
_getch();
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
bool ConvertToHexIp(unsigned int *nHexIP, unsigned int *nDecIP, char c)
{
if ( nDecIP[0] < 0 || nDecIP[0] > 255
|| nDecIP[1] < 0 || nDecIP[1] > 255
|| nDecIP[2] < 0 || nDecIP[2] > 255
|| nDecIP[3] < 0 || nDecIP[3] > 255
|| c != '\n')
{
return false;
}
*nHexIP = (nDecIP[0] << 24) + (nDecIP[1] << 16) + (nDecIP[2] << 8) + nDecIP[3];
return true;
}
int main()
{
void* handle = NULL;
unsigned int nIP[4] = {0};
char c = '\0';
unsigned int nIpAddr = 0, nNetWorkMask = 0, nDefaultGateway = 0;
do
{
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
{
for (
unsigned int i = 0; i < stDeviceList.
nDeviceNum; i++)
{
printf("[device %d]:\n", 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_s("%d", &nIndex);
{
printf("Input error!\n");
break;
}
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
char chSerialNumber[16] = {0};
printf("serial number:[%s]\n", chSerialNumber);
printf("Please input ip, example: 192.168.1.100\n");
if ( 5 != scanf("%d.%d.%d.%d%c", &nIP[0], &nIP[1], &nIP[2], &nIP[3], &c) )
{
printf("input count error\n");
break;
}
if (!ConvertToHexIp(&nIpAddr, nIP, c))
{
printf("input IpAddr format is not correct\n");
break;
}
printf("Please input NetMask, example: 255.255.255.0\n");
if ( 5 != scanf("%d.%d.%d.%d%c", &nIP[0], &nIP[1], &nIP[2], &nIP[3], &c) )
{
printf("input count error\n");
break;
}
if (!ConvertToHexIp(&nNetWorkMask, nIP, c))
{
printf("input NetMask format is not correct\n");
break;
}
printf("Please input DefaultWay, example: 192.168.1.1\n");
if ( 5 != scanf("%d.%d.%d.%d%c", &nIP[0], &nIP[1], &nIP[2], &nIP[3], &c) )
{
printf("input count error\n");
break;
}
if (!ConvertToHexIp(&nDefaultGateway, nIP, c))
{
printf("input DefaultWay format is not correct\n");
break;
}
if(bAccessible)
{
{
printf("MV_GIGE_SetIpConfig fail! nRet [%x]\n", nRet);
break;
}
printf("set IPConfig succeed\n");
{
printf("MV_GIGE_ForceIpEx fail! nRet [%x]\n", nRet);
break;
}
printf("set IP succeed\n");
}
else
{
{
printf("MV_GIGE_ForceIpEx fail! nRet [%x]\n", nRet);
break;
}
printf("set IP succeed\n");
handle = NULL;
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
{
printf("MV_GIGE_SetIpConfig fail! nRet [%x]\n", nRet);
break;
}
printf("set IPConfig succeed\n");
}
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
printf("Finish, everything is OK.\n");
} while (0);
if (handle != NULL)
{
handle = NULL;
}
printf("Press a key to exit.\n");
WaitForKeyPress();
return 0;
}
bool WritesToRegisters(char* chSerialNumber, unsigned int nIpAddr, unsigned int nNetWorkMask, unsigned int nDefaultGateway)
{
printf("Now start writing registers ...\n");
if (NULL == chSerialNumber)
{
printf("Serial number is invalid\n");
return false;
}
unsigned int nIndex = -1;
void* handle = NULL;
do
{
{
printf("Enum devices fail! nRet [0x%x]\n", nRet);
break;
}
{
for (
unsigned int i = 0; i < stDeviceList.
nDeviceNum; i++)
{
if (NULL == pDeviceInfo)
{
printf("Pointer invalid!\n");
break;
}
{
nIndex = i;
break;
}
}
}
else
{
printf("Find no devices!\n");
return false;
}
if (-1 == nIndex)
{
printf("Can't find the device, that just modified IP\n");
return false;
}
{
printf("Create handle fail! nRet [0x%x]\n", nRet);
break;
}
{
printf("Open device fail! nRet [0x%x]\n", nRet);
break;
}
unsigned int nConfig = htonl(0x05);
nRet =
MV_CC_WriteMemory(handle, (
const void*)&(nConfig), PERSISTENT_CONFIG_REG,
sizeof(nConfig));
{
printf("Write config fail! nRet [0x%x]\n", nRet);
break;
}
unsigned int nIpAddr1 = htonl(nIpAddr);
nRet =
MV_CC_WriteMemory(handle, (
const void*)&(nIpAddr1), PERSISTENT_IPADDR_REG,
sizeof(nIpAddr));
{
printf("Write IP fail! nRet [0x%x]\n", nRet);
break;
}
unsigned int nNetWorkMask1 = htonl(nNetWorkMask);
nRet =
MV_CC_WriteMemory(handle, (
const void*)&(nNetWorkMask1), PERSISTENT_SUBNETMASK_REG,
sizeof(nNetWorkMask));
{
printf("Write network mask fail! nRet [0x%x]\n", nRet);
break;
}
unsigned int nDefaultGateway1 = htonl(nDefaultGateway);
nRet =
MV_CC_WriteMemory(handle, (
const void*)&(nDefaultGateway1), PERSISTENT_DEFAULTGATEWAY_REG,
sizeof(nDefaultGateway));
{
printf("Write default gateway fail! nRet [0x%x]\n", nRet);
break;
}
{
printf("Close device fail! nRet [0x%x]\n", nRet);
break;
}
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
printf("Writing registers success\n");
} while (0);
if (handle != NULL)
{
handle = NULL;
}
{
return false;
}
return true;
}