基于 Web HID API 的HID透传测试工具(纯前端)

news2024/7/6 18:36:25

前言

最近再搞HID透传 《STM32 USB使用记录:HID类设备(后篇)》 。

市面上的各种测试工具都或多或少存在问题,所以就自己写一个工具进行测试。目前来说纯前端方案编写这个工具应该是最方便的,这里放上相关代码。

项目地址与代码示例

项目地址:https://github.com/NaisuXu/HID_Passthrough_Tool

在这里插入图片描述

下面代码保存到 index.html 文件,双击打开文件即可使用:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HID Passthrough Tool</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            height: 100vh;
            background-color: #f7f7ff;
        }

        div {
            height: calc(100% - 4rem);
            padding: 2rem;
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            grid-template-rows: 2rem 1fr;
            row-gap: 1rem;
            column-gap: 2rem;
        }

        textarea {
            resize: none;
            overflow-y: scroll;
            overflow-x: hidden;
            padding: 1rem;
        }
    </style>
</head>

<body>
    <div>
        <button id="btnOpen">open</button>
        <button id="btnSend">send</button>
        <button id="btnClear">clear</button>
        <textarea id="iptLog" readonly></textarea>
        <textarea id="iptOutput">D0 D1 D2 D3 D4 D5 D6 D7</textarea>
        <textarea id="iptInput" readonly></textarea>
    </div>
    <script>
        const btnOpen = document.querySelector("#btnOpen");
        const btnSend = document.querySelector("#btnSend");
        const btnClear = document.querySelector("#btnClear");
        const iptLog = document.querySelector("#iptLog");
        const iptOutput = document.querySelector("#iptOutput");
        const iptInput = document.querySelector("#iptInput");

        iptLog.value += "HID Passthrough Tool\n\n";
        iptLog.value += "This is an HID Passthrough device read/write Tool.\n\n";
        iptLog.value += "Device must have one collection with one input and one output.\n\n";
        iptLog.value += "For more detail see below:\n\n";
        iptLog.value += "https://github.com/NaisuXu/HID_Passthrough_Tool\n\n";
        iptLog.value += "《STM32 USB使用记录:HID类设备(后篇)》\nhttps://blog.csdn.net/Naisu_kun/article/details/131880999\n\n";
        iptLog.value += "《使用 Web HID API 在浏览器中进行HID设备交互(纯前端)》\nhttps://blog.csdn.net/Naisu_kun/article/details/132539918\n\n";

        let device; // 需要连接或已连接的设备
        let inputDataLength; // 发送数据包长度
        let outputDataLength; // 发送数据包长度

        // 打开设备相关操作
        btnOpen.onclick = async () => {
            try {
                // requestDevice方法将显示一个包含已连接设备列表的对话框,用户选择可以并授予其中一个设备访问权限
                const devices = await navigator.hid.requestDevice({ filters: [] });

                // const devices = await navigator.hid.requestDevice({
                //     filters: [{
                //         vendorId: 0xabcd,  // 根据VID进行过滤
                //         productId: 0x1234, // 根据PID进行过滤
                //         usagePage: 0x0c,   // 根据usagePage进行过滤
                //         usage: 0x01,       // 根据usage进行过滤
                //     },],
                // });

                // let devices = await navigator.hid.getDevices(); // getDevices方法可以返回已连接的授权过的设备列表

                if (devices.length == 0) {
                    iptLog.value += "No device selected\n\n";
                    iptLog.scrollTop = iptLog.scrollHeight; // 滚动到底部
                    return;
                }

                device = devices[0]; // 选择列表中第一个设备

                if (!device.opened) { // 检查设备是否打开
                    await device.open(); // 打开设备

                    // 下面几行代码和我的自定义的透传的HID设备有关
                    // 我的设备中有一个collection,其中有一个input、一个output
                    // inputReports和outputReports数据是Array,reportSize是8
                    // reportCount表示一包数据的字节数,USB-FS 和 USB-HS 设置的reportCount最大值不同
                    if ((device.collections[0].inputReports[0].items[0].isArray) &&
                        (device.collections[0].inputReports[0].items[0].reportSize === 8)) {
                        // 发送数据包长度必须和报告描述符中描述的一致
                        inputDataLength = device.collections[0].inputReports[0].items[0].reportCount ?? 0;
                    }
                    if ((device.collections[0].outputReports[0].items[0].isArray) &&
                        (device.collections[0].outputReports[0].items[0].reportSize === 8)) {
                        // 发送数据包长度必须和报告描述符中描述的一致
                        outputDataLength = device.collections[0].outputReports[0].items[0].reportCount ?? 0;
                    }

                    iptLog.value += `Open device: \n${device.productName}\nPID-${device.productId} VID-${device.vendorId}\ninputDataLength-${inputDataLength} outputDataLength-${outputDataLength}\n\n`;
                    iptLog.scrollTop = iptLog.scrollHeight; // 滚动到底部
                }
                // await device.close(); // 关闭设备
                // await device.forget() // 遗忘设备

                // 电脑接收到来自设备的消息回调
                device.oninputreport = (event) => {
                    console.log(event); // event中包含device、reportId、data等内容

                    let array = new Uint8Array(event.data.buffer); // event.data.buffer就是接收到的inputreport包数据了
                    let hexstr = "";
                    for (const data of array) {
                        hexstr += (Array(2).join(0) + data.toString(16).toUpperCase()).slice(-2) + " "; // 将字节数据转换成(XX )形式字符串
                    }
                    iptInput.value += hexstr;
                    iptInput.scrollTop = iptInput.scrollHeight; // 滚动到底部

                    iptLog.value += `Received ${event.data.byteLength} bytes\n\n`;
                    iptLog.scrollTop = iptLog.scrollHeight; // 滚动到底部
                };

            } catch (error) {
                iptLog.value += `${error}\n\n`;
                iptLog.scrollTop = iptLog.scrollHeight; // 滚动到底部
            }
        };

        // 发送数据相关操作
        btnSend.onclick = async () => {
            try {
                if (!device?.opened) {
                    throw "Device not opened";
                }

                const outputData = new Uint8Array(outputDataLength); // 要发送的数据包

                let outputDatastr = iptOutput.value.replace(/\s+/g, ""); // 去除所有空白字符
                if ((outputDatastr.length % 2 == 0) && (/^[0-9a-fA-F]+$/).test(outputDatastr)) { // 检查长度和字符是否正确
                    // 一包长度不能大于报告描述符中规定的长度
                    const byteLength = ((outputDatastr.length / 2) > outputDataLength) ? outputDataLength : outputDatastr.length / 2;
                    // 将字符串转成字节数组数据
                    for (let i = 0; i < byteLength; i++) {
                        outputData[i] = parseInt(outputDatastr.substr(i * 2, 2), 16);
                    }
                } else {
                    throw "Data is not even or 0-9、a-f、A-F";
                }

                await device.sendReport(0, outputData); // 发送数据,第一个参数为reportId,填0表示不使用reportId
                iptLog.value += `Send ${outputData.length} bytes\n\n`;
                iptLog.scrollTop = iptLog.scrollHeight; // 滚动到底部

            } catch (error) {
                iptLog.value += `${error}\n\n`;
                iptLog.scrollTop = iptLog.scrollHeight; // 滚动到底部
            }
        };

        // 全局HID设备插入事件
        navigator.hid.onconnect = (event) => {
            console.log("HID connected: ", event.device); // device 的 collections 可以看到设备报告描述符相关信息
            iptLog.value += `HID connected:\n${event.device.productName}\nPID ${event.device.productId} VID ${event.device.vendorId}\n\n`;
            iptLog.scrollTop = iptLog.scrollHeight; // 滚动到底部
        };

        // 全局HID设备拔出事件
        navigator.hid.ondisconnect = (event) => {
            device = null; // 释放当前设备
            iptLog.value += `HID disconnected:\n${event.device.productName}\nPID ${event.device.productId} VID ${event.device.vendorId}\n\n`;
            iptLog.scrollTop = iptLog.scrollHeight; // 滚动到底部
        };

        // 清空数据接收窗口
        btnClear.onclick = () => { iptInput.value = ""; };
    </script>
</body>

</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/974693.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

通过idea实现springboot集成mybatys

概述 使用springboot 集成 mybatys后&#xff0c;通过http请求接口&#xff0c;使得通过http请求可以直接直接操作数据库&#xff1b; 完成后端功能框架&#xff1b;前端是准备上小程序&#xff0c;调用https的请求接口用。简单实现后端框架&#xff1b; 详细 springboot 集…

qt中子窗口最小化后再恢复显示窗口区域显示为全白色

问题&#xff1a; qt中子窗口最小化后再恢复显示窗口区域显示为全白色&#xff0c;如下图&#xff1a; 原因&#xff1a; 恢复显示后窗口为及时刷新。 解决办法&#xff1a; 重写showEvent函数&#xff0c;如下&#xff1a; void MyClass::showEvent(QShowEvent *event) {se…

OS | 第5章 插叙:进程API

OS | 第5章 插叙&#xff1a;进程API 文章目录 OS | 第5章 插叙&#xff1a;进程API5.1 fork()系统调用代码过程分析 5.2 wait()系统调用5.3 exec() 系统调用执行过程 为什么这样设计API&#xff1f;shell执行过程shell 重定向pipe()>>>>> 欢迎关注公众号【三戒…

YOLOv5:解读metrics.py

YOLOv5&#xff1a;解读metrics.py 前言前提条件相关介绍metrics.pyfitnesssmoothbox_iouConfusionMatrix ★ ★ \bigstar\bigstar ★★bbox_iou ★ ★ \bigstar\bigstar ★★compute_apap_per_class&#xff08;难度&#xff1a; ⋆ ⋆ ⋆ ⋆ ⋆ \star\star\star\star\star ⋆…

openpnp - 底部相机高级矫正后,底部相机看不清吸嘴的解决方法

文章目录 openpnp - 底部相机高级矫正后,底部相机看不清吸嘴的解决方法概述解决思路备注补充 - 新问题 - N1吸嘴到底部相机十字中心的位置差了很多END openpnp - 底部相机高级矫正后,底部相机看不清吸嘴的解决方法 概述 自从用openpnp后, 无论版本(dev/test), 都发现一个大概…

mac建议装双系统吗,详细分析苹果电脑双系统的利弊

mac建议装双系统吗&#xff0c;Mac电脑上安装双系统有哪些利弊呢&#xff0c;一起来看看吧&#xff01; 苹果Mac电脑安装双系统利&#xff1a; 1、用来办公更加方便&#xff1a;苹果系统功能也是很强大的&#xff0c;但是用来办公非常不方便&#xff0c;是由于一些常用的exe软…

Error: The project seems to require yarn but it‘s not installed.

把之前做过的vue项目拷贝到新电脑上&#xff0c;运行启动命令后发现报了如下错误&#xff1a; 我是这么解决的&#xff1a; 是因为项目中存在yarn.lock 文件&#xff0c;先把这个文件删除掉。 把这个文件删除后&#xff0c;执行如下命令&#xff1a; npm install -g yarn 下…

Docker Desktop 设置镜像环境变量

点击run 展开Optional settings container name &#xff1a;容器名称 Ports&#xff1a;根据你需要的端口进行输入&#xff0c;不输入则默认 后面这个 比如我这个 5432 Volumes&#xff1a;卷&#xff0c;也就是做持久化 需要docker 数据保存的地方 Environment variables…

TCP的滑动窗口协议有什么用?

分析&回答 滑动窗口协议&#xff1a; TCP协议的使用维持发送方/接收方缓冲区 缓冲区是 用来解决网络之间数据不可靠的问题&#xff0c;例如丢包&#xff0c;重复包&#xff0c;出错&#xff0c;乱序 在TCP协议中&#xff0c;发送方和接受方通过各自维护自己的缓冲区。通…

react16之前diff算法的理解和总结

此篇文章所讨论的是 React 16 以前的 Diff 算法。而 React 16 启用了全新的架构 Fiber&#xff0c;相应的 Diff 算法也有所改变&#xff0c;本片不详细讨论Fiber。 fiber架构是为了支持react进行可中断渲染&#xff0c;降低卡顿&#xff0c;提升流畅度。 react16之前的版本&…

什么是standard cell (标准单元) ?

参考文章&#xff1a; 聊一聊芯片后端的标准单元-standard cell - 知乎 (zhihu.com) standard cell中的7T和9T中的"T"指的是什么&#xff1f;或者是什么的缩写&#xff1f; - Layout讨论区 - EETOP 创芯网论坛 (原名&#xff1a;电子顶级开发网) - 数字后端基本概念介…

qemu-system-x86_64 命令创建虚拟机,报gtk initialization failed的

因为是ssh命令行启动&#xff0c;增加--nographic # /opt/debug/bin/qemu-system-aarch64 -machine virt-6.2 -qmp tcp:localhost:1238,server,nowait --nographic configure accelerator virt-6.2 start machine init start cpu init start add rom file: virtio-net-pci…

GIT实战篇,教你如何使用GIT可视化工具

系列文章目录 手把手教你安装Git&#xff0c;萌新迈向专业的必备一步 GIT命令只会抄却不理解&#xff1f;看完原理才能事半功倍&#xff01; 快速上手GIT命令&#xff0c;现学也能登堂入室 GIT实战篇&#xff0c;教你如何使用GIT可视化工具 系列文章目录一、GIT有哪些常用工具…

漆包线工厂云MES解决方案

漆包线行业老板痛点&#xff1a; 1.漆包线比较传统的行业&#xff0c;一般都是靠人工去管理&#xff0c;老板想及时知道工厂的生产&#xff0c;销售、出入库、库存情况&#xff1b; 2.型号多称重打印易错&#xff0c;没有系统前 &#xff1a;称重打印&#xff0c;出入库&#x…

解决github连接不上的问题

改 hosts 我们在浏览器输入 GitHub 的网址时&#xff0c;会向 DNS 服务器发送一个请求&#xff0c;获取到 GitHub 网站所在的服务器 IP 地址&#xff0c;从而进行访问。 就像你是一名快递员&#xff0c;在送快递前要先找中间人询问收件人的地址。而 DNS 就是这个告诉你目标地址…

java开发之个微机器人的二次开发

简要描述&#xff1a; 取消消息接收 请求URL&#xff1a; http://域名地址/cancelHttpCallbackUrl 请求方式&#xff1a; POST 请求头Headers&#xff1a; Content-Type&#xff1a;application/json 参数&#xff1a; 参数名类型说明codestring1000成功&#xff0c;1…

顶尖211“小清华”!强过985,不要错过它!

一、学校及专业介绍 西安电子科技大学&#xff08;Xidian University&#xff09;&#xff0c;简称“西电” &#xff0c;位于陕西省西安市&#xff0c;是中央部属高校&#xff0c;直属于教育部&#xff0c;为全国重点大学&#xff0c;位列国家“双一流”“211工程”&#xff…

宝塔面板开心版出问题升级到正版的解决方案,有效解决TypeError: ‘NoneType‘ object is not subscriptable

服务器之前图开心装了个宝塔面板的开心版&#xff0c;前几天突然出现问题&#xff0c;报错TypeError: ‘NoneType’ object is not subscriptable。没法正常打开软件商店&#xff0c;也没法修复和升级系统&#xff0c;很烦躁。因为里面很多业务还在跑&#xff0c;实在不想重装。…

呜呜呼呼无无话

姓名和手机号脱敏 function nameDesen(value) {if (!value) return return value.substring(0, 1) new Array(value.length).join(*) } const bklnameDesen(宝矿力) console.log(bkl) //宝**function telephoneDesen(value) {if (!value) return value value.toString()ret…

伦敦金投资中挂单的优势

在伦敦金交易中&#xff0c;其实挂单具有无可比拟的优势&#xff1f;首先&#xff0c;如果我们不是全职的投资者&#xff0c;我们在交易时间内可能有其他的工作&#xff0c;那么挂单就可以帮助我们捕捉到市场的机会&#xff0c;即便我们不一定会坐在电脑面前&#xff0c;也可以…