This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
130 lines (110 loc) · 3.31 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* A module which scans for available arduino ports, and
* emits an event when an valid port is found.
*
* @author UBCSailbot
* @author areksredzki
* @author joshuabaker2
*/
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var SerialPort = require('serialport');
var boards = require('./boards.js');
/**
* Constructor
*
* @param {Object} options Options for the consumer to pass in
*/
var ArduinoScanner = function(opts) {
var self = this;
EventEmitter.call(self);
opts = opts || {};
self.options = {
debug: opts.debug || false,
// Strict matching for a specific serial port
port: opts.port,
// Strict matching for a specifc serial number
serialNumber: opts.serialNumber,
// Restricts matching if defined
board: opts.board
};
self.debug = self.options.debug ? function(message) {
console.log('Arduino: ' + message);
} : function() {};
/**
* Searches the serial ports for any device that has a vendor id and product
* id that matches the arduino's. It only emits the first Arduino it finds. If
* you want it to return multiple Arduinos, take out the 'return matched'
* within the ports.some(function(port){}). If you want more information on
* all the ports that it is skipping over, pass 'true' as the second parameter
* to scan.start()
*
* i.e.
* scan.start(500, true)
*/
this.search = function() {
var self = this;
SerialPort.list(function(err, ports) {
if (err || ports.length === 0) {
self.emit('noPortsFound', {
message: 'Nothing detected in serial ports. Check connections.'
});
return;
}
ports.some(function(port) {
var matched = true;
if (self.options.port) {
matched = matched && port.comName === self.options.port;
}
if (self.options.serialNumber) {
matched = matched && port.serialNumber === self.options.serialNumber;
}
if (port.productId){
matched = matched && port.productId in boards;
if (self.options.board) {
matched = matched && boards[port.productId].indexOf(self.options.board) !== -1;
}
}
if (matched) {
self.emit('arduinoFound', {
port: port.comName,
message: 'Arduino found at port ' + port.comName + '.'
});
} else {
self.emit('arduinoNotFound', {
message: 'Arduino not at port ' + port.comName + '.'
});
}
return matched;
});
});
};
};
util.inherits(ArduinoScanner, EventEmitter);
/**
* Starts scanning for valid Arduino serial ports.
* It will emit an 'arduinoFound event once a port is found.
*
* @param {Number} interval Time in milliseconds before trying port reads again
*/
ArduinoScanner.prototype.start = function(interval) {
// If the interval isn't set, default to 500 ms.
interval = interval || 500;
var self = this;
self.searchInterval = setInterval(function() {
self.search();
}, interval);
};
/**
* Stop searching.
*/
ArduinoScanner.prototype.stop = function() {
var self = this;
if (self.searchInterval) {
clearInterval(self.searchInterval);
self.debug('Arduino scan stopped.');
} else {
self.debug('Arduino scan was not active.');
}
};
module.exports = ArduinoScanner;