Created
April 7, 2016 14:06
-
-
Save renebigot/202b1237cc7d99f25962727912e94bcc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//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