30 lines
829 B
JavaScript
30 lines
829 B
JavaScript
"use strict";
|
|
async function connect() {
|
|
const port = await navigator.serial.requestPort();
|
|
// - Wait for the port to open.
|
|
await port.open({ baudRate: 9600 });
|
|
let decoder = new TextDecoderStream();
|
|
const inputDone = port.readable.pipeTo(decoder.writable);
|
|
const inputStream = decoder.readable;
|
|
reader = inputStream.getReader();
|
|
await readLoop();
|
|
}
|
|
async function clickConnect() {
|
|
// CODELAB: Add connect code here.
|
|
await connect();
|
|
}
|
|
async function readLoop() {
|
|
// CODELAB: Add read loop here.
|
|
while (true) {
|
|
const { value, done } = await reader.read();
|
|
if (value) {
|
|
log.textContent += value + '\n';
|
|
}
|
|
if (done) {
|
|
console.log('[readLoop] DONE', done);
|
|
reader.releaseLock();
|
|
break;
|
|
}
|
|
}
|
|
}
|