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");
}

}

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();".

Tejas Chauhan
  • 69
  • 3
  • 9

1 Answers1

1

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.

Reilly Grant
  • 5,590
  • 1
  • 13
  • 23
  • thanks for answering! Reilly Grant. I have something understand from your answer. but, what should I do to break the while loop because the code blocks on the read() function and therefore the value of "done" does not become true because the code not going to the next line? – Tejas Chauhan Mar 13 '22 at 09:42
  • Your code which processes the data received from the device must decide when all the data has been received and break out of the loop itself. – Reilly Grant Mar 14 '22 at 16:33
  • it means the last value of received all data is /a, then I have to match it and if it is matched then execute the "break;" statement. am I right? – Tejas Chauhan Mar 14 '22 at 17:34
  • That is correct. – Reilly Grant Mar 15 '22 at 22:40
  • I have read the stream API concept, this API has a controller. close() function "when we call this function the value of Done(variable) become false" so in serial API has this kind of function to close the readable stream .? because when I reading the data , there is not all data has the same value it also has different temperature data, serial number, and may more. so how can I perform this task in a different way? – Tejas Chauhan Mar 16 '22 at 04:47
  • The controller.close() method is an internal component of how a stream is implemented. You wouldn't call it yourself. In the case of the serial API, it's actually never called, as explained before, it doesn't fit the model of how a serial port works. I don't understand your question at this point. Why would you want to close the stream to read different data? I recommend asking a new question with an example of your code so far. – Reilly Grant Mar 17 '22 at 06:12