2017-08-23 92 views
0

我正在使用条形码读取器(像HID一样工作)。如果我启动我的应用程序,没有任何反应,OnDataReceived处理程序不起作用。如果我删除条形码读取器,usb_OnDeviceRemoved被称为(它跟踪所有USB设备),插入后 - usb_OnDeviceArrivedusb_OnSpecifiedDeviceArrived之后被调用,之后,我接收数据。重新连接设备后,USB OnDataReceived正在呼叫

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()); 
     } 
    } 

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

回答

0

问题是我在创建事件处理程序之前调用了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(); 

    }