summaryrefslogtreecommitdiffstats
path: root/assets/js/serialUtil.js
diff options
context:
space:
mode:
authorErnesto Castellotti <mail@ernestocastellotti.it>2023-06-09 00:36:09 +0200
committerGitHub <noreply@github.com>2023-06-09 00:36:09 +0200
commit745d8de475e6757d33907db77007126cee920e8a (patch)
treec676b094156eb8498a7faf00982c7c62b507d005 /assets/js/serialUtil.js
parentConvert changeBaudrate, waitEndImageLoad and flashImageMtd to SerialReadWrite (#229) (diff)
downloadhack-gpon.github.io-745d8de475e6757d33907db77007126cee920e8a.tar
hack-gpon.github.io-745d8de475e6757d33907db77007126cee920e8a.tar.gz
hack-gpon.github.io-745d8de475e6757d33907db77007126cee920e8a.tar.bz2
hack-gpon.github.io-745d8de475e6757d33907db77007126cee920e8a.tar.lz
hack-gpon.github.io-745d8de475e6757d33907db77007126cee920e8a.tar.xz
hack-gpon.github.io-745d8de475e6757d33907db77007126cee920e8a.tar.zst
hack-gpon.github.io-745d8de475e6757d33907db77007126cee920e8a.zip
Diffstat (limited to 'assets/js/serialUtil.js')
-rw-r--r--assets/js/serialUtil.js96
1 files changed, 45 insertions, 51 deletions
diff --git a/assets/js/serialUtil.js b/assets/js/serialUtil.js
index ebef166..779bd4a 100644
--- a/assets/js/serialUtil.js
+++ b/assets/js/serialUtil.js
@@ -2,23 +2,6 @@ function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
-class LineBreakTransformer {
- constructor() {
- this.chunks = "";
- }
-
- transform(chunk, controller) {
- this.chunks += chunk;
- const lines = this.chunks.split("\n");
- this.chunks = lines.pop();
- lines.forEach((line) => controller.enqueue(line));
- }
-
- flush(controller) {
- controller.enqueue(this.chunks);
- }
-}
-
class SerialReadWrite {
constructor(port, baudrate) {
this.port = port;
@@ -36,6 +19,47 @@ class SerialReadWrite {
await this.port.close();
}
+ async readBytes(retryError = 5, errorNum = 0) {
+ let reader = undefined;
+
+ if (this.isPortOpen === false) {
+ await this.openPort();
+ this.isPortOpen = true;
+ }
+
+ try {
+ if (reader === undefined) {
+ reader = this.port.readable.getReader();
+ }
+
+ const promiseResult = await reader.read();
+
+ if (promiseResult === undefined) {
+ return undefined;
+ }
+
+ return promiseResult.value;
+ } catch (e) {
+ if (e instanceof DOMException &&
+ (e.name === "BreakError" || e.name === "FramingError" || e.name === "ParityError")) {
+ console.log(e);
+
+ if (errorNum > retryError) {
+ throw e;
+ }
+
+ return await this.readBytes(retryError, errorNum++);
+ } else {
+ throw e;
+ }
+ } finally {
+ if (reader) {
+ reader.releaseLock();
+ reader = undefined;
+ }
+ }
+ }
+
async readLine(readCallback, timeout = undefined) {
let reader = undefined;
let extraChunk = "";
@@ -90,7 +114,7 @@ class SerialReadWrite {
}
}
- async writeString(str) {
+ async writeBytes(bytes) {
let writer = undefined;
if (this.isPortOpen === false) {
@@ -103,7 +127,7 @@ class SerialReadWrite {
writer = this.port.writable.getWriter();
}
- writer.write(this.textEncoder.encode(str));
+ writer.write(bytes);
} finally {
if (writer) {
writer.releaseLock();
@@ -111,38 +135,8 @@ class SerialReadWrite {
}
}
}
-}
-
-async function openPortLineBreak(port, baudRate) {
- await port.open({ baudRate: baudRate });
- const textDecoder = new TextDecoderStream();
- const readableStreamClosed = port.readable.pipeTo(textDecoder.writable);
- const reader = await textDecoder.readable.pipeThrough(new TransformStream(new LineBreakTransformer())).getReader();
- const textEncoderStream = new TextEncoderStream();
- const writerStreamClosed = textEncoderStream.readable.pipeTo(port.writable);
- const writer = await textEncoderStream.writable.getWriter();
-
- return { reader, writer, readableStreamClosed, writerStreamClosed };
-}
-
-async function closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed) {
- if (reader) {
- reader.cancel();
- }
-
- if (readableStreamClosed) {
- await readableStreamClosed.catch(() => { /* Ignore the error */ });
- }
- if (writer) {
- writer.close();
- }
-
- if (writerStreamClosed) {
- await writerStreamClosed;
- }
-
- if (port) {
- await port.close();
+ async writeString(str) {
+ await this.writeBytes(this.textEncoder.encode(str));
}
}