Skip to content

Instantly share code, notes, and snippets.

@claytonbez
Last active July 4, 2018 07:00
Show Gist options
  • Save claytonbez/5970aa29b4790b2e5c16bb67ad107e1b to your computer and use it in GitHub Desktop.
Save claytonbez/5970aa29b4790b2e5c16bb67ad107e1b to your computer and use it in GitHub Desktop.
NodeJS Serial Port Handler
//--------------------------------------------------------------------------------
// SERIAL PORT HANDLER
// This module can be imported into nodejs using require method.
// It uses .start(device,baudrate); to create aconnection to the serial hardware
// After that, it manager disconnects to the hardware and retries.
// It employs an array buffer to slow down the processing of incoming commands
// to a speed that your program can handle
//--------------------------------------------------------------------------------
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var serialport = require('serialport');
//--------------------------------------------------------------------------------
// GLOBAL VAR
//--------------------------------------------------------------------------------
var _d = String.fromCharCode(13);
var sp;
var serialInterval;
var buffer = [];
var hold = false;
var spDev, spBaud;
//--------------------------------------------------------------------------------
// MAIN FUNCTION
//--------------------------------------------------------------------------------
var main = function () {
var self = this;
console.log('SP MODULE')
self.start = function (device, baud) {
spDev = device;
spBaud = baud;
sp = new serialport(device, {
baudrate: baud,
parser: serialport.parsers.readline(_d)
});
sp.on('open', () => {
self.emit('open');
});
sp.on('data', serialdata => {
buffer.push(serialdata);
});
sp.on('close', function () {
self.emit('close');
});
sp.on('error', function (err) {
self.emit('error',err);
});
serialInterval = setInterval(function () {
if (buffer.length > 0 && hold == false) {
hold = true;
var cmd = buffer[0];
buffer.shift();
self.emit('data', cmd);
hold = false;
}
}, 0);
};
self.write = function (str) {
sp.write(str + _d);
},
self.restart = function () {
if (spDev, spBaud) {
setTimeout(function () {
self.emit('reconnect');
self.start(spDev, spBaud);
});
}
}
}
//--------------------------------------------------------------------------------
// EXPORTS
//--------------------------------------------------------------------------------
util.inherits(main, EventEmitter);
module.exports = main;
//--------------------------------------------------------------------------------
// USAGE EXAMPLE WITHIN YOUR PROGRAM
// In my case, i use CHAR 13 or EOL as terminator to the port, you may elect to
// change that within the module code above i used [email protected] module
//--------------------------------------------------------------------------------
var port = require('./SerialPortEventHandler.js');
var serialport = new port();
serialport.on('open', function () {
console.log('port open');
});
serialport.on('data', function (data) {
// already in String() format, no need to convert a buffer.
console.log(`serialport data:${data}`);
});
serialport.on('close', function () {
console.log('port closed');
});
serialport.on('reconnect', function () {
console.log('port trying to re-establish');
});
serialport.start('COM10', 57600);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment