Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

javascript - Serial port not working?

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.

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.

When sending via node the RX light on the arduino flashes but nothing else happens.

Can anyone help?

Node.js program:

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());
});

Arduino Program:

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() > 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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I recently dabbled into this. The Arduino automatically resets when it receives serial communication from most things other than the Arduino IDE. This is why you can send from the IDE but not node.js.

I have an Uno and put a capacitor between Reset and Ground.Here's a page with some good info on the subject.
Good luck. http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...