簡體   English   中英

Web Serial Api ->{value,done} -> 即使在讀取了串口設備發送的全部數據后,done 也沒有變為 true

[英]Web Serial Api ->{value,done} -> the done is not getting true even after reading the whole data sent by the serial port device

const { value, done } = await reader.read();

文檔( https://web.dev/serial/ )中使用了上面的這一行來讀取來自串行端口的數據。 文檔說,當串行設備發送的所有數據都被讀取時,done 將變為 true。 但事實並非如此,因為它在 while 循環中,所以我的代碼卡在這條線上,即使串行設備已發送所有數據,也要進一步等待串行設備發送新數據。

(我使用的是Chrome瀏覽器,Windows,代碼是用js文件寫的)

["

while (port && port.readable){
    const { value, done } = await reader.read();
        if (done) {
            break;
        }
        // value is a string.
        ReceivedData +=value;
        if(ReceivedData.includes('\u0004\u0003')){
            break;
        }
}

我在文檔中閱讀了相同的信息,也很困惑。 我在調用reader.cancel()后才觀察到done=true 也許應該澄清文檔。

取消后需要退出while循環。

您可以在我的https://webserial.io源代碼中看到它:

https://github.com/williamkapke/webserial/blob/8ae91a2fd978eede59182c2a4b28725c12c932a8/src/stores/connection.js#L107

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM