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() {
Serial.begin(9600);
}
void loop() {
while (crash_count) {
Serial.println(millis());
delay(1000);
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 = Date.now();
this.openPort();
}
openPort() {
var self = this;
this.port = new serialport(this.config.port, {
baudRate: this.config.baudrate || 9600
});
const parser = new sp_readline();
this.port.pipe(parser);
this.port.on('close', function(error) {
if(error)
console.log('on close error', error.message);
else
console.log('on close');
});
parser.on('data', function(data) {
self.last_data = Date.now();
console.log(new Date(self.last_data).toLocaleTimeString(), data);
});
this.interval = setInterval(function() {
console.log('Checking...');
var millis = Date.now() - self.last_data;
if(millis>10000) {
console.log('Crash detected');
self.port.close(function() {
self.openPort();
});
clearInterval(self.interval);
}
}, 5000);
}
};
var idx = 0;
var ports = [];
serialport.list(function (err, p) {
p.forEach(function(p) {
ports.push(p.comName);
});
if(process.argv.length==2) {
console.log('COM port list:');
ports.forEach(function(p) {
console.log(' [' + idx + '] ' + p);
idx++;
});
}
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});
}
});
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 Checking... Checking... Checking... Crash detected 22:37:27 0 22:37:28 999
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.
Upload to your Arduino code using Arduino IDE.
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 https://github.com/roboulbricht/arduinoslovakia
cd arduinoslovakia\node.js\arduino_reopen_after_crash\node
npm install
10.02.2018