From da566db421c03f2e268cdeb7624a55e25947cd75 Mon Sep 17 00:00:00 2001 From: Takahiro Poly Horikawa Date: Wed, 26 Jul 2017 00:16:01 +0900 Subject: [PATCH 1/3] Add line break delimiter. --- index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 872a140..ccafbdd 100644 --- a/index.js +++ b/index.js @@ -18,12 +18,20 @@ function Connection(port, address){ const self = this; this.port = port; this.address = address; + this.buffer = new Buffer(0); + this.delimiter = Buffer.from('\n', 'utf8'); const read = function () { process.nextTick(function() { if (self.isOpen()) { - self.port.read(function(err, data){ + self.port.read(function(err, chunk){ if(err) return self.emit('error', err); - self.emit('data', data); + let data = Buffer.concat([self.buffer, chunk]); + let position; + while ((position = data.indexOf(self.delimiter)) !== -1) { + self.emit('data', data.slice(0, position)); + data = data.slice(position + self.delimiter.length); + } + self.buffer = data; read(); }); } From fa3d5384cf7678a4f804b555d0a1e5b430924565 Mon Sep 17 00:00:00 2001 From: Takahiro Poly Horikawa Date: Mon, 28 Aug 2017 18:56:22 +0900 Subject: [PATCH 2/3] Add delimiter options. --- index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index ccafbdd..083b904 100644 --- a/index.js +++ b/index.js @@ -25,13 +25,17 @@ function Connection(port, address){ if (self.isOpen()) { self.port.read(function(err, chunk){ if(err) return self.emit('error', err); - let data = Buffer.concat([self.buffer, chunk]); - let position; - while ((position = data.indexOf(self.delimiter)) !== -1) { - self.emit('data', data.slice(0, position)); - data = data.slice(position + self.delimiter.length); + if (self.delimiter) { + let data = Buffer.concat([self.buffer, chunk]); + let position; + while ((position = data.indexOf(self.delimiter)) !== -1) { + self.emit('data', data.slice(0, position)); + data = data.slice(position + self.delimiter.length); + } + self.buffer = data; + } else { + self.emit('data', chunk); } - self.buffer = data; read(); }); } From ac10d91931dcbd0d90918f798b385b80da845490 Mon Sep 17 00:00:00 2001 From: Takahiro Poly Horikawa Date: Thu, 26 Oct 2017 22:09:15 +0900 Subject: [PATCH 3/3] Set default delimiter to none. --- example/index.js | 1 + index.js | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/example/index.js b/example/index.js index f538595..cbb0d01 100644 --- a/example/index.js +++ b/example/index.js @@ -19,6 +19,7 @@ device bluetooth.connect(address, channel, function(err, connection){ if(err) return console.error(err); + connection.delimiter = Buffer.from('\n', 'utf8'); connection.on('data', (buffer) => { console.log('received message:', buffer.toString()); }); diff --git a/index.js b/index.js index 083b904..571fecb 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,6 @@ function Connection(port, address){ this.port = port; this.address = address; this.buffer = new Buffer(0); - this.delimiter = Buffer.from('\n', 'utf8'); const read = function () { process.nextTick(function() { if (self.isOpen()) {