node-serialport是一个Node.js的包,用来对串口数据进行读写操作。
基本示例代码:
var SerialPort = require("serialport").SerialPortvar serialPort = new SerialPort("/dev/tty-usbserial1", { baudrate: 57600}, false); // this is the openImmediately flag [default is true]serialPort.open(function (error) { if ( error ) { console.log('failed to open: '+error); } else { console.log('open'); serialPort.on('data', function(data) { console.log('data received: ' + data); }); serialPort.write("ls\n", function(err, results) { console.log('err ' + err); console.log('results ' + results); }); }});罗列所有串口:
var serialPort = require("serialport");serialPort.list(function (err, ports) { ports.forEach(function(port) { console.log(port.comName); console.log(port.pnpId); console.log(port.manufacturer); });});串口配置:
baudRate
dataBits
stopBits
parity
rtscts
xon
xoff
xany
flowControl
bufferSize
parser
encoding
dataCallback
disconnectedCallback
platformOptions-setsplatformspecificoptions,seebelow.
目前已有很多项目在使用这个包进行串口处理:
Johnny-Five-FirmatabasedArduinoFramework.
Cylon.js-JavaScriptRobotics,ByYourCommand.
node-l8smartlight(source)AnodelibrarytocontroltheL8SmartlightviaBluetoothorUSBport
firmataTalknativelytoArduinousingthefirmataprotocol.
tmpadsource-aDIYmidipadusinginfrared,arduino,andnodejs.Video
duino-AhigherlevelframeworkforworkingwithArduinosinnode.js.
ArduinoDrinkingGameExtravaganza-AKA"TheRussian"ahexidecimaldrinkinggameforgeeksbyUxebupresentedatJSConfEU2011.
Arduinocontrollingpopcorn.js-Controllingapopcorn.jsvideowithanArduinokit.
RoboticJavaScript-Thefirstlivepresentationofthenode-serialportcodesetaspresentedatJSConfEU2010.
devicestack-Thismodulehelpsyoutorepresentadeviceanditsprotocol.
reflectaAcommunicationprotocolthatcombinesArduinoLibrariesandNodeJSintoanintegratedsystem.
rc4pt-node-ControlPopcorntimewithanInfraredreceiverandArduino.
评论