Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close and Disconnect events doesn't work properly #458

Closed
sminodonte opened this issue Feb 9, 2015 · 19 comments
Closed

Close and Disconnect events doesn't work properly #458

sminodonte opened this issue Feb 9, 2015 · 19 comments

Comments

@sminodonte
Copy link

sminodonte commented Feb 9, 2015

When I gets unplugged a device, I cannot find a way to detect this. There is no automatic event...nothing! I know that it's an old bug (#393) but I've tryed with Windows 8.1 and Windows 7 and I've always had the same response namely no event. For example this code:

var serialPort = require("node-serialport");
var SerialPort = serialPort.SerialPort;
var sys = require('sys');

var spDevice = null;
var i = 1;

// setting up the serial connection

var connectDevice = function() {
    //---------------------------------------

serialPort.list(function (err, ports) {

      var port = null;  

      var found = ports.some(function(p, index, array) {
        console.log(p.comName);
        console.log(p.pnpId);
        console.log(p.manufacturer);

        if(p.manufacturer.indexOf("Arduino") > -1)
        {
            port = p;
            return true;    
        }

        return false;
      });


      if(found)
      {
        spDevice = new SerialPort(port.comName.replace("cu","tty"), {
            baudrate: 9600,
            parser: serialPort.parsers.readline("\n"),
            disconnectedCallback: function() {console.log('You pulled the plug!');}         
        });         

        // do something with incoming data
        spDevice.on('data', function (data) {
             sys.debug('count : ' + (i++));
            console.log('data received: ' + data);
        });

        spDevice.on('close', function(){
            console.log('ARDUINO PORT CLOSED');
            spDevice = null;
            reconnectDevice();
        });

        spDevice.on('error', function (err) {
            console.log("ERROR");
            spDevice = null;
            console.error("error", err);
            reconnectDevice();
        });

        spDevice.on('disconnected', function (err) {
            console.log('on.disconnect');
            spDevice = null;
            reconnectDevice();
        }); 
      }
      else
      {   
        setTimeout(connectDevice, 1000);
      }       
    });
    //---------------------------------------
}

connectDevice();

// check for connection errors or drops and reconnect
var reconnectDevice = function () {
  console.log('INITIATING RECONNECT');
  setTimeout(function(){
    console.log('RECONNECTING TO ARDUINO');
    connectDevice();
  }, 2000);
};

I try with node v0.10.26 and v0.12.0.
Anybody still have this problem on Windows?
thanks

@humblewizard
Copy link

The unplug shows up with:

  process.on('uncaughtException', function(err) {
      console.log(err);
  });

It will say: "Type error: undefined is not a function".

How to use this to try to reconnect I'm not sure.

I've been struggling with how to gracefully disconnect and reconnect ports without having to restart the whole node program and I'm not doing well.

@humblewizard
Copy link

I've never been able to get

    serialport.on('error', function(err){
            console.log(err);
            // try to restart port
    });

to fire no matter what happens.

If this would fire when a port is disconnected then it would help to know what to do next.

@sminodonte
Copy link
Author

Thanks for your reply.
My problem is that I can not intercept any event when I unplug the Arduino usb port.

Nothing, also with
process.on('uncaughtException', function(err) {
console.log(err);
});

I have to develop an application where the users don't know nothing about the serial port and they have to connect and disconnect the device at random time and the App have to stay ON and always ready to listen a new device....but if I can not intercepet event I can't do that application. The unique solution could will be that user manage close connection manually on front end application...do u know what I mean? (sorry about my english)

@humblewizard
Copy link

I need to do the same thing and I don't know how to do it either.
process.on('uncaughtException', function(err) {} is a death warning for the process. I think this is a flaw in serialport that needs to be addressed.

@crapthings
Copy link

windows 8.1 64bit
node.js 0.12.0

I've got same issue with nw.js then i've found usb-detection, but it failed complie on windows.

How to reconnect serial port without reload whole app?

@voodootikigod
Copy link
Collaborator

Are these all windows only?

On Friday, February 13, 2015, crapthings notifications@github.com wrote:

i've got same issue with nw.js then i've found usb-detection, but it
failed complie on windows.


Reply to this email directly or view it on GitHub
#458 (comment)
.

Chris Williams

@voodootikigod http://twitter.com/voodootikigod | GitHub
http://github.com/voodootikigod

Maker of Improbable Things: JSConf http://jsconf.com/ | RobotsConf
http://robotsconf.com/ | Beer.js http://beerjs.com | Logo.js
https://github.com/voodootikigod/logo.js | node-serialport
http://nodebots.io/

@sminodonte
Copy link
Author

The problem is on Windows 7 and Windows 8.1 for me. I've never tryed on linux

@humblewizard
Copy link

My issue is with Windows 7.

I'm going down the road of spawning a separate process for each serial port and when
process.on('uncaughtException',function(){}); happens then close the child process and restart. It seems like a waste of CPU cycles to add extra stream buffering but it's the only way I can get the main process to survive an unplugged USB:Serial device on my WIndows 7 box.

@humblewizard
Copy link

Having a similar issue with Ubuntu 14.04

A serial port (Prolific) connected over a USB port is taking data fine. Then when the USB cable is disconnected there is no error generated, data stops, and yet the other parts of the process keep working just fine. In this case the

  process.on('uncaughtException',function(){})

that is thrown in the Windows 7 case is not thrown in Linux. So in the Linux case we could look for no data received in x seconds to indicate that the port is disconnected and raise a stink about it with the user. It would be nice if the registered p.on('error') function would be called as soon as the USB cable is pulled. Just plugging the cable back in does not restart the connection.

Port Listing:

    com.list(.......);

is able to see the port come and go so I guess we could just keep running the logging every second or two and when the port gets plugged back in then call for the reconnect.

@crapthings
Copy link

+1 for reconnect.

@arahlf
Copy link
Contributor

arahlf commented Feb 21, 2015

+1 for a way to do this

@MadLittleMods
Copy link

👍

.on('close', cb) never fires.

  • serialport v1.7.1
  • Windows 8.1
  • Node v0.12.1

@sword36
Copy link

sword36 commented Aug 19, 2015

I track to data event from the serial port and update variable lastDataTime. I use setInterval to call my function of checking every second. Function of checking looks something like this:

if (Date.now() - lastDataTime > 1000) {
    throw "Disconnect";
}

It worked for me.

@jaymes-bearden
Copy link

I'm having the same issue too. On Windows, supplying options.disconnectedCallback to new SerialPort(...) is not called when the connection is interrupted.

Also, on('close', ...) and on('disconnect', ...) are not called either.

@mizraith
Copy link

mizraith commented Nov 6, 2015

Can confirm as well. Wanted to around with Node.js and some basic serial port communication. I'm using the basic readdata example with a callback (setTimeout). If I set the timeout to 1000, I get about 2-3 responses before the port silently closes. If I set the timeout to 300, I might get more. Something is timing out, but there is no reporting, no errors, nothing that i can find to log out.

Also -- I created a serialPort.on('close'...) to reopen the port, but this never gets fired.

Here's the code:

var com = require("serialport");

var serialPort = new com.SerialPort("COM23", {
    baudrate: 9600,
    parser: com.parsers.readline('>')
});

serialPort.on('open',function() {
    console.log('Port open');
    setTimeout(serialPort.write("C\r\n"), 300);
});

serialPort.on('data', function(data) {
    console.log("Temp C: " , data);
    setTimeout(serialPort.write("C\r\n"), 300);

});

// This 'close' never fires.  just dies silently.
serialPort.on('close', function() {
    console.log("it got closed...going to reopen");
    serialPort = new com.SerialPort("COM23", {
        baudrate: 9600,
        parser: com.parsers.readline('>')
    });
})

Here's the output:

Port open
Temp C:  21

Temp C:  21

I've tried different ways to set the timeouts, from external to the callback as well, but I get the same problem -- the port just closes.

@reconbot
Copy link
Member

Disconnected happens in a few different places. We also don't emit the event if we provide a handler, nor do consistently call the disconnect logic. On top of that we remove all events if we're disconnected.

Adding this to the backlog to clearly define in #702

@reconbot
Copy link
Member

reconbot commented Apr 6, 2016

Disconnect logic is still in a poor state but we've fixed a few crashes and places where errors were not being handled correctly. When it comes to disconnects windows will work a lot easier now in our latest beta and you should be getting your events.

#733 Please try it out serialport@2.0.7-beta4 and report back!

@reconbot reconbot changed the title Close and Disconnect event doesn't work properly Close and Disconnect events doesn't work properly May 16, 2016
@reconbot
Copy link
Member

We're tracking the disconnect issues here #702

I think we've solved your other issues so I'm going to close this for now. Feel free to reopen.

@keyiis
Copy link

keyiis commented Mar 23, 2017

@humblewizard Having a similar issue with electron,are you fixed?

@lock lock bot locked as resolved and limited conversation to collaborators May 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests