I hope you are well, Is there any way to access the values outside the function? My code is
const SerialPort = require("serialport");
const Readline = require("@serialport/parser-readline");
const port = new SerialPort("COM4");
const parser = port.pipe(new Readline({ delimiter: "\r\n" }));
parser.on("data", function(chunk) {
valrec = chunk.toString();
console.log(valrec);
v = valrec.split(" ");
var r = v[3];
var l = v[2];
});
Everything works fine inside the function, but outside it I can't access any of my variables r
or l
. So please can you help me to reach my variable outside this function to use them in my code.
Or this version of code let me access at least the variable "valrec"
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const port = new SerialPort('COM4')
const parser = port.pipe(new Readline({ delimiter: '\r\n' }))
var valrec;
var doStuff = function (param) {
return console.log(param);
};
parser.on('data', function (chunk) {
valrec = chunk.toString();
doStuff(valrec);
});
Thank you very much.