您的位置:首页 > 其它

VC HID设备

2016-03-31 10:47 330 查看
关于USB-HID开发:

======================================================

#include <dbt.h>

extern "C" {

// Declare the C libraries used

#include "setupapi.h" // Must link in setupapi.lib

#include "hidsdi.h" // Must link in hid.lib

}

#pragma comment(lib,"setupapi")

#pragma comment(lib,"hid")

======================================================

GUID DevGuid;

HidD_GetHidGuid(&DevGuid); //获取HID设备的GUID

//注册HID设备改变事件 WM_DEVICECHANGE ,

//MFC:ON_WM_DEVICECHANGE() OnDeviceChange(UINT nEventType, DWORD dwData)

//MFC :DEV_BROADCAST_DEVICEINTERFACE* dbd = (DEV_BROADCAST_DEVICEINTERFACE*)dwData;

static bool RegisterForUsbEvents(HANDLE handle, GUID *DevGuid) {

DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;

memset(&NotificationFilter, 0, sizeof(DEV_BROADCAST_DEVICEINTERFACE));

NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);

NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

NotificationFilter.dbcc_classguid = *DevGuid;

HDEVNOTIFY hDevNotify;

hDevNotify = RegisterDeviceNotification(handle, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);

if (!hDevNotify)

{

//int Err = GetLastError();

//

return false;

}

return true;

}

//查找哪一个HID设备

static CString FindDevice(int VendorID, int ProductID, int Flash_outputsize, int Flash_inputsize)

{

CString devicePath = L"";

GUID HidGuid;

HDEVINFO DevInfo;

SP_DEVICE_INTERFACE_DATA DevData;

PSP_DEVICE_INTERFACE_DETAIL_DATA DevDetail;

ULONG Length;

int Index;

BOOL ok;

int DevCount = 0;

/* Get GUID for all System HIDs */

HidD_GetHidGuid(&HidGuid);

/* Get Device Information for all present devices */

DevInfo = SetupDiGetClassDevs(&HidGuid,

NULL,

NULL,

(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)

);

DevData.cbSize = sizeof(DevData);

DevDetail = NULL;

Index = -1;

/* Scan all Devices */

do {

Index++;

/* Device Interface Element of a Device Information set */

ok = SetupDiEnumDeviceInterfaces(DevInfo,

0,

&HidGuid,

Index,

&DevData

);

if (!ok) break;

/* Get Device Interface Details - Get Length */

ok = SetupDiGetDeviceInterfaceDetail(DevInfo,

&DevData,

NULL,

0,

&Length,

NULL

);

/* Allocate memory for Device Detailed Data */

DevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);

/* Set cbSize in the DevDetail structure */

DevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

/* Get Device Interface Details */

ok = SetupDiGetDeviceInterfaceDetail(DevInfo,

&DevData,

DevDetail,

Length,

NULL,

NULL

);

if (!ok)

{

free(DevDetail);

DevDetail = NULL;

continue;

}

devicePath = DevDetail->DevicePath;

CLog::Debug(__FILEW__, __FUNCTIONW__, __LINE__, devicePath);

free(DevDetail);

/* Create File for Device Read/Write */

CString search;

search.Format(L"vid_%04x&pid_%04x", VendorID, ProductID);

if (devicePath.Find(search) >= 0){

CLog::Debug(__FILEW__, __FUNCTIONW__, __LINE__, L"is ok");

break;

}

devicePath = L"";

} while (1);

SetupDiDestroyDeviceInfoList(DevInfo);

return devicePath;

}

///然后是CreateFile,WriteFile,ReadFile操作

//以下是读取信息

PHIDP_PREPARSED_DATA PreparsedData;

_HIDD_ATTRIBUTES hidAttributes;

HidD_GetAttributes(m_hHandle, &hidAttributes);

if (HidD_GetPreparsedData(m_hHandle, &PreparsedData))// get windows to read the device data into an internal buffer

{

// extract the device capabilities from the internal buffer

HidP_GetCaps(PreparsedData, &Capabilities);

printf("InputReportByteLength:%d\r\n", Capabilities.InputReportByteLength);

HidD_FreePreparsedData(PreparsedData);
// before we quit the funtion, we must free the internal buffer reserved in

GetPreparsedData

}else // GetPreparsedData failed? Chuck an exception

{

CloseHandle(m_hHandle);

m_hHandle = NULL;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: