Read serial port data with javascript, node

0

Good afternoon, I need a framework, or some alternative to read data from a COM5 usb port, with bound 115200. Has anyone ever used any type of tool? I have seen many in java, c ++ etc.

I'm working with a sensor libelium waspmote, ac meter, this sensor emits data via usb, and I need to use that data in an angular 5 front-end application that I'm building.

Update: I need to store this data in my database, so any alternative that performs this procedure, is already good for me, thank you for the help.

Update 2, I'm using the serialport library on nodejs

npm install --save-dev serialport

var SerialPort = require('serialport');
var port = new SerialPort("COM5", {
    baudRate: 115200
  });

port.on('open', function() {
  port.write('main screen turn on', function(err) {
    if (err) {
      return console.log('O cabo usb não está conectado: ', err.message);
    }
    console.log('message written');
  });
});

// open errors will be emitted as an error event
port.on('data', function (data) {
    // var dados = data;
    // console.log(dados.toString());
    var stringDatas = data.toString();
    // var numberDatas = Number(stringDatas);
    if (stringDatas !== 'start'){
        console.log(stringDatas);
        lenghtData(stringDatas);
    }

  });


  function lenghtData(value) {
    var result = value.lenght;    
    return result;
  }
    
asked by anonymous 28.06.2018 / 17:31

1 answer

1

On Chrome 61 you have a WebUSB API available , although you're not sure if you'll get what you want with this, I hope that at least can put you on the right path or search for solutions that may be appropriate. You have a blog post that might be useful if it is this type 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 connected. You can use a library like this , or others that exist (I did not search any more) 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 this data in a database, and this will depend 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, Google or Amazon.

I would probably create a FREE plan (note that when you reach the limits you can be charged) on a Google service (Firebase is example, you have other options ) or Amazon , and probably saved the data in NoSQL format for simplicity, both should have solutions for realtime data and more suitable for receiving data from IoT devices.

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

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

    
28.06.2018 / 18:24