USB Communication DLL

如果你想打开一个USB管道,你首先要知道这种USB设备的GUID和管理道名称,
获取句柄以后就可以使用ReadFile/WriteFile进行读写了!
以下是代码,请参考!!

// filename是管道名称,如pipe01等
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[i],
    "\\"
     );

    strcat (outnamebuf[i],
    filename
    );


   h= CreateFile(outnamebuf[i],
         GENERIC_WRITE | GENERIC_READ,
         FILE_SHARE_WRITE | FILE_SHARE_READ,
         NULL,
         OPEN_EXISTING,
         0,
         NULL);
   if (h == INVALID_HANDLE_VALUE) {
  handUsbArray[i]=NULL;
 } else {
  handUsbArray[i]=h;
  successOpened++;
 }
}
return successOpened;
}


int CUSBImpObject::OpenAllUsbDevices(LPGUID pGuid)   //打开所有的GUID为
pGuid的USB器件
{        //输出名存在outnamebuf中
ULONG NumberDevices;
HANDLE hOut = INVALID_HANDLE_VALUE;    //HANDLE Phout[8];
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,       // Define no enumerator (global)
 NULL,       // Define no
 (DIGCF_PRESENT |     // Only Devices present
 DIGCF_INTERFACEDEVICE));    // Function class devices.

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;
  }
  }

 }  //end for
}    //end while
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,      // probing so no output buffer yet
      0,       // probing so output buffer length of zero
      &requiredLength,
      NULL);      // not interested in the specific dev-node


  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,      // no SECURITY_ATTRIBUTES structure
         OPEN_EXISTING,    // No special create flags
         0,      // No special attributes
         NULL);     // No template file

  if (INVALID_HANDLE_VALUE == hOut) {

  }
free( functionClassDeviceData );
return hOut;

}

<article from : http://www.xici.net/d11718892.htm>

利用USB接口进行通信的数据源码MemberIndex = 0 Do 'The cbSize element of the MyDeviceInterfaceData structure must be set to 'the structure's size in bytes. The size is 28 bytes. MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData) Result = SetupDiEnumDeviceInterfaces _ (DeviceInfoSet, _ 0, _ HidGuid, _ MemberIndex, _ MyDeviceInterfaceData) Call DisplayResultOfAPICall("SetupDiEnumDeviceInterfaces") If Result = 0 Then LastDevice = True 'If a device exists, display the information returned. If Result <> 0 Then lstResults.AddItem " DeviceInfoSet for device #" & CStr(MemberIndex) & ": " lstResults.AddItem " cbSize = " & CStr(MyDeviceInterfaceData.cbSize) lstResults.AddItem _ " InterfaceClassGuid.Data1 = " & Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data1) lstResults.AddItem _ " InterfaceClassGuid.Data2 = " & Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data2) lstResults.AddItem _ " InterfaceClassGuid.Data3 = " & Hex$(MyDeviceInterfaceData.InterfaceClassGuid.Data3) lstResults.AddItem _ " Flags = " & Hex$(MyDeviceInterfaceData.Flags) '****************************************************************************** 'SetupDiGetDeviceInterfaceDetail 'Returns: an SP_DEVICE_INTERFACE_DETAIL_DATA structure 'containing information about a device. 'To retrieve the information, call this function twice. 'The first time returns the size of the structure in Needed. 'The second time returns a pointer to the data in DeviceInfoSet. 'Requires: 'A DeviceInfoSet returned by SetupDiGetClassDevs and 'an SP_DEVICE_INTERFACE_DATA structure returned by SetupDiEnumDeviceInterfaces. '******************************************************************************* MyDeviceInfoData.cbSize = Len(MyDeviceInfoData) Result = SetupDiGetDeviceInterfaceDetail _ (DeviceInfoSet, _ MyDeviceInterfaceData, _ 0, _ 0, _ Needed, _ 0) DetailData = Needed Call DisplayResultOfAPICall("SetupDiGetDeviceInterfaceDetail") lstResults.AddItem " (OK to say too small)" lstResults.AddItem " Required buffer size for the data: " & Needed 'Store the structure's size. MyDeviceInterfaceDetailData.cbSize = _ Len(MyDeviceInterfaceDetailData) 'Use a byte array to allocate memory for 'the MyDeviceInterfaceDetailData structure ReDim DetailDataBuffer(Needed) 'Store cbSize in the first four bytes of the array. Call RtlMoveMemory _ (DetailDataBuffer(0), _ MyDeviceInterfaceDetailData, _ 4) 'Call SetupDiGetDeviceInterfaceDetail again. 'This time, pass the address of the first element of DetailDataBuffer 'and the returned required buffer size in DetailData. Result = SetupDiGetDeviceInterfaceDetail _ (DeviceInfoSet, _ MyDeviceInterfaceData, _ VarPtr(DetailDataBuffer(0)), _ DetailData, _ Needed, _ 0) Call DisplayResultOfAPICall(" Result of second call: ") lstResults.AddItem " MyDeviceInterfaceDetailData.cbSize: " & _ CStr(MyDeviceInterfaceDetailData.cbSize) 'Convert the byte array to a string. DevicePathName = CStr(DetailDataBuffer()) 'Convert to Unicode. DevicePathName = StrConv(DevicePathName, vbUnicode) 'Strip cbSize (4 bytes) from the beginning. DevicePathName = Right$(DevicePathName, Len(DevicePathName) - 4) lstResults.AddItem " Device pathname: " lstResults.AddItem " " & DevicePathName '****************************************************************************** 'CreateFile 'Returns: a handle that enables reading and writing to the device. 'Requires: 'The DevicePathName returned by SetupDiGetDeviceInterfaceDetail. '****************************************************************************** HidDevice = CreateFile _ (DevicePathName, _ GENERIC_READ Or GENERIC_WRITE, _ (FILE_SHARE_READ Or FILE_SHARE_WRITE), _ 0, _ OPEN_EXISTING, _ 0, _ 0) Call DisplayResultOfAPICall("CreateFile") lstResults.AddItem " Returned handle: " & Hex$(HidDevice) & "h" 'Now we can find out if it's the device we're looking for. '****************************************************************************** 'HidD_GetAttributes 'Requests information from the device. 'Requires: The handle returned by CreateFile. 'Returns: an HIDD_ATTRIBUTES structure containing 'the Vendor ID, Product ID, and Product Version Number. 'Use this information to determine if the detected device 'is the one we're looking for. '****************************************************************************** 'Set the Size property to the number of bytes in the structure. DeviceAttributes.Size = LenB(DeviceAttributes) Result = HidD_GetAttributes _ (HidDevice, _ DeviceAttributes) Call DisplayResultOfAPICall("HidD_GetAttributes") If Result <> 0 Then lstResults.AddItem " HIDD_ATTRIBUTES structure filled without error." Else lstResults.AddItem " Error in filling HIDD_ATTRIBUTES structure." End If lstResults.AddItem " Structure size: " & DeviceAttributes.Size lstResults.AddItem " Vendor ID: " & Hex$(DeviceAttributes.VendorID) lstResults.AddItem " Product ID: " & Hex$(DeviceAttributes.ProductID)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值