diff --git a/web/app.js b/web/app.js index 47f683f..3026fa2 100644 --- a/web/app.js +++ b/web/app.js @@ -1,69 +1,9 @@ +/** + * If this character is included in the received data chunk, + * it means that transfer completed. + */ var EOT = '\u0004'; -class SerialPortHandler { - constructor(options, onConnect, onDisconnect) { - this.encoder = new TextEncoder(); - this.decoder = new TextDecoder(); - this.onConnect = onConnect; - this.onDisconnect = onDisconnect; - this.options = options; - this.port = null; - this.isOpened = false; - this.#setupListeners(); - } - - async open() { - try { - const port = await navigator.serial.requestPort(); - await port.open(this.options); - - this.port = port; - this.isOpened = true; - - return this.port.getInfo(); - } catch (error) { - console.error(error); - throw error; - } - } - - async close() { - await this.port.close(); - this.isOpened = false; - } - - async write(data) { - const writer = this.port.writable.getWriter(); - const encoded = this.encoder.encode(data); - await writer.write(encoded); - writer.releaseLock(); - } - - async read() { - const reader = this.port.readable.getReader(); - let chunks = ''; - while (true) { - const { value, done } = await reader.read(); - const decoded = this.decoder.decode(value); - - chunks += decoded; - - if (done || decoded.includes('\u0004')) { - console.log('Reading done.'); - reader.releaseLock(); - break; - } - } - - return chunks; - } - - #setupListeners() { - navigator.serial.addEventListener('connect', this.onConnect); - navigator.serial.addEventListener('disconnect', this.onDisconnected); - } -} - class Application { constructor(root) { if (!('serial' in navigator)) { @@ -148,4 +88,68 @@ class Application { } } +class SerialPortHandler { + constructor(options, onConnect, onDisconnect) { + this.encoder = new TextEncoder(); + this.decoder = new TextDecoder(); + this.onConnect = onConnect; + this.onDisconnect = onDisconnect; + this.options = options; + this.port = null; + this.isOpened = false; + this.#setupListeners(); + } + + async open() { + try { + const port = await navigator.serial.requestPort(); + await port.open(this.options); + + this.port = port; + this.isOpened = true; + + return this.port.getInfo(); + } catch (error) { + console.error(error); + throw error; + } + } + + async close() { + await this.port.close(); + this.isOpened = false; + } + + async write(data) { + const writer = this.port.writable.getWriter(); + const encoded = this.encoder.encode(data); + await writer.write(encoded); + writer.releaseLock(); + } + + async read() { + const reader = this.port.readable.getReader(); + let chunks = ''; + while (true) { + const { value, done } = await reader.read(); + const decoded = this.decoder.decode(value); + + chunks += decoded; + + if (done || decoded.includes('\u0004')) { + console.log('Reading done.'); + reader.releaseLock(); + break; + } + } + + return chunks; + } + + #setupListeners() { + navigator.serial.addEventListener('connect', this.onConnect); + navigator.serial.addEventListener('disconnect', this.onDisconnected); + } +} + new Application(document.getElementById('app'));