Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save renebigot/202b1237cc7d99f25962727912e94bcc to your computer and use it in GitHub Desktop.
Save renebigot/202b1237cc7d99f25962727912e94bcc to your computer and use it in GitHub Desktop.
//Found at : http://stackoverflow.com/questions/23684802/node-js-serialport-synchronous-write-read
function Device (serial) {
this._serial = serial;
this._queue = queue;
this._busy = false;
this._current = null;
var device = this;
serial.on('data', function (data) {
if (!device._current) return;
device._current[1](null, data);
device.processQueue();
});
}
Device.prototype.send = function (data, callback) {
this._queue.push([data, callback]);
if (this._busy) return;
this._busy = true;
this.processQueue();
};
Device.prototype.processQueue = function () {
var next = this._queue.shift();
if (!next) {
this._busy = false;
return;
}
this._current = next;
this._serial.write(next[0]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment