項目工程文件下載: 工程文件下載地址
看了很多網上的博客,講述如何用C#進行USB設備操作,很多都是不對的。以至於南轅北轍。我們可以使用usb庫。在c下有usblib庫,在C#下該如何使用libusb呢,下面介紹C#下的強大的開源USB類庫就登場了:LibUSBDotNet,沒錯就是.NET下的libusb,這也是個開源項目,已經把libusb封裝成了一個完整的類庫它是sourceforge上的一個開源項目,下載WIN下的EXE安裝即可,里面包含了很多的范例,還有說明文檔(CHM格式的,超級方便的)。下面簡單介紹一下該如何使用LibUSBDotNet。
1、首先你需要創建一個C#的應用程序(控制台、窗體都可以)
2、將LibUsbDotNet安裝目錄下Src目錄下LibWinUsb拷貝一份到你的工程根目錄下
3、不需要多說了吧,在你的解決方案上右擊,添加現有項目,將LibWinUsb目錄下的項目包含進來
4、在你的項目上右擊,添加引用,選擇LibUSBDotNet項目,如下圖:
下面提供一個讀取數據的范例:筆者插上了一個U盤,用bushound檢查到該U盤的VID和PID:
然而,單純這樣是不夠的,我們還需要用到usblib的工具,注冊這個設備才能使用。libusb-win32 filter這個工具,只有在這個工具里面選擇過的設備,才能是用libusbdotnet的庫函數打開。
那么VID是0x0930,PID是0x6544.填入函數中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibUsbDotNet;
using LibUsbDotNet.Main;
using LibUsbDotNet.Info;
using LibUsbDotNet.Descriptors;
using LibUsbDotNet.LibUsb;
using LibUsbDotNet.WinUsb;
namespace USBTest
{
internal class Program
{
public static UsbDevice MyUsbDevice;
#region SET YOUR USB Vendor and Product ID!
public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x930,0x6544);//U盤ID,可以通過Bushound讀出來
#endregion
public static void Main(string[] args)
{
ErrorCode ec = ErrorCode.None;
try
{
// Find and open the usb device.
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
// If the device is open and ready
if (MyUsbDevice == null) throw new Exception("Device Not Found.");
// If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
// it exposes an IUsbDevice interface. If not (WinUSB) the
// 'wholeUsbDevice' variable will be null indicating this is
// an interface of a device; it does not require or support
// configuration and interface selection.
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// This is a "whole" USB device. Before it can be used,
// the desired configuration and interface must be selected.
// Select config #1
wholeUsbDevice.SetConfiguration(1);
// Claim interface #0.
wholeUsbDevice.ClaimInterface(0);
}
// open read endpoint 1.
UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
byte[] readBuffer = new byte[1024];
while (ec == ErrorCode.None)
{
int bytesRead;
// If the device hasn't sent data in the last 5 seconds,
// a timeout error (ec = IoTimedOut) will occur.
ec = reader.Read(readBuffer, 5000, out bytesRead);
if (bytesRead == 0) throw new Exception(string.Format("{0}:No more bytes!", ec));
Console.WriteLine("{0} bytes read", bytesRead);
// Write that output to the console.
Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
}
Console.WriteLine("\r\nDone!\r\n");
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
}
finally
{
if (MyUsbDevice != null)
{
if (MyUsbDevice.IsOpen)
{
// If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
// it exposes an IUsbDevice interface. If not (WinUSB) the
// 'wholeUsbDevice' variable will be null indicating this is
// an interface of a device; it does not require or support
// configuration and interface selection.
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// Release interface #0.
wholeUsbDevice.ReleaseInterface(0);
}
MyUsbDevice.Close();
}
MyUsbDevice = null;
// Free usb resources
UsbDevice.Exit();
}
// Wait for user input..
Console.ReadKey();
}
}
}
}
項目工程文件下載:工程文件下載地址
————————————————
版權聲明:本文為CSDN博主「奧利奧冰茶」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/shizhibuyi1234/article/details/77943479