LibUsbDotNet使用方法
最近在用C#调试USB程序,libusb源码是C语言的,C#用起来不方便,偶然在网上看到了LibUsbDotNet,这是开源的项目,下载后参考Example,用起来非常方便。
LibUsbDotNet下载 - http://sourceforge.net/projects/libusbdotnet/
我写的示例工程(附件传不上来,只能直接贴代码了^_^) - Enjoy...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;
using LibUsbDotNet.DeviceNotify;
using LibUsbDotNet.LibUsb;
namespace USBLib
{
public partial class Form1 : Form
{
const int myPID = 0x050F; //产品ID
const int myVID = 0x0425; //供应商ID
public static UsbDevice MyUsbDevice;//USB设备
public static DeviceNotifier DeviceNotifier = new DeviceNotifier();//设备变化通知函数
public static UsbEndpointWriter writer = null;
public static UsbEndpointReader reader = null;
delegate void SetTextCallback(string text);//安全线程访问txtReadInt的值
Boolean EnbaleInt = false;//是否使用中断接收
public Form1()
{
InitializeComponent();
}
private void ShowCon(string msg)
{
lblConnState.Text = msg;
}
private void ShowMsg(string msg)
{
lblMsg.Text = msg;
}
private void Form1_Load(object sender, EventArgs e)
{
if (FindAndOpenUSB(myVID, myPID) == true)
ShowCon("设备已连接");
else
ShowCon("设备未连接");
DeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;
writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep03);
reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
if( EnbaleInt == true)
{
reader.DataReceived += (OnRxEndPointData);
reader.DataReceivedEnabled = true;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
CloseUSB();
}
#region USB
///
/// 初始化USB设备
///
/// 设备PID
/// 设备VID
///
private bool FindAndOpenUSB(int PID, int VID)
{
UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(PID, VID);
UsbRegistry myUsbRegistry = UsbGlobals.AllDevices.Find(MyUsbFinder);
if (ReferenceEquals(myUsbRegistry, null))
{
return false;
}
// Open this usb device.
if (!myUsbRegistry.Open(out MyUsbDevice))
{
return false;
}
MyUsbDevice.SetConfiguration(1);
((LibUsbDevice)MyUsbDevice).ClaimInterface(0);
ShowMsg(string.Format("Find Device:{0}",myUsbRegistry[SPDRP.DeviceDesc]));
return true;
}
//关闭USB设备
public void CloseUSB()
{
if (!ReferenceEquals(reader, null))
reader.Dispose();
if (!ReferenceEquals(writer, null))
writer.Dispose();
if (!ReferenceEquals(MyUsbDevice,null))
MyUsbDevice.Close();
}
//获得上次错误信息
public string GetLastError()
{
return UsbGlobals.LastErrorString;
}
//设备变化消息相应函数
private void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
{
if (e.EventType == EventType.DeviceArrival)
{
ShowMsg(string.Format("发现有新USB设备连接,PID = 0x{0:X},VID = 0x{1:X}.\r\n设备的详细信息{2}", e.Device.IdProduct, e.Device.IdVendor, e.Device.ToString()));
//看看目前新连接的USB设备是不是目标设备
if (e.Device.IdProduct == myPID && e.Device.IdVendor == myVID)
{
ShowMsg("该USB设备是目标设备");
//发现目标设备并打开该设备
FindAndOpenUSB(myPID,myVID);
}
else
{
ShowMsg("该USB设备不是目标设备\r\n");
}
}
else if (e.EventType == EventType.DeviceRemoveComplete)
{
ShowMsg(string.Format("发现有USB设备移除,PID = 0x{0:X}, VID = 0x{1:X}\r\n设备的详细信息{2}", e.Device.IdProduct, e.Device.IdVendor, e.Device.ToString()));
//看看目前移除的USB设备是不是目标设备
if (e.Device.IdProduct == myPID && e.Device.IdVendor == myVID)
{
ShowMsg(string.Format("移除的USB设备是目标设备\r\n"));
CloseUSB();
}
else
{
ShowMsg(string.Format("移除的USB设备不是目标设备\r\n"));
}
}
}
//USB中断接收函数
private void OnRxEndPointData(object sender, EndpointDataEventArgs e)
{
//txtReadInt.Text = Encoding.Default.GetString(e.Buffer, 0, e.Count);
//MessageBox.Show(Encoding.Default.GetString(e.Buffer, 0, e.Count));
SetText(Encoding.Default.GetString(e.Buffer, 0, e.Count));
}
#endregion
private void btnSend_Click(object sender, EventArgs e)
{
ErrorCode ec = ErrorCode.None;
int bytesWritten;
try
{
ec = writer.Write(Encoding.Default.GetBytes(txtSend.Text), 2000, out bytesWritten);
if (ec != ErrorCode.None)
throw new Exception(UsbGlobals.LastErrorString);
}
catch (Exception ex)
{
ShowMsg("Error:" + ex.Message);
}
finally
{
}
}
private void btnRead_Click(object sender, EventArgs e)
{
ErrorCode ec = ErrorCode.None;
byte[] readBuffer = new byte[1024];
int bytesRead;
try
{
ec = reader.Read(readBuffer, 100, out bytesRead);
if (bytesRead == 0)
throw new Exception("No more bytes!");
txtRead.Text = Encoding.Default.GetString(readBuffer, 0, bytesRead);
}
catch (Exception ex)
{
ShowMsg("Error:" + ex.Message);
}
finally
{
}
}
//线程安全访问txtReadInt
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtReadInt.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.txtReadInt.Text = text;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;
using LibUsbDotNet.DeviceNotify;
using LibUsbDotNet.LibUsb;
namespace USBLib
{
public partial class Form1 : Form
{
const int myPID = 0x050F; //产品ID
const int myVID = 0x0425; //供应商ID
public static UsbDevice MyUsbDevice;//USB设备
public static DeviceNotifier DeviceNotifier = new DeviceNotifier();//设备变化通知函数
public static UsbEndpointWriter writer = null;
public static UsbEndpointReader reader = null;
delegate void SetTextCallback(string text);//安全线程访问txtReadInt的值
Boolean EnbaleInt = false;//是否使用中断接收
public Form1()
{
InitializeComponent();
}
private void ShowCon(string msg)
{
lblConnState.Text = msg;
}
private void ShowMsg(string msg)
{
lblMsg.Text = msg;
}
private void Form1_Load(object sender, EventArgs e)
{
if (FindAndOpenUSB(myVID, myPID) == true)
ShowCon("设备已连接");
else
ShowCon("设备未连接");
DeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;
writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep03);
reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
if( EnbaleInt == true)
{
reader.DataReceived += (OnRxEndPointData);
reader.DataReceivedEnabled = true;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
CloseUSB();
}
#region USB
///
/// 初始化USB设备
///
/// 设备PID
/// 设备VID
///
private bool FindAndOpenUSB(int PID, int VID)
{
UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(PID, VID);
UsbRegistry myUsbRegistry = UsbGlobals.AllDevices.Find(MyUsbFinder);
if (ReferenceEquals(myUsbRegistry, null))
{
return false;
}
// Open this usb device.
if (!myUsbRegistry.Open(out MyUsbDevice))
{
return false;
}
MyUsbDevice.SetConfiguration(1);
((LibUsbDevice)MyUsbDevice).ClaimInterface(0);
ShowMsg(string.Format("Find Device:{0}",myUsbRegistry[SPDRP.DeviceDesc]));
return true;
}
//关闭USB设备
public void CloseUSB()
{
if (!ReferenceEquals(reader, null))
reader.Dispose();
if (!ReferenceEquals(writer, null))
writer.Dispose();
if (!ReferenceEquals(MyUsbDevice,null))
MyUsbDevice.Close();
}
//获得上次错误信息
public string GetLastError()
{
return UsbGlobals.LastErrorString;
}
//设备变化消息相应函数
private void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
{
if (e.EventType == EventType.DeviceArrival)
{
ShowMsg(string.Format("发现有新USB设备连接,PID = 0x{0:X},VID = 0x{1:X}.\r\n设备的详细信息{2}", e.Device.IdProduct, e.Device.IdVendor, e.Device.ToString()));
//看看目前新连接的USB设备是不是目标设备
if (e.Device.IdProduct == myPID && e.Device.IdVendor == myVID)
{
ShowMsg("该USB设备是目标设备");
//发现目标设备并打开该设备
FindAndOpenUSB(myPID,myVID);
}
else
{
ShowMsg("该USB设备不是目标设备\r\n");
}
}
else if (e.EventType == EventType.DeviceRemoveComplete)
{
ShowMsg(string.Format("发现有USB设备移除,PID = 0x{0:X}, VID = 0x{1:X}\r\n设备的详细信息{2}", e.Device.IdProduct, e.Device.IdVendor, e.Device.ToString()));
//看看目前移除的USB设备是不是目标设备
if (e.Device.IdProduct == myPID && e.Device.IdVendor == myVID)
{
ShowMsg(string.Format("移除的USB设备是目标设备\r\n"));
CloseUSB();
}
else
{
ShowMsg(string.Format("移除的USB设备不是目标设备\r\n"));
}
}
}
//USB中断接收函数
private void OnRxEndPointData(object sender, EndpointDataEventArgs e)
{
//txtReadInt.Text = Encoding.Default.GetString(e.Buffer, 0, e.Count);
//MessageBox.Show(Encoding.Default.GetString(e.Buffer, 0, e.Count));
SetText(Encoding.Default.GetString(e.Buffer, 0, e.Count));
}
#endregion
private void btnSend_Click(object sender, EventArgs e)
{
ErrorCode ec = ErrorCode.None;
int bytesWritten;
try
{
ec = writer.Write(Encoding.Default.GetBytes(txtSend.Text), 2000, out bytesWritten);
if (ec != ErrorCode.None)
throw new Exception(UsbGlobals.LastErrorString);
}
catch (Exception ex)
{
ShowMsg("Error:" + ex.Message);
}
finally
{
}
}
private void btnRead_Click(object sender, EventArgs e)
{
ErrorCode ec = ErrorCode.None;
byte[] readBuffer = new byte[1024];
int bytesRead;
try
{
ec = reader.Read(readBuffer, 100, out bytesRead);
if (bytesRead == 0)
throw new Exception("No more bytes!");
txtRead.Text = Encoding.Default.GetString(readBuffer, 0, bytesRead);
}
catch (Exception ex)
{
ShowMsg("Error:" + ex.Message);
}
finally
{
}
}
//线程安全访问txtReadInt
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtReadInt.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.txtReadInt.Text = text;
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人