Author Topic: handling Hid Device insertion and removal  (Read 17975 times)

kittu

  • Member
  • ***
  • Posts: 16
handling Hid Device insertion and removal
« on: August 04, 2010, 06:41:49 am »
Hi All !!

  I want to handle my hid device insertion and removal , i found it in VC++
using WM_DEVICECHANGE ,DBT_DEVICEARRIVAL and  DBT_DEVICEREMOVECOMPLETE
but i want in VB 6.0
can any body knows it?
pls reply...
         

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: handling Hid Device insertion and removal
« Reply #1 on: August 04, 2010, 09:29:35 am »
A web search on:

WM_DEVICECHANGE "vb6"

turns up some pages with example code.

Jan

jcddcjjcd

  • Member
  • ***
  • Posts: 14
Re: handling Hid Device insertion and removal
« Reply #2 on: August 09, 2010, 05:31:12 am »
I originally used that method, but it is possible to write to a device between when it is disconnected and when you are told it has.
In all my applications I do 10 reads with a timeout of 500mS each and then I send a couple of bytes to the device. If you are connected the write will succeed. If it does not succeed then the software goes into a loop trying to send again each second until it is connected. Even after days it will immediately continue when a device is plugged in.
I have devices that have been running for years like this and it has been very reliable.
Just an alternative way of doing it.
Regards,
John.

jcddcjjcd

  • Member
  • ***
  • Posts: 14
Re: handling Hid Device insertion and removal
« Reply #3 on: August 10, 2010, 08:55:54 pm »
This is how I typically do it.
Regards,
John.   



 //This is a BackgroundWorker Thread
      private void bwkUSB_DoWork(object sender, DoWorkEventArgs e)
        {

            byte[] testarray = new byte[2];
            bool weareconnected = false;

            do //Until StopUSBRead
            {
                do //Until weareconnected
                {
                   
                    if (!hid1.MyDeviceDetected)
                        hid1.FindTheHid();

                    if (hid1.MyDeviceDetected)
                    {
                        //if we can write then we are connected
                  //the device knows to ignore this data
                        testarray[0] = Convert.ToByte(hid_interface.COMMANDenum.areWeConnectedenum); //no responce from this
                        testarray[1] = 0; //data bytes to follow
                        if (hid1.OutputToDevice(testarray)) //true for successfull write
                        {
                            weareconnected = true;
                        }
                        else
                        {
                            weareconnected = false;
                            hid1.MyDeviceDetected = false;
                            mfref.AccessFormMarshal("rtbMonitor", "We are not connected, will try again:", Color.Black);
                            //Thread.Sleep(2000);
                        }
                    }
                    else
                    {
                        mfref.AccessFormMarshal("rtbMonitor", "Failled to connect, will try again:", Color.Black);
                        Thread.Sleep(2000); //before we try again
                    }



                    if (hid1.StopUSBRead)
                    {
                        break;
                    }

                } while (!weareconnected && !hid1.StopUSBRead);

                if (hid1.StopUSBRead)
                    break;

                for (int times = 0; times < 10; times++) //only check connection every so many loops
                {
                    byte[] receivearray = new byte[hid1.Capabilities.InputReportByteLength];
                    //a small read timeout so that we can check for sends more often
                    if (hid1.InputFromDevice(ref receivearray, 200)) //values less than 10 dont mean much
                    {
                        times = 0; //reset it as we are obviously connected
                        //Do something with the data



                        AccessFormMarshal("rtbMonitor", "Some Data was Received.", Color.Black);

                    }
                    //else
                    //{
                    //    AccessFormMarshal("rtbMonitor", "No Data was Received.", Color.Black);
                    //}

                    if (hid1.StopUSBRead) //so that other code can inform this tread to stop
                    {
                        Debug.WriteLine("bwkUSB Stopped");
                        break;                     
                    }
                }


            } while (!hid1.StopUSBRead);

            hid1.StopUSBRead = false;
            mfref.AccessFormMarshal("rtbMonitor", "BackgroundWorker bwkUSB is closing.", Color.Black);


        }

kittu

  • Member
  • ***
  • Posts: 16
Re: handling Hid Device insertion and removal
« Reply #4 on: August 11, 2010, 05:10:50 am »
Thanks a lot both of you ... I have got the code and its working....

Thanks again...  :)