0

I am trying to show sensor data and it should so something like:

0,0
0,128
0,150
120,250

code

var SerialPort = require("serialport");
var serialport = new SerialPort("COM4");

 serialport.on('open', function(){
    console.log('Serial Port Opend');

    serialport.on('data', function(data){
      console.log(data);

    nsp.emit('live-data', data);
  });
});

but I get :

enter image description here

I tried data[0] then some weird data shows up and not wht i am looking for. if I check data1 then it shows undefined.

Murlidhar Fichadia
  • 2,589
  • 6
  • 43
  • 93

1 Answers1

3

Found this in the docs for node serialport:

Event: "data"

The data event puts the port in flowing mode. Data is emitted as soon as it's received. Data is a Buffer object with a varying amount of data in it. The readLine parser converts the data into string lines. See the parsers section for more information on parsers, and the Node.js stream documentation for more information on the data event.

Kind: event emitted by SerialPort

(https://github.com/EmergingTechnologyAdvisors/node-serialport/blob/5.0.0-beta8/README.md#module_serialport--SerialPort+event_data)

Which points you to this:

(https://github.com/EmergingTechnologyAdvisors/node-serialport/blob/5.0.0-beta8/README.md#module_serialport--SerialPort.parsers)

SerialPort.parsers : object

The default Parsers are Transform streams that parse data in different ways to transform incoming data.

To use the parsers, you must create them and then pipe the Serialport to the parser. Be careful to only write to the SerialPort object and not the parser.

Examples from the docs:

Example

var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = new Readline();
port.pipe(parser);
parser.on('data', console.log);
port.write('ROBOT PLEASE RESPOND\n');

// Creating the parser and piping can be shortened to
var parser = port.pipe(new Readline());

To use the ByteLength parser, provide the length of the number of bytes:

var SerialPort = require('serialport');
var ByteLength = SerialPort.parsers.ByteLength
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(new ByteLength({length: 8}));
parser.on('data', console.log);

To use the Delimiter parser, provide a delimiter as a string, buffer, or array of bytes:

var SerialPort = require('serialport');
var Delimiter = SerialPort.parsers.Delimiter;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(new Delimiter({delimiter: Buffer.from('EOL')}));
parser.on('data', console.log);

To use the Readline parser, provide a delimiter (defaults to '\n')

var SerialPort = require('serialport');
var Readline = SerialPort.parsers.Readline;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(Readline({delimiter: '\r\n'}));
parser.on('data', console.log);

To use the Ready parser provide a byte start sequence. After the bytes have been received data events will be passed through.

var SerialPort = require('serialport');
var Ready = SerialPort.parsers.Ready;
var port = new SerialPort('/dev/tty-usbserial1');
var parser = port.pipe(Ready({data: 'READY'}));
parser.on('data', console.log); // all data after READY is received
chris van hooser
  • 388
  • 3
  • 11