2

I'm sending a string from my sketch over Serial port as follows:

Serial.println(F("Found ILI9341 LCD driver"));

On the other side, I have a nodejs app using the serialport npm module, with something like this:

var SerialPort = require('serialport')
var serialPort = new SerialPort('/dev/ttyACM1', {
  baudrate: 9600
})

// it opens the connection and register an event 'data'
serialPort.on('open', function () {
  console.log('Communication is on!')
})

// when your app receives data, this event is fired
// so you can capture the data and do what you need
serialPort.on('data', function (data) {
  console.log('data received: ' + data)
})

Communication works, but on nodejs side I get the output chunked, as if it had line breaks, like this:

Communication is on!
data received: Pa
data received: int!
data received: 

data received: Fou
data received: nd I
data received: LI93
data received: 41 L
data received: CD d
data received: rive
data received: r

Why does this happen?

5

It seems I needed to add a parser to SerialPort nodejs module, in order to specify how the input should be treated.

I changed my code to this:

var SerialPort = require('serialport')
var Readline = SerialPort.parsers.Readline

var serialPort = new SerialPort('/dev/ttyACM0', {
  baudrate: 9600
})

var parser = new Readline()
serialPort.pipe(parser)
parser.on('data', function (data) {
  console.log('data received: ' + data)
})

serialPort.on('open', function () {
  console.log('Communication is on!')
})

Basically, I'm specifying a Readline parser, which matches with the Serial.println function calls on my arduino sketch. Now the console output works as expected:

Communication is on!
data received: Found ILI9341 LCD driver
  • You do not need to pipe(): you can new SerialPort('/dev/ttyACM0', { baudrate: 9600, parser: Readline("\r\n") }). – Edgar Bonet Jul 22 '17 at 12:05
  • @EdgarBonet I'm not sure about that, because according to npmjs.com/package/… you need to register for the 'data' event in the parser, not the port, so you need the reference to the parser to do a parser.on('data', yourFunction). Recommended usage seems to be using pipe(). – jotadepicas Jul 22 '17 at 16:40
  • 1
    You are right. I was using an older version of the serialport module that doesn't work in the same way. Sorry for the confusion. – Edgar Bonet Jul 22 '17 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.