【发布时间】:2021-07-02 11:25:06
【问题描述】:
我在 Chrome 中使用 WebHID 与支持 USB 的数字秤进行通信。我可以连接到体重秤并订阅体重数据流,如下所示:
// Get a reference to the scale.
// 0x0922 is the vendor of my particular scale (Dymo).
let device = await navigator.hid.requestDevice({filters:[{vendorId: 0x0922}]});
// Open a connection to the scale.
await device[0].open();
// Subscribe to scale data inputs at a regular interval.
device[0].addEventListener("inputreport", event => {
const { data, device, reportId } = event;
let buffArray = new Uint8Array(data.buffer);
console.log(buffArray);
});
我现在收到Uint8Array(5) [2, 12, 255, 0, 0] 格式的常规输入,其中第四个位置是重量数据。如果我把东西放在秤上,它会变成Uint8Array(5) [2, 12, 255, 48, 0],即 4.8 磅。
我想将秤归零(去皮),使其当前的受阻状态成为新的归零点。成功归零后,我希望秤再次开始返回Uint8Array(5) [2, 12, 255, 0, 0]。我目前对此的最佳猜测是:
device[0]
.sendReport(0x02, new Uint8Array([0x02]))
.then(response => { console.log("Sent output report " + response) });
这是基于HID Point of Sale Usage Tables 中的下表:
第一个字节是报告 ID,根据表格为 2。对于第二个字节,我希望 ZS 操作设置为 1,因此设置为 00000010,因此也设置为 2。sendReport 将报告 ID 作为第一个参数,并将所有后续数据的数组作为第二个参数。当我将它发送到设备时,它不会被拒绝,但不会将比例归零,并且response 未定义。
如何使用 WebHID 将此比例归零?
【问题讨论】:
-
您可以尝试使用 sendFeatureReport() 代替 sendReport() 吗?通常通过功能报告配置设备。
-
看看我再次发布的图,你是绝对正确的——它说的是示例比例控制功能报告。我刚刚尝试使用
sendFeatureReport代替sendReport发送相同的命令,但我仍然没有得到我预期的结果。不过,这是我需要研究的一个新领域!
标签: google-chrome usb hid webhid