Created
February 8, 2015 06:34
-
-
Save houhr/378085abfd8f1dbec43c to your computer and use it in GitHub Desktop.
This file contains 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
var serialport = require('serialport'), | |
SerialPort = serialport.SerialPort, // make a local instance | |
portName = "/dev/cu.usbmodem1451", // replace the string with your own | |
latestData = 0; | |
var myPort = new SerialPort(portName, { | |
baudRate: 9600, | |
// look for return and newline at the end of each data packet | |
parser: serialport.parsers.readline("\r\n") | |
}); | |
myPort.on('open', showPortOpen); | |
myPort.on('data', saveLatestData); | |
myPort.on('close', showPortClose); | |
myPort.on('error', showError); | |
function showPortOpen() { | |
console.log('port open. Data rate: ' + myPort.options.baudRate); | |
} | |
function saveLatestData(data) { | |
console.log(data); | |
latestData = data; | |
} | |
function showPortClose() { | |
console.log('port closed.'); | |
} | |
function showError(error) { | |
console.log('Serial port error: ' + error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment