4

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

user3358452
  • 77
  • 1
  • 1
  • 5

1 Answers1

3

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.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • 1
    Thanks, that works. Now I understand my wrong thinking. Is there a way to read the status of myserialport (is it open or closed) or should I set a flag, when opening/closing. Another question when coming back to main page with a back arrow on the browser I need to refresh it (the page) manually. Is the a way to force a automatic refresh? – user3358452 Mar 18 '14 at 03:47