82838

SerialPort 'close' event never fire

Question:

I'm using nodeJS's SerialPort Package for connecting to computer ports.

About Package's close event <a href="https://github.com/EmergingTechnologyAdvisors/node-serialport#module_serialport--SerialPort+event_close" rel="nofollow">here</a>.

I have created this code, I would like to know why close/disconnect event never fire when I disconnect the 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;

I'm trying to think if I use it wrong because I havn't saw example for using this event.

Thanks for the help :)

Answer1:

I have created timestamp every time I recive data from the COM, And check it with 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);

Recommend

  • VB.net XML Get a group of nodes Attributes and group them by node?
  • Programmatically Add ButtonColumn to GridView From DataTable
  • PHP 5.4.0 - Register Globals Deprecated
  • How to show a element immediately, such as a loading indicator, with Vue? nextTick isn't workin
  • show border grid lines only between elements
  • Why jnz requires 2 cycles to complete in an inner loop
  • Is there any way to target the plain JavaScript object type in TypeScript?
  • g++ c++17 class template argument deduction not working in a very specific case
  • Why does the parameterless Guid constructor generate an empty GUID?
  • how to implement OnClickListener for custom ViewHolder in my recycler view?
  • Emberjs custom config variables
  • Hosting existing node.js apps on Windows Azure
  • Unable to access local Web API from Visual Studio 2015 WIndows 10 Phone Emulator
  • Add click to imagebutton inside gridview dynamically
  • Google Cloud Dataflow consume external source
  • How to add GeoJSON(MultiLineString) layer to a Google Map
  • div is displayed centered in Firefox, but not in IE
  • Filter by multiple patterns with filter() and str_detect()
  • Extract the data specified in brackets '[ ]' from a string message in python
  • google fusion table- not able to color the layer in map after 5 colors
  • Two instances of blur.js possible?
  • Forward slash vs backward slash for file path in git bash
  • wxPython UltimateListCtrl check (tick) listitem programatically
  • Spongycastle is missing many algorithms when loaded on android
  • Circular Left Rotation Algorithm in C#
  • How to use array in autohotkey?
  • Disable account chooser FirebaseUI React
  • SQL Server 2012 not showing unicode character in results
  • Java .policy file - how to prevent java.util.Date() from being accessible
  • How to define something in JavaScript [closed]
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Google App Engine Datastore: Dealing with eventual consistency
  • ssh remote server login script
  • How to call different template for different category archive page in woocommerce