Почему Node.js выходит без исключения через 2 - 5 минут

РЕДАКТИРОВАТЬ - Узел работает в среде Windows 8.1. Я использую модуль serialport и socket.io для чтения данных из Arduino. Я получаю данные красиво в течение 2 - 5 минут. Затем узел перестает работать, исключение не выбрасывается, и я остаюсь в неведении. Я пробовал только с модулем "http", и с "экспресс". Те же результаты в любом случае. Полный исходный код ниже.


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>

3 ответа

Решение

Я думаю, была такая же проблема у вас. Я решил, когда serialport выпустил новую версию 3 дня назад (2.0.4).

Моя проблема на github последовательного порта: https://github.com/voodootikigod/node-serialport/issues/609

Попробуйте переустановить модуль serialport, используя npm, полностью совместимый с узлом 4.2.2 LTS:)

Похоже, это связано как минимум с одной известной проблемой с библиотекой serialport. Похоже, лучшим краткосрочным решением является откат к предыдущему выпуску serialport. Вопрос задокументирован на github. Спасибо за вашу помощь. Жаль, что я нашел это до публикации.

Я внес некоторые изменения в ваш исходный код 'server.js'. Пожалуйста, используйте измененный исходный код, представленный ниже. Я включил отдельные события для регистрации различных событий как для Socket.IO, так и для последовательного порта.

http: работает в порту 3000
socket.io: работает в порту как http
serialPort: подключен к серверу через последовательный порт

  1. Открытое http соединение
  2. Открыть серийный порт
  3. Open 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);
    });
    
Другие вопросы по тегам