CYUSB

/*Summary
The application cydesc is used to open the device with cypress GUID and get the device descriptor
*/
//参考CyAPI.pdf和圈圈教你学USB


#include <windows.h>

#include <stdio.h>
#include "cyapi.h"
#include <conio.h>
#include <iostream>
#include <string>

using namespace std;

/*------------------------------------------
设备描述符(DEVICE)             1
配置描述符(CONFIGURATION)      2
字符串描述符(STRING)           3
接口描述符(INTERFACE)          4
端点描述符(ENDPOINT)           5
-------------------------------------------*/
void main()
{
    char buf[512];
    string s;
    string DevName;
    CCyUSBDevice *USBDevice;
    USB_DEVICE_DESCRIPTOR descr;

    USB_INTERFACE_DESCRIPTOR InterfaceDescr;
    
    USB_CONFIGURATION_DESCRIPTOR ConfigurationDes;


    USBDevice = new CCyUSBDevice(NULL);   // Create an instance of CCyUSBDevice

    printf("device count = %d \n",USBDevice->DeviceCount());//USB总线上USB设备数量

    for (int i=0; i < USBDevice->DeviceCount(); i++)
    {
        if (USBDevice->Open(i))
        {
            USBDevice->GetDeviceDescriptor(&descr);//设备描述符 

            printf("设备描述符GetDeviceDescriptor\n");//DeviceDscr
            printf("bLength \t\t 0x%02x\n",descr.bLength);//设备描述符的长度 18  参考圈圈教你学USB(Page 78)
            printf("bDescriptorType \t 0x%02x\n",descr.bDescriptorType);//设备描述符的类型 01
            printf("bcdUSB \t\t\t 0x%04x\n",descr.bcdUSB);//本设备所使用的USB协议,它使用的是BCD码表示 USB2.0协议就是0x0200,USB1.1协议就是0x0110,USB协议中使用的是小端模式
            printf("bDeviceClass \t\t 0x%02x\n",descr.bDeviceClass);//类代码,
            printf("bDeviceSubClass \t 0x%02x\n",descr.bDeviceSubClass);//子类代码
            printf("bDeviceProtocol \t 0x%02x\n",descr.bDeviceProtocol);//设备使用的协议
            printf("bMaxPacketSize0 \t 0x%02x\n",descr.bMaxPacketSize0);//端点0最大包长
            printf("idVendor \t\t 0x%04x\n",descr.idVendor);//厂商的ID
            printf("idProduct \t\t 0x%04x\n",descr.idProduct);//产品ID
            printf("bcdDevice \t\t 0x%04x\n",descr.bcdDevice);//设备版本号
            printf("iManufacturer \t\t 0x%02x\n",descr.iManufacturer);//描述厂商的字符串索引
            printf("iProduct \t\t 0x%02x\n",descr.iProduct);//产品的字符串索引
            printf("iSerialNumber \t\t 0x%02x\n",descr.iSerialNumber);//产品序列号字符串索引
            printf("bNumConfigurations \t 0x%02x\n\n",descr.bNumConfigurations);//配置数
            printf("\n");

            USBDevice-> GetConfigDescriptor(&ConfigurationDes);

            printf("配置描述符GetConfigDescriptor\n");//
            printf("bLength \t\t 0x%02x\n",ConfigurationDes.bLength);//配置描述符的长度 09
            printf("bDescriptorType \t\t 0x%02x\n",ConfigurationDes.bDescriptorType);//描述符的类型 02  配置描述符为02
            printf("wTotalLength \t\t 0x%04x\n",ConfigurationDes.wTotalLength);//整个配置描述符的总长度
            printf("bNumInterfaces \t\t 0x%02x\n",ConfigurationDes.bNumInterfaces);
            printf("bConfigurationValue \t\t 0x%02x\n",ConfigurationDes.bConfigurationValue);
            printf("iConfiguration \t\t 0x%02x\n",ConfigurationDes.iConfiguration);
            printf("bmAttributes \t\t 0x%02x\n",ConfigurationDes.bmAttributes);//D6表示供电方式 1表示设备自供电,0表示总线供电,D5=1时,支持远程唤醒
            printf("MaxPower \t\t 0x%02x\n",ConfigurationDes.MaxPower);//表示设备需要从总线上获取的电流值,单位为2ma
            printf("\n");

            USBDevice->GetIntfcDescriptor(&InterfaceDescr);

            printf("接口描述符GetIntfcDescriptor\n");//;; Interface Descriptor
            printf("bLength \t\t 0x%02x\n",InterfaceDescr.bLength);//接口描述符的长度 09
            printf("bDescriptorType \t\t 0x%02x\n",InterfaceDescr.bDescriptorType);//描述符的类型 04
            printf("bInterfaceNumber \t\t 0x%02x\n",InterfaceDescr.bInterfaceNumber);//接口的编号
            printf("bAlternateSetting \t\t 0x%02x\n",InterfaceDescr.bAlternateSetting);//接口的备用编号
            printf("bNumEndpoints \t\t 0x%02x\n",InterfaceDescr.bNumEndpoints);//该接口使用的端点数(不包括0端点)
            printf("bInterfaceClass \t\t 0x%02x\n",InterfaceDescr.bInterfaceClass);////接口所使用的类 子类 以及协议
            printf("bInterfaceSubClass \t\t 0x%02x\n",InterfaceDescr.bInterfaceSubClass);
            printf("bInterfaceProtocol \t\t 0x%02x\n",InterfaceDescr.bInterfaceProtocol);
            printf("iInterface \t\t 0x%02x\n",InterfaceDescr.iInterface);//字符串的索引值
            printf("\n");

            


            UCHAR num=USBDevice->EndPointCount();//返回接口的端点数,加1(包括0端点)
            printf("接口端点数num \t\t 0x%02x\n",num);
            printf("\n");

            UCHAR i=0;
            CCyBulkEndPoint *BulkInEpt=NULL;//建立一个端点对象
            CCyBulkEndPoint *BulkOutEpt=NULL;
            printf("传输方向\n");
            for(i=0;i<num;i++)
            {
                bool bIn=USBDevice->EndPoints[i]->Address&0x80;//D7=1代表Input D7=0代表OutPut;
                bool bBulk=(USBDevice->EndPoints[i]->Attributes==2);/*是否批量传输 D1-D0代表传输类型 0控制传输 1为等时传输 2为批量传输 3为中断传输*/

                if(bIn && bBulk)//
                {
                    BulkInEpt=(CCyBulkEndPoint *)USBDevice->EndPoints[i];
                    printf("输入端点BulkInEpt= \t\t 0x%02x\n",USBDevice->EndPoints[i]->Address);
                }    
                if(!bIn && bBulk)
                {
                    BulkOutEpt=(CCyBulkEndPoint *)USBDevice->EndPoints[i];
                    printf("输出端点BulkOutEpt= \t\t 0x%02x\n",USBDevice->EndPoints[i]->Address);
                }
                    
            }
            printf("\n");
        

            CCyUSBConfig cfg = USBDevice->GetUSBConfig(i);//获取配置描述符中的信息
            printf("bLength: 0x%x\n",cfg.bLength);;
            printf("bDescriptorType: %d\n",cfg.bDescriptorType);
            printf("wTotalLength: %d (0x%x)\n",cfg.wTotalLength,cfg.wTotalLength);
            printf("bNumInterfaces: %d\n",cfg.bNumInterfaces);
            printf("bConfigurationValue: %d\n",cfg.bConfigurationValue);
            printf("iConfiguration: %d\n",cfg.iConfiguration);
            printf("bmAttributes: 0x%x\n",cfg.bmAttributes);
            printf("MaxPower: %d\n",cfg.MaxPower);

            printf("DeviceName: %s\n",USBDevice->DeviceName);//返回USB设备名称

            USBDevice->Close();
        }
        else
        {
            printf("unable to open device\n");
        }
    }

    USBDevice->Close();//关闭USB设备
    _getch();

}

 

posted @ 2018-08-12 17:30  陈世超  阅读(2158)  评论(1编辑  收藏  举报