Created
March 13, 2015 22:43
-
-
Save monteslu/b5ad4c46c9b6b78f7aea to your computer and use it in GitHub Desktop.
SerialPort TCP/IP Host server
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 net = require('net'); | |
var SerialPort = require('serialport').SerialPort; | |
//possibly something like COM1 on windows | |
var SERIAL_PORT = process.env.SERIAL_PORT || '/dev/tty.usbmodem1421'; | |
var PORT = process.env.PORT || 3001; | |
var serialPort = new SerialPort(SERIAL_PORT,{ | |
baudrate: 57600, | |
buffersize: 1 | |
}); | |
var clients = {}; | |
serialPort.on('data', function(data){ | |
for(var id in clients){ | |
try{ | |
clients[id].write(data); | |
}catch(exp){ | |
console.log('error writing to client', id, exp); | |
} | |
} | |
}); | |
var server = net.createServer(function(c) { | |
c.id = 'c' + Math.random() + '_' + Date.now(); | |
clients[c.id] = c; | |
console.log('client connected', c.id); | |
c.on('end', function() { | |
console.log('client disconnected', c.id); | |
delete clients[c.id]; | |
}); | |
c.on('data', function(data){ | |
serialPort.write(data); | |
}); | |
}); | |
server.listen(PORT, function() { | |
console.log('server bound to port: ' + PORT); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment