Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1319900
  • 博文数量: 482
  • 博客积分: 13297
  • 博客等级: 上将
  • 技术积分: 2890
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-12 16:25
文章分类

全部博文(482)

文章存档

2012年(9)

2011年(407)

2010年(66)

分类: 嵌入式

2010-08-26 22:41:37

6.         报告描述符及数据通信程序示例

报告描述符(由于是汇编代码,所以不必留意其语法,仅需注意表中的每个数据都占 1 个字节): 

_ReportDescriptor:                              // 报告描述符

       .dw 0x06,  0x00, 0xff               // 用法页

    .dw 0x09,  0x01                     // 用法 ( 供应商用法 1)

    .dw 0xa1,  0x01                      // 集合开始

    .dw 0x85,  0x01                         // 报告 ID(1)

    .dw 0x09,  0x01                  // 用法 ( 供应商用法 1)  

    .dw 0x15,  0x00                 // 逻辑最小值 (0)

    .dw 0x26,  0xff, 0x0                     // 逻辑最大值 (255)

    .dw 0x75,  0x08               // 报告大小 (8)

    .dw 0x95,  0x07                        // 报告计数 (7)

    .dw 0x81,  0x06                // 输入 (数据,变量,相对值)

 

    .dw 0x09,  0x01                     // 用法 ( 供应商用法 1)  

    .dw 0x85,  0x03                         // 报告 ID  3 

    .dw 0xb1,   0x06                         // 特征 (数据,变量,相对值)

 

       .dw 0x09,  0x01                    // 用法 ( 供应商用法 1)

    .dw 0x85,  0x02                         // 报告 ID  2 

    .dw 0xb1,  0x06                         // 特征 (数据,变量,相对值)

 

     .dw 0x09,  0x01                     // 用法 ( 供应商用法 1)  

    .dw 0x85,  0x04                         // 报告 ID  4 

    .dw 0x91,   0x06                         // 输出 (数据,变量,相对值)

    .dw   0xc0                    // 结合结束

_ReportDescriptor_End:

这个报告描述符,定义了 4 个不同的报告:输入报告 1 ,特征报告 2 ,特征报告 3 ,输出报告 4 (数字代表其报告 ID )。为了简化,每个报告都是 7 个字节(加上报告 ID 就是 8 个字节)。下面用一个简单的示例来描述 PC 端与 USB HID 设备进行通信的一般方法。

  1. #define     USB_VID       0xFC0   
  2. #define     USB_PID       0x420   
  3. HANDLE OpenMyHIDDevice(int overlapped);   
  4. void HIDSampleFunc()   
  5. {   
  6.     HANDLE       hDev;   
  7.     BYTE         recvDataBuf[8];   
  8.     BYTE         reportBuf[8];   
  9.     DWORD        bytes;   
  10.     hDev = OpenMyHIDDevice(0);                                // 打开设备,不使用重叠(异步)方式 ;   
  11.     if (hDev == INVALID_HANDLE_VALUE)   
  12.         return;   
  13.     reportBuf[0] = 4;                                         // 输出报告的报告 ID 是 4   
  14.     memset(reportBuf, 0, 8);   
  15.     reportBuf[1] = 1;   
  16.     if (!WriteFile(hDev, reportBuf, 8, &bytes, NULL))         // 写入数据到设备   
  17.          return;   
  18.     ReadFile(hDev, recvDatatBuf, 8, &bytes, NULL);            // 读取设备发给主机的数据   
  19. }   
  20.     
  21. HANDLE OpenMyHIDDevice(int overlapped)   
  22. {   
  23.     HANDLE hidHandle;   
  24.     GUID hidGuid;   
  25.     HidD_GetHidGuid(&hidGuid);   
  26.     HDEVINFO hDevInfo = SetupDiGetClassDevs(&hidGuid,   
  27.                                             NULL,   
  28.                                             NULL,   
  29.                                             (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));   
  30.     if (hDevInfo == INVALID_HANDLE_VALUE)   
  31.     {   
  32.         return INVALID_HANDLE_VALUE;   
  33.     }   
  34.     SP_DEVICE_INTERFACE_DATA devInfoData;   
  35.     devInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);   
  36.     int deviceNo = 0;   
  37.     SetLastError(NO_ERROR);   
  38.     while (GetLastError() != ERROR_NO_MORE_ITEMS)   
  39.     {   
  40.         if (SetupDiEnumInterfaceDevice (hDevInfo,   
  41.                                         0,   
  42.                                        &hidGuid,   
  43.                                        deviceNo,   
  44.                                        &devInfoData))   
  45.         {   
  46.             ULONG  requiredLength = 0;   
  47.             SetupDiGetInterfaceDeviceDetail(hDevInfo,   
  48.                                             &devInfoData,   
  49.                                             NULL,   
  50.                                             0,   
  51.                                             &requiredLength,   
  52.                                              NULL);  
  53.             PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc (requiredLength);   
  54.             devDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);   
  55.             if(!SetupDiGetInterfaceDeviceDetail(hDevInfo,   
  56.                                                  &devInfoData,   
  57.                                                  devDetail,   
  58.                                                  requiredLength,   
  59.                                                  NULL,   
  60.                                                  NULL))   
  61.             {   
  62.                 free(devDetail);   
  63.                 SetupDiDestroyDeviceInfoList(hDevInfo);   
  64.                 return INVALID_HANDLE_VALUE;   
  65.             }   
  66.             if (overlapped)   
  67.             {   
  68.                 hidHandle = CreateFile(devDetail->DevicePath,   
  69.                                        GENERIC_READ | GENERIC_WRITE,   
  70.                                        FILE_SHARE_READ | FILE_SHARE_WRITE,   
  71.                                        NULL,   
  72.                                        OPEN_EXISTING,           
  73.                                        FILE_FLAG_OVERLAPPED,   
  74.                                        NULL);   
  75.             }   
  76.             else   
  77.             {   
  78.                 hidHandle = CreateFile(devDetail->DevicePath,   
  79.                                        GENERIC_READ | GENERIC_WRITE,   
  80.                                        FILE_SHARE_READ | FILE_SHARE_WRITE,   
  81.                                        NULL,   
  82.                                        OPEN_EXISTING,           
  83.                                        0,   
  84.                                        NULL);   
  85.             }   
  86.             free(devDetail);   
  87.             if (hidHandle==INVALID_HANDLE_VALUE)   
  88.             {   
  89.                 SetupDiDestroyDeviceInfoList(hDevInfo);   
  90.                 free(devDetail);   
  91.                 return INVALID_HANDLE_VALUE;   
  92.             }   
  93.             _HIDD_ATTRIBUTES hidAttributes;   
  94.             if(!HidD_GetAttributes(hidHandle, &hidAttributes))   
  95.             {   
  96.                 CloseHandle(hidHandle);   
  97.                 SetupDiDestroyDeviceInfoList(hDevInfo);   
  98.                 return INVALID_HANDLE_VALUE;   
  99.             }   
  100.             if (USB_VID == hidAttributes.VendorID   
  101.                 && USB_PID  == hidAttributes.ProductID)   
  102.             {   
  103.                 break;   
  104.             }   
  105.             else   
  106.             {   
  107.                 CloseHandle(hidHandle);   
  108.                 ++deviceNo;   
  109.             }   
  110.         }   
  111.     }   
  112.     SetupDiDestroyDeviceInfoList(hDevInfo);   
  113.     return hidHandle;   
  114. }   
阅读(1688) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~