0

I'm using Visual Studio 2017(version 15.5.7) ,I'm going to create a UWP project which try to connect with Hid Device .Here are my codes

private async void EnumerateHidDevices()
    {
        ushort VID = 0x0416;
        ushort PID = 0x412C;
        ushort usagePage = 0x000c;
        ushort usageId = 0x0001;
        string selector = HidDevice.GetDeviceSelector(usagePage, usageId,VID,PID);
        var deviceInformation = await DeviceInformation.FindAllAsync(selector);
        if(deviceInformation.Count!=0)
        {
         try
            {
                if (deviceInformation != null && deviceInformation.Count > 0)
                {
                    HidDevice hidDevice = await HidDevice.FromIdAsync(deviceInformation[0].Id,FileAccessMode.ReadWrite);
                }
                else
                {
                    TextBox_1.Text += "" + Environment.NewLine + "Device Information=null";
                }
            }
            catch(Exception ex)
            {
                TextBox_1.Text +=""+Environment.NewLine+ "hidDevice Connect Fail!"+Environment.NewLine+ex;
            }
        }
    }

Although deviceInformation shown correct imfo. about my device,but HidDevice.FromIdAsync() return null.

I'd try to FileAccessMode.Read and ReadWrite , both were get the exception says "System.NullReferenceException: Object reference not set to an instance of an object."

I'm sure that device can connect and send report on other application,and I also had review the document of UWP Hid . How can I correctly connected with my hidDevice??

  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Diado Mar 06 '18 at 09:41
  • Sorry , maybe I have to ask how HidDevice hidDevice = await HidDevice.FromIdAsync(deviceInformation[0].Id,FileAccessMode.ReadWrite); Work corrected ? – Zsamina Mar 07 '18 at 01:36

0 Answers0