forked from serialport/node-serialport
/
manuallyValidateOpenCloseDisconnect.js
98 lines (69 loc) · 2.56 KB
/
manuallyValidateOpenCloseDisconnect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*jslint node: true */
/*global describe, it */
"use strict";
// The purpose of this app is to manually validate node-serialport eventing.
// The app finds the name of the last comPort provided by list(), opens the port,
// and sends/receives an echo packet. It looks for disconnected events which
// would occur due to manual removal of the serial port device and attempts
// to reconnect after the port reappears in list().
// The goal for this test is to provide a reproduction case of bug #265,
// https://github.com/voodootikigod/node-serialport/issues/265
// and provide validation of the necessary api changes and the fix.
var chai = require('chai');
var util = require('util');
var serialPortFactory = require('../serialport');
serialPortFactory.on('error', function(err) {
chai.assert.fail('no error', err, util.inspect(err));
});
// Open a port, hook its events
function openPort(portName) {
console.log('opening ' + portName);
var port = new serialPortFactory.SerialPort(portName, null, false);
var data = new Buffer("hello");
var sendDataIntervalId;
port.on('disconnected', function() {
clearInterval(sendDataIntervalId);
console.log('disconnected');
var intervalId = setInterval(function () {
reconnect(portName, intervalId);
}, 2000 );
});
port.on('error', function(err) {
chai.assert.fail('no error', err, util.inspect(err));
});
port.on('data', function(d) {
chai.assert.equal(data.toString(), d.toString(), 'incorrect data received');
process.stdout.write('r'); // data properly received
});
port.on('open', function() {
console.log('opened');
sendDataIntervalId = setInterval(function () {
process.stdout.write('s'); // sending data
port.write(data);
}, 200 );
});
port.on('close', function() {
clearInterval(sendDataIntervalId);
console.log('closed');
});
port.open();
};
function reconnect(portName, intervalId) {
serialPortFactory.list(function(err, ports) {
chai.assert.isUndefined(err, util.inspect(err));
chai.assert.isDefined(ports, 'ports is not defined');
if (ports.length > 0 && portName == ports.slice(-1)[0].comName) {
clearInterval(intervalId);
openPort(portName);
} else {
console.log('Port ' + portName + ' not found, retrying...');
}
});
};
serialPortFactory.list(function(err, ports) {
chai.assert.isUndefined(err, util.inspect(err));
chai.assert.isDefined(ports, 'ports is not defined');
chai.assert.isTrue(ports.length > 0, 'no ports found');
var portName = ports.slice(-1)[0].comName;
openPort(portName);
});