created scripts

This commit is contained in:
sam
2023-09-27 23:26:43 +02:00
parent d41abf7222
commit 1220d1810c
2 changed files with 54 additions and 0 deletions

7
web/js-ts/serial.js Normal file
View File

@@ -0,0 +1,7 @@
let port;
function setup() {
port = createSerial();
port.open(9600);
}

47
web/js-ts/serial.ts Normal file
View File

@@ -0,0 +1,47 @@
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;
let reader = inputStream.getReader();
const encoder = new TextEncoderStream();
const outputDone = encoder.readable.pipeTo(port.writable);
const outputStream = encoder.writable;
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;
}
}
}
await function writeToStream(){
// CODELAB: Write to output stream
const writer = outputStream.getWriter();
lines.forEach((line) => {
console.log('[SEND]', line);
writer.write(line + '\n');
});
writer.releaseLock();
}