c# - 发送 HidOutputReport 失败
问题描述
我想通过隐藏将输出报告发送到设备。
var device = await HidDevice.FromIdAsync(deviceDescriptor.Id, Windows.Storage.FileAccessMode.ReadWrite);
HidOutputReport outReport = device.CreateOutputReport();
byte[] buffer = new byte[] { 10, 20, 30, 40 };
DataWriter dataWriter = new DataWriter();
dataWriter.WriteBytes(buffer);
outReport.Data = dataWriter.DetachBuffer();
但它异常失败。
Message:
System.ArgumentException : Value does not fall within the expected range.
Stack Trace:
HidOutputReport.put_Data(IBuffer value)
它有什么问题?代码是从文档中的示例复制而来的。
如果新的缓冲区长度与旧的 outReport.Data.Length 匹配,那么我在
await device.SendOutputReportAsync(outReport);
// System.ArgumentException: 'Value does not fall within the expected range.'
StackTrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MyMethod()
我正在尝试在 xunit 测试中运行此方法。
解决方案
原来缓冲区中的第一个字节是reportId,它不能是随机的。将第一个字节设置为更正 Id 后,一切正常。
此外,buffer.Length 必须与 outReport.Data 的容量完全相同。
推荐阅读
- python - 使用 Regex 和 python 解析代理地址
- python - “无法执行子进程子进程'software-properties-gtk'(没有这样的文件或目录)”发生
- java - 如何将 MVVM 与 App/activity 和 AsyncTask 的 UI 组件一起使用
- javascript - 删除父元素和下一个直到元素
- html - 内联块元素中的意外填充
- javascript - 隐藏滚动条并制作自定义滚动条
- java - 从模数和指数重建 RSA 私钥失败
- ios - Swift 第三方库混淆 Apple 允许吗?
- c++ - 数组指针符号 C++(移动数组时)
- python - 来自两个来源的列表理解