Arduino和Node.js脚本之间的通信发生未知错误

时间:2019-07-15 16:46:47

标签: node.js arduino

我正在尝试从nodejs脚本向Arduino发送一个两个字母的字符串(例如“ cc”),但没有收到任何错误,但arduino并未按照预期的方式进行响应。

RX指示灯闪烁,所以我认为我在Arduino方面做错了事,但我对此一无所知。

我正在关注this tutorial

Arduino代码:


// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B connections
int enB = 3;
int in3 = 5;
int in4 = 4;
String var1;
const byte DATA_MAX_SIZE = 32;
char data[DATA_MAX_SIZE];   // an array to store the received data

void setup()
{
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}
void receiveData() {
  static char endMarker = '\n'; // message separator
  char receivedChar;     // read char from serial port
  int ndx = 0;          // current index of data buffer  // clean data buffer
  memset(data, 32, sizeof(data));  // read while we have data available and we are
  // still receiving the same message.
  while (Serial.available() > 0) {
    receivedChar = Serial.read();    if (receivedChar == endMarker) {
      data[ndx] = '\0'; // end current message
      return;
    }    // looks like a valid message char, so append it and
    // increment our index
    data[ndx] = receivedChar;
    ndx++;    
    if (ndx >= DATA_MAX_SIZE) {
      break;
    }
  } 
  memset(data, 32, sizeof(data));
}
void loop()
{
  analogWrite(enA, 255);
  analogWrite(enB, 255);
  receiveData();
  if (data[0] == 'c') { //cc
    if (data[1] == 'c') {
      digitalWrite(in3, LOW);
      digitalWrite(in4, LOW);
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
    }
    else if (data[1] == 'o') { //co
      digitalWrite(in3, LOW);
      digitalWrite(in4, LOW);
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
    }
  }
  else if (data[0] == 'w') { //wu
    if (data[1] == 'u') {
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW);
    }
    if (data[1] == 'd') { //wd
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH);
    }
  }

  else {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
  }
}

Node.js代码:


const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');const port = new SerialPort('COM3', { baudRate: 9600 });
const parser = port.pipe(new Readline({ delimiter: '\n' }));
var stdin = process.stdin;

stdin.setRawMode( true );

stdin.resume();

stdin.setEncoding( 'utf8' );

  // on any data into stdin
stdin.on( 'data', function( key ){
//other if/elses
  if (key == 'j') {

        port.write('cc\n', (err) => {
          if (err) {
              return console.log('Error on write: ', err.message);
            }
              console.log('message written');
            });
  }
  if (key == 'l') {

        port.write('oc\n', (err) => {
          if (err) {
              return console.log('Error on write: ', err.message);
            }
              console.log('message written');
            });
  }
  if (key == 'i') {

        port.write('wu\n', (err) => {
          if (err) {
            return console.log('Error on write: ', err.message);
          }
            console.log('message written');
          });
  }
  if (key == 'k') {

        port.write('wd\n', (err) => {
          if (err) {
              return console.log('Error on write: ', err.message);
            }
              console.log('message written');
            });
  }
  if (key == '\u0003') { process.exit(); }

});

1 个答案:

答案 0 :(得分:1)

您错过了您遵循的教程的第一行代码:

 Serial.begin(9600); // Starts the serial communication

我不确定您所指的RX LED是什么,但是它闪烁是有意义的,因为您正在从计算机发送数据,但是由于未在Arduino代码上初始化端口,因此Micro不会监听数据,因此什么也没收到。