title: Serial port not working?
body: <p>I made a program that sends out data to my arduino which detects what was sent and then turns on the correct pin according to what key is pressed.</p>
  
  <p>When using the arduino software from my windows computer the arduino sketch works fine, I can make each pin turn on and off by sending either W A S Or D.</p>
  
  <p>When sending via node the RX light on the arduino flashes but nothing else happens.</p>
  
  <p>Can anyone help?</p>
  
  <p>Node.js program:</p>
  
  <pre><code>var httpServer = require('http').createServer(function(req, response){ /* Serve your static files */ })
  httpServer.listen(8080);
  
  var nowjs = require("now");
  var everyone = nowjs.initialize(httpServer);
  
  everyone.now.logStuff = function(msg){
      console.log(msg);
  }
  
  var SerialPort = require('serialport2').SerialPort;
  var assert = require('assert');
  
  var portName;
  
  if (process.platform == 'win32') {
    portName = 'COM4';
  } else if (process.platform == 'darwin') {
    portName = '/dev/cu.usbserial-A800eFN5';
  } else {
    portName = '/dev/ttyUSB0';
  }
  
  var readData = '';
  var sp = new SerialPort();
  
  sp.on('close', function (err) {
    console.log('port closed');
  });
  
  sp.on('error', function (err) {
    console.error("error", err);
  });
  
  sp.on('open', function () {
    console.log('port opened... Press reset on the Arduino.');
  });
  
  sp.open(portName, {
    baudRate: 9600,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
    flowControl: false
  });
  
  everyone.now.forward = function() {
  sp.write("w");
  }
  
  everyone.now.back = function() {
  sp.write("s");
  }
  
  everyone.now.left = function() {
  sp.write("a");
  }
  
  everyone.now.right = function() {
  sp.write("d");
  }
  
  sp.on('data', function(data) {
    console.log(data.toString());
  });
  </code></pre>
  
  <p>Arduino Program:</p>
  
  <pre><code>void setup(){
    Serial.begin(9600);
    Serial.write("READY");
    //Set all the pins we need to output pins
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
  }
  
  void loop (){
    if (Serial.available() &gt; 0) {
  
      //read serial as a character
      char ser = Serial.read();
      Serial.write(ser);
      //NOTE because the serial is read as "char" and not "int", the read value must be compared to character numbers
      //hence the quotes around the numbers in the case statement
      switch (ser) {
        case 'w':
          move(8);
          break;
        case 's':
          move(9);
          break;
        case 'a':
          move(10);
          break;
        case 'q':
          move(10);
          move(8);        
          break;
        case 'd':
          move(11);
          break;
        case 'e':
          move(11);
          move(8);
          break;
      }
    }
  }
  
  void move(int pin){
    Serial.print(pin);  
    digitalWrite(pin, HIGH);
    delay(1);
    digitalWrite(pin, LOW);
  }
  </code></pre>