コード例 #1
0
ファイル: HIDUSBDevice.cs プロジェクト: bryan610/Channel9
        //---#+************************************************************************
        //---NOTATION:
        //-  bool searchDevice()
        //-
        //--- DESCRIPTION:
        //--  tries to find the device with specified vendorID and productID
        //                                                             Autor:      F.L.
        //-*************************************************************************+#*
        /// <summary>
        /// Searches the device with soecified vendor and product id an connect to it.
        /// </summary>
        /// <returns></returns>
        private bool searchDevice()
        {
            //no device found yet
            bool deviceFound = false;

            this.deviceCount = 0;
            this.devicePath  = string.Empty;

            myUSB.CT_HidGuid();
            myUSB.CT_SetupDiGetClassDevs();

            int result       = -1;
            int resultb      = -1;
            int device_count = 0;
            int size         = 0;
            int requiredSize = 0;

            //search the device until you have found it or no more devices in list
            while (result != 0)
            {
                //open the device
                result = myUSB.CT_SetupDiEnumDeviceInterfaces(device_count);
                //get size of device path
                resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetail(ref requiredSize, 0);
                size    = requiredSize;
                //get device path
                resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetailx(ref requiredSize, size);

                //is this the device i want?
                string deviceID = this.vendorID + "&" + this.productID;
                if (myUSB.DevicePathName.IndexOf(deviceID) > 0)
                {
                    //yes it is

                    //store device information
                    this.deviceCount = device_count;
                    this.devicePath  = myUSB.DevicePathName;
                    deviceFound      = true;

                    //init device
                    myUSB.CT_SetupDiEnumDeviceInterfaces(this.deviceCount);

                    size         = 0;
                    requiredSize = 0;

                    resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetail(ref requiredSize, size);
                    resultb = 0;
                    resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetailx(ref requiredSize, size);
                    resultb = 0;
                    //create HID Device Handel
                    resultb = myUSB.CT_CreateFile(this.devicePath);

                    //we have found our device so stop searching
                    break;
                }
                device_count++;
            }

            //set connection state
            this.setConnectionState(deviceFound);
            //return state
            return(this.getConnectionState());
        }