Comunicación serial RS232 con servidor WebSocket en Windows
(DIR="C:\Program Files\nodejs")
- Descargar Node Windows Installer
- Instalar en DIR
- CMD → cd DIR → npm install socket.io
(Abrir CMD como Admin Windows/system32/cmd.exe)
- Guardar "nodeServer.js" en DIR
- Copiar librería JS C:\Program Files\nodejs\node_modules\socket.io\node_modules\socket.io-client\dist\socket.io.min.js al directorio JS de nuestra web.
- Arrancar serproxy
- CMD → cd DIR → node nodeServer.js
nodeServer.js
var sys = require('sys');
var net = require('net');
var io = require('socket.io').listen(3400);
var tcp = net.createConnection(5331); // usar serproxy para usar localhost:5331
tcp.setEncoding('ascii');
tcp.addListener('connect', function(){
sys.puts('conectado a TCP');
});
io.sockets.on('connection', function (socket) {
socket.emit('newconnection', { hello: 'world' });
socket.on('dataclient', function (data) {
sys.puts(data.theword) // escribir en la consola
tcp.write(data.theword);
});
socket.on('disconnect', function () {
sys.puts('user disconnected');
});
websocket = socket;
});
tcp.addListener('data', function(dat){
sys.puts('dato TCP');
sys.puts(sys.inspect(dat)); // output array object
websocket.emit('datatcp', { data: dat });
});
index.html
<script id="stormEngineC" src="js/socket.io.min.js"></script>
<script>
var ws;
openWebSocket = function(ip, port) {
ws = io.connect('ws://'+ip+':'+port+'/');
ws.on('newconnection', function(data) {
console.log(data);
$('#DIVID_webSocketStatus').html("<span style='color:#00CC00'><b>CONECTADO A WEBSOCKET</b></span>");
});
ws.on('datatcp', function(data) {
console.log(data);
$('#DIVID_webSocketResponse').html(data.data);
});
}
sendTX = function(word) {
ws.emit('dataclient', { theword: word });
}
</script>
<table border='0' cellpadding='15' cellspacing="0">
<tr>
<td>
<div id="DIVID_webSocketStatus"></div>
<script>
if ("WebSocket" in window) {
}else{
$('#DIVID_webSocketStatus').html("<span style='color:#CC0000'>Navegador NO soporta WebSocket</span>");
}
</script>
</td>
<td id="TDID_webSocket" align="left" valign="top" style='width:200px;'>
<input type="text" id="INPUTID_ipWebSocket" value="localhost"/>
<input type="text" id="INPUTID_portWebSocket" value="3400"/>
<a href="#" onclick="openWebSocket($('#INPUTID_ipWebSocket').val(), $('#INPUTID_portWebSocket').val())">Conectar a servidor WebSocket</a>
</td>
</tr>
<tr>
<td id="TDID_webSocketResponse" style="display:none">
<div id="DIVID_webSocketResponse"></div>
</td>
<td>
<input type="text" id="INPUTID_sendTX" value="L"/>
<button type="button" onclick="sendTX('L')">SEND</button>
</td>
</tr>
</table>
INSTALACIÓN
- Descargar Node Windows Installer
- Instalar en DIR
- CMD → cd DIR → npm install socket.io
(Abrir CMD como Admin Windows/system32/cmd.exe)
USO
- Guardar "nodeServer.js" en DIR
- Copiar librería JS C:\Program Files\nodejs\node_modules\socket.io\node_modules\socket.io-client\dist\socket.io.min.js al directorio JS de nuestra web.
- Arrancar serproxy
- CMD → cd DIR → node nodeServer.js
nodeServer.js
var sys = require('sys');
var net = require('net');
var io = require('socket.io').listen(3400);
var tcp = net.createConnection(5331); // usar serproxy para usar localhost:5331
tcp.setEncoding('ascii');
tcp.addListener('connect', function(){
sys.puts('conectado a TCP');
});
io.sockets.on('connection', function (socket) {
socket.emit('newconnection', { hello: 'world' });
socket.on('dataclient', function (data) {
sys.puts(data.theword) // escribir en la consola
tcp.write(data.theword);
});
socket.on('disconnect', function () {
sys.puts('user disconnected');
});
websocket = socket;
});
tcp.addListener('data', function(dat){
sys.puts('dato TCP');
sys.puts(sys.inspect(dat)); // output array object
websocket.emit('datatcp', { data: dat });
});
index.html
<script id="stormEngineC" src="js/socket.io.min.js"></script>
<script>
var ws;
openWebSocket = function(ip, port) {
ws = io.connect('ws://'+ip+':'+port+'/');
ws.on('newconnection', function(data) {
console.log(data);
$('#DIVID_webSocketStatus').html("<span style='color:#00CC00'><b>CONECTADO A WEBSOCKET</b></span>");
});
ws.on('datatcp', function(data) {
console.log(data);
$('#DIVID_webSocketResponse').html(data.data);
});
}
sendTX = function(word) {
ws.emit('dataclient', { theword: word });
}
</script>
<table border='0' cellpadding='15' cellspacing="0">
<tr>
<td>
<div id="DIVID_webSocketStatus"></div>
<script>
if ("WebSocket" in window) {
}else{
$('#DIVID_webSocketStatus').html("<span style='color:#CC0000'>Navegador NO soporta WebSocket</span>");
}
</script>
</td>
<td id="TDID_webSocket" align="left" valign="top" style='width:200px;'>
<input type="text" id="INPUTID_ipWebSocket" value="localhost"/>
<input type="text" id="INPUTID_portWebSocket" value="3400"/>
<a href="#" onclick="openWebSocket($('#INPUTID_ipWebSocket').val(), $('#INPUTID_portWebSocket').val())">Conectar a servidor WebSocket</a>
</td>
</tr>
<tr>
<td id="TDID_webSocketResponse" style="display:none">
<div id="DIVID_webSocketResponse"></div>
</td>
<td>
<input type="text" id="INPUTID_sendTX" value="L"/>
<button type="button" onclick="sendTX('L')">SEND</button>
</td>
</tr>
</table>
Comentarios
Publicar un comentario