serialport / node-serialport

Access serial ports with JavaScript. Linux, OSX and Windows. Welcome your robotic JavaScript overlords. Better yet, program them!
https://serialport.io
MIT License
5.79k stars 1.01k forks source link

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

Closed tmpvar closed 9 years ago

tmpvar commented 10 years ago

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 commented 10 years ago

This stack exchange article suggests moving to

process.on('SIGINT', ...

Does that fix your issue?

tmpvar commented 10 years ago

@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 commented 10 years ago

What happens if you don't console log?

JayBeavers commented 10 years ago

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 commented 10 years ago

@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 commented 9 years ago

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

tmpvar commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 https://github.com/voodootikigod/node-serialport/issues/229#issuecomment-56453852 .

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 commented 9 years ago

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 commented 9 years ago

linux or mac?

tmpvar commented 9 years ago

osx 10.9.4