要访问USB端口,首先要知道这种USB设备的GUID和管道名称,获取句柄以后就可以使用ReadFile/WriteFile进行读写了!下面放上代码,以供参考。
-
- int CUSBImpObject::open_file( char *filename)
- {
- int successOpened = 0,i;
- int NumOpened;
- HANDLE h;
- NumOpened=OpenAllUsbDevices((LPGUID) &GUID_CLASS_I82930_BULK);
- if(NumOpened<=0)
- return 0;
- for(i=0;i<NumOpened;i++){
- strcat (outnamebuf,
- "\"
- );
- strcat (outnamebuf,
- filename
- );
- h= CreateFile(outnamebuf,
- GENERIC_WRITE | GENERIC_READ,
- FILE_SHARE_WRITE | FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL);
- if (h == INVALID_HANDLE_VALUE) {
- handUsbArray=NULL;
- } else {
- handUsbArray=h;
- successOpened++;
- }
- }
- return successOpened;
- }
- int CUSBImpObject::OpenAllUsbDevices(LPGUID pGuid)
- pGuid的USB器件
- {
- ULONG NumberDevices;
- HANDLE hOut = INVALID_HANDLE_VALUE;
- HDEVINFO hardwareDeviceInfo;
- SP_INTERFACE_DEVICE_DATA deviceInfoData;
- ULONG i,flag=1,j;
- ULONG NumDevicesOpened=0;
- BOOLEAN done;
- PUSB_DEVICE_DESCRIPTOR usbDeviceInst;
- PUSB_DEVICE_DESCRIPTOR *UsbDevices = &usbDeviceInst;
- char DeviceName[256]="";
- *UsbDevices = NULL;
- UsbDevicesOpened = 0;
- hardwareDeviceInfo = SetupDiGetClassDevs (
- pGuid,
- NULL,
- NULL,
- (DIGCF_PRESENT |
- DIGCF_INTERFACEDEVICE));
- NumberDevices = 4;
- done = FALSE;
- deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);
- i=0 ;
- while (!done) {
- NumberDevices *= 2;
- if (*UsbDevices) {
- *UsbDevices =
- (struct _USB_DEVICE_DESCRIPTOR *)realloc (*UsbDevices, (NumberDevices *
- sizeof (USB_DEVICE_DESCRIPTOR)));
- } else {
- *UsbDevices = (struct _USB_DEVICE_DESCRIPTOR *)calloc (NumberDevices,
- sizeof (USB_DEVICE_DESCRIPTOR));
- }
- if (NULL == *UsbDevices) {
- SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
- return 0 ;
- }
- usbDeviceInst = *UsbDevices + i;
- for (; i < NumberDevices; i++) {
- if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo,
- 0,
- pGuid,
- i,
- &deviceInfoData))
- {
- hOut = OpenOneDevice (hardwareDeviceInfo, &deviceInfoData, DeviceName);
- if ( hOut != INVALID_HANDLE_VALUE )
- {
- handUsbArray[UsbDevicesOpened]=hOut;
- if(!outnamebuf[UsbDevicesOpened])
- {
- return 0;
- }
- for(j=0;j<256;j++)
- {
- *(outnamebuf[UsbDevicesOpened]+j)=*(DeviceName+j);
- *(DeviceName+j)=0;
- }
- UsbDevicesOpened++;
- }
- }
- else
- {
- if(ERROR_NO_MORE_ITEMS == GetLastError())
- {
- done = TRUE;
- break;
- }
- }
- }
- }
- SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
- free ( *UsbDevices );
- return UsbDevicesOpened ;
- }
-
- HANDLE CUSBImpObject::OpenOneDevice (
- IN HDEVINFO HardwareDeviceInfo,
- IN PSP_INTERFACE_DEVICE_DATA DeviceInfoData,
- IN char *devName
- )
- {
- PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;
- ULONG predictedLength = 0;
- ULONG requiredLength = 0;
- HANDLE hOut = INVALID_HANDLE_VALUE;
- SetupDiGetInterfaceDeviceDetail (
- HardwareDeviceInfo,
- DeviceInfoData,
- NULL,
- 0,
- &requiredLength,
- NULL);
- predictedLength = requiredLength;
- functionClassDeviceData =(struct _SP_DEVICE_INTERFACE_DETAIL_DATA_A *)
- malloc (predictedLength);
- functionClassDeviceData->cbSize = sizeof
- (SP_INTERFACE_DEVICE_DETAIL_DATA);
- if (! SetupDiGetInterfaceDeviceDetail (
- HardwareDeviceInfo,
- DeviceInfoData,
- functionClassDeviceData,
- predictedLength,
- &requiredLength,
- NULL)) {
- free( functionClassDeviceData );
- return INVALID_HANDLE_VALUE;
- }
- strcpy( devName,functionClassDeviceData->DevicePath) ;
- hOut = CreateFile (
- functionClassDeviceData->DevicePath,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL);
- if (INVALID_HANDLE_VALUE == hOut) {
- }
- free( functionClassDeviceData );
- return hOut;
- }
|