⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbif.cpp

📁 CyPress的C8051F32X系列底层驱动(C语言)及上位机demo(vc环境)
💻 CPP
字号:
/************************************************************************
 *
 *  Module:       UsbIF.cpp
 *  Description:  USB Interface for Silabs USB development board
 *  Company:      Silicon Laboratories
 *
 ************************************************************************/

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "UsbIF.h"

// standard constructor
CUsbIF::CUsbIF()
{
	memset(m_DeviceName, 0, sizeof(m_DeviceName));
}

// destructor
CUsbIF::~CUsbIF()
{
}

// Initialize the GUID	设置GUID值,对每个HID设备来说是必需的且唯一
/*
 *	typedef struct _GUID {          // size is 16
    DWORD Data1;
    WORD   Data2;
    WORD   Data3;
    BYTE  Data4[8];
} GUID;
 */
void CUsbIF::SetGUID(GUID newGUID)
{
	m_GUID = newGUID;
}


BOOL CUsbIF::OpenUsbDevice()
{
	BOOL bRet = FALSE;

	// Retrieve device list for GUID that has been specified.
	/*HDEVINFO	返回一个满足GUID要求的设备信息句柄(DDK函数)
  SetupDiGetClassDevs(
    IN LPGUID  ClassGuid,  OPTIONAL
    IN PCTSTR  Enumerator,  OPTIONAL
    IN HWND  hwndParent,  OPTIONAL
    IN DWORD  Flags
    );
不需要是调用SetupDiDestroyInfoList()来释放资源*/
	HDEVINFO hDevInfoList = SetupDiGetClassDevs (&m_GUID, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); 

	if (hDevInfoList != NULL)
	{
		SP_DEVICE_INTERFACE_DATA deviceInfoData;

		for (int MemberIndex = 0; MemberIndex < 127; MemberIndex++)
		{
			// Clear data structure
			ZeroMemory(&deviceInfoData, sizeof(deviceInfoData));
			deviceInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

			// retrieves a context structure for a device interface of a device information set.
		//SetupDiEnumDeviceInterfaces函数用来读取识别一个接口的结构的指针
		//每次调用用传递一个数组索引值(MemberIndex)来指定一个接口
			if (SetupDiEnumDeviceInterfaces (hDevInfoList, 0, &m_GUID, MemberIndex, &deviceInfoData)) 
			{
				// Must get the detailed information in two steps
				// First get the length of the detailed information and allocate the buffer
				// retrieves detailed information about a specified device interface.

		/*SetupDiGetDeviceInterfaceDetail 函数传回一个结构,此结构与前一个函数
				SetupDiEnumDeviceInterfaces 所识别的接口类别有关,结构中的
				DevicePath成员是一个设备路径,应用程序可以此路径来开启与该设备的通信*/
				
				PSP_DEVICE_INTERFACE_DETAIL_DATA     functionClassDeviceData = NULL;
				ULONG  predictedLength, requiredLength;
				predictedLength = requiredLength = 0;
				SetupDiGetDeviceInterfaceDetail (
						hDevInfoList,
						&deviceInfoData,
						NULL,			// Not yet allocated
						0,				// Set output buffer length to zero 
						&requiredLength,// Find out memory requirement第一次调用找出结构大小
						NULL);			

				predictedLength = requiredLength;
				functionClassDeviceData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc (predictedLength);
				functionClassDeviceData->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);

				// Second, get the detailed information
				if ( SetupDiGetDeviceInterfaceDetail (
							hDevInfoList,
							&deviceInfoData,
							functionClassDeviceData,
							predictedLength,
							&requiredLength,
							NULL)) 
				{
					// Save the device name for subsequent pipe open calls
					//把路径名保存位设备名,应用程序可以访问该设备了
					strcpy( m_DeviceName, functionClassDeviceData->DevicePath);
					free( functionClassDeviceData );
					bRet = TRUE;
					break;
				}

				free( functionClassDeviceData );

			}
			else
			{
				if ( GetLastError() == ERROR_NO_MORE_ITEMS ) 
					break;
			}
		}
	}

	// SetupDiDestroyDeviceInfoList() destroys a device information set
	// and frees all associated memory.
	SetupDiDestroyDeviceInfoList (hDevInfoList);

	return bRet;
}

HANDLE CUsbIF::open_file( char *filename)	//打开设备中的相应管道,见USBTestDlg.cpp(171)
{
	HANDLE hFile;

    if(filename)
	{
		strcat (m_DeviceName, "\\");                      
		strcat (m_DeviceName, filename);                                      
	}

	hFile = CreateFile(	m_DeviceName,
					GENERIC_WRITE | GENERIC_READ,
					FILE_SHARE_WRITE | FILE_SHARE_READ,
					NULL,
					OPEN_EXISTING,
					0,
					NULL);

	return hFile;
}

/*************************** EOF **************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -