首页 > 解决方案 > Arduino和nodejs脚本之间的通信出现未知错误

问题描述

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

RX led 正在闪烁,所以我认为我在 Arduino 方面做错了什么,但我对此一无所知。

我正在关注本教程

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

});

标签: node.jsarduino

解决方案


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

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

我不确定你的意思是什么 RX LED,但它闪烁是有意义的,因为你正在从你的计算机发送数据,但由于端口没有在你的 Arduino 代码上初始化,所以 micro 没有监听数据,所以什么都没有已收到。


推荐阅读