reading from console working + in jsonarray

This commit is contained in:
sam
2023-10-11 14:34:06 +02:00
parent 554a410998
commit d54e2ca305

View File

@@ -4,13 +4,18 @@ let reader;
let decoder = new TextDecoder("utf-8");
let counterElement = document.getElementById("counter");
let readibleoutput = 0;
let temp = 0;
let humid = 0;
let licht = 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!");
console.log("Port is open!");
readLoop()
}
// Read data
@@ -26,24 +31,35 @@ async function readLoop() {
for (let iByte=0; iByte < value.length; iByte++){
let singleByte = value[iByte];
if (singleByte = 10)
buffer.push(singlebyte);
if (singleByte != 10){
buffer.push(singleByte);
}
else {
let sensorString =
console.log(buffer);
buffer = [];
let sensorString = decoder.decode(new Uint8Array(buffer));
let SensorValues;
try{
SensorValues = JSON.parse(sensorString);
} catch(e){
console.log("json niet geparserd");
}
if (SensorValues) {
console.log(SensorValues)
}
console.log(sensorString);
buffer = [];
// var array = sensorString.split(','),
// temp = array[0], humid = array[1], licht = array[2];
// console.log(temp);
// console.log(humid);
// console.log(licht);
}
// 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);
// }
// Exit the loop when done
if (done) {
console.log('[readLoop] DONE', done);
@@ -51,8 +67,15 @@ async function readLoop() {
break;
}
}
}
}
}
// Close the port
async function disconnect() {
await reader.cancel();