public static List <HIDDeviceAvalible> GetDevicesListByVID(int nVid)
        {
            List <HIDDeviceAvalible> devices = new List <HIDDeviceAvalible>();
            string strSearch = string.Format("vid_{0:x4}&pid_", nVid);
            Guid   gHid;

            HidD_GetHidGuid(out gHid);

            IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

            try
            {
                uint nIndex = 0;
                DeviceInterfaceData oInterface = new DeviceInterfaceData();
                oInterface.Size = Marshal.SizeOf(oInterface);

                while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, nIndex, ref oInterface))
                {
                    string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);
                    if (strDevicePath.IndexOf(strSearch) >= 0)
                    {
                        HIDDeviceAvalible device = new HIDDeviceAvalible();
                        device.Path        = strDevicePath;
                        device.Description = HIDDevice.GetProductString(device.Path);

                        int    cut       = device.Path.IndexOf("vid_");
                        string xid_start = device.Path.Substring(cut + 4);
                        device.VendorId = Convert.ToUInt16(xid_start.Substring(0, 4), 16);

                        cut              = device.Path.IndexOf("pid_");
                        xid_start        = device.Path.Substring(cut + 4);
                        device.ProductId = Convert.ToUInt16(xid_start.Substring(0, 4), 16);

                        if (!String.IsNullOrEmpty(device.Description))
                        {
                            devices.Add(device);
                        }
                    }
                    nIndex++;
                }
            }
            catch (Exception ex)
            {
                throw HIDDeviceException.GenerateError(ex.ToString());
            }
            finally
            {
                SetupDiDestroyDeviceInfoList(hInfoSet);
            }

            return(devices);
        }
Beispiel #2
0
        public bool Open(HIDDeviceAvalible Device)
        {
            bool success = true;

            SpecifiedDevice = SpecifiedDevice.OpenSpecifiedDevice(Device.Path);

            if (SpecifiedDevice == null)
            {
                success = false;
            }
            else
            {
                OnSpecifiedDeviceArrived?.Invoke(this, new EventArgs());
                if (OnDataRecieved != null)
                {
                    SpecifiedDevice.DataRecieved += new DataRecievedEventHandler(OnDataRecieved);
                }
                if (OnDataSend != null)
                {
                    SpecifiedDevice.DataSend += new DataSendEventHandler(OnDataSend);
                }
            }
            return(success);
        }