Created
October 16, 2016 03:40
-
-
Save MrTrick/d1f3fb7b9d25b79e1054ab0dc000baca 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
/** | |
* Send a single G-code line, and resolve with the response. | |
* @param {string} line A single line of G-Code | |
* @return {Promise} A promise that resolves with the response line, or rejects with the serial error. | |
*/ | |
send: function(line) { | |
var self = this, port = this.port; | |
//Clear any incoming data | |
return port.flush() | |
//Write the G-code line into the serial port, and set up a waiter with the response. | |
.then(function() { return Promise.all([ | |
port.write(line), | |
new Promise(function(res, rej) { | |
port.once("data", res); | |
setTimeout(function () { port.off("data", res); rej("Timed out waiting for a response"); }, self.options.timeout); | |
}) | |
]) }) | |
//and yield with the output line. | |
.then(function(results) { return results[0]; }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment