reading from console working + in jsonarray
This commit is contained in:
@@ -4,6 +4,10 @@ let reader;
|
|||||||
let decoder = new TextDecoder("utf-8");
|
let decoder = new TextDecoder("utf-8");
|
||||||
let counterElement = document.getElementById("counter");
|
let counterElement = document.getElementById("counter");
|
||||||
let readibleoutput = 0;
|
let readibleoutput = 0;
|
||||||
|
let temp = 0;
|
||||||
|
let humid = 0;
|
||||||
|
let licht = 0;
|
||||||
|
|
||||||
// Request a port and open a connection.
|
// Request a port and open a connection.
|
||||||
async function connect() {
|
async function connect() {
|
||||||
//vraag aan de browser om een serial port te selecteren
|
//vraag aan de browser om een serial port te selecteren
|
||||||
@@ -11,6 +15,7 @@ async function connect() {
|
|||||||
await port.open({ baudRate: 9600 });
|
await port.open({ baudRate: 9600 });
|
||||||
reader = port.readable.getReader();
|
reader = port.readable.getReader();
|
||||||
console.log("Port is open!");
|
console.log("Port is open!");
|
||||||
|
readLoop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read data
|
// Read data
|
||||||
@@ -26,24 +31,35 @@ async function readLoop() {
|
|||||||
for (let iByte=0; iByte < value.length; iByte++){
|
for (let iByte=0; iByte < value.length; iByte++){
|
||||||
let singleByte = value[iByte];
|
let singleByte = value[iByte];
|
||||||
|
|
||||||
if (singleByte = 10)
|
if (singleByte != 10){
|
||||||
buffer.push(singlebyte);
|
buffer.push(singleByte);
|
||||||
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
let sensorString =
|
let sensorString = decoder.decode(new Uint8Array(buffer));
|
||||||
console.log(buffer);
|
let SensorValues;
|
||||||
buffer = [];
|
|
||||||
|
try{
|
||||||
|
SensorValues = JSON.parse(sensorString);
|
||||||
|
} catch(e){
|
||||||
|
console.log("json niet geparserd");
|
||||||
}
|
}
|
||||||
// Show the received data in the console
|
|
||||||
// if (value) {
|
if (SensorValues) {
|
||||||
// readibleoutput = (decoder.decode(value));
|
console.log(SensorValues)
|
||||||
// var array = readibleoutput.split(','),
|
}
|
||||||
|
console.log(sensorString);
|
||||||
|
buffer = [];
|
||||||
|
// var array = sensorString.split(','),
|
||||||
// temp = array[0], humid = array[1], licht = array[2];
|
// temp = array[0], humid = array[1], licht = array[2];
|
||||||
// console.log(temp);
|
// console.log(temp);
|
||||||
// console.log(humid);
|
// console.log(humid);
|
||||||
// console.log(licht);
|
// console.log(licht);
|
||||||
// }
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show the received data in the console
|
||||||
|
|
||||||
// Exit the loop when done
|
// Exit the loop when done
|
||||||
if (done) {
|
if (done) {
|
||||||
console.log('[readLoop] DONE', done);
|
console.log('[readLoop] DONE', done);
|
||||||
@@ -51,8 +67,15 @@ async function readLoop() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Close the port
|
// Close the port
|
||||||
async function disconnect() {
|
async function disconnect() {
|
||||||
await reader.cancel();
|
await reader.cancel();
|
||||||
|
Reference in New Issue
Block a user