Skip to content

Commit

Permalink
huge fix on parsing data from serial
Browse files Browse the repository at this point in the history
  • Loading branch information
brugnara committed Dec 15, 2014
1 parent 95591d1 commit 9a26372
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
36 changes: 21 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ function Modem(port, options, cb) {
cb && (cb = once(cb));
//
this.serialPort = new SerialPort(port, {
baudrate: this.options.baudrate,
parser: serialport.parsers.readline('\n')
baudrate: this.options.baudrate
});
// events
this.handleEvents(this.serialPort, cb);
Expand All @@ -55,6 +54,7 @@ Modem.prototype.on = function(event, handler) {
};

Modem.prototype.handleEvents = function(serialPort, cb) {
var line = '';
serialPort.on('open', function() {
this.ready = true;
cb && cb();
Expand All @@ -72,20 +72,26 @@ Modem.prototype.handleEvents = function(serialPort, cb) {
}.bind(this));
//
serialPort.on('data', function(data) {
var res = data.toString().trim();
if (res === this.lastCommand && this.next.expect != '>') {
return;
}
// is something we are waiting for? ie: RING
if (this.events.indexOf(res) !== -1) {
this.eventEmitter.emit(res);
}
if (this.next) {
if (res.indexOf('ERROR') !== -1) {
this.next.cb && this.next.cb(new Error(res));
} else {
this.next.cb && this.next.cb(null, res);
line+=data;
var res = line.trim();
if (line[line.length-1] === '\n' || line.indexOf('>') !== -1) {
// is something we are waiting for? ie: RING
if (this.events.indexOf(res) !== -1) {
this.eventEmitter.emit(res);
}
// console.log('expected:', this.next.expect, 'got:', res);
if (this.next) {
if (line.indexOf('ERROR') !== -1) {
return this.next.cb(new Error(line));
}
if (res === this.next.expect) {
// console.log('calling next');
return this.next.cb(null, res);
}
}
line = '';
} else {
// console.log('Mmmmm:', line);
}
}.bind(this));
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-modem",
"version": "0.1.2",
"version": "0.1.3",
"description": "a modem manager.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 9a26372

Please sign in to comment.