Declaring a new SerialPort as a variable doesn't allow me to read data? SerialPort node js

Viewed 153

What am I trying to do?

For my electron project I am trying to declare my port as a variable so that the path can be changed by the user if necessary. (Need this functionality)

I did see the port.update() function in the documentation but I couldn't get it working for updating the path. Just trying to figure out a way of dynamically updating the port path while still be able to receive incoming data.

What's my problem?

I have a parser that listens for incoming data from the micro-controller but when I declare the port as a variable it doesn't receive any incoming data. If I declare the port as a constant, the parser is able to read incoming data no problem.

Code

Declaring as variable

// where arg is the com specified by the user, I tried it with COM4 but still doesn't work
var port = new SerialPort(arg, {
    baudRate: 115200,
    autoOpen:false
})

Declaring as constant

const port = new SerialPort("COM4",{
    autoOpen:false,
    baudRate:115200
});

Parser listener

const Readline = SerialPort.parsers.Readline;
const parse = port.pipe(new Readline({ delimiter: '\r\n' }));
parse.on('data',function(data){console.log(data)})

console output for port declared as variable

is port open => true
message written

console output for port declared as const

is port open => true
message written
Hello from Micro-controller
0 Answers
Related