firmata/firmata.js

Firmata - Multiple SoftwareSerial

Closed this issue · 13 comments

Hey, guys,

I'm trying to communicate serial devices (pins rx / tx) on the arduino with StandardFirmataPlus, in NodeJS using the "firmata" module, do the following:

var firmata = require("firmata");

var board = new firmata('/dev/ttyUSB0');

board.on("ready", function () {

console.log('Board is ready');

var SerialPort = board.SERIAL_PORT_IDs.DEFAULT;

board.serialConfig({
portId: SPort,
baud: 115200,
rxPin: 50,
txPin: 52
});

board.serialRead(SPort, 1024, function(data) {

if (data) {
console.log(new Buffer.from(data).toString("ascii"));
board.serialWrite(SPort, new Buffer.from('Hello', 'ascii'));
}

});

});

I saw that I need to use a serial port ID, would this be for communication with the host computer?

But what I really want to know is: how can I connect more than one SoftwareSerial device like that?

I'm using the serial for YS-IRTM infrared module.

dtex commented

Arduino Uno?

serialPort is unused and SPort is undefined. Are these supposed to be the same?

Sorry I forgot to get the updated code,

here it is:

var firmata = require("firmata");

var board = new firmata('/dev/ttyUSB0'); //My Arduino Mega 2560 on /dev/ttyUSB0

board.on("ready", function () {
console.log('Board is ready');

var SerialPort = board.SERIAL_PORT_IDs.DEFAULT;

board.serialConfig({
portId: SerialPort,
baud: 115200,
rxPin: 50,
txPin: 52
});

board.serialRead(SerialPort, 1024, function(data) {
if (data) {
console.log(new Buffer.from(data).toString("ascii"));
board.serialWrite(SerialPort, new Buffer.from('Hello', 'ascii'));
}
});

});

dtex commented

Oh wait, you're on a Mega right?

dtex commented

On a mega I would try to use one of the hardware serial ports. Software serial is not reliable at 115,200. You wouldn't want to go over 57,600.

dtex commented

Here's the code updated to use hardware serial. Looking at this I can see that hardware serial 1 is attached to pins 18(TX) and 19(RX). You don't have to specify them.

const firmata = require("firmata");
const board = new firmata('/dev/ttyUSB0');
const SerialPort = board.SERIAL_PORT_IDs.HW_SERIAL1;

board.on("ready", () => {
  console.log('Board is ready');
  
  board.serialConfig({
    portId: SerialPort,
    baud: 115200
  });

  board.serialRead(SerialPort, 1024, (data) => {
    if (data) {
      console.log(new Buffer.from(data).toString("ascii"));
      board.serialWrite(SerialPort, new Buffer.from('Hello', 'ascii'));
    }
  });
});

Yeah man, it works. But how i do it with others pins, just declare in serialConfig() options?

Can i use another pins?

dtex commented

Yes, but...

You can use any GPIO pin, but only with software serial. Software serial is limited to 57,600 baud max. Can the device you are talking to be configured to use <=57,600? If not then you'll need to use one of the hardware pin combinations (14 & 15, 16 & 17, or 18 & 19).

The infrared module works at 9600 baud rate. No problem, right?

image

That's the module.

dtex commented

Right, 9600 should be fine. I've used software serial @ 9600 baud with GPS breakouts no problem.

const firmata = require("firmata");
const board = new firmata('/dev/ttyUSB0');
const serialPort = board.SERIAL_PORT_IDs.SW_SERIAL0;

board.on("ready", () => {
  console.log('Board is ready');
  
  board.serialConfig({
    baud: 9600,
    rxPin: 50,
    txPin: 52
  });

  board.serialRead(SerialPort, 1024, (data) => {
    if (data) {
      console.log(new Buffer.from(data).toString("ascii"));
      board.serialWrite(SerialPort, new Buffer.from('Hello', 'ascii'));
    }
  });
});
dtex commented

According to this spec sheet, 9600 is the default speed.