Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
tmpvar opened this Issue · 12 comments

4 participants

@tmpvar

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

@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

@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

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

@tmpvar

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

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
@tmpvar

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

linux or mac?

@tmpvar

osx 10.9.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.