Arduino NodeJS SerialPort速度

时间:2018-02-17 05:28:05

标签: node.js arduino node-serialport

我有一个基本的NodeJS脚本,它将数据发送到我的Arduino,而Arduino正在发回数据。如果从NodeJS到Arduino的通信每两秒或更长时间发生一次,一切正常。如果我比这更快,数据不再被来回发送,或者我的Arduino代码可能不再检测到readStringUntil()

这是我的nodejs代码:

var SerialPort = require('serialport');
const parsers = SerialPort.parsers;

const parser = new parsers.Readline({
  delimiter: '\r\n'
});

var port = new SerialPort('/dev/tty.usbmodem1421',{
  baudRate: 9600,
  dataBits: 8,
  parity: 'none',
  stopBits: 1,
  flowControl: false
});

port.pipe(parser);
port.on('open', () => console.log('Port open'));
parser.on('data', function(data) {
  console.log( 'Received: ' + data );
  console.log( '------------------------------' );
});
var counter = 0;
setInterval(function() {
  counter ++;
  switch(counter % 4) {
    case 0:
      port.write( "pin3-on,");
      break;
    case 1:
      port.write( "pin3-off,");
      break;
    case 2:
      port.write( "pin4-on,");
      break;
    case 3:
      port.write( "pin4-off,");
      break;
  }
}, 5000);

这是我的Arduino代码:

String incomingJSON;

void serialSend() {
  // Send serial data
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& data = jsonBuffer.createObject();
  data["pin2"] = digitalRead(2);
  data["pin3"] = digitalRead(3);
  data["pin4"] = digitalRead(4);
  data.printTo(Serial);
  Serial.println();
}

void serialReceive() {
  if (Serial.available() > 0) {
    incomingJSON = Serial.readStringUntil(",");
    if (incomingJSON.indexOf( "pin3-on" ) >= 0) {
      digitalWrite(3,HIGH);
    } else if (incomingJSON.indexOf( "pin3-off" ) >= 0) {
      digitalWrite(3,LOW);
    } else if (incomingJSON.indexOf( "pin4-on" ) >= 0) {
      digitalWrite(4,HIGH);
    } else if (incomingJSON.indexOf( "pin4-off" ) >= 0) {
      digitalWrite(4,LOW);
    }
  }
}

void setup() {
  pinMode( 2, INPUT );
  pinMode( 3, OUTPUT );
  pinMode( 4, OUTPUT );
  Serial.begin(9600);
  while (!Serial) continue;
}

void loop() {
  serialReceive();
  serialSend();
  delay( 500 );
}

知道什么可能会减慢速度吗?或者这仅仅是串口通信的本质?

1 个答案:

答案 0 :(得分:0)

这不是一个解决方案,但我已经修改了上面的代码,使用JSON将nodejs形成为Arduino,从Arduino到nodejs:

这是我的nodejs代码:

var SerialPort = require('serialport');
const parsers = SerialPort.parsers;

const parser = new parsers.Readline({
    delimiter: '\r\n'
});

var port = new SerialPort('/dev/tty.usbmodem1421',{ 
    baudRate: 9600,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
    flowControl: false
});

port.pipe(parser);

port.on('open', function() {});

parser.on('data', function(data) {

    console.log( 'Received: ' + data );

});

var counter = 0;

setInterval(function(){

    counter ++;

    switch( counter % 6 )
    {
        case 0:
            port.write( "{3:1}" );
            // console.log( "{3:1}" );
            break;
        case 1:
            port.write( "{3:0}" );
            // console.log( "{3:0}" );
            break;
        case 2:
            port.write( "{4:1}" );
            // console.log( "{4:1}" );
            break;
        case 3:
            port.write( "{4:0}" );
            // console.log( "{4:0}" );
            break;
        case 4:
            port.write( "{3:1,4:1}" );
            // console.log( "{3:1,4:1}" );
            break;
        case 5:
            port.write( "{3:0,4:0}" );
            // console.log( "{3:0,4:0}" );
            break;
    }

}, 2000);

这是我的Arduino代码:

#include <ArduinoJson.h>

String lastOutgoingJSON;
String nextOutgoingJSON;

void serialSend() {

  // Create a new JSON object
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& data = jsonBuffer.createObject();

  // Populate JSON object with pin data
  data["2"] = digitalRead(2);
  data["3"] = digitalRead(3);
  data["4"] = digitalRead(4);

  // Convert pin data to a JSON string
  nextOutgoingJSON = "";
  data.printTo(nextOutgoingJSON);

  // Check if JSON data has changed since last time
  if( lastOutgoingJSON != nextOutgoingJSON )
  {
    Serial.println(nextOutgoingJSON);
  }

  // Update the last JSON data wit hthe new JSON data
  lastOutgoingJSON = nextOutgoingJSON;

}

void serialReceive() {

  // Check if there is serial data available
  if (Serial.available() > 0) {

    // Place incoming JSON string into a variable
    String incomingJSON = Serial.readString();

    // Convert JSON string to a JSON object
    StaticJsonBuffer<200> JSONBuffer;
    JsonObject& parsedJSON = JSONBuffer.parseObject(incomingJSON);

    // Check for pin commands and exectue
    if (parsedJSON.containsKey("3"))
      digitalWrite(3,parsedJSON["3"]);

    if (parsedJSON.containsKey("4"))
      digitalWrite(4,parsedJSON["4"]);

  }

}

void setup() {

  pinMode( 2, INPUT );
  pinMode( 3, OUTPUT );
  pinMode( 4, OUTPUT );

  Serial.begin(9600);
  while (!Serial) continue;

}


void loop() {

  serialReceive();
  serialSend();

  delay( 100 );
  delay( 100 );

}