重新连接设备后调用 USB OnDataReceived

USB OnDataReceived is calling after reconnecting device

我正在使用条形码 reader(类似于 HID)。如果我启动我的应用程序,没有任何反应,OnDataReceived 处理程序不起作用。如果我删除条形码 reader,usb_OnDeviceRemoved 被调用(它跟踪所有 USB 设备),插入后 - usb_OnDeviceArrivedusb_OnSpecifiedDeviceArrived 被调用,之后我收到数据。

private IContainer components = (IContainer) null;
private UsbHidPort usb = null;
private void InitializeComponent()
{
    this.components = new System.ComponentModel.Container();
    this.usb = new UsbLibrary.UsbHidPort(this.components);
    ....
 }

在 FormLoaded 上:

if((xmlNode["scaner"].InnerText.Contains("HID"))){
                        string[] scanerInfo = xmlNode["scaner"].InnerText.Split(':');
                        this.usb.VendorId = int.Parse(scanerInfo[1], NumberStyles.HexNumber);
                        this.usb.ProductId = int.Parse(scanerInfo[2], NumberStyles.HexNumber);
                        this.usb.CheckDevicePresent();
                        ScanMethod = "HID";
                    /*************/

if(ScanMethod == "HID"){
                this.usb.OnSpecifiedDeviceArrived += new System.EventHandler(this.usb_OnSpecifiedDeviceArrived);
                this.usb.OnSpecifiedDeviceRemoved += new System.EventHandler(this.usb_OnSpecifiedDeviceRemoved);
                this.usb.OnDeviceArrived += new System.EventHandler(this.usb_OnDeviceArrived);
                this.usb.OnDeviceRemoved += new System.EventHandler(this.usb_OnDeviceRemoved);
                this.usb.OnDataRecieved += new UsbLibrary.DataRecievedEventHandler(this.usb_OnDataRecieved);

        }

protected override void OnHandleCreated(EventArgs e)
{
  base.OnHandleCreated(e);
  this.usb.RegisterHandle(this.Handle);
}

protected override void WndProc(ref Message m)
{
  this.usb.ParseMessages(ref m);
  base.WndProc(ref m);
}
 private void usb_OnDeviceArrived(object sender, EventArgs e)
{
    if (this.InvokeRequired)
    this.Invoke((Delegate) new EventHandler(this.usb_OnDeviceArrived), sender, (object) e);
  else{
    MessageBox.Show("usb_OnDeviceArrived" +" " + this.usb.VendorId + ":" + this.usb.ProductId);
  }
}

private void usb_OnDeviceRemoved(object sender, EventArgs e)
{
  if (this.InvokeRequired)
    this.Invoke((Delegate) new EventHandler(this.usb_OnDeviceRemoved), sender, (object) e);
  else{
    String vendor = this.usb.VendorId.ToString();
    String product = this.usb.ProductId.ToString();
    MessageBox.Show("usb_OnDeviceRemoved" +" " + this.usb.VendorId + ":" + this.usb.ProductId);
  }
}

private void usb_OnSpecifiedDeviceArrived(object sender, EventArgs e)
{
  if (this.InvokeRequired)
    this.Invoke((Delegate) new EventHandler(this.usb_OnSpecifiedDeviceArrived), sender, (object) e);
  else{
    MessageBox.Show("usb_OnSpecifiedDeviceArrived" +" " + this.usb.VendorId + ":" + this.usb.ProductId); 
    }
}
 private void usb_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
{
   if (this.InvokeRequired)
    this.Invoke((Delegate) new EventHandler(this.usb_OnSpecifiedDeviceRemoved), sender, (object) e);
  else{
    MessageBox.Show("usb_OnSpecifiedDeviceRemoved" +" " + this.usb.VendorId + ":" + this.usb.ProductId); 
    }

}

 private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)
{
  if (this.InvokeRequired)
  {

    try
    {
      this.Invoke((Delegate) new DataRecievedEventHandler(this.usb_OnDataRecieved), sender, (object) args);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
  }
  else
  {
   //Parsing received data
    }
   }

并且来自 DLL 文件:

public void ParseMessages(ref Message m)
    {
        if (m.Msg == Win32Usb.WM_DEVICECHANGE)  // we got a device change message! A USB device was inserted or removed
        {
            switch (m.WParam.ToInt32()) // Check the W parameter to see if a device was inserted or removed
            {
                case Win32Usb.DEVICE_ARRIVAL:   // inserted
                    if (OnDeviceArrived != null)
                    {
                        OnDeviceArrived(this, new EventArgs());
                        CheckDevicePresent();
                    }
                    break;
                case Win32Usb.DEVICE_REMOVECOMPLETE:    // removed
                    if (OnDeviceRemoved != null)
                    {
                        OnDeviceRemoved(this, new EventArgs());
                        CheckDevicePresent();
                    }
                    break;
            }
        }
    }

    /// <summary>
    /// Checks the devices that are present at the moment and checks if one of those
    /// is the device you defined by filling in the product id and vendor id.
    /// </summary>
    public void CheckDevicePresent()
    {
        try
        {
            //Mind if the specified device existed before.
            bool history = false;
            if(specified_device != null ){
                history = true;
            }

            specified_device = SpecifiedDevice.FindSpecifiedDevice(this.vendor_id, this.product_id);    // look for the device on the USB bus
            if (specified_device != null)   // did we find it?
            {
                if (OnSpecifiedDeviceArrived != null)
                {
                    this.OnSpecifiedDeviceArrived(this, new EventArgs());
                    specified_device.DataRecieved += new DataRecievedEventHandler(OnDataRecieved);
                    specified_device.DataSend += new DataSendEventHandler(OnDataSend);
                }
            }
            else
            {
                if (OnSpecifiedDeviceRemoved != null && history)
                {
                    this.OnSpecifiedDeviceRemoved(this, new EventArgs());
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

如果我在加载表单时调用 CheckDevicePresent(),为什么我必须重新连接 USB 设备!?

问题是我在创建事件处理程序之前调用了 CheckDevicePresent()。

if(ScanMethod == "HID"){
            this.usb.OnSpecifiedDeviceArrived += new System.EventHandler(this.usb_OnSpecifiedDeviceArrived);
            this.usb.OnSpecifiedDeviceRemoved += new System.EventHandler(this.usb_OnSpecifiedDeviceRemoved);
            this.usb.OnDeviceArrived += new System.EventHandler(this.usb_OnDeviceArrived);
            this.usb.OnDeviceRemoved += new System.EventHandler(this.usb_OnDeviceRemoved);
            this.usb.OnDataRecieved += new UsbLibrary.DataRecievedEventHandler(this.usb_OnDataRecieved);
            this.usb.VendorId = a1; 
            this.usb.ProductId = a2;
            this.usb.CheckDevicePresent();

    }