2017-09-11 19 views
7

Bilgisayar bağlantı noktalarına bağlanmak için nodeJS'nin SerialPort paketini kullanıyorum.SerialPort 'close' olayı hiç ateş etmiyor

Paketin Hakkında close olay here. close/disconnect olay ben COM

const express = require('express'); 
const router = express.Router(); 
// 
const cors = require('cors'); 
router.use(cors()); 
// 
const SerialPort = require('serialport'); 
const Readline = SerialPort.parsers.Readline; 
const port = new SerialPort('COM7',function (err) { 
    if (err) { 
     return console.log('Error: ', err.message); 
    } 
}); 
const parser = port.pipe(new Readline()); 
/* 
var lastresult = ''; 
var count = 0; 
*/ 

port.on('open', function() { 
    console.log('~Port is open.'); 
    parser.on('data', console.log); 
}); 

port.on('disconnect', function() { 
    console.log('disconnected'); 
}); 
port.on('close', function() { 
    console.log('closed'); 
}); 

router.get('/', function (req, res) { 
    res.send('index'); 
}); 

module.exports = router; 

herhalde yanlış ben havn't çünkü kullanırsanız düşünmeye çalışıyorum kesmek zaman ateş neden hiç bilmek istiyorum ben bu kodu oluşturduk

, Bu olayı kullanmak için örnek gördüm. Yardım için

Teşekkür :) Ben zaman damgası her zaman oluşturduk

+0

Orada bir şey olup olmadığını görmek için '' error'' olayını dinlemeyi denediniz mi? – tiblu

+0

elbette, hiçbir şey ortaya çıkmaz, onun için işi? – itzikb

+0

Bakın: https://stackoverflow.com/questions/13408476/detecting-when-a-serialport-gets-disconnected –

cevap

2

Ben COM, veri tarifesi Ve interval() her zaman onu kontrol edin.

setInterval(function() { 
    if (Date.now() - lastDataTime > 1000 || !comStatus) { 
     comStatus = false; 
     port.close(); 
     port = new SerialPort(comPort, function (err) { 
      if (err) { 
       error = 'Error: ' + err.message; 
       return console.log(error); 
      } 
     }); 
    } 
}, 1000); 
İlgili konular