0

编辑——节点在 Windows 8.1 环境中运行。我正在使用串口模块和 socket.io 从 Arduino 读取数据。我在 2 - 5 分钟内获得精美的数据。然后节点停止运行,没有抛出异常,我一无所知。我试过只使用“http”模块和“express”。无论哪种方式,结果都相同。完整的源代码如下。


服务器.js

var express = require('express');
var app = express();
var server = app.listen(3000, function () {

var host = server.address().address
var port = server.address().port

console.log('Example app listening at http://%s:%s', host, port)

});
var io = require('socket.io').listen(server);
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
var serialPort = new SerialPort("COM3",{
baudrate:9600,
dataBits: 8,
parity: 'none',
flowControl: false,
parser: serialport.parsers.readline("<EOD>")
});


app.use(express.static(__dirname));

app.get('/', function (req, res) {
res.render('./index.html')
});

serialPort.on('open', function(){
// Now server is connected to Arduino
console.log('Serial Port Opened');

 var lastValue;
io.sockets.on('connection', function (socket) {
  //Connecting to client 
  console.log('Socket connected');
  //socket.emit('connected');
  var lastValue;

      serialPort.on('data', function(data){
      //if(lastValue !== data.toString()){
          socket.emit('data', data.toString());
          console.log(data.toString());
      //}
      lastValue = data.toString();
    });
  });
});

index.html 脚本:包含所有 socket.io 功能。

<script>

    function handleIO(){

        function serverDisconnect(){
            console.log("Server Disconnected.");
            console.log(new Date().toTimeString());
        }
        socket.timeout = 60*60*1000;

        socket.on('disconnect', serverDisconnect);

        socket.on('data', function(data) {
            //console.log("Got Data");
            console.log(data);
            render(data);   
        });

        console.log("Connected To Server.");
        console.log(new Date().toTimeString());
    }

    var socket = io();

    socket.on('connect', handleIO);


</script>

编辑#2:控制台日志:

C:\Users\Dave\Desktop\js\js\Websensors2>node serverexp.js
Example app listening at http://:::3000
Port open
New Socket Client connected: JPozMKfwH77x8uMNAAAA
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.80
Sent data to connected Socket clients
74.77
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.77
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.73
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.70
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.66
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.41
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.41
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.62
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.44
Sent data to connected Socket clients
74.48
Sent data to connected Socket clients
74.52
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.55
Sent data to connected Socket clients
74.59
Sent data to connected Socket clients
74.55

C:\Users\Dave\Desktop\js\js\Websensors2>
4

3 回答 3

0

我想你有同样的问题。我在 3 天前(2.0.4)串行端口发布新版本时解决了。

我在串口 github 上的问题:https ://github.com/voodootikigod/node-serialport/issues/609

现在尝试使用 npm 重新安装串口模块,与 node 4.2.2 LTS 完全兼容 :)

于 2015-11-12T13:48:59.337 回答
0

这似乎与串行端口库的至少一个已知问题有关。似乎最好的短期修复是回滚到串行端口的先前版本。该问题记录在 github 上。谢谢你的帮助。希望我在发布之前找到它。

于 2015-10-27T14:23:13.737 回答
-1

我对您的源代码“server.js”进行了一些更改,请使用下面提供的修改后的源代码。我已经包含了单独的事件来记录 Socket.IO 和串行端口的不同事件。

http:在 3000 端口
运行 socket.io:在端口作为 http 运行
serialPort:通过串口连接到服务器

  1. 打开http连接
  2. 打开串口
  3. 打开 Socket.IO
  4. 从串口接收数据
  5. 将接收到的数据从串口转发到 Socket IO 客户端

    server.js

    var express = require('express');
    var app = express();
    
    var SerialPort = require('serialport');
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    
    var options_serialPort = {
        baudrate: 9600,
        dataBits: 8,
        parity: 'none',
        flowControl: false,
        parser: serialport.parsers.readline("<EOD>")
    };
    
    var serialPort = new SerialPort("COM3", options_serialPort);
    
    app.use(express.static(__dirname));
    
    app.get('/', function (req, res) {
        res.render('./index.html')
    });
    
    serialPort.on('open', function () {
        // Callback called when serial port is opened
        console.log('Port open');
    });
    
    serialPort.on('data', function (data) {
        // Callback called when serial port is receiving data from Arduino
        sendReceivedDataToSocketIOClients(data);
        console.log(data.toString());
    });
    
    serialPort.on('close', function (error) {
        // Callback called when serial port is closed
        console.log('Port closed');
    });
    
    serialPort.on('error', function (error) {
        // Callback called when serial port receives any error
        console.log('Error in Port: ' + error);
    });
    
    function sendReceivedDataToSocketIOClients(data) {
    
        // sending to all socket clients
        io.emit('data', data.toString());
        console.log('Sent data to connected Socket clients');
    
        //console.log('No Socket clients connected to send data');
    }
    
    io.on('connection', function (socket) {
        console.log("New Socket Client connected: " + socket.id);
    
        socket.on("connect", function () {
            console.log("Socket Client Connected!");
        });
    
        socket.on('disconnect', function () {
            console.log("Socket Client Disconnected!");
        });
    
    });
    
    http.listen(3000, function () {
        var host = server.address().address;
        var port = server.address().port;
    
        console.log('Example app listening at http://%s:%s', host, port);
    });
    
于 2015-10-23T08:41:35.187 回答