I am trying to get my Arduino to send data to my computer using Node.js Serial port. Everything compiles, my Arduino is working well and sending data in the serial monitor, but the program always goes into myPort.on('close')
, and I can't figure out why.
//import serial port js package
const SerialPort = require('serialport');
//opening a port
var myPort = new SerialPort("/dev/ttyS2", {
baudRate: 9600,
//parser: SerialPort.parsers.raw
});
//how to pass the readline
const Readline = SerialPort.parsers.Readline;
const parser = new Readline();
myPort.pipe(parser);
myPort.on("open", onPortOpen);
myPort.on("data" , onData);
myPort.on("close" , onClose);
myPort.on("error" , onError);
function onPortOpen(){
console.log("Port open");
}
function onData(data){
console.log("data transfer completed..");
console.log(data);
}
function onError(){
console.log("Error. something went wrong..")
}
function onClose(){
console.log("Port is closed. Communication terminated");
}
Would anyone know what are the potential reasons? I double checked my port and it really is COM3
, therefore, /dev/ttyS2
for Windows 10.