2021-11-30

Node.js writing parse data to a variable

I'm writng a node.js app to read messages from Serial Port. Reading data and logging it into console works fine, althought I'm wondering how to save data value from Serial Port to a variable. I want to pass it further to a MySQL, so I need the data to be stored in variable. I tried to use global variable, but it keeps saying "undefined". I also tried to pass the value using return in js function, but it doesn't work too. Here's my code:

var SerialPort = require('serialport');
const parsers = SerialPort.parsers;
const parser = new parsers.Readline({
    delimiter: '\r\n'
});

var port = new SerialPort('COM10',{ 
    baudRate: 9600,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
    flowControl: false
});

port.pipe(parser);

parser.on('data', function(data) {
    
    console.log('Received data from port: ' + data);
});

Please tell me how to store data from parser.on in a variable.



from Recent Questions - Stack Overflow https://ift.tt/3E7G6Ld
https://ift.tt/eA8V8J

No comments:

Post a Comment