Unable to close serial port in nodejs

user3358452 picture user3358452 · Mar 14, 2014 · Viewed 8.7k times · Source

I'm using node-serialport available here. Open the port and reading works very well, however I am unable to close the port.

I use the following code for that:

myserialport.on('close', function (err) {
    console.log('port closed', err);  
});

The callback function is never executed (no console output). Can anyone, please show me the way?

I would like to be able to close one port and open another if needed or just close the port if user changes the page.

Jan

Answer

Jonathan Lonowski picture Jonathan Lonowski · Mar 17, 2014

You can use the .close() method to instruct the serial port to close:

myserialport.close(function (err) {
    console.log('port closed', err);
});

.on('close') allows you to add the function as a listener of the 'close' event. But, it will simply wait for the event to occur (to be emitted) rather than instruct it to be done.