Web串行API可读流代码不能很好地工作

发布于 2025-01-13 08:53:46 字数 439 浏览 5 评论 0原文

 while (true) {
    let { value, done } = await reader.read();
    if (done) {
      // |reader| has been canceled.
      console.log("brack");
      break;
    }
    console.log(value);
    console.log()
  }
} catch (error) {
  // Handle |error|...
} finally {
  reader.releaseLock();
  console.log("f1");
}

当我从串行端口读取数据时,我的整个数据都被完美接收,但是“done”变量的

值永远不会改变,它仍然为假,并且代码在此阶段停止“let {value,done}=等待读者。读();”。

 while (true) {
    let { value, done } = await reader.read();
    if (done) {
      // |reader| has been canceled.
      console.log("brack");
      break;
    }
    console.log(value);
    console.log()
  }
} catch (error) {
  // Handle |error|...
} finally {
  reader.releaseLock();
  console.log("f1");
}

}

when I read data from the serial port, my whole data is perfectly received, but the value of the "done" variable never changes it remains false and the code is stop on this stage " let { value, done } = await the reader.read();".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-01-20 08:53:46

除非串行端口遇到错误,否则您始终能够读取更多数据,因此 done 永远不会设置为 true。如果您的程序已经读取了它想要读取的所有数据,那么它应该停止调用 read() 直到它想要接收更多数据。请注意,如果设备发送的数据多于打开端口时指定的缓冲区大小,并且您的应用程序未调用 read() 将其从缓冲区中拉出,则该数据可能会丢失。

Unless the serial port encounters an error you will always be able to read more data and so done is never set to true. If your program has read all the data it intends to then it should stop calling read() until it wants to receive more data. Note, if the device sends more data than the buffer size specified when opening the port this data may be lost if your application isn't calling read() to pull it out of the buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文