var WebSocketServer = require('websocket').server;
var http = require('http');
var SerialPort = require("serialport").SerialPort

/*var serialPort = new SerialPort("COM1", {
    baudrate: 500000
});

serialPort.on("open", function () {
    console.log('open');
    serialPort.on('data', function(data) {
        console.log('data received: ' + data);
    });
    serialPort.write("ls\n", function(err, results) {
        console.log('err ' + err);
        console.log('results ' + results);
    });
});*/

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});

server.listen(4000, function() {
    console.log((new Date()) + ' Server is listening on port 4000');
});

wsServer = new WebSocketServer({
    httpServer: server,
    autoAcceptConnections: false
});

wsServer.on('request', function(request) {
    var connection = request.accept('calc-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');

    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);
            connection.sendUTF(message.utf8Data);
        } else if (message.type === 'binary') {
            console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');

            if (message.binaryData.length == 768) {
                var x = 0;
                var y = 0;

                var drawBuffer = "";

                for (var i = 0; i < message.binaryData.length; i++) {
                    for (var bit = 0; bit < 8; bit++) {
                        if (message.binaryData[i] & (0x80 >> bit)) {
                            drawBuffer += 'X';
                        } else {
                            drawBuffer += ' ';
                        }

                        x += 1;
                        if (x >= 96) {
                            x = 0;
                            y += 1;
                            drawBuffer += '\r\n';
                        }
                    }
                }

                process.stdout.write(drawBuffer);
            } else {
                console.log("weird length", message.binaryData.length);
            }
        }
    });

    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});

@@ -131,10 +131,8 @@
 %0D%0A%0D%0A
-/*
 var 
@@ -163,17 +163,18 @@
 ort(%22COM
-1
+37
 %22, %7B%0D%0A  
@@ -204,325 +204,8 @@
 %0D%0A%0D%0A
-serialPort.on(%22open%22, function () %7B%0D%0A    console.log('open');%0D%0A    serialPort.on('data', function(data) %7B%0D%0A        console.log('data received: ' + data);%0D%0A    %7D);%0D%0A    serialPort.write(%22ls%5Cn%22, function(err, results) %7B%0D%0A        console.log('err ' + err);%0D%0A        console.log('results ' + results);%0D%0A    %7D);%0D%0A%7D);*/%0D%0A%0D%0A
 var 
@@ -596,32 +596,299 @@
 : false%0D%0A%7D);%0D%0A%0D%0A
+var TIMEOUT = 5000; // ms%0D%0Avar lastFrame = null;%0D%0Avar lastTime = null;%0D%0A%0D%0AserialPort.on(%22data%22, function(data) %7B%0D%0A    console.log(%22serial: %22, data);%0D%0A%0D%0A    if (lastFrame !== null)%0D%0A        serialPort.write(lastFrame);%0D%0A%0D%0A    lastTime = (new Date()).getTime();%0D%0A%7D)%0D%0A%0D%0A
 wsServer.on('req
@@ -1450,24 +1450,340 @@
  == 768) %7B%0D%0A
+                var now = (new Date()).getTime();%0D%0A                if (serialPort.isOpen() && (lastTime === null %7C%7C (now %3E lastTime + TIMEOUT))) %7B%0D%0A                    serialPort.write(message.binaryData);%0D%0A                    lastTime = now;%0D%0A                %7D%0D%0A%0D%0A                lastFrame = message.binaryData;%0D%0A%0D%0A
             

Send frame without ack after timeout.