Share via


How to read data from HID barcode scanner?

Question

Thursday, August 24, 2017 11:15 AM

The problem is that my code doesn't wont to run on some PCs. I am using the same barcode scanner on each computer and 3 of 5 fail to receive data. (They have same OS - Win7, same model - HP and using same USB port)

My code:

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

On form load:

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; //parsed from xml
            this.usb.ProductId = a2; //parsed from xml
            this.usb.CheckDevicePresent();

    }

Methods:

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

A part from DLL (UsbLibrary c#) that I found:

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

On devices where my program runs good - usb_OnSpecifiedDeviceArrived is called first and after it I can receive data. On other - I dont get usb_OnSpecifiedDeviceArrived called until I reconnect USB scanner (usb_OnDeviceRemoved -> usb_OnDeviceArrived -> usb_OnSpecifiedDeviceArrived  are called) but usb_OnDataReceived doesn't work when I scan barcode. Why is that?