HidDevice.FromIdAsync以FileAccessMode.ReadWrite模式无法打开设备,但是FileAccessMode.Read能打开

Sab1e 0 信誉分
2024-11-04T20:08:05.4333333+00:00

我尝试使用这个页面给出的例程来打开HID设备,但是FromIdAsync返回了一个null,

HidDevice device = await HidDevice.FromIdAsync(devices.ElementAt(0).Id,FileAccessMode.ReadWritwe);

经过我的测试,使用FileAccessMode.Read时可以正常打开我的设备,这是为什么?
我尝试搜索相关内容但是没有找到解决办法。

下面是运行效果:
图片

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Devices.Enumeration;
using Windows.Devices.HumanInterfaceDevice;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Core;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace App1
{
```typescript
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
    }
    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        myButton.Content = "Clicked";
        EnumerateHidDevices();
    }
    // Find HID devices.
    private async void EnumerateHidDevices()
    {
        // Microsoft Input Configuration Device.
        ushort vendorId = 0x1234;
        ushort productId = 0x0064;
        ushort usagePage = 0x0001;
        ushort usageId = 0x0004;
        // Create the selector.
        string selector =
            HidDevice.GetDeviceSelector(usagePage, usageId, vendorId, productId);
        // Enumerate devices using the selector.
        var devices = await DeviceInformation.FindAllAsync(selector);
        if (devices.Any())
        {
            // At this point the device is available to communicate with
            // So we can send/receive HID reports from it or 
            // query it for control descriptions.
            info.Text = "HID devices found: " + devices.Count + "\n";
            // Open the target HID device.
            HidDevice device =
                await HidDevice.FromIdAsync(devices.ElementAt(0).Id,
                FileAccessMode.ReadWrite);
            if (device != null)
            {
                info.Text += "\nHID OK.";
                // Input reports contain data from the device.
                device.InputReportReceived += async (sender, args) =>
                {
                    HidInputReport inputReport = args.Report;
                    IBuffer buffer = inputReport.Data;
                    // Create a DispatchedHandler as we are interracting with the UI directly and the
                    // thread that this function is running on might not be the UI thread; 
                    // if a non-UI thread modifies the UI, an exception is thrown.
                    await this.Dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        new DispatchedHandler(() =>
                        {
                            info.Text += "\nHID Input Report: " + inputReport.ToString() +
                            "\nTotal number of bytes received: " + buffer.Length.ToString();
                        }));
                };
            }
            else
            {
                info.Text += "\nFailed to connect HID Device.";
            }
        }
        else
        {
            // There were no HID devices that met the selector criteria.
            info.Text = "HID device not found";
        }
    }
}
```}

```xml


xaml
<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="App1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
        <TextBlock x:Name="info"/>
    </StackPanel>
</Window>

图片

Windows 应用 SDK
Windows 应用 SDK
一组 Microsoft 开源库、框架、组件和工具,可供各种应用使用,以便在多个版本的 Windows 上访问 Windows 平台功能。 以前称为 Project Reunion。
32 个问题
{count} 票

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。