GetDevices() public method

Gets a list of connected USB devices.
public GetDevices ( ) : IEnumerable
return IEnumerable
Beispiel #1
0
    public void Check()
    {
        var devices = loader.GetDevices(vid, pid);

        signerCount = devices.Count();
        if (devices.Count() == 0)//直接移除设备
        {
            atcdevice = null;
            stream    = null;
            return;
        }
        if (atcdevice == null)//打开设备
        {
            atcdevice = devices.First();
            stream    = atcdevice.Open();
            return;
        }
        bool bFind = false;

        foreach (var d in devices)
        {
            if (d.DevicePath == atcdevice.DevicePath)
            {//same
                bFind = true;
            }
        }
        //找不到设备了
        if (!bFind)
        {
            atcdevice = null;
            stream    = null;
        }
    }
Beispiel #2
0
    /**
     * Get a handle for USB device file
     * @param filename the name of the file OR vendor and device ids formatted as "vid&pid"
     * @param report_size [optional] report size in bytes
     * @return open read/write Stream
     */
    public override Stream GetUSBHandle(string filename, int report_size)
    { 
        HidDeviceLoader loader = new HidDeviceLoader();
        int vid = 0;
        int pid = 0;
        if (filename.IndexOf("&") > 0) {
            String[] parts = filename.Split(new Char[]{'&'});
            vid = Convert.ToInt32(parts[0]);
            pid = Convert.ToInt32(parts[1]);
        } else {
            System.Console.WriteLine("Invalid device specification: " + filename);
            return null;
        }

        HidDevice dev = loader.GetDeviceOrDefault(vid, pid, null, null);
        if (dev == null) {
            System.Console.WriteLine("Could not find requested device: " + filename);
            var devices = loader.GetDevices().ToArray();
            foreach (HidDevice d in devices) {
                System.Console.WriteLine(d);
            }
            return null;
        }

        HidStream stream;
        if (!dev.TryOpen(out stream)) {
            System.Console.WriteLine("Found requested device but cannot connect: " + filename);
            return null;
        }

        return stream;
    }
Beispiel #3
0
    public static int CheckDevice()
    {
        var devices     = loader.GetDevices(0x0483, 0x572b);
        var signerCount = devices.Count();

        if (devices.Count() == 0)//直接移除设备
        {
            atcdevice = null;
            stream    = null;
            return(0);
        }
        if (atcdevice == null)//打开设备
        {
            atcdevice = devices.First();
            stream    = atcdevice.Open();
            return(signerCount);
        }
        bool bFind = false;

        foreach (var d in devices)
        {
            if (d.DevicePath == atcdevice.DevicePath)
            {//same
                bFind = true;
            }
        }
        //找不到设备了
        if (!bFind)
        {
            atcdevice = null;
            stream    = null;
        }
        return(signerCount);
    }
Beispiel #4
0
        /* write config to connected devices */
        public bool Program(eeprom.eep_config config)
        {
            var loader = new HidDeviceLoader();
            var device = loader.GetDevices(0x1234, 0x9876).First();

            HidStream stream;
            if (!device.TryOpen(out stream))
                return false;
            if (!CheckBootloader(stream))
                return false;

            // convert config to a byte array buffer
            int size = Marshal.SizeOf(config);
            byte[] buffer = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(config, ptr, true);
            Marshal.Copy(ptr, buffer, 0, size);
            Marshal.FreeHGlobal(ptr);

            // write EEPROM
            //if (ExecuteHIDCommand(stream, (int)BootloaderCommands.RESET_POINTER) == null)
            //	return false;
            if (ExecuteHIDCommand(stream, (int)BootloaderCommands.WRITE_EEPROM, 0, buffer) == null)
                return false;

            return true;
        }
Beispiel #5
0
        public HidManager()
        {
            HidDeviceLoader = new HidDeviceLoader();
            FoundDevices = new ObservableCollection<HidDeviceRepresentation>();

            _hidScanThread = new Thread(() =>
            {
                while (true)
                {
                    var currentDevices = HidDeviceLoader.GetDevices().Select(hidDevice => new HidDeviceRepresentation(hidDevice)).ToList();

                    if (!Thread.CurrentThread.IsAlive) { break; }

                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        foreach (var newDevice in currentDevices.Where(device => !FoundDevices.Contains(device)))
                        {
                            FoundDevices.Add(newDevice);
                        }
                    });

                    Thread.Sleep(TimeSpan.FromSeconds(10));
                }
                // ReSharper disable once FunctionNeverReturns
            });
            _hidScanThread.Start();
        }
Beispiel #6
0
        /* read config from first connected device */
        /* returns null on failure */
        public config Read()
        {
            var loader = new HidDeviceLoader();
            var device = loader.GetDevices(0x1234, 0x9876).First();

            HidStream stream;
            if (!device.TryOpen(out stream))
                return null;
            if (!CheckBootloader(stream))
                return null;

            // read EEPROM
            var result = ExecuteHIDCommand(stream, (int)BootloaderCommands.READ_EEPROM, 0);
            if (result == null)
                return null;

            if (result.Length != (4 + 32))	// header + one EEPROM page
            {
                Console.WriteLine("Incorrect size response from bootloader.");
                return null;
            }

            // marshall the bytes from result into a new eep_config object
            var eep = new eeprom.eep_config();
            int size = Marshal.SizeOf(eep);
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.Copy(result, 4, ptr, size);
            eep = (eeprom.eep_config)Marshal.PtrToStructure(ptr, typeof(eeprom.eep_config));
            Marshal.FreeHGlobal(ptr);

            // test EEPROM config and convert to config
            string error_message;
            var cfg = eeprom.DecodeEEPROMConfig(eep, out error_message);
            if (cfg == null)
            {
                Console.WriteLine(error_message);
                return null;
            }

            return cfg;
        }
Beispiel #7
0
 //internal static bool TestKeyboard()
 //{
 //    //FIND WHETHER A USB KEYBOARD IS PLUGGED IN
 //    var hd = new HidDeviceLoader();
 //    if (hd.GetDevices().Any(item => item.ProductName.Contains("Keyboard")))
 //        return true;
 //    //FIND WHETHER A PS/2 KEYBOARD IS PLUGGED IN.
 //    const string query = "select * from Win32_Keyboard";
 //    var oQuery = new ObjectQuery(query);
 //    var searcher = new ManagementObjectSearcher(oQuery);
 //    var recordSet = searcher.Get();
 //    return
 //        recordSet.Cast<ManagementObject>()
 //            .Any(record => record.Properties["Description"].Value.ToString().Contains("Keyboard"));
 //}
 internal static bool TestCherryKeyboard()
 {
     var hd = new HidDeviceLoader();
     return hd.GetDevices().Any(item => item.VendorID == 1130);
 }
        public override bool Open(string devicePath)
        {
            var loader = new HidDeviceLoader();

            // search for HID
            _currentHidDevice = loader.GetDevices(VendorId, ProductId).FirstOrDefault();

            if (_currentHidDevice == null)
            {
                Log.ErrorFormat("Couldn't find device with VID: {0}, PID: {1}",
                    VendorId, ProductId);
                return false;
            }

            // open HID
            if (!_currentHidDevice.TryOpen(out _currentHidStream))
            {
                Log.ErrorFormat("Couldn't open device {0}", _currentHidDevice);
                return false;
            }

            // since these devices have no MAC address, generate one
            DeviceAddress = PhysicalAddress.Parse(MacAddressGenerator.NewMacAddress);

            IsActive = true;
            Path = devicePath;

            return IsActive;
        }
Beispiel #9
0
        /// <summary>
        /// Initializes communication with the DTS Card
        /// </summary>
        /// <returns>True if update is sucessful, false if there was a problem.</returns>
        public bool initialize()
        {
            if (_device == null) 
            {
                dispose();
            }

            HidDeviceLoader deviceLoader = new HidDeviceLoader();
            foreach (HidDevice device in deviceLoader.GetDevices(0x04d8, 0xf64e))
            {
                if (device.SerialNumber.Equals(SerialNumber))
                {
                    _device = device;
                    break;
                }
            }

            if (_device != null)
            {
                _device.TryOpen(out _stream);
            }

            return _stream != null;
        }
Beispiel #10
0
        /// <summary>
        /// Attempts to connect to a BlinkStick device.
        /// 
        /// After a successful connection, a DeviceAttached event will normally be sent.
        /// </summary>
        /// <returns>True if a Blinkstick device is connected, False otherwise.</returns>
        public bool OpenDevice()
        {
            bool result;

            this._VersionMajor = -1;
            this._VersionMinor = -1;

            if (this.device == null) {
                HidDeviceLoader loader = new HidDeviceLoader();
                HidDevice adevice = loader.GetDevices(VendorId, ProductId).FirstOrDefault();
                result = OpenDevice (adevice);
            } else {
                result = OpenCurrentDevice();
            }

            CheckRequiresSoftwareColorPatch();

            return result;
        }
Beispiel #11
0
        /// <summary>
        /// Find all BlinkStick devices.
        /// </summary>
        /// <returns>An array of BlinkStick devices</returns>
        public static BlinkStick[] FindAll()
        {
            List<BlinkStick> result = new List<BlinkStick>();

            HidDeviceLoader loader = new HidDeviceLoader();
            foreach (HidDevice adevice in loader.GetDevices(VendorId, ProductId).ToArray())
            {
                BlinkStick hid = new BlinkStick();
                hid.device = adevice;
                result.Add(hid);
            }

            return result.ToArray();
        }
        /// <summary>
        /// Initializes the dongle to receive buzzer inputs. Uses HidSharp Library.
        /// </summary>
        public void InitWBuzzReceiver()
        {
            // Looks for the Device according to its VID and PID, then tries to open a stream to write to it
            var loader = new HidDeviceLoader();
            var device = loader.GetDevices(0x054C, 0x1000).First();
            HidStream stream;
            device.TryOpen(out stream);

            // Sends a 7-byte message to make it ready to communicate with the buzzers
            var message = new byte[] { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            stream.Write(message);
            stream.Close();
        }