Connecting to serialport after process.stdin.on('end',...) has unexpected results #229

Closed
tmpvar opened this Issue Oct 21, 2013 · 12 comments

Projects

None yet

4 participants

@tmpvar
Contributor
tmpvar commented Oct 21, 2013

here's a test case:

var SerialPort = require('serialport').SerialPort;

process.stdin.on('data', function() {});

process.stdin.on('end', function() {
  var sp = new SerialPort('/dev/tty.usbmodemfd121');
  sp.on('open', function() {
    console.log('SerialPort FD:', sp.fd);

    sp.write('this will not get sent');

    setTimeout(process.exit, 100);
  });
});

run with echo "hello" | node stdin-fail.js

example output:

$ echo "hello" | node stdin-fail.js
SerialPort FD: 0

Notice that sp.fd is 0, which is certainly not correct. Moving the SerialPort creation outside of the process.stdin.on('end', ...) callback results in sp.fd being equal to 11 on my system.

EDIT:

I should note that I've tried various versions of node-serialport (1.1.3, 1.2.2, git, etc..) on node v0.10.18

@JayBeavers
Collaborator

This stack exchange article suggests moving to

process.on('SIGINT', ...

Does that fix your issue?

@tmpvar
Contributor
tmpvar commented Oct 29, 2013

@JayBeavers hey, thanks for looking into this. Unfortunately this appears to be a bug somewhere in serialport where the serialport fd is somehow being mapped to stdin assuming you perform operations in the manner described above.

Could be that somewhere sp.fd is initialized to 0 and due to the timing of opening the sp it never gets set? I'll dig around in there when I get a spare cycle or 2

btw: SIGINT is the signal that is sent to your process when you press control-c, definitely not what I'm doing!

@reconbot
Collaborator

What happens if you don't console log?

@JayBeavers
Collaborator

I notice that your open callback is not handling the first err parameter. Can you handle that and log it as well? e.g.

sp.on('open', function(err) {
    if (err) console.log(err);
    ...
@tmpvar
Contributor
tmpvar commented Nov 1, 2013

@reconbot the console.log is to demonstrate why sp.write does not work as expected. Removing it has no effect on the sp.write call. sp.fd == 0 is a bad thing

@JayBeavers to be a good node citizen, serialport should provide an error event (which it does: https://github.com/voodootikigod/node-serialport/blob/master/serialport.js#L94). The event handler for open should not pass an error (which it does not: https://github.com/voodootikigod/node-serialport/blob/master/serialport.js#L242).

I've noticed that this issue is flagged as "needsdata", can someone confirm that this is an issue on more than just my system? I'd be happy to provide more information if you cannot duplicate.

@voodootikigod
Collaborator

Is this still an issue? I believe we have fixed this, can you confirm @tmpvar

@tmpvar
Contributor
tmpvar commented Sep 22, 2014

Still a bug, using the same test case as before:

$ echo "hello" | node serialport-229.js
SerialPort FD: 0

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Serialport not open.
    at SerialPortFactory.SerialPort.write (/Users/tmpvar/work/js/tmp/node_modules/serialport/serialport.js:211:17)
    at SerialPort.<anonymous> (/Users/tmpvar/work/js/tmp/serialport-229.js:10:8)
    at SerialPort.emit (events.js:92:17)
    at /Users/tmpvar/work/js/tmp/node_modules/serialport/serialport.js:203:12
$ head node_modules/serialport/package.json
{
  "name": "serialport",
  "version": "1.4.6",
  "description": "Welcome your robotic javascript overlords. Better yet, program them!",
  "author": {
    "name": "Chris Williams",
    "email": "voodootikigod@gmail.com",
    "url": "http://www.voodootikigod.com"
  },
  "binary": {

@tmpvar
Contributor
tmpvar commented Sep 22, 2014

That error is being emitted from the sp.write() function call which is inside sp.on('open'...)!

EDIT:

Oh, and there is an arduino attached running grbl

$ ls /dev/tty.usbmodem*
/dev/tty.usbmodemfd121
@voodootikigod
Collaborator

Can you cat serialport-22.js

On Monday, September 22, 2014, Elijah Insua notifications@github.com
wrote:

That error is being emitted from the sp.write() function call which is
inside sp.on('open'...)!


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

Chris Williams

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

The things I make that you should check out:
SaferAging http://www.saferaging.com/ | JSConf http://jsconf.com/ |
RobotsConf http://robotsconf.com/ | RobotsWeekly
http://robotsweekly.com/

Help me end the negativity on the internet, share this
http://jsconf.eu/2011/an_end_to_negativity.html.

@tmpvar
Contributor
tmpvar commented Sep 22, 2014

exactly the same as the OP

$ cat serialport-229.js
var SerialPort = require('serialport').SerialPort;

process.stdin.on('data', function() {});

process.stdin.on('end', function() {
  var sp = new SerialPort('/dev/tty.usbmodemfd121');
  sp.on('open', function() {
    console.log('SerialPort FD:', sp.fd);

    sp.write('this will not get sent');

    setTimeout(process.exit, 100);
  });
});
@voodootikigod
Collaborator

linux or mac?

@tmpvar
Contributor
tmpvar commented Sep 24, 2014

osx 10.9.4

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