gpt4 book ai didi

usb - 如何在 Windows 10 通用应用程序中使用键盘支持从磁条阅读器读取输入流

转载 作者:行者123 更新时间:2023-12-05 07:52:02 24 4
gpt4 key购买 nike

我们已经尝试了建议的方法:

https://msdn.microsoft.com/en-us/library/windows/hardware/dn312121(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/hardware/dn303343(v=vs.85).aspx

我们可以使用下面的代码片段找出所有磁性设备的列表

var magneticDevices = await DeviceInformation.FindAllAsync(aqsFilter);

但是我们无法从下面的代码中获取 HidDevice 对象。它给出空值。

HidDevice device = await HidDevice.FromIdAsync(magneticDevices[0].Id

我们还在应用 list 文件中设置了设备功能,如下所示。

<DeviceCapability Name="humaninterfacedevice">
<Device Id="vidpid:0ACD 0520">
<Function Type="usage:0001 0006"/>
</Device>
</DeviceCapability>

<DeviceCapability Name="usb">
<Device Id="vidpid:0ACD 0520">
<Function Type="winUsbId:4d1e55b2-f16f-11cf-88cb-001111000030"/>
</Device>
</DeviceCapability>

完整函数的代码

 private async Task<bool> HasCardReader()
{
bool hasCardReader = false;
ushort usagePage = 0x0001;
ushort usageId = 0x0006;
ushort vendorId = 0x0ACD;
ushort productId = 0x0520;
var aqsFilter = HidDevice.GetDeviceSelector(usagePage, usageId, vendorId, productId);
var magneticDevices = await DeviceInformation.FindAllAsync(aqsFilter);
try
{
if (magneticDevices != null && magneticDevices.Count > 0)
{
HidDevice device = await HidDevice.FromIdAsync(magneticDevices[0].Id, Windows.Storage.FileAccessMode.Read);
inputReportEventHandler = new TypedEventHandler<HidDevice, HidInputReportReceivedEventArgs>(this.OnInputReportEvent);
device.InputReportReceived += inputReportEventHandler;
var watcher = DeviceInformation.CreateWatcher(aqsFilter);
watcher.Added += WatcherAdded;
watcher.Removed += WatcherRemoved;
watcher.Start();
hasCardReader = true;
}
else
{

}
}
catch (Exception ex)
{
Logging.LoggingSessionScenario.LogMessageAsync(ex.Message, LoggingLevel.Error);
}
return hasCardReader;
}

最佳答案

空返回值有多种原因,但我认为您的代码没有问题,因为您可以通过调用 FindAllAsync 找到设备。我建议您使用 this official HIDDevice sample 解决此问题在 GitHub 上。

通过将 vid & pid & usagepage & usageid 更改为我的设备,我使用该示例成功连接到我的外部 hid 设备。

在EventHandlerForDevice.cs中,找到函数OpenDeviceAsync,当FromIdAsync返回null时,你会注意到以下可能的原因。

else
{
successfullyOpenedDevice = false;

notificationStatus = NotifyType.ErrorMessage;

var deviceAccessStatus = DeviceAccessInformation.CreateFromId(deviceInfo.Id).CurrentStatus;

if (deviceAccessStatus == DeviceAccessStatus.DeniedByUser)
{
notificationMessage = "Access to the device was blocked by the user : " + deviceInfo.Id;
}
else if (deviceAccessStatus == DeviceAccessStatus.DeniedBySystem)
{
// This status is most likely caused by app permissions (did not declare the device in the app's package.appxmanifest)
// This status does not cover the case where the device is already opened by another app.
notificationMessage = "Access to the device was blocked by the system : " + deviceInfo.Id;
}
else
{
// Most likely the device is opened by another app, but cannot be sure
notificationMessage = "Unknown error, possibly opened by another app : " + deviceInfo.Id;
}
}

尝试使用该示例 (Scenario1) 并更改 appxmanifest 和 SampleConfiguration.cs(class Device) 中的 ID。如果您在设备列表中看不到您的设备,则表示您的设备配置不正确。

关于usb - 如何在 Windows 10 通用应用程序中使用键盘支持从磁条阅读器读取输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com