forked from joshmarinacci/ElectronIDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.js
40 lines (37 loc) · 1.09 KB
/
serial.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
var SerialPort = require('serialport').SerialPort;
var sp = null;
exports.open = function(port, rate, cb) {
console.log('opening the serial port',port,'at rate',rate);
sp = new SerialPort(port,{
baudrate:rate
});
sp.on('open',function() {
console.log('port finally opened');
sp.on('data',function(data) {
//console.log("got some data",data.toString());
cb(data);
});
sp.on('close',function(err) {
console.log('port closed from the other end',err);
cb(err);
});
sp.on('error',function(err) {
console.log('serial port error',err);
cb(err);
});
});
}
exports.close = function(port, cb) {
console.log("closing the serial port",port);
sp.close(function(err) {
console.log("the port is really closed now");
if(cb) cb();
});
}
exports.send = function(message, cb) {
sp.write(message+'\n',function(err, results) {
console.log('err', err);
console.log('results',results);
if(cb)cb(err,results);
});
}