HidDevice.FromIdAsync以FileAccessMode.ReadWrite模式无法打开设备,但是FileAccessMode.Read能打开
Sab1e
0
信誉分
我尝试使用这个页面给出的例程来打开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>
登录以回答