Last active
June 17, 2016 03:53
-
-
Save aman-tiwari/bafd2a76b4705395068c4f9b01e3ab91 to your computer and use it in GitHub Desktop.
node server for talking to XY Plotter
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
var express = require('express') | |
var bodyparser = require('body-parser') | |
var sp = require('serialport') | |
var SerialPort = sp.SerialPort | |
var app = express() | |
app.all('/', function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS"); | |
res.header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, Session"); | |
next(); | |
}); | |
var curx = 0; | |
var cury = 0; | |
var curs = 100; | |
//parse application/json | |
app.use(bodyparser.json()); | |
// Expects requests of the form {x : <float in range [0, 1]> y : <float in range [0, 1]>]} | |
app.use(function (req, res, next) { | |
console.log(req.body); | |
// For CORS, you might need to remove this | |
if (req.method === "OPTIONS") { | |
return res.status(200).end(); | |
} | |
// Flip coords | |
var x = 1 - parseFloat(req.body.x); | |
var y = 1 - parseFloat(req.body.y); | |
if(x == NaN) return; | |
// The actual GCODE commands | |
// Other GCODE commands may also work | |
// Also, look at the mDraw python documentation | |
console.log("G1 X" + (x * 380) + ".00 Y" + (y * 310) + ".00\n"); | |
port.write("G1 X" + (x * 380) +".00 Y" + (y * 310) + ".00\n", | |
function(err, written) { | |
curs = curs += 5; | |
console.log("M2 U" + curs + " D" + curs + "\n") | |
port.write("M2 U" + curs + " D" + curs + "\n", function(err, written) { | |
port.write("G1 X0.00 Y0.00\n", function() {port.write('M10\n')}); | |
}) | |
}); | |
next(); // Continuation | |
}) | |
sp.list((err, ports) => {for (var port of ports) { console.log(port) } } ) | |
var port = new SerialPort('/dev/cu.wchusbserial1410', { | |
parser : sp.parsers.readline('\n'), | |
baudrate: 115200}) | |
port.on('open', function () { port.write('M11\n', function() {port.write('M11\n')}) }) | |
port.on('data', function (data) { | |
console.log('Data: ' + data); | |
}); | |
app.listen(8999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment