如何通过nodejs中的TCP / IP获取传感器数据? [英] How to get sensor data over TCP/IP in nodejs?

查看:529
本文介绍了如何通过nodejs中的TCP / IP获取传感器数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用socket.io的nodejs应用程序。要测试这个,请将以下列表保存为app.js.安装节点,然后npm安装socket.io,最后在命令提示符下运行:node app.js
$ b

I have a nodejs app with socket.io. To test this, save the below listing as app.js. Install node, then npm install socket.io and finally run on command prompt: node app.js

var http = require('http'),
fs = require('fs'),
// NEVER use a Sync function except at start-up!
index = fs.readFileSync(__dirname + '/index.html');

// Send index.html to all requests
var app = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(index);
});

// Socket.io server listens to our app
var io = require('socket.io').listen(app);

// Send current time to all connected clients
function sendTime() {
    io.sockets.emit('time', { time: new Date().toJSON() });
}

// Send current time every 10 secs
setInterval(sendTime, 5000);

// Emit welcome message on connection
io.sockets.on('connection', function(socket) {
    socket.emit('welcome', { message: 'Welcome!' });

    socket.on('i am client', console.log);
});

app.listen(3000);

此代码将数据发送到文件index.html。 运行app.js后,在浏览器中打开此文件。

This code sends data to the file index.html. After running the app.js, open this file in your browser.

<!doctype html>
<html>
    <head>
        <script src='http://code.jquery.com/jquery-1.7.2.min.js'></script>
        <script src='http://localhost:3000/socket.io/socket.io.js'></script>
        <script>
            var socket = io.connect('//localhost:3000');

            socket.on('welcome', function(data) {
                $('#messages').html(data.message);

                socket.emit('i am client', {data: 'foo!'});
            });
            socket.on('time', function(data) {
                console.log(data);
                $('#messages').html(data.time);
            });
            socket.on('error', function() { console.error(arguments) });
            socket.on('message', function() { console.log(arguments) });
        </script>
    </head>
    <body>
        <p id='messages'></p>
    </body>
</html>

目前发送的数据是当前时间,index.html正常工作,每五秒。

The data sent right now is the current time and index.html works fine, updates the time every five seconds.

我想修改代码,以便通过TCP读取我的传感器数据。我的传感器通过数据采集系统连接,并通过IP传输传感器数据:172.16.103.32端口:7700。 (这是局域网,所以你不能访问。)

I want to modify the code so that, it reads my sensor data over TCP. My sensors are connected thru a data acquisition system and relays the sensor data over IP: 172.16.103.32 port:7700. (This is over LAN, so will not the accessible to you.)

如何在nodejs中实现?

SensorMonkey 是一种可行的替代方案吗?如果是这样,关于如何使用它的任何指针?

Is SensorMonkey a viable alternative ? If so, any pointers on how to go about using it?

推荐答案

我有一个正常的黑客,现在正在工作,因为我请求读者评论....

I have a decent hack that is working right now, for which I request the readers to comment on....

var net = require('net'),
http = require('http'),
port = 7700,                    // Datalogger port
host = '172.16.103.32',         // Datalogger IP address
fs = require('fs'),
// NEVER use a Sync function except at start-up!
index = fs.readFileSync(__dirname + '/index.html');

// Send index.html to all requests
var app = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(index);
});

// Socket.io server listens to our app
var io = require('socket.io').listen(app);

// Emit welcome message on connection
io.sockets.on('connection', function(socket) {
    socket.emit('welcome', { message: 'Welcome!' });

    socket.on('i am client', console.log);
});

//Create a TCP socket to read data from datalogger
var socket = net.createConnection(port, host);

socket.on('error', function(error) {
  console.log("Error Connecting");
});

socket.on('connect', function(connect) {

  console.log('connection established');

  socket.setEncoding('ascii');

});

socket.on('data', function(data) {

  console.log('DATA ' + socket.remoteAddress + ': ' + data);
  io.sockets.emit('livedata', { livedata: data });        //This is where data is being sent to html file 

});

socket.on('end', function() {
  console.log('socket closing...');
});

app.listen(3000);






参考文献:


References:


  1. Socket.io网站 - www.socket.io - 它现在是流行词。

  2. TCP Socket编程

  3. Nodejsnet module

  4. 最简单的socket.io示例。

  1. Socket.io Website - www.socket.io - Its the buzzword now.
  2. TCP Socket Programming
  3. Nodejs "net" module
  4. Simplest possible socket.io example.

这篇关于如何通过nodejs中的TCP / IP获取传感器数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆