0

編集 -- ノードは Windows 8.1 環境で実行されています。serialport モジュールと 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

あなたと同じ問題があったと思います。serialport が 3 日前に新しいバージョン (2.0.4) をリリースしたときに解決しました。

serialport github に関する私の問題: https://github.com/voodootikigod/node-serialport/issues/609

ノード4.2.2 LTSと完全に互換性のある、npmを使用してserialportモジュールを再インストールしてみてください:)

于 2015-11-12T13:48:59.337 に答える
-1

ソース コード「server.js」にいくつかの変更を加えました。以下に示す変更されたソース コードを使用してください。Socket.IO とシリアル ポートの両方の異なるイベントをログに記録するために、個別のイベントを含めました。

http: ポート 3000 で
実行 socket.io: http としてポートで実行
serialPort: シリアル ポート経由でサーバーに接続

  1. http 接続を開く
  2. シリアルポートを開く
  3. Socket.IO を開く
  4. シリアルポートからデータを受信
  5. 受信したデータをシリアル ポートからソケット 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 に答える