用于检测USB设备的USB库
本文是关于USB库的,USBClassLibrary 它使您可以管理USB设备的Attach和Detach事件并检测您自己的设备。
在项目属性的“构建”选项卡中,不要在“平台目标”下拉列表中选择“任何CPU” ,而是选择x86或x64。
类库下载链接:DLL下载
1、Windows Forms
//引用
using USBClassLibrary;
private USBClassLibrary.USBClass USBPort;
private List<USBClassLibrary.USBClass.DeviceProperties> ListOfUSBDeviceProperties;
USBPort = new USBClass();
ListOfUSBDeviceProperties = new List<USBClassLibrary.USBClass.DeviceProperties>();
//设备的插入与移除
USBPort.USBDeviceAttached += new USBClass.USBDeviceEventHandler(USBPort_USBDeviceAttached);
USBPort.USBDeviceRemoved += new USBClass.USBDeviceEventHandler(USBPort_USBDeviceRemoved);
//注册您的表单,以便在添加或删除设备时接收Windows消息
USBPort.RegisterForDeviceChange(true, this.Handle);
//设备是否连接
if (USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID, ref ListOfUSBDeviceProperties, false))
{
//My Device is connected
MyUSBDeviceConnected = true;
}
//设备变动事件
private void USBPort_USBDeviceAttached(object sender,
USBClass.USBDeviceEventArgs e)
{
if (!MyUSBDeviceConnected)
{
if (USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID,
ref ListOfUSBDeviceProperties, false))
{
//My Device is connected
MyUSBDeviceConnected = true;
}
}
}
private void USBPort_USBDeviceRemoved(object sender,
USBClass.USBDeviceEventArgs e)
{
if (!USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID,
ref ListOfUSBDeviceProperties, false))
{
//My Device is removed
MyUSBDeviceConnected = false;
}
}
//处理窗体中的Windows消息,将它们传递给USBClass类
protected override void WndProc(ref Message m)
{
bool IsHandled = false;
USBPort.ProcessWindowsMessage(m.Msg, m.WParam, m.LParam, ref IsHandled);
base.WndProc(ref m);
}
2、WPF
//using 在代码中添加 指令:
using USBClassLibrary;
//声明一个实例 USBClass
private USBClassLibrary.USBClass USBPort;
//DeviceProperties 如果要读取设备的属性,则声明该类的实例List <T>
private List<USBClassLibrary.USBClass.DeviceProperties> ListOfUSBDeviceProperties;
//创建USBClass 该类的实例
USBPort = new USBClass();
//创建DeviceProperties 该类的实例List <T>
ListOfUSBDeviceProperties = new List<USBClassLibrary.USBClass.DeviceProperties>();
//为USBClass 该类公开的事件添加处理程序
USBPort.USBDeviceAttached += new USBClass.USBDeviceEventHandler(USBPort_USBDeviceAttached);
USBPort.USBDeviceRemoved += new USBClass.USBDeviceEventHandler(USBPort_USBDeviceRemoved);
//覆盖OnSourceInitialized以便:
//检索Windows句柄
//添加一个接收所有窗口消息的事件处理程序
//注册您的表单以在添加或删除设备时接收Windows消息
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
//USB Connection
USBPort.RegisterForDeviceChange(true, source.Handle);
USBTryMyDeviceConnection();
MyUSBDeviceConnected = false;
}
//处理表单中的Windows消息以将其传递给 USBClass 类
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
USBPort.ProcessWindowsMessage(msg, wParam, lParam, ref handled);
return IntPtr.Zero;
}
//然后,检查您的设备是否尚未连接
if (USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID, ref ListOfUSBDeviceProperties, false))
{
//My Device is connected
MyUSBDeviceConnected = true;
}
//然后,检查您的设备是否尚未连接
private void USBPort_USBDeviceAttached(object sender,
USBClass.USBDeviceEventArgs e)
{
if (!MyUSBDeviceConnected)
{
if (USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID,
ref ListOfUSBDeviceProperties, false))
{
//My Device is connected
MyUSBDeviceConnected = true;
}
}
}
private void USBPort_USBDeviceRemoved(object sender,
USBClass.USBDeviceEventArgs e)
{
if (!USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID,
ref ListOfUSBDeviceProperties, false))
{
//My Device is removed
MyUSBDeviceConnected = false;
}
}
3、其它设备属性
public struct DeviceProperties
{
public string FriendlyName;
public string DeviceDescription;
public string DeviceType;
public string DeviceManufacturer;
public string DeviceClass;
public string DeviceLocation;
public string DevicePath;
public string DevicePhysicalObjectName;
public string COMPort;
}
public static bool GetUSBDevice(UInt32 VID, UInt32 PID, ref List<deviceproperties> ListOfDP, bool GetCOMPort, Nullable<uint32> MI=null)
{
...
DP.DevicePhysicalObjectName = String.Empty;
if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData,
(UInt32)Win32Wrapper.SPDRP.SPDRP_PHYSICAL_DEVICE_OBJECT_NAME,
ref RegType, intptrBuffer, BUFFER_SIZE, ref RequiredSize))
{
DP.DevicePhysicalObjectName = Marshal.PtrToStringAuto(intptrBuffer);
}
...
}
4、获取关连的com信息
如果您的设备模拟串行端口,则可以检索其COM端口。
该GetUSBDevice函数具有第四个参数GetCOMPort:
public static bool GetUSBDevice(UInt32 VID, UInt32 PID, ref List<deviceproperties> ListOfDP, bool GetCOMPort, Nullable<uint32> MI=null)
if (USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID,
ref ListOfUSBDeviceProperties, true))
{
String COMPort;
//My Device is connected
MyUSBDeviceConnected = true;
COMPort = DP.COMPort;
}