1.根据名字打开相机
int CBasicDemoDlg::OpenDevice(void)
{
int nDeviceNum = -1;
char* ccdName = { "相机" };
CString strMsg;
//char* ccdName = { "CCDName" };//设置相机名字
// ch:清除设备列表框中的信息 | en:Clear Device List Information
m_ctrlDeviceCombo.ResetContent();
// ch:初始化设备信息列表 | en:Device Information List Initialization
memset(&m_stDevList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// ch:枚举子网内所有设备 | en:Enumerate all devices within subnet
int nRet = CMyCamera::EnumDevices(&m_stDevList);
// ch:将值加入到信息列表框中并显示出来 | en:Add value to the information list box and display
unsigned int i;
int nIp1, nIp2, nIp3, nIp4;
for (i = 0; i < m_stDevList.nDeviceNum; i++)
{
MV_CC_DEVICE_INFO* pDeviceInfo = m_stDevList.pDeviceInfo[i];
//m_ctrlDeviceCombo.AddString(strMsg);//不用显示
if (m_stDevList.pDeviceInfo[i]->nTLayerType == MV_GIGE_DEVICE)
{
if (strcmp((const char*)m_stDevList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.chUserDefinedName, ccdName) == 0)
{
nDeviceNum = i;
printf("Gige_devicen_nDeviceNum %d\n", nDeviceNum);
}
}
else if (m_stDevList.pDeviceInfo[i]->nTLayerType == MV_USB_DEVICE)
{
if (strcmp((const char*)m_stDevList.pDeviceInfo[i]->SpecialInfo.stUsb3VInfo.chUserDefinedName, ccdName) == 0)
{
nDeviceNum = i;
printf("USB_devicen_DeviceNum %d\n", nDeviceNum);
}
}
continue;
}
if (0 == m_stDevList.nDeviceNum)
{
ShowErrorMsg(TEXT("No device"), 0);
return STATUS_ERROR;
}
// ch:由设备信息创建设备实例 | en:Device instance created by device information
if (NULL == m_stDevList.pDeviceInfo[nDeviceNum])
{
ShowErrorMsg(TEXT("Device does not exist"), 0);
return STATUS_ERROR;
}
if (NULL != m_pcMyCamera)
{
return STATUS_ERROR;
}
m_pcMyCamera = new CMyCamera;
if (NULL == m_pcMyCamera)
{
return STATUS_ERROR;
}
//int nRet = m_pcMyCamera->Open(m_stDevList.pDeviceInfo[nDeviceNum]);
nRet = m_pcMyCamera->Open(m_stDevList.pDeviceInfo[nDeviceNum]);
if (MV_OK != nRet)
{
delete m_pcMyCamera;
m_pcMyCamera = NULL;
return nRet;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (m_stDevList.pDeviceInfo[nDeviceNum]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = m_pcMyCamera->GetOptimalPacketSize();
if (nPacketSize > 0)
{
nRet = m_pcMyCamera->SetIntValue("GevSCPSPacketSize", nPacketSize);
if (nRet != MV_OK)
{
ShowErrorMsg(TEXT("Warning: Set Packet Size fail!"), nRet);
}
}
else
{
ShowErrorMsg(TEXT("Warning: Get Packet Size fail!"), nPacketSize);
}
}
m_bOpenDevice = TRUE;
return MV_OK;
}
2.根据ip地址打开相机
//根据网卡和相机ip地址直接连接相机
//指定网卡ip,相机ip打开相机,需注意,不要写错ip地址,否则会报错206问题
int nRet = 0;
int nDeviceNum = -1;
MV_CC_DEVICE_INFO stDevInfo = { 0 };
MV_GIGE_DEVICE_INFO stGigEDev = { 0 };
//stGigEDev.nNetExport = 0xa9fe7e79;//网卡ip地址 169.254.126.121 在我收藏的网址https://www.infobyip.com/ip-10.64.57.91.html里HEX可以找到转换
stGigEDev.nCurrentIp = 0xa9fea281;//相机ip地址 169.254.162.129
stDevInfo.nTLayerType = MV_GIGE_DEVICE;// ch:仅支持GigE相机 | en:Only support GigE camera
stDevInfo.SpecialInfo.stGigEInfo = stGigEDev;
nRet = MV_CC_CreateHandle(&handle, &stDevInfo);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
return -1;
}
nRet = MV_CC_GetDeviceInfo(handle, &stDevInfo);//补全设备信息
if (nRet != MV_OK)
{
MV_CC_DestroyHandle(handle);
handle = NULL;
return false;
}
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet %x\n", nRet);
return nRet;
}
3.参考文章
工业相机如何调整SDK枚举顺序或者打开顺序