1

我正在尝试运行此 WebUSB 演示: https ://developers.google.com/web/updates/2016/03/access-usb-devices-on-the-web

我使用的是 SparkFun Pro Micro,所以我将 vendorId 更改为 0x1b4f,当我调用它时它成功显示在弹出窗口中

device = await navigator.usb.requestDevice({ filters: [{ vendorId: 0x1b4f }] })

选择设备并按连接后,我收到此错误:DOMException: 设备不可用

将 Pro Micro 插入 USB 端口(位于 /dev/ttyACM0 ),并在其上加载了演示 Arduino 代码,该代码在连接时发送串行握手(参见上面的演示链接)

我还编辑了文件 ArduinoRawHID.rules 以包含 0x1b4f 并将其放在 /etc/udev/rules.d/

有什么明显我在这里失踪的吗?

<html>
<body>
<button id="request-device">Click me</button>

<script>
var device;

let button = document.getElementById('request-device');
button.addEventListener('click', async () => {

    device = await navigator.usb.requestDevice({ filters: [{ vendorId: 0x1b4f }] })
.then(selectedDevice => {
   device = selectedDevice;
   return device.open(); // Begin a session.
 })
.then(() => device.selectConfiguration(1)) // Select configuration #1 for the device.
.then(() => device.claimInterface(2)) // Request exclusive control over interface #2.
.then(() => device.controlTransferOut({
    requestType: 'class',
    recipient: 'interface',
    request: 0x22,
    value: 0x01,
    index: 0x02})) // Ready to receive data
.then(() => device.transferIn(5, 64)) // Waiting for 64 bytes of data from endpoint #5.
.then(result => {
  let decoder = new TextDecoder();
  console.log('Received: ' + decoder.decode(result.data));
})
.catch(error => { console.log(error); });

});
</script>
</html>

---编辑我正在尝试一个不同的演示,它说 Pro Micro 已配对但我收到“连接错误:SecurityError:访问被拒绝。”

        function serialConnect() {
            console.log('Connecting to ' + serialPort.device_.productName + '...');
            serialPort.connect().then(() => {
                console.log(serialPort);
                console.log('Connected.');
                document.getElementById("divStepperStatus").innerHTML = "Stepper connected";
                serialPort.send(textEncoder.encode('\n'));// 1st command will be lost anyway
                serialPort.onReceive = data => {
                    let textDecoder = new TextDecoder();
                    var decodedStr = textDecoder.decode(data);
                    var splitedsStr = decodedStr.trim().split(" ");
                    var recongizedString = false;
                    if (splitedsStr[0] && splitedsStr[0] == 'S') {
                        if (splitedsStr[1]) {
                            var leftSteps = parseInt(splitedsStr[1]);
                            recongizedString = true;
                            document.getElementById("divStepperSteps").innerHTML = 'Steps: ' + leftSteps;
                        }
                    }
                    if (!recongizedString) console.log(textDecoder.decode(data));
                }
                serialPort.onReceiveError = error => {
                    console.log('Receive error: ' + error);
                };
            }, error => {
                console.log('Connection error: ' + error);
            });
        };

我还对 USBCore.h 进行了 #define USB_VERSION 0x210 修改

我还缺少什么会导致SecurityError的东西?

4

0 回答 0