knowldgegebase code
This commit is contained in:
@@ -1,17 +1,39 @@
|
|||||||
|
// Define port and reader globally to be used in other functions
|
||||||
let port;
|
let port;
|
||||||
|
let reader
|
||||||
|
let decoder = new TextDecoder("utf-8");
|
||||||
|
let counterElement = document.getElementById("counter");
|
||||||
|
|
||||||
async function start() {
|
// Request a port and open a connection.
|
||||||
port = createSerial();
|
async function connect() {
|
||||||
port.open(9600);
|
port = await navigator.serial.requestPort();
|
||||||
|
await port.open({ baudRate: 9600 });
|
||||||
|
reader = port.readable.getReader();
|
||||||
if (port.opened()) {
|
console.log("Port is open!");
|
||||||
console.log("adsnij")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readloop() {
|
// Read data
|
||||||
|
async function readLoop() {
|
||||||
console.log(port.read());
|
// loop until reader.cancel() is called
|
||||||
|
while (true) {
|
||||||
|
// Wait for data
|
||||||
|
const { value, done } = await reader.read();
|
||||||
|
// Show the received data in the console
|
||||||
|
if (value) {
|
||||||
|
console.log(decoder.decode(value));
|
||||||
|
}
|
||||||
|
// Exit the loop when done
|
||||||
|
if (done) {
|
||||||
|
console.log('[readLoop] DONE', done);
|
||||||
|
reader.releaseLock();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the port
|
||||||
|
async function disconnect() {
|
||||||
|
await reader.cancel();
|
||||||
|
await port.close();
|
||||||
|
console.log("Port is closed!");
|
||||||
|
}
|
Reference in New Issue
Block a user