node.js - SerialPort 'close' event never fire -
i'm using nodejs's serialport
package connecting computer ports.
about package's close
event here.
i have created code, know why close
/disconnect
event never fire when disconnect 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 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;
i'm trying think if use wrong because havn't saw example using event.
thanks :)
i have created timestamp every time recive data com
, , check interval()
every time.
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);
Comments
Post a Comment