Created
October 5, 2015 14:17
-
-
Save auycro/71210f1787be4c228e8e to your computer and use it in GitHub Desktop.
NodeJS Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copyright (c) 2015 Gumpanat Keardkeawfa | |
//Licensed under the MIT license | |
//Add Lib | |
var srListPort = require("serialport"); //.SerialPort | |
var SerialPort = require("serialport").SerialPort | |
//List port (you may not need this line) | |
srListPort.list(function (err, ports) { | |
ports.forEach(function(port) { | |
console.log(port.comName); | |
console.log(port.pnpId); | |
console.log(port.manufacturer); | |
}); | |
}); | |
var arduinoport = "YOUR PORT HERE"; | |
var serialPort = new SerialPort(arduinoport, { | |
baudrate: 9600, | |
// defaults for Arduino serial communication | |
dataBits: 8, | |
parity: 'none', | |
stopBits: 1, | |
flowControl: false | |
}); | |
var i = 0; | |
var sendcmd = ''; | |
//Async data recieve from Arduino | |
serialPort.on('data', function(data) { | |
console.log('received: '+data.toString()); | |
switch(data.toString()) { | |
case 'hello': | |
sendcmd = 'hello'; | |
break; | |
case 'finish': | |
process.exit(1); | |
break; | |
default: | |
i++; | |
sendcmd = i; | |
break; | |
} | |
if (i > 5) { | |
sendcmd = 'close'; | |
} | |
serialPort.write(sendcmd + ';'); // use 'E' as end command character | |
console.log('Send: '+sendcmd); | |
sendcmd = ''; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment