-
Notifications
You must be signed in to change notification settings - Fork 2
/
telescope.js
61 lines (50 loc) · 1.63 KB
/
telescope.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
/*
* (C) Copyright 2014 Travis Miller (http://raceconditions.net/).
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var serialport = require('serialport');
var Telescope = function(device) {
var self = this;
self.lastCommand = '';
var serialDevice = new serialport(device, {
baudrate: 9600,
parser: serialport.parsers.raw,
autoOpen: false
});
this.start = function() {
serialDevice.open(function (err) {
if(err == null) {
console.log('OPENED serial connection to device:', device);
} else {
console.log('FAILED to open serial connection:', err);
}
});
serialDevice.on('data', function(buffer){
self.emit('data', buffer);
});
};
this.stop = function() {
serialDevice.close(function() {
console.log('CLOSED serial connection to device:', device);
});
};
this.write = function(buffer) {
serialDevice.write(buffer, function(err){
if(err != undefined)
console.log(err);
});
};
};
util.inherits(Telescope, EventEmitter);
module.exports = Telescope;