Can't read data from Arduino Uno with serialport Node JS library

624 views Asked by At

I'm using the [email protected] Node JS library, and have used it before (a prior version, 4.0.7) to communicate successfully with a GSM modem.

Now I'm using it with an Arduino Uno. However I can't seem to read data from it the way I used to with the GSM modem.

This is what I'm trying to do:

this.parser.on('data', (buffer) => {
  console.log('in on data');
  data += buffer.toString();
  console.log("DATA: " + data);
  var parts = data.split('\r\n');
  console.log('PARTS:', parts);
  data = parts.pop();
  console.log(data)
  console.log('POPPED DATA: ' + data);
})

I get nothing from it. I even tried using a parser, hence the this.parser.on. However I can't get it to work.

I can get data using this though:

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

But since I'm trying to process the data in the first place, it's useless.

Here are my serialport settings:

_.defaults(options, {
  baudrate: 9600,
  dataBits: 8,
  parity: 'none',
  stopBits: 1,
  flowControl: false,
  autoOpen: false,
});
this.options = options;
this.serial = new SerialPort(this.port, this.options);
this.eventEmitter = new events.EventEmitter();
this.parser = new Readline({ delimiter: '\n' });

I'm invoking the serialport from a class. And yes, I get the port to open.

I can also write to the port.

1

There are 1 answers

0
Agatha Uy On BEST ANSWER

Resolved it, apparently the "buffer" variable in the on.data portion of my sample code already receives a string. Hence I can just print out the string as console.log(buffer).

The weird part is why the data += buffer.toString() portion behaves weirdly. I would expect that I would get at least something in the first console.log after that line. But it returns blank.