Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active June 26, 2018 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joepie91/1e5d84fc1c32995bea10131fd7c7d9cb to your computer and use it in GitHub Desktop.
Save joepie91/1e5d84fc1c32995bea10131fd7c7d9cb to your computer and use it in GitHub Desktop.
// file: serial.js
const SerialPort = require('serialport')
module.exports = function() {
const port = SerialPort('path/to/serial/port')
const e = new events.EventEmitter()
// listen for incoming serial data
port.on('data', function (data) {
// emit event 'serialdata'
e.emit('serialdata', data);
})
return e
}
// file app.js
const serial = require('./serial')
const events = require('events')
const e = serial()
e.on('serialdata', ()=>{
console.log('Serial data received')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment