Beispiel #1
0
        /// <summary>
        /// Sets the integer value of a Hid feature code.
        /// </summary>
        /// <param name="featureCode">The Hid feature code to set.</param>
        /// <param name="value">The value to set.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool HidSetFeature(byte featureCode, int value)
        {
            bool HidSetFeatureRet = default;
            var  hfile            = HidFeatures.OpenHid(this);

            if (hfile == IntPtr.Zero)
            {
                return(false);
            }
            var mm = new MemPtr();

            mm.Alloc(5L);
            mm.ByteAt(0L)        = featureCode;
            mm.IntAtAbsolute(1L) = value;
            if (!UsbLibHelpers.HidD_SetFeature(hfile, mm, 5))
            {
                HidSetFeatureRet = false;
            }
            else
            {
                HidSetFeatureRet = true;
            }

            HidFeatures.CloseHid(hfile);
            mm.Free();
            return(HidSetFeatureRet);
        }
Beispiel #2
0
        private void StartWatching(HidDeviceInfo d)
        {
            IntPtr h;

            int i = 0;

            if (_devThread is object)
            {
                StopWatching();
            }

            var s = new ObservableCollection <string>();

            _lastDevice = d;

            this.ViewingArea.ItemsSource = s;

            for (i = 0; i <= 255; i++)
            {
                s.Add("");
            }

            var th = new Thread(() =>
            {
                cts = new CancellationTokenSource();
                h   = HidFeatures.OpenHid(d);
                if ((long)h <= 0L)
                {
                    return;
                }
                var mm = new MemPtr(65L);
                try
                {
                    do
                    {
                        this.Dispatcher.Invoke(() =>
                        {
                            for (i = 0; i <= 255; i++)
                            {
                                mm.LongAtAbsolute(1L) = 0L;
                                mm.ByteAt(0L)         = (byte)i;
                                if (HidD_GetFeature(h, mm, 65))
                                {
                                    s[i] = "HID CODE " + i.ToString("X2") + " = " + mm.IntAtAbsolute(1L);
                                }
                            }
                        });

                        Thread.Sleep(1000);
                        if (cts is null || cts.IsCancellationRequested)
                        {
                            break;
                        }
                    }while (true);
                    mm.Free();
                    HidFeatures.CloseHid(h);
                    cts = null;
                    return;
                }
                catch (ThreadAbortException)
                {
                    mm.Free();
                    HidFeatures.CloseHid(h);
                    cts = null;
                    return;
                }
                catch (Exception)
                {
                    mm.Free();
                    HidFeatures.CloseHid(h);
                    cts = null;
                    return;
                }
            });

            th.IsBackground = true;
            th.SetApartmentState(ApartmentState.STA);
            _devThread = th;
            th.Start();
        }