【问题标题】:Web Serial API - Uncaught (in promise) DOMException: Failed to open serial port / required member baudRate is undefinedWeb Serial API - Uncaught (in promise) DOMException: Failed to open serial port / required member baudRate is undefined
【发布时间】:2021-01-30 08:24:34
【问题描述】:

下面的代码在我的 Xubuntu 机器上工作,但现在我在 Kubuntu 上,它不再工作 - 它不会打开端口。

Arduino IDE 工作正常(可以向开发板写入代码),我可以在 Chrome 中选择设备 (Arduino Uno),但是当我尝试打开端口时代码将停止:Uncaught (in promise) DOMException: Failed to open serial port 或 @ 987654323@会出现。

const filters = [
  // Filter on devices with the Arduino Uno USB Vendor/Product IDs.
  { usbVendorId: 0x2341, usbProductId: 0x0043 },
  { usbVendorId: 0x2341, usbProductId: 0x0001 },
];

  async function getPortAndStartReading() {
    if (!portFound) {
      const port = await navigator.serial.requestPort({ filters });
      await port.open({ baudRate: 9600 }) //problem here
      reader = port.readable.getReader();
      outputStream = port.writable
      readLoop();
      if (port) {
          connectionToPortSuccessfulMessage = 'Connection successful'
          setPortFound(true)
      }
    }
  }

我已经尝试按照this 更改串口的权限,所以现在如果我运行groups user 我会得到user : user adm dialout cdrom sudo dip plugdev lpadmin lxd sambashare,但它仍然不起作用。

我还检查了chrome://device-log,看看是否可以找到任何错误,但我得到的只是有关(物理上)添加或删除 USB 设备的信息。

【问题讨论】:

  • 我在 Windows 中遇到了同样的问题,但在 OSX 上没有。
  • 在设备日志中,如果您插入和拔出设备,您是否看到:Serial->Event: Serial Device Added path=COM3, etc 和 USB->USER: USB Device added, etc 以及类似的已删除?如果我记录port.getSignals 的输出,那我会看到状态为“拒绝”且结果为“DOMException:设备已丢失”的 Promise
  • 另外,对我来说,初始连接有效,但后续连接会产生错误。

标签: javascript google-chrome webapi webusb serialapi


【解决方案1】:

我相信会员名称最近已从“baudrate”更改为“baudRate”。至少在我的情况下,从“波特率”(过去可以工作)更改为“波特率”为我修复了它。可能是 Kubuntu 使用的是旧的 chrome 版本,它需要“波特率”。

【讨论】:

  • 这为我修好了!谢谢!我现在担心还有什么变化。
猜你喜欢
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
  • 2018-04-03
  • 2020-07-10
  • 1970-01-01
相关资源
最近更新 更多