c++代码使用MvCameraControl库查询硬件版本的顺序步骤
时间: 2023-12-13 20:02:57 浏览: 28
使用MvCameraControl库查询硬件版本的顺序步骤如下:
1. 打开相机
2. 获取相机属性信息
3. 判断相机是否支持获取硬件版本信息
4. 如果支持,则调用获取硬件版本信息的函数
5. 关闭相机
具体的C++代码实现如下:
```
#include "MvCameraControl.h"
#include <iostream>
using namespace std;
int main()
{
int nRet = MV_OK;
// 打开相机
MV_CC_DEVICE_INFO_LIST stDevList;
memset(&stDevList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDevList);
if (MV_OK != nRet)
{
cout << "MV_CC_EnumDevices failed! nRet " << nRet << endl;
return nRet;
}
if (stDevList.nDeviceNum == 0)
{
cout << "No device found!" << endl;
return MV_OK;
}
MV_CC_DEVICE_INFO* pDeviceInfo = stDevList.pDeviceInfo[0];
if (NULL == pDeviceInfo)
{
cout << "Find device failed!" << endl;
return MV_OK;
}
MV_CC_HANDLE hDevice = NULL;
nRet = MV_CC_CreateHandle(&hDevice, pDeviceInfo);
if (MV_OK != nRet)
{
cout << "MV_CC_CreateHandle failed! nRet " << nRet << endl;
return nRet;
}
nRet = MV_CC_OpenDevice(hDevice);
if (MV_OK != nRet)
{
cout << "MV_CC_OpenDevice failed! nRet " << nRet << endl;
MV_CC_DestroyHandle(hDevice);
return nRet;
}
// 获取相机属性信息
MVCC_INTVALUE stIntValue = { 0 };
nRet = MV_CC_GetIntValue(hDevice, "PayloadSize", &stIntValue);
if (MV_OK != nRet)
{
cout << "MV_CC_GetIntValue failed! nRet " << nRet << endl;
MV_CC_CloseDevice(hDevice);
MV_CC_DestroyHandle(hDevice);
return nRet;
}
// 判断相机是否支持获取硬件版本信息
bool bSupport = false;
MVCC_BOOLVALUE stBoolValue = { 0 };
nRet = MV_CC_GetBoolValue(hDevice, "DeviceUserID", &stBoolValue);
if (MV_OK != nRet)
{
cout << "MV_CC_GetBoolValue failed! nRet " << nRet << endl;
MV_CC_CloseDevice(hDevice);
MV_CC_DestroyHandle(hDevice);
return nRet;
}
if (stBoolValue.bCurValue)
{
bSupport = true;
}
// 如果支持,则调用获取硬件版本信息的函数
if (bSupport)
{
MVCC_STRINGVALUE stVersion = { 0 };
nRet = MV_CC_GetStringValue(hDevice, "DeviceVersion", &stVersion);
if (MV_OK != nRet)
{
cout << "MV_CC_GetStringValue failed! nRet " << nRet << endl;
MV_CC_CloseDevice(hDevice);
MV_CC_DestroyHandle(hDevice);
return nRet;
}
cout << "Hardware version is " << stVersion.chCurValue << endl;
}
else
{
cout << "Hardware version is not supported!" << endl;
}
// 关闭相机
nRet = MV_CC_CloseDevice(hDevice);
if (MV_OK != nRet)
{
cout << "MV_CC_CloseDevice failed! nRet " << nRet << endl;
MV_CC_DestroyHandle(hDevice);
return nRet;
}
nRet = MV_CC_DestroyHandle(hDevice);
if (MV_OK != nRet)
{
cout << "MV_CC_DestroyHandle failed! nRet " << nRet << endl;
return nRet;
}
return MV_OK;
}
```
注意:以上代码中的查询硬件版本信息的属性名为"DeviceVersion",具体属性名可能因相机型号而异,需要根据相机的SDK文档进行查找。