I am working on a nodejs project that utilizes a serial port for communicating with a microcontroller. I can get everything to work using serialport but I'm struggling to determine the best method for handling received data.
Here's the basic program I'm testing with:
const serialPort = require('serialport');
const delimiter = require('@serialport/parser-delimiter')
const readline = require('readline');
const EventEmitter = require('events');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.setPrompt('> ');
rl.prompt();
const uart = new serialPort('path-to-port', {
baudRate: 9600
});
const parser = uart.pipe(new delimiter({ delimiter: '\n'}));
const event = new EventEmitter();
parser.on('data', (data) => {
event.emit('dataReady', data);
});
rl.on('line', (input) => {
if(input === 'close'){
console.log('Bye');
process.exit();
}
uart.write(input + '\n', (err) => {
if (err) {
console.log('uart write error');
}
});
console.log('Waiting on data...');
readAsync().then((data) => {
console.log(' ->', data.toString());
rl.prompt();
});
});
const readAsync = () => {
const promise = new Promise((resolve, reject) => {
event.once('dataReady', (data) => {
resolve(data);
});
});
return promise;
}
I would like to return a promise that will be fulfilled once data has been received. Currently I'm emitting an event and using a listener to fulfill the promise. Is this the best method or is there a better option?
UPDATE
I plan to export a function for writing to the serial port that fulfills a promise once data is received.
Example:
module.exports.write = (cmd) => {
uart.write(input + '\n', (err) => {
if (err) {
console.log('uart write error');
}
});
return readAsync();
}