Skip to content

Instantly share code, notes, and snippets.

@MrTrick
Created October 16, 2016 03:40
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 MrTrick/d1f3fb7b9d25b79e1054ab0dc000baca to your computer and use it in GitHub Desktop.
Save MrTrick/d1f3fb7b9d25b79e1054ab0dc000baca to your computer and use it in GitHub Desktop.
/**
* 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