tspree
tspree

Reputation: 41

serialport node.js read issue with buffer

Objective
I need to fetch data over serial port for a batch of commands like

write to Serial: 'command'
expected response from Serial receiver: HEX Value

and I used the Serial Port package.


serial.js

var serialport = require('serialport');
var Readline = serialport.parsers.Readline; // make instance of Readline parser
var parser = new Readline(); // make a new parser to read ASCII lines


// list serial ports:
serialport.list(function (err, ports) {
    ports.forEach(function (port) {
        console.log("Serial Device: " + port.comName);
    });
});

// get port name from the command line:
var path = '/dev/cu.SLAB_USBtoUART' ;

var myPort = new serialport(path ,{
    baudRate: 230400,
});

// Read Line Parser
myPort.pipe(parser); // pipe the serial stream to the parser


// Handling Events
myPort.on('open', showPortOpen);
// Calling with set interval
setInterval(writeToSerial, 2000);
myPort.on('data', readSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);

// Callback Functions
function showPortOpen() {
    console.log('port open. Data rate: ' + myPort.baudRate);
}


function writeToSerial() {
    myPort.write('?1VB');
}

function readSerialData(data) {
    buff = data;
    console.log(data);
}

function showPortClose() {
    console.log('port closed.');
}

function showError(error) {
    console.log('Serial port error: ' + error);
}

The above code is basically setting up the port and writing data to the serial port using setInterval(function(){}, time) followed by a read data event and its callback function. The read data event is working fine but the data received is in the form of buffer and it while reading the data it breaks into chunks. Here is the output attached Output

Please help me to receive the full buffer data instead of random bytes.

Upvotes: 1

Views: 7939

Answers (2)

Marcus
Marcus

Reputation: 1162

The data is being output as a byte buffer because you have subscribed the readSerialData event handler to the port instead of subscribing it to the parser. See also the Serialport documentation on Parsers

You can simply fix it by replacing the following line:

myPort.on('data', readSerialData);

with

parser.on('data', readSerialData);

Upvotes: 5

tagyoureit
tagyoureit

Reputation: 1286

Serial port is correctly receiving the repeating values (in hex) of 0x0c, 0xda, 0x0c, 0xda, etc.

Because you have myPort.on('data', readSerialData); and readSerialData is outputting directly to the console it appears as <Buffer 0c da> because that is what is received and that string is how a Buffer object is treated when you print it.

There are built-in parsers that will take care of different incoming formats (for example, only emitting data when a new line character is received or after a certain number of specific bytes).

Depending on what you want to do, you can use one of those parsers or if they don't fit your needs just keep pushing the data into a new array, buffer or other object for processing later.

If you want to do something with those hex values there are many questions already posted like convert buffer to array and the Node documentation itself that are very handy.

Upvotes: 2

Related Questions