1
Problem :
Search for an alternative to read data from a COM5 usb port, with bound 115200.
I’m working with a libelium waspmote sensor, Alternating current meter, this sensor emits data via usb, and I need to use this data in a front-end application in angular 5 that I’m building.
Updating the solution found : serialport in nodejs,
At the end of the post, has an answer of the solution found in more detail.
npm install --save-dev serialport
// This configurations uses serialport lib to catch usb values in serial COM (N) and baudRate
/* eslint-disable node/no-missing-require */
'use strict';
// Use a Readline parser
const SerialPort = require('serialport');
const parsers = SerialPort.parsers;
// Use a `\r\n` as a line terminator
const parser = new parsers.Readline({
delimiter: '\r\n'
});
const port = new SerialPort('COM3', {
baudRate: 115200
});
port.pipe(parser);
port.on('open', () => console.log('Port open'));
// parser.on('data', console.log);
parser.on('data', function(data){
console.log('main.js => retorno =>', data);
sendToLoopback(data);
});
// The parser will emit any string response
/**
* This function uses the request lib to sendo request to api loopback
* @param {*} data
*/
function sendToLoopback(data) {
var request = require('request');
if(data){
request.post(
'http://localhost:3000/api/sender',
{ json: { corrente: data } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('Sender to loopback =>', body)
}
}
);
} else {
console.log('No datas !');
}
}
What kind of information do you want on your app? It is important that it is Realaltime and you will want to save the information available from X in X seconds for later consult?
– Leite
@Milk save the information even bro x in x seconds, understand? insert in the database, will help me already in the problem.
– Philip Developer