-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (41 loc) · 1.08 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
var SerialPort = require('serialport');
var Duplex = require('stream').Duplex;
var EventEmitter = require('events').EventEmitter;
var util = require('util');
function S2Serial(path, options) {
Duplex.call(this);
var self = this;
// strip parser option if set. Force default (raw)
if (options.parser)
options.parser = undefined;
this._serialport = new SerialPort(path, options);
this.wrap(this._serialport);
this._write = function _write(buf, encoding, callback) {
self._serialport.write(buf, function (err, res) {
if (err) {
callback(err);
return;
}
if (res && res != buf.length) {
callback(new Error("write error: " +
"wrote %d bytes, expected to write %d",
res, buf.length));
return;
}
callback()
});
};
this._serialport.on('open', function () {
self.emit('open');
});
this._serialport.on('close', function () {
self.emit('close');
});
this.open = this._serialport.open;
this.close = this._serialport.close;
this.flush = this._serialport.flush;
};
util.inherits(S2Serial, Duplex);
module.exports = {
S2Serial: S2Serial
}