0

I'm trying to use node to print the Serial from Arduino uno connected to USB

I have a file script.js:

    var SerialPort= require("serialport");

    SerialPort.list(function(err,ports){
            ports.forEach(function(port){
            console.log(port.comName);
            console.log(port.manufacturer);
        });
        });

    var port= new SerialPort("/dev/cu.usbmodem641",{
            baudRate: 9600,
            parser: SerialPort.parsers.readline('n')
        });


    port.on('open',function(){
    console.log('opened');
        });



    port.on('close',function(){
            console.log('closed');
        });
port.on('data',function(data){
    console.log(data);
    });

port.on('error',function(error){
        console.log("Errore: "+error);
    });

console.log(":-)");

I run sudo node script.js while Arduino has Serial.println(1);

on terminal I see: opened closed

If I open Arduino IDE's serial monitor it correctly works!

Why my serial port doesn't stay opened?

Phil
  • 115
  • 1
  • 11
  • Couple things to consider, not necessarily the answer though: 1. Is this for sure the correct COM port 2. It could be that the Arduino IDE is forcing the port to close so that it can gain control over the Arduino. If possible, try to run the program with the Arduino IDE closed Also looks like it may be solved here: http://stackoverflow.com/questions/29735047/node-serialport-only-communicates-with-arduino-if-another-app-has-already-connec?rq=1 – The Dude Aug 30 '16 at 14:25
  • 1. Yes, I'm **sure** (Arduino IDE tell is that AND serliaport list indicates that as well) 2. I attemped several times with IDE opened, close, open IDE after node and viceversa 3. I tried that solution – Phil Aug 30 '16 at 14:37
  • Perhaps it is the line `parser: SerialPort.parsers.readline('n')`. This will look for the character "n" not newline. This may be what you intended but it may need to be `parser: SerialPort.parsers.readline('\n')` to parse on newline instead of "n". – The Dude Aug 30 '16 at 15:13
  • You're right I meant "\n", corrected but still not work. The fact is that the port closes as it is opened leaving non time do to anything! – Phil Aug 30 '16 at 15:28
  • Very strange, I am sure this is caused by the Arduino rebooting when it is attached to a serial port so that it can enter the bootloader. I'm unsure of why implementing the solution I linked to before isn't solving the issue, perhaps extend the delay to ensure that the bootloader has executed and the code is running. Is there any noticeable difference when using this solution? – The Dude Aug 30 '16 at 15:50
  • I tries up to 10 seconds (I imagine is enough, when it as rebooted its led blink a couple of time) when implemented this solution it open (and soon close) the connection two times (I printout "open" and "close") – Phil Aug 30 '16 at 15:53
  • I am trying http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection, now **surely** it does not reset, but still not remain open! – Phil Aug 30 '16 at 15:59
  • Although it shouldn't reset, it says on that page that it may still. This is because of the variation in current delivered by the FTDI chip. Not to say that it is, but it may. One thing to try is to add `dataBits: 8, parity: 'none', stopBits: 1, flowControl: false });` The flow control bit should force the computer to leave DTR alone, which may help. – The Dude Aug 30 '16 at 16:31
  • When it reset it blinks when open serial in IDE now it does nothing so I think it correctly resets! **tried** your parameters but nothing changes! I also tried *authOpen* false – Phil Aug 30 '16 at 16:35
  • Interesting, sounds like you are correct in that it is not resetting. http://playground.arduino.cc/Interfacing/Python although this is for python, it may be worth quickly implenting to ensure that your Arduino serial comm is working in general outside of the Serial Monitor. – The Dude Aug 30 '16 at 16:38
  • Ok *python* works correctly! Also *screen* command works! – Phil Aug 30 '16 at 16:52
  • Ah, it may perhaps be the layout of your NodeJS code. Try moving your `port.on('data',function(data){ console.log(data); });` inside of `port.on('open',function(){ console.log('opened'); });` so to refactor `port.on('open',function(){ console.log('opened'); port.on('data',function(data){ console.log(data); }); });` – The Dude Aug 30 '16 at 17:05

1 Answers1

0

I give up and did this in python!

https://github.com/fvalle1/Arduino/

Phil
  • 115
  • 1
  • 11