/*
 * SerialServer.js
 * Kylie Dale and Michael Guida
 */


/*
	from http://www.tigoe.com/pcomp/code/arduinowiring/1096/
	to run: node SerialServer.js /dev/tty.usbmodem728331 (replace with your device port)

	serialServer.js
	a node.js app to read serial strings and send them to webSocket clients
	requires:
		* node.js (http://nodejs.org/)
		* express.js (http://expressjs.com/)
		* socket.io (http://socket.io/#how-to-use)
		* serialport.js (https://github.com/voodootikigod/node-serialport)
	based on the core examples for socket.io and serialport.js
	created 21 Aug 2012
	modified 11 Feb 2014
	by Tom Igoe
	Patches and improvements suggested by Steve Klise, Lia Martinez, and Will Jennings
*/

var serialport = require("serialport"),			// include the serialport library
	SerialPort  = serialport.SerialPort,		// make a local instance of serial
	portName = process.argv[2],					// third word of the command line should be serial port name
	express = require('express'),
	http = require('http'),
	app = express(),
	server = http.createServer(app),
	io = require('socket.io').listen(server),
	connected = false;

app.get('/', function (req, res) {
	res.sendFile(__dirname + '/public/index.html');
});

app.use(express.static(__dirname, 'public'));

server.listen(8080, function(){
	console.log("Express server listening on port " + 8080);
});

// listen for new socket.io connections:
io.sockets.on('connection', function (socket) {
	// notify when connected
	console.log('socket.io connected');
	connected = true;

	// open the serial port using the name entered on the command line:
	var myPort = new SerialPort(portName, {
		// look for return and newline at the end of each data packet:
		parser: serialport.parsers.readline("\r\n")
	});
	console.log('serial port connected');

	// if there's a socket client, listen for new serial data:
	myPort.on('data', function (data) {
		// send a serial event to the web client with the data:
		socket.emit('serialEvent', data);
	});

	// notify when disconnected
	socket.on('disconnect', function() {
		console.log('socket.io disconnected');
		connected = false;
		myPort.close(function (err) {
		    console.log('serial port disconnected');
				console.error(err);
		});
	});

	// if the client sends you data, act on it:
	socket.on('data', function(data) {
		data = JSON.stringify(data);
		myPort.write(data);
	});
});

@@ -53,647 +53,88 @@
 a%0A *
-/%0A%0A%0A/*%0A%09from http://www.tigoe.com/pcomp/code/arduinowiring/1096/%0A%09to run: node SerialServer.js /dev/tty.usbmodem728331 (replace with your device port)%0A%0A%09serialServer.js%0A%09a node.js app to read serial strings and send them to webSocket clients%0A%09requires:%0A%09%09* node.js (http://nodejs.org/)%0A%09%09* express.js (http://expressjs.com/)%0A%09%09* socket.io (http://socket.io/#how-to-use)%0A%09%09* serialport.js (https://github.com/voodootikigod/node-serialport)%0A%09based on the core examples for socket.io and serialport.js%0A%09created 21 Aug 2012%0A%09modified 11 Feb 2014%0A%09by Tom Igoe%0A%09Patches and improvements suggested by Steve Klise, Lia Martinez, and Will Jennings%0A
+%0A * to run: node SerialServer.js /dev/tty.usbmodem728331 (update to your port)%0A 
 */%0A%0A
@@ -176,44 +176,8 @@
 t%22),
-%09%09%09// include the serialport library
 %0A%09Se
@@ -214,138 +214,37 @@
 ort,
-%09%09// make a local instance of serial%0A%09portName = process.argv%5B2%5D,%09%09%09%09%09// third word of the command line should be serial port name
+%0A%09portName = process.argv%5B2%5D,
 %0A%09ex
@@ -1041,16 +1041,17 @@
 %22)%0A%09%7D);%0A
+%0A
 %09console

clean SerialServer