node.js to read usb port signals

若如初见. 提交于 2020-01-10 14:26:17

问题


I have installed serialport module for node.js using npm.(npm install serialport). Now I would like to send some messages from my android phone to node application via usb port of my computer(assuming node can read serial port signals). Has anyone done this before?

Thanks in advance.


回答1:


Why, yes, it's certainly doable. There's a plenty of examples listed at the serialport package homepage itself.

Your node.js application will start with...

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/tty-usbserial1");

serialPort.on('data', function(data) { /* processing data */ });



回答2:


For testing your ports on Windows (you may have to install sf "npm install sf"):

var serialport = require('serialport');
var sf = require('sf');

serialport.list(function (err, results) {
  if (err) {
    throw err;
  }

  for (var i = 0; i < results.length; i++) {
    var item = results[i];
    console.log(sf('{comName,-15} {pnpId,-20} {manufacturer}', item));
  }
});

Output should be something like:

COM8 FTDIBUS\VID_0403+PID_6001+A100DKP7A\0000 FTDI
COM1 ACPI\PNP0501\4&2E24A907&0 (Standardanschlusstypen)

Now use the port your device is connected to, in my case COM8:

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("COM8");

regards



来源:https://stackoverflow.com/questions/12077223/node-js-to-read-usb-port-signals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!