WebUSB串行连接-DOMException:设备不可用

时间:2019-04-13 21:19:56

标签: javascript google-chrome raspberry-pi webusb

我正在尝试运行此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 }] })

选择设备并按connect后,出现以下错误: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吗?

0 个答案:

没有答案