Skip to content

Instantly share code, notes, and snippets.

@houhr
Created February 8, 2015 06:34
Show Gist options
  • Save houhr/378085abfd8f1dbec43c to your computer and use it in GitHub Desktop.
Save houhr/378085abfd8f1dbec43c to your computer and use it in GitHub Desktop.
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