LibUsbDotNet的简单理解

LibUsbDotNet是一个.NET C#的USB库,适用于WinUsb,libusb-win32,Linux libusb v1.x

LibUsbDotNet是一个为.NET软件开发的工具,可以设计出一个简洁快速的通用USB驱动的方法(使用与Unix,window) 
所有基本的USB设备功能可以通过常见的设备类允许您执行写操作系统和驱动程序独立的代码
Caution: LibUsbDotNet is not in any way an end-user driver or a replacement driver for existing windows class drivers.

特点:

支持WinUSB,
All WinUSB interfaces are treated as seperate devices. IE each interface can be used by a different application.

打开对于USB设备
1 通过vendor id(供应商id) 和product id(产品id)每个USB设备都有,Vendor ID用来标记哪个厂商生产的这个USB设备,Product ID用来标记不同的产品。
2 得到UsbDeviceDescriptor(设备描述符) and theUsbConfigDescriptor(配置描述符).
3  如果存在,得到ManufacturerString(厂商字符串)
4 如果存在,得到产品字符串ProductString
5 如果存在,得到序列字符串SerialString, if one exists. 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;
using System.Collections.ObjectModel;

namespace ShowInfo_libusbdotnet
{
    public partial class Form1 : Form
    {
        public static UsbDevice MyUsbDevice;
        delegate void DelWriteToText(string str);
        public Form1()
        {
            InitializeComponent();
        }

       //画一条线(与libusb无关)
        private void button1_Click(object sender, EventArgs e)//点击鼠标就是一个事件
        {
            MessageBox.Show(e.ToString());
            // 创建GDI  
            Graphics g = this.CreateGraphics();
            //创建两个点  
            Point n1 = new Point(20, 20);
            Point n2 = new Point(100, 100);

            //创建画笔  
            Pen p = new Pen(Brushes.Black);
            g.DrawLine(p, n1, n2); 
                  
         }

        public  void ReadUSB()
        {
            //转储所有的设备和描述符
            UsbRegDeviceList allDevices = UsbDevice.AllDevices;
            foreach (UsbRegistry  usbRegistry in allDevices)
            {
                if(usbRegistry.Open(out MyUsbDevice))
                {
                    WriteToRichTextBox(MyUsbDevice.Info.ToString());//设备描述符
                    for(int iConfig=0;iConfig<MyUsbDevice.Configs.Count;iConfig++)
                    {
                        UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
                        WriteToRichTextBox(configInfo.ToString());//配置描述符

                        ReadOnlyCollection<UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
                        for(int iInterface=0;iInterface<interfaceList.Count;iInterface++)
                        {
                            UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                            WriteToRichTextBox("interfaceList" + interfaceList.ToString());//接口描述符

                            ReadOnlyCollection<UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
                            for(int iEndpoint=0;iEndpoint<endpointList.Count;iEndpoint++)
                            {
                                WriteToRichTextBox("endpointList"+endpointList[iEndpoint].ToString());//端点描述符
                            }
                        }
                    }            
                }
            }
            UsbDevice.Exit();         
        }
        //显示在richTextBox中
        private  void WriteToRichTextBox(string str)
        {
            if(this.richTextBox1.InvokeRequired)
            {
                DelWriteToText delWriteToRichText = new DelWriteToText(WriteToRichTextBox);
                this.Invoke(delWriteToRichText,str);
            }
            else
            {
                this.richTextBox1.Text += "\n"+str;
            }
        }
    }
}




各种描述符详细的记录了外围设备相关的一切信息。

其中端点描述符简单介绍

端点描述符具有7个字节长度,用来描述端点的属性以及各端点的位置。

typedef __packed struct _USB_ENDPOINT_DESCRIPTOR
{
    byte      bLength;             //描述符大小 = 07H
    byte      bDescriptorType;      //端点描述符类型 = 05H
    byte      bEndpointAddress;    //USB端点地址:bit 0…3 端点号
                                               // bit 4…6 保留
                                               // bit 7 方向控制 0:OUT 1:IN
    byte       bmAttributes;      // 端点属性
                                          // bit 0…1
                                          // 00:控制  01:同步
                                         // 02:批量  03:中断
    word     wMaxPacketSize;   // 本端点接受或发送的最大信息包的大小
    byte       bInterval;     // 轮询数据传送端点的时间间隔
                    //对于批量传送的端点以及控制传送的端点忽略
                    //对于同步传送的端点此域必须为1
                    //对于中断传送的端点,此域值的范围为1~255
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值