Biblioteca USB para detectar dispositivos USB
Este artículo trata sobre la biblioteca USB, USBClassLibrary, que le permite administrar los eventos Adjuntar y Desconectar de dispositivos USB y detectar sus propios dispositivos.
En la pestaña "Construir" de las propiedades del proyecto, no seleccione "Cualquier CPU" en la lista desplegable "Destino de plataforma", sino que elija x86 o x64.
Enlace de descarga de la biblioteca de clases: descarga de DLL
1 、 Formularios de Windows
//引用
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. Otros atributos del equipo
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. Obtenga información relacionada con las comunicaciones
Si su dispositivo simula un puerto serie, puede recuperar su puerto COM.
La función GetUSBDevice tiene el cuarto parámetro 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;
}