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

Event handling upon unsuccessful serial connection #177

Closed
myrtleTree33 opened this Issue Jun 17, 2013 · 6 comments

Comments

3 participants
@myrtleTree33

myrtleTree33 commented Jun 17, 2013

Hello,

Is there any way for the SerialPort instance to trigger a failure event, if let's say e.g. the specified device is not found?

I have tried to catch such errors by listening for uncaughtException, but apparently the program gets stuck at events.js: Uncaught Error: cannot open /dev/ttyUSB0.

How should I code the node app such that the program exits upon Serial device error?

@adrai

This comment has been minimized.

Show comment
Hide comment
@adrai

adrai Jun 18, 2013

Contributor

Have you tried with this?

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/tty-usbserial1", {
  baudrate: 57600
}, false); // this is the openImmediately flag [default is true]

serialPort.open(function (err) {
  if (err) {
     console.log(err);
     return;
  }
  console.log('open');
  serialPort.on('data', function(data) {
    console.log('data received: ' + data);
  });  
  serialPort.write("ls\n", function(err, results) {
    console.log('err ' + err);
    console.log('results ' + results);
  });  
});
Contributor

adrai commented Jun 18, 2013

Have you tried with this?

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/tty-usbserial1", {
  baudrate: 57600
}, false); // this is the openImmediately flag [default is true]

serialPort.open(function (err) {
  if (err) {
     console.log(err);
     return;
  }
  console.log('open');
  serialPort.on('data', function(data) {
    console.log('data received: ' + data);
  });  
  serialPort.write("ls\n", function(err, results) {
    console.log('err ' + err);
    console.log('results ' + results);
  });  
});
@myrtleTree33

This comment has been minimized.

Show comment
Hide comment
@myrtleTree33

myrtleTree33 Jun 19, 2013

Yes, I tried, with the false flag set.

It returns me a events.js:2817: Uncaught Error: Cannot open /dev/ttyUSB0

I've been extra careful about the error handling as well:

serialPort.open(function(err) {
    if (err) {
        console.log(err);
        return;
        process.exit(1);

    }
...

myrtleTree33 commented Jun 19, 2013

Yes, I tried, with the false flag set.

It returns me a events.js:2817: Uncaught Error: Cannot open /dev/ttyUSB0

I've been extra careful about the error handling as well:

serialPort.open(function(err) {
    if (err) {
        console.log(err);
        return;
        process.exit(1);

    }
...
@adrai

This comment has been minimized.

Show comment
Hide comment
@adrai

adrai Jun 19, 2013

Contributor

ok, try this:

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/tty-usbserial1", {
  baudrate: 57600
}, false); // this is the openImmediately flag [default is true]

serialPort.on('error', function(err) {
  console.log(err); // THIS SHOULD WORK!
});

serialPort.open(function (err) {
  if (err) {
     console.log(err);
     return;
  }
  console.log('open');
  serialPort.on('data', function(data) {
    console.log('data received: ' + data);
  });  
  serialPort.write("ls\n", function(err, results) {
    console.log('err ' + err);
    console.log('results ' + results);
  });  
});
Contributor

adrai commented Jun 19, 2013

ok, try this:

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/tty-usbserial1", {
  baudrate: 57600
}, false); // this is the openImmediately flag [default is true]

serialPort.on('error', function(err) {
  console.log(err); // THIS SHOULD WORK!
});

serialPort.open(function (err) {
  if (err) {
     console.log(err);
     return;
  }
  console.log('open');
  serialPort.on('data', function(data) {
    console.log('data received: ' + data);
  });  
  serialPort.write("ls\n", function(err, results) {
    console.log('err ' + err);
    console.log('results ' + results);
  });  
});
@myrtleTree33

This comment has been minimized.

Show comment
Hide comment
@myrtleTree33

myrtleTree33 Jun 19, 2013

Great!!! That worked very well. Thank you so much!!

myrtleTree33 commented Jun 19, 2013

Great!!! That worked very well. Thank you so much!!

@JayBeavers

This comment has been minimized.

Show comment
Hide comment
@JayBeavers

JayBeavers Jul 18, 2013

Collaborator

This seems like a pretty clear 'design oversight'. serialport.open should return an error on port not found to the callback as well as via the error event. @voodootikigod concur? Happy to write up a patch if you feel this is the right behavior.

Collaborator

JayBeavers commented Jul 18, 2013

This seems like a pretty clear 'design oversight'. serialport.open should return an error on port not found to the callback as well as via the error event. @voodootikigod concur? Happy to write up a patch if you feel this is the right behavior.

@JayBeavers

This comment has been minimized.

Show comment
Hide comment
@JayBeavers

JayBeavers Nov 12, 2013

Collaborator

Believe this is fixed with the new serialportfactory object and associated error handling reworking. You should now be able to use either:

var serialPortFactory = require("serialport");
serialPortFactory.on('error', ...);
var port = new serialPortFactory.SerialPort(...);

or

var serialPortFactory = require("serialport");
var port = new serialPortFactory.SerialPort(..., function(err) { ... });
Collaborator

JayBeavers commented Nov 12, 2013

Believe this is fixed with the new serialportfactory object and associated error handling reworking. You should now be able to use either:

var serialPortFactory = require("serialport");
serialPortFactory.on('error', ...);
var port = new serialPortFactory.SerialPort(...);

or

var serialPortFactory = require("serialport");
var port = new serialPortFactory.SerialPort(..., function(err) { ... });

@JayBeavers JayBeavers closed this Nov 12, 2013

@lock lock bot locked as resolved and limited conversation to collaborators May 7, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.