分享

C#下操作USB设备的方法

 VoidOc 2018-11-22

 想必大家对LibUSB不陌生,没错,它就是很有名的开源usb驱动,其提供的API可以很方便的操作linux或者WIN下的USB设备,非常的方便!但是libusb是基于c语言的,那么在C#下是不是就不能使用libusb呢?当然不是了,你完全可以把libusb提供的dll封装成自己的C#库,但是这个工作量是非常大的而且调试的过程中肯定会有些意想不到的事情发生,那么在C#下该如何使用libusb呢,下面介绍C#下的强大的开源USB类库就登场了:LibUSBDotNet,没错就是.NET下的libusb,这也是个开源项目,已经把libusb封装成了一个完整的类库,可以去下面链接下载:

http://download.csdn.net/detail/cumtwys/7713473(CSDN不要分)

或者

http:///projects/libusbdotnet/(官方)

它是sourceforge上的一个开源项目,下载WIN下的EXE安装即可,里面包含了很多的范例,还有说明文档(CHM格式的,超级方便的)。

下面简单介绍一下该如何使用LibUSBDotNet。

1、首先你需要创建一个C#的应用程序(控制台、窗体都可以)

2、将LibUsbDotNet安装目录下Src目录下LibWinUsb拷贝一份到你的工程根目录下

3、不需要多说了吧,在你的解决方案上右击,添加现有项目,将LibWinUsb目录下的项目包含进来

4、在你的项目上右击,添加引用,选择LibUSBDotNet项目,如下图:

5、在你的CS文件开头,添加引用:

[csharp] view plain copy
  1. using LibUsbDotNet;  
  2. using LibUsbDotNet.Main;  
  3. using LibUsbDotNet.Info;  
  4. using LibUsbDotNet.Descriptors;  
  5. using LibUsbDotNet.LibUsb;  
  6. using LibUsbDotNet.WinUsb;  
6、下面提供一个读取数据的范例(摘自CHM说明文档)

[csharp] view plain copy
  1. using System;  
  2. using System.Text;  
  3. using LibUsbDotNet;  
  4. using LibUsbDotNet.Main;  
  5.   
  6. namespace Examples  
  7. {  
  8.     internal class ReadPolling  
  9.     {  
  10.         public static UsbDevice MyUsbDevice;  
  11.  
  12.         #region SET YOUR USB Vendor and Product ID!  
  13.   
  14.         public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(1234, 1);  
  15.  
  16.         #endregion  
  17.   
  18.         public static void Main(string[] args)  
  19.         {  
  20.             ErrorCode ec = ErrorCode.None;  
  21.   
  22.             try  
  23.             {  
  24.                 // Find and open the usb device.  
  25.                 MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);  
  26.   
  27.                 // If the device is open and ready  
  28.                 if (MyUsbDevice == nullthrow new Exception("Device Not Found.");  
  29.   
  30.                 // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)  
  31.                 // it exposes an IUsbDevice interface. If not (WinUSB) the   
  32.                 // 'wholeUsbDevice' variable will be null indicating this is   
  33.                 // an interface of a device; it does not require or support   
  34.                 // configuration and interface selection.  
  35.                 IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;  
  36.                 if (!ReferenceEquals(wholeUsbDevice, null))  
  37.                 {  
  38.                     // This is a "whole" USB device. Before it can be used,   
  39.                     // the desired configuration and interface must be selected.  
  40.   
  41.                     // Select config #1  
  42.                     wholeUsbDevice.SetConfiguration(1);  
  43.   
  44.                     // Claim interface #0.  
  45.                     wholeUsbDevice.ClaimInterface(0);  
  46.                 }  
  47.   
  48.                 // open read endpoint 1.  
  49.                 UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);  
  50.   
  51.   
  52.                 byte[] readBuffer = new byte[1024];  
  53.                 while (ec == ErrorCode.None)  
  54.                 {  
  55.                     int bytesRead;  
  56.   
  57.                     // If the device hasn't sent data in the last 5 seconds,  
  58.                     // a timeout error (ec = IoTimedOut) will occur.   
  59.                     ec = reader.Read(readBuffer, 5000, out bytesRead);  
  60.   
  61.                     if (bytesRead == 0) throw new Exception(string.Format("{0}:No more bytes!", ec));  
  62.                     Console.WriteLine("{0} bytes read", bytesRead);  
  63.   
  64.                     // Write that output to the console.  
  65.                     Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));  
  66.                 }  
  67.   
  68.                 Console.WriteLine("\r\nDone!\r\n");  
  69.             }  
  70.             catch (Exception ex)  
  71.             {  
  72.                 Console.WriteLine();  
  73.                 Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);  
  74.             }  
  75.             finally  
  76.             {  
  77.                 if (MyUsbDevice != null)  
  78.                 {  
  79.                     if (MyUsbDevice.IsOpen)  
  80.                     {  
  81.                         // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)  
  82.                         // it exposes an IUsbDevice interface. If not (WinUSB) the   
  83.                         // 'wholeUsbDevice' variable will be null indicating this is   
  84.                         // an interface of a device; it does not require or support   
  85.                         // configuration and interface selection.  
  86.                         IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;  
  87.                         if (!ReferenceEquals(wholeUsbDevice, null))  
  88.                         {  
  89.                             // Release interface #0.  
  90.                             wholeUsbDevice.ReleaseInterface(0);  
  91.                         }  
  92.   
  93.                         MyUsbDevice.Close();  
  94.                     }  
  95.                     MyUsbDevice = null;  
  96.   
  97.                     // Free usb resources  
  98.                     UsbDevice.Exit();  
  99.   
  100.                 }  
  101.   
  102.                 // Wait for user input..  
  103.                 Console.ReadKey();  
  104.             }  
  105.         }  
  106.     }  
  107. }  
7、怎么样简单吧,方便吧,并且安装目录下有个驱动文件自动生成器,非常的好用就是InfWizard.exe,这是个驱动生成向导,对于开发自己的USB设备需要写WIN驱动的时候完全可以考虑使用LIBUSB来做驱动,那么使用这个工具你的工作量会在一分钟之内搞定,太强大了……


from: http://blog.csdn.net/cumtwys/article/details/38371419


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多