2017-08-23 5 views
0

J'utilise un lecteur de code-barres (fonctionne comme HID). Si je démarre mon application, rien ne se passe, le gestionnaire OnDataReceived ne fonctionne pas. Si je supprime le lecteur de code-barres, usb_OnDeviceRemoved est appelé (il suit tous les périphériques USB), après le branchement - usb_OnDeviceArrived et après usb_OnSpecifiedDeviceArrived sont appelés, après que je reçois des données.USB OnDataReceived appelle après avoir reconnecté le périphérique

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

Sur 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 
    } 
    } 

Et à partir du fichier 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()); 
     } 
    } 

Pourquoi dois-je reconnecter un périphérique USB, si je l'appelle CheckDevicePresent() lorsque le formulaire se charge !?

Répondre

0

Le problème était que j'appelais CheckDevicePresent() avant de créer des gestionnaires d'événements.

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

    }