I am using barcode reader (works like HID). If I start my app, nothing happens, OnDataReceived
handler doesn't work. If I remove barcode reader, usb_OnDeviceRemoved
is called (it tracks all usb devices), after plugging in - usb_OnDeviceArrived
and after usb_OnSpecifiedDeviceArrived
are called, after that I receive data.
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 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
}
}
And from DLL file:
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());
}
}
Why do I have to reconnect usb device, if I call CheckDevicePresent() when form loads!?
The problem was that I was calling CheckDevicePresent() before creating event handlers.
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();
}