48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
// Define port and reader globally to be used in other functions
|
|
let port;
|
|
let reader;
|
|
let decoder = new TextDecoder("utf-8");
|
|
let counterElement = document.getElementById("counter");
|
|
let readibleoutput = 0;
|
|
// Request a port and open a connection.
|
|
async function connect() {
|
|
//vraag aan de browser om een serial port te selecteren
|
|
port = await navigator.serial.requestPort();
|
|
await port.open({ baudRate: 9600 });
|
|
reader = port.readable.getReader();
|
|
console.log("Port is open!");
|
|
}
|
|
|
|
// Read data
|
|
async function readLoop() {
|
|
// 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) {
|
|
readibleoutput = (decoder.decode(value));
|
|
var array = readibleoutput.split(','),
|
|
temp = array[0], humid = array[1], licht = array[2];
|
|
console.log(temp);
|
|
console.log(humid);
|
|
console.log(licht);
|
|
setTimeout(() => {
|
|
|
|
}, 400);
|
|
}
|
|
// 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!");
|
|
} |