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.