Skip to content

Commit

Permalink
serial ws proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetancollaud committed Sep 16, 2018
1 parent 6eb5fb4 commit 65a08ae
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea/
node_modules/
87 changes: 87 additions & 0 deletions serial-ws-proxy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
"use strict";
process.title = 'node-serial-ws';

// Websocket
var webSocketsServerPort = 1337;
var webSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
// Not important for us. We're writing WebSocket server, not HTTP server
});
var clients = [];

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

var wsServer = new webSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket request is just
// an enhanced HTTP request. For more info http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
});

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

var index = clients.push(connection) - 1;

// user sent some message
connection.on('message', function(message) {
onReceive(message);
});

// user disconnected
connection.on('close', function(connection) {
if (userName !== false && userColor !== false) {
console.log((new Date()) + " Peer "
+ connection.remoteAddress + " disconnected.");
// remove user from the list of connected clients
clients.splice(index, 1);
}
});

});

function onReceive(msg)
{
console.log("ws msg:" + msg);
serialPort.write(msg);
}

function onSerial(msg)
{
console.log("uart msg:" + msg);
for (var i=0; i < clients.length; i++)
clients[i].sendUTF(msg);
}

// Serial port
var SerialPort = require("serialport").SerialPort
var portName = 'COM60';
var buffer = "";

var serialPort = new SerialPort(portName, {
baudrate: 9600,
// defaults for Arduino serial communication
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});

serialPort.on("open", function () {
console.log('open serial communication');
// Listens to incoming data
serialPort.on('data', function(data) {

buffer += new String(data);
var lines = buffer.split("\n");
while ( lines.length > 1 )
onSerial( lines.shift() );
buffer = lines.join("\n");

});
});
13 changes: 13 additions & 0 deletions serial-ws-proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "serial-ws-proxy",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts":{
"start" : "node index.js"
},
"dependencies": {
"http": "^0.0.0",
"websocket": "^1.0.26"
}
}
44 changes: 44 additions & 0 deletions serial-ws-proxy/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


debug@^2.2.0:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
dependencies:
ms "2.0.0"

http@^0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/http/-/http-0.0.0.tgz#86e6326d29c5d039de9fac584a45689f929f4f72"

is-typedarray@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

nan@^2.3.3:
version "2.11.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.11.0.tgz#574e360e4d954ab16966ec102c0c049fd961a099"

typedarray-to-buffer@^3.1.2:
version "3.1.5"
resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080"
dependencies:
is-typedarray "^1.0.0"

websocket@^1.0.26:
version "1.0.26"
resolved "https://registry.yarnpkg.com/websocket/-/websocket-1.0.26.tgz#a03a01299849c35268c83044aa919c6374be8194"
dependencies:
debug "^2.2.0"
nan "^2.3.3"
typedarray-to-buffer "^3.1.2"
yaeti "^0.0.6"

yaeti@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/yaeti/-/yaeti-0.0.6.tgz#f26f484d72684cf42bedfb76970aa1608fbf9577"

0 comments on commit 65a08ae

Please sign in to comment.