问题
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