1

I'm trying to get a message from my Arduino to NodeJS using SerialPort. Here is my NodeJS code:

var SerialPort = require('serialport');
var port = new SerialPort('/dev/tty.usbmodem1421',{
  baudRate: 9600
});

port.on('data',(data) => {
  console.log(data);
});

And here is my Arduino code:

void setup() {
    Serial.begin(9600);
}

void loop() {
    Serial.println("Message");
    delay( 1000 );
}

It's working. However, the message I am receiving looks like this:

<Buffer 4d 65 73>
<Buffer 73 61 67 65>
<Buffer 0d 0a>

I've tried a lot of different things to try and read the message properly. If it makes a difference I would eventually like my message to be JSON. Here are a few things I have tried:

I've added this:

parser: SerialPort.parsers.readline('\r\n')

I think that might be out dated, as I get readline is not a function errors messages.

I have tried using a Readline object:

var parser = new Readline();
parser.on('data', function(data){console.log( data );});

Any help would be very much appreciated!

2 Answers 2

2

For version 12, the new docs say:

const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const port = new SerialPort({ path: '/dev/ROBOT', baudRate: 14400 })

const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }))
parser.on('data', console.log)

https://serialport.io/docs/api-parser-readline

Sign up to request clarification or add additional context in comments.

Comments

1

Ends up I was looking at the wrong documentation. The version of SerialPort I was using is 6.x, most of the help out there is for a much older version. The solution was in the GitHub most recent examples:

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

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

const port = new SerialPort('/dev/tty-usbserial1', {
  baudRate: 9600
});

port.pipe(parser);

parser.on('data', console.log);

The process of parsing has changed dramatically.

https://github.com/node-serialport/node-serialport/blob/master/examples/readline.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.