Keep track of uninterrupted Arduino communication using Node.js

Zápisník experimentátora

Hierarchy: Node.js

If you have Arduino connected to your computer and is sending some data to your computer all the time, the communication may stop. Arduino will jam or the connection through the serial port is disconnected. In this example, we'll show you how to track this communication in Node.js, and automatically restore it in case of problems.


For Arduino, I have written a simple code that simulates Arduino jamming at some point. This is simulated using the variable crash_count. When it reaches 0, it stops sending values to the serial port.

int crash_count = 10;

void setup() {

void loop() {
  while (crash_count) {


The program works by listing a serial port list on the console if you run it through the node index.js. If you run it through the node index.js 0, you tell it that he has to open a port with a sequence number of 0. Then it creates a variable ph of type PortHandler that takes care of the whole communication.

PortHandler is a class with two functions. In the constructor, the internal variables are set and the entire activity is performed in the function openPort. The time of the last communication is recorded in the event date. The variable interval is a function that starts every 5 seconds. When the communication is interrupted for more than 10 seconds, the serial port connection restarts.

'use strict';

const serialport = require('serialport');
const sp_readline = serialport.parsers.Readline;

class PortHandler {

  constructor(config) {
    this.config = config;
    this.last_data =;

  openPort() {
    var self = this;
    this.port = new serialport(this.config.port, {
      baudRate: this.config.baudrate || 9600

      const parser = new sp_readline();
      this.port.on('close', function(error) {
          console.log('on close error', error.message);
          console.log('on close');
      parser.on('data', function(data) {
        self.last_data =;
        console.log(new Date(self.last_data).toLocaleTimeString(), data);
      this.interval = setInterval(function() {
        var millis = - self.last_data;
        if(millis>10000) {
          console.log('Crash detected');
          self.port.close(function() {
      }, 5000);

var idx = 0;
var ports = [];

serialport.list(function (err, p) {
  p.forEach(function(p) {

  if(process.argv.length==2) {
    console.log('COM port list:');
    ports.forEach(function(p) {
      console.log(' [' + idx + '] ' + p);

  if(process.argv.length==3) {
    var port = ports[parseInt(process.argv[2])];
    console.log('Connecting to', port);
    const ph = new PortHandler({port: port, baudrate: 9600});

Output from the program

That's how it looks from the program. In this case, after three seconds I disconnected Arduino from the USB port. Program has also recorded the actual disconnection. Then, for several seconds, the program tried to recognize what was happening, and when the response did not arrive after a set time, it attempted an automatic restart. Arduino was already connected to USB, so communication could be resumed.

Connecting to COM6
22:37:12 0
22:37:13 999
22:37:14 1999
22:37:15 3000
on close error Reading from COM port (ReadIOCompletion): Operation aborted
Crash detected
22:37:27 0
22:37:28 999

Source code

The source code is located on the GitHub server.

If you have a git installed, you can install the source code as follows. If you do not have it, you can also download zip archives.

Installation for Arduino

Upload to your Arduino code using Arduino IDE.

Installation for Node.js

Run the Command Line Console. Important is the last line that installs all the related libraries that are in package.json.

cd d:
mkdir arduino
cd arduino
git clone
cd arduinoslovakia\node.js\arduino_reopen_after_crash\node
npm install