コード例 #1
0
        private void ScanLoop()
        {
            var culture = CultureInfo.InvariantCulture;

            Thread.CurrentThread.CurrentCulture   = culture;
            Thread.CurrentThread.CurrentUICulture = culture;

            // The read has a timeout parameter, so every X milliseconds
            // we check if the user wants us to continue scanning.
            while (asyncScanOn)
            {
                try
                {
                    IntPtr device_info   = HidApi.hid_enumerate(vendorId, productId);
                    bool   device_on_bus = device_info != IntPtr.Zero;
                    // freeing the enumeration releases the device,
                    // do it as soon as you can, so we dont block device from others
                    HidApi.hid_free_enumeration(device_info);
                    if (device_on_bus && !deviceConnected)
                    {
                        // just found new device
                        deviceConnected = true;
                        if (DeviceArrived != null)
                        {
                            DeviceArrived(this, EventArgs.Empty);
                        }
                    }
                    if (!device_on_bus && deviceConnected)
                    {
                        // just lost device connection
                        deviceConnected = false;
                        if (DeviceRemoved != null)
                        {
                            DeviceRemoved(this, EventArgs.Empty);
                        }
                    }
                }
                catch (Exception e)
                {
                    // stop scan, user can manually restart again with StartAsyncScan()
                    asyncScanOn = false;
                }
                // when read 0 bytes, sleep and read again
                Thread.Sleep(ScanIntervalInMillisecs);
            }
        }
コード例 #2
0
 // scanning for device when it is open by another process will return false
 public static bool ScanOnce(ushort vid, ushort pid)
 {
     return(HidApi.hid_enumerate(vid, pid) != IntPtr.Zero);
 }