Search code examples
node.jsarduinonode-serialport

NodeJS doesnt work when serial monitor is closed


I know it may sounds weird and Im really dont know whats going on here.

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

var sp = new SerialPort("/dev/ttyACM1", {
  baudrate: 9600,
  parser: serialport.parsers.readline("\n")
});

sp.on('open', function () 
{
    console.log("writing...");
    sp.write('b');
    console.log("done");
});

this is my nodejs script when i run with my serial monitor open makes my led in my arduino blinks

void setup() 
{                
  Serial.begin(9600);
  pinMode(13, OUTPUT);

}
void loop() 
{
    if(Serial.available() > 0)
    {
        digitalWrite(13, HIGH);
        delay(1000);
        digitalWrite(13, LOW);
        delay(1000);
    }
}

now, my problem here is when I closed my serial monitor and run my nodejs script it doesnt blink my led. tho, i can see "writing..." and "done" in console.

any ideas why is this happening? or suggestion what to do? thanks in advance.

node version: 6.9.2 npm version: 3.10.9 OS: Ubuntu 14.04 LTS


Solution

  • I've read that the arduino cannot immediately receive a data after opening of port. Or something like that. So, ive tried this syntax and it perfectly works.

    var serialport = require("serialport");
    var SerialPort = serialport.SerialPort;
    
    var sp = new SerialPort("/dev/ttyACM0", {
      baudrate: 9600,
      parser: serialport.parsers.readline("\n")
    });
    
    sp.on('open', function() {
        sp.on('data', function (data) {
            sp.write("b");
        });
    });