Read serial port data with javascript, Node

Asked

Viewed 3,397 times

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?

  • @Milk save the information even bro x in x seconds, understand? insert in the database, will help me already in the problem.

2 answers

2

In Chrome 61 you have a Webusb API available, although I’m not sure if you’re gonna get what you want by using this, I hope I can at least put you on the right track or look for solutions that might be appropriate. You have a blog post that can be useful if this is the kind of solution you want.

Another option, if you want to use Javascript and have a record of the information passed by the device, is to have a service running with Node, which runs on the machine where the device is turned on. You can use a library like this, or others that exist (not searched anymore) to interact with your USB device and collect the information, from there you do whatever you want with it.

My recommendation would be to put that data into a database, and that depends on what you want to do with that information, whether you use Nosql, or a relational database. You can use one hosted by you, or a 3rd party service, for example, from Google or Amazon.

I would probably create a FREE plan (please note that when you reach the limits you can be charged) on a Google (Firebase is an example, you have other options) or Amazon, and probably stored the data in Nosql format to simplify, both must have data solutions in Altime and more suitable to receive data from Iot devices.

Usually these solutions give you an API that you can access after your Angular application without having to create an API yourself. There are also other tools, for example the Athena from Amazon, where you can read the data you saved and do queries to format the data in a certain way.

Another alternative to data collection may be to use one of the examples you found in Java, C++ or etc and replace that application on Node to do so, the rest of the steps I would do the same.

0


Excuse the delay to update this answer here, I had really forgotten, well I used a library called seriaport, she is excellent for working with style sensors Adian, libelium among others.

To connection was via usb, in case the COM3, and the baudrate: 115200, using this library the next step is just to send the data to the back-end in this case, the Loopback.

Below are also extra settings to delete unwanted data, blanks, and so on, to avoid database inconsistency.

// 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 !');
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.