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

There is this above line used in the docs (https://web.dev/serial/) to read the data coming from the serial port. Docs say that the done will turn out to be true when all of the data sent by the serial device is read. But it's not getting true and since it is in the while loop, my code is getting stuck on this line, waiting further for the serial device to send new data even though all of the data has been sent by the serial device.

(I am using Chrome browser, Windows and the code is written in js file)

2

There are 2 answers

0
Akash Bhardwaj On

I found a workaround. What I did was whenever the ReceivedData was updated with '\u0004\u0003' string, it meant that the serial device has completed the transfer of data since '\u0004\u0003' means EOTETX (End of transmission, end of text). Atleast in my case it worked since I knew the way in which the serial device wraps it's actual message within STX and EOTETX.

Refer the code below to have a proper idea of what I'm talking about. I wrote it in this way:

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;
        }
}
0
TwistedKap On

I read that same info in the docs and was also confused. I have only observed done=true after calling reader.cancel(). Perhaps the docs should be clarified.

It is needed after cancel to break out of the while loop.

You can see it in my source code for https://webserial.io :

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