为什么 Node.js 在 2 - 5 分钟后无异常退出

Why does Node.js exit without an Exception after 2 - 5 minutes

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


server.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>

我对您的源代码做了一些修改'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);
    });
    

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

我想你也有同样的问题。我解决了3天前serialport发布新版本(2.0.4)的问题。

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

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