HID-USB设备读写开发

1. 遍历设备, 获取HID device的 device path

  • bool ISMCol::getHIDDevs(TCHAR **arrWireless, TCHAR **arrWired, LPDWORD lpdwWirelessNums, LPDWORD lpdwWiredNums)
    {
    	GUID hidGuid;
    	HDEVINFO hDevInfo;
    	DWORD dwDevIndex;
    	SP_INTERFACE_DEVICE_DATA interfaceData;
    
    	DWORD dwWireless = 0, dwWired = 0;
    
    	HidD_GetHidGuid(&hidGuid);
    	interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
    
    	hDevInfo = SetupDiGetClassDevs(&hidGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
    	if (hDevInfo == INVALID_HANDLE_VALUE)
    	{
    		return false;
    	}
    
    	for (dwDevIndex = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &hidGuid, dwDevIndex, &interfaceData); ++dwDevIndex)
    	{
    		DWORD dwRequiredSize;
    		PSP_INTERFACE_DEVICE_DETAIL_DATA detailData;
    		SP_DEVINFO_DATA devinfoData;
    		TCHAR szDevPath[MAX_PATH];
    		ZeroMemory(szDevPath, MAX_PATH);
    		SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, NULL, 0, &dwRequiredSize, NULL);
    		detailData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)new BYTE[dwRequiredSize];
    		detailData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
    		devinfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    
    		if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, detailData, dwRequiredSize, NULL, &devinfoData))
    		{			
    			_tcscpy(szDevPath, detailData->DevicePath);
    			_tcslwr(szDevPath);
    
    			// wireless
    			if (((_tcsstr(szDevPath, HID_WIRELESS_VID) != NULL) && (_tcsstr(szDevPath, HID_WIRELESS_PID) != NULL))
    				|| ((_tcsstr(szDevPath, HID_WIRELESS_OLD_VID) != NULL) && (_tcsstr(szDevPath, HID_WIRELESS_OLD_PID) != NULL)))
    			{
    				arrWireless ? _tcscpy(arrWireless[__getIndexFromDevPath(szDevPath)], szDevPath) : NULL;
    				dwWireless++;
    			}
    
    
    			//wired
    			if ((_tcsstr(szDevPath, HID_WIRED_VID) != NULL) && (_tcsstr(szDevPath, HID_WIRED_PID) != NULL))
    			{
    				arrWired ? _tcscpy(arrWireless[dwWired], szDevPath) : NULL;
    				dwWired++;
    			}
    		}
    
    		delete[](PBYTE)detailData;
    
    	}
    
    	SetupDiDestroyDeviceInfoList(hDevInfo);
    	*lpdwWirelessNums = dwWireless;
    	*lpdwWiredNums = dwWired;
    	return true;
    }

2. 通过Device Path , 连接HID device

  • bool ISMCol::connDevice(TCHAR *szDevPath)
    {
    	m_Handle = CreateFile(
    		szDevPath,
    		GENERIC_READ | GENERIC_WRITE,
    		FILE_SHARE_READ | FILE_SHARE_WRITE,
    		NULL,
    		OPEN_EXISTING,
    		FILE_FLAG_OVERLAPPED,
    		NULL);
    
    	if (m_Handle == INVALID_HANDLE_VALUE)
    	{
    		return false;
    	}
    
    	if (!__hidCaps(m_Handle, &m_devCaps))
    		return false;
    
    	//workround
    	__workRound(szDevPath);
    
    	return true;
    
    }
    bool ISMCol::__hidCaps(HANDLE handle, PHIDP_CAPS pDevCaps)
    {
    	if (handle == INVALID_HANDLE_VALUE)
    		return false;
    
    	PHIDP_PREPARSED_DATA PreparsedData;
    
    	if (!HidD_GetPreparsedData(handle, &PreparsedData))
    	{
    		HidD_FreePreparsedData(PreparsedData);
    		return false;
    	}
    
    	if (HIDP_STATUS_SUCCESS != HidP_GetCaps(PreparsedData, pDevCaps))
    	{
    		HidD_FreePreparsedData(PreparsedData);
    		return false;
    	}
    
    	HidD_FreePreparsedData(PreparsedData);
    	return true;
    
    }

3. 读取buffer

  • bool ISMCol::readBuffer(BYTE *buffer)
    {
    	WaitForSingleObject(g_Evt, INFINITE);
    
    	if (m_Handle == INVALID_HANDLE_VALUE)
    		return false;
    
    	DWORD dwInput;
    	DWORD dwReadLens;
    	DWORD dwReturn;
    	OVERLAPPED overLapped;
    
    	dwInput = m_devCaps.InputReportByteLength;
    	overLapped = { 0 };
    	overLapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    
    	if (ReadFile(m_Handle, buffer, dwInput, &dwReadLens, (LPOVERLAPPED)&overLapped))
    	{
    		return true;
    	}
    
    	if (ERROR_IO_PENDING == GetLastError())
    	{
    		dwReturn = WaitForSingleObject(overLapped.hEvent, INFINITE);
    		if (dwReturn == WAIT_OBJECT_0)
    		{
    			if (GetOverlappedResult(m_Handle, &overLapped, &dwReadLens, INFINITE))
    			{
    				return true;
    			}
    		}
    
    	}
    
    	return false;
    
    }

4. 写入buffer

  • bool ISMCol::writeBuffer(BYTE *buffer)
    {
    
    	ResetEvent(g_Evt);
    
    	if (m_Handle == INVALID_HANDLE_VALUE)
    		return false;
    
    	DWORD dwOnput;
    	DWORD dwWriteLens;
    	DWORD dwReturn;
    	OVERLAPPED overLapped;
    
    	dwOnput = m_devCaps.OutputReportByteLength;
    	overLapped = { 0 };
    	overLapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    
    	if (WriteFile(m_Handle, buffer, dwOnput, &dwWriteLens, (LPOVERLAPPED)&overLapped))
    	{
    		SetEvent(g_Evt);
    		return true;
    	}
    
    	if (ERROR_IO_PENDING == GetLastError())
    	{
    		dwReturn = WaitForSingleObject(overLapped.hEvent, INFINITE);
    		if (dwReturn == WAIT_OBJECT_0)
    		{
    			if (GetOverlappedResult(m_Handle, &overLapped, &dwWriteLens, INFINITE))
    			{
    				SetEvent(g_Evt);
    				return true;
    			}
    		}
    
    	}
    
    	SetEvent(g_Evt);
    	return false;
    
    }
    

 

你可能感兴趣的:(C/C++)