您现在的位置: USB开发网 > USB技术文档 > USB PC驱动及应用程序
- USB PC驱动及应用程序

用C++访问USB端口示例

------分隔线----------------------------

要访问USB端口,首先要知道这种USB设备的GUID和管道名称,获取句柄以后就可以使用ReadFile/WriteFile进行读写了!下面放上代码,以供参考。

 

  1. // filename是管道名称,如pipe01等 
  2. int CUSBImpObject::open_file( char *filename) 
  3. int successOpened = 0,i; 
  4. int NumOpened; 
  5. HANDLE h; 
  6. NumOpened=OpenAllUsbDevices((LPGUID) &GUID_CLASS_I82930_BULK); 
  7. if(NumOpened<=0) 
  8. return 0; 
  9. for(i=0;i<NumOpened;i++){ 
  10. strcat (outnamebuf, 
  11. "\" 
  12. ); 
  13. strcat (outnamebuf, 
  14. filename 
  15. ); 
  16. h= CreateFile(outnamebuf, 
  17. GENERIC_WRITE | GENERIC_READ, 
  18. FILE_SHARE_WRITE | FILE_SHARE_READ, 
  19. NULL, 
  20. OPEN_EXISTING, 
  21. 0, 
  22. NULL); 
  23. if (h == INVALID_HANDLE_VALUE) { 
  24. handUsbArray=NULL; 
  25. else { 
  26. handUsbArray=h; 
  27. successOpened++; 
  28. return successOpened; 
  29. int CUSBImpObject::OpenAllUsbDevices(LPGUID pGuid) //打开所有的GUID为 
  30. pGuid的USB器件 
  31. //输出名存在outnamebuf中 
  32. ULONG NumberDevices; 
  33. HANDLE hOut = INVALID_HANDLE_VALUE; //HANDLE Phout[8]; 
  34. HDEVINFO hardwareDeviceInfo; 
  35. SP_INTERFACE_DEVICE_DATA deviceInfoData; 
  36. ULONG i,flag=1,j; 
  37. ULONG NumDevicesOpened=0; 
  38. BOOLEAN done; 
  39. PUSB_DEVICE_DESCRIPTOR usbDeviceInst; 
  40. PUSB_DEVICE_DESCRIPTOR *UsbDevices = &usbDeviceInst; 
  41. char DeviceName[256]=""//器件名 
  42. *UsbDevices = NULL; 
  43. UsbDevicesOpened = 0; //打开器件数置零 
  44. hardwareDeviceInfo = SetupDiGetClassDevs ( 
  45. pGuid, 
  46. NULL, // Define no enumerator (global) 
  47. NULL, // Define no 
  48. (DIGCF_PRESENT | // Only Devices present 
  49. DIGCF_INTERFACEDEVICE)); // Function class devices. 
  50. NumberDevices = 4; 
  51. done = FALSE; 
  52. deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA); 
  53. i=0 ; 
  54. while (!done) { 
  55. NumberDevices *= 2; 
  56. if (*UsbDevices) { 
  57. *UsbDevices = 
  58. (struct _USB_DEVICE_DESCRIPTOR *)realloc (*UsbDevices, (NumberDevices * 
  59. sizeof (USB_DEVICE_DESCRIPTOR))); 
  60. else { 
  61. *UsbDevices = (struct _USB_DEVICE_DESCRIPTOR *)calloc (NumberDevices, 
  62. sizeof (USB_DEVICE_DESCRIPTOR)); 
  63. if (NULL == *UsbDevices) { 
  64. SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); 
  65. return 0 ; 
  66. usbDeviceInst = *UsbDevices + i; 
  67. for (; i < NumberDevices; i++) { 
  68. if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo, 
  69. 0, 
  70. pGuid, 
  71. i, 
  72. &deviceInfoData)) 
  73. hOut = OpenOneDevice (hardwareDeviceInfo, &deviceInfoData, DeviceName); 
  74. if ( hOut != INVALID_HANDLE_VALUE ) 
  75. handUsbArray[UsbDevicesOpened]=hOut; 
  76. if(!outnamebuf[UsbDevicesOpened]) 
  77. return 0; 
  78. for(j=0;j<256;j++) 
  79. *(outnamebuf[UsbDevicesOpened]+j)=*(DeviceName+j); 
  80. *(DeviceName+j)=0; 
  81. UsbDevicesOpened++; 
  82. else 
  83. if(ERROR_NO_MORE_ITEMS == GetLastError()) 
  84. done = TRUE; 
  85. break
  86. //end for 
  87. //end while 
  88. SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); 
  89. free ( *UsbDevices ); 
  90. return UsbDevicesOpened ; 
  91. //-------------------------------------------------------------------- 
  92. HANDLE CUSBImpObject::OpenOneDevice ( 
  93. IN HDEVINFO HardwareDeviceInfo, 
  94. IN PSP_INTERFACE_DEVICE_DATA DeviceInfoData, 
  95. IN char *devName 
  96. PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL; 
  97. ULONG predictedLength = 0; 
  98. ULONG requiredLength = 0; 
  99. HANDLE hOut = INVALID_HANDLE_VALUE; 
  100. SetupDiGetInterfaceDeviceDetail ( 
  101. HardwareDeviceInfo, 
  102. DeviceInfoData, 
  103. NULL, // probing so no output buffer yet 
  104. 0, // probing so output buffer length of zero 
  105. &requiredLength, 
  106. NULL); // not interested in the specific dev-node 
  107. predictedLength = requiredLength; 
  108. functionClassDeviceData =(struct _SP_DEVICE_INTERFACE_DETAIL_DATA_A *) 
  109. malloc (predictedLength); 
  110. functionClassDeviceData->cbSize = sizeof 
  111. (SP_INTERFACE_DEVICE_DETAIL_DATA); 
  112. if (! SetupDiGetInterfaceDeviceDetail ( 
  113. HardwareDeviceInfo, 
  114. DeviceInfoData, 
  115. functionClassDeviceData, 
  116. predictedLength, 
  117. &requiredLength, 
  118. NULL)) { 
  119. free( functionClassDeviceData ); 
  120. return INVALID_HANDLE_VALUE; 
  121. strcpy( devName,functionClassDeviceData->DevicePath) ; 
  122. hOut = CreateFile ( 
  123. functionClassDeviceData->DevicePath, 
  124. GENERIC_READ | GENERIC_WRITE, 
  125. FILE_SHARE_READ | FILE_SHARE_WRITE, 
  126. NULL, // no SECURITY_ATTRIBUTES structure 
  127. OPEN_EXISTING, // No special create flags 
  128. 0, // No special attributes 
  129. NULL); // No template file 
  130. if (INVALID_HANDLE_VALUE == hOut) { 
  131. free( functionClassDeviceData ); 
  132. return hOut; 

 

 

------分隔线----------------------------
联系我们
  • Q Q: 1148374829 点击这里给我发消息
  • 旺旺:jhoneqhsieh 点击这里给我发消息
  • 电话:(0)15923141204
  • 淘宝网店
USB开源项目