From 72eff29d84760a91ae2dbcb16117b711bfff8f66 Mon Sep 17 00:00:00 2001 From: Chris Williams Date: Tue, 28 Jun 2011 10:15:37 -0400 Subject: [PATCH] pushing 0.2.6 with possible fix for arduino reading. --- README.md | 2 +- changelog.md | 5 +++ package.json | 2 +- serialport.js | 44 +++++++++++--------------- serialport_native/serialport_native.cc | 2 +- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 6f320d0..7ad247d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ 8ee88 88ee 88 8 88 88 8 88eee 88 8eee8 88 8 88 -Version: 0.2.5 - Released June 27, 2011 +Version: 0.3.0 - Released June 28, 2011 ***** diff --git a/changelog.md b/changelog.md index 3006ddb..33fb2cc 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,8 @@ +Version 0.2.5 - Version 0.2.6 +----------------------------- +- Debugging issue with IOWatcher not holding in the event loop in node.js. +- Converted to ReadStream instead of IOWatcher. + Version 0.2.4 ------------- - Integrated arduino tests (rwaldron) diff --git a/package.json b/package.json index 9f1fcb9..c30c4bc 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "name" : "serialport", - "version" : "0.2.5", + "version" : "0.2.6", "description" : "Welcome your robotic javascript overlords. Better yet, program them!", "author": "Chris Williams ", "main": "./serialport", diff --git a/serialport.js b/serialport.js index 0165231..287f966 100644 --- a/serialport.js +++ b/serialport.js @@ -73,37 +73,29 @@ function SerialPort(path, options) { this.port = path; this.fd = serialport_native.open(this.port, options.baudrate, options.databits, options.stopbits, options.parity, options.flowcontrol); - - this.readWatcher = new IOWatcher(); - this.empty_reads = 0; - this.readWatcher.callback = (function (file_id, me) { - return function () { - var buffer = new Buffer(options.buffersize); - var bytes_read = 0, err = null; - try { - bytes_read = serialport_native.read(file_id, buffer); - } catch (e) { - err = e; - } - if (bytes_read <= 0) { - // assume issue with reading. - me.emit("error", (err ? err :"Read triggered, but no bytes available. Assuming error with serial port shutting down.")); - me.close(); - } else { - options.parser(me, buffer.slice(0, bytes_read)); - } - } - })(this.fd, this); - this.readWatcher.set(this.fd, true, false); - this.readWatcher.start(); - + if (this.fd == -1) { + this.emit("error", new Error('could not open serial port')) + } else { + this.readStream = fs.createReadStream(this.port); + var dataCallback = (function (me) { + return (function (buffer) { + options.parser(me, buffer) + }); + })(this); + var errorCallback = (function (me) { + return (function (err) { + me.emit("error", err); + }); + }); + this.readStream.on("data", dataCallback); + this.readStream.on("error", errorCallback); + } } sys.inherits(SerialPort, stream.Stream); SerialPort.prototype.close = function () { - this.readWatcher.stop(); - + this.readStream.close(); if (this.fd) { serialport_native.close(this.fd); this.fd = null; diff --git a/serialport_native/serialport_native.cc b/serialport_native/serialport_native.cc index 9333c8e..e4f7b2a 100644 --- a/serialport_native/serialport_native.cc +++ b/serialport_native/serialport_native.cc @@ -249,7 +249,7 @@ namespace node { int fd = open(*path, flags); if (fd == -1) { - perror("open_port: Unable to open specified serial port connection."); + // perror("open_port: Unable to open specified serial port connection."); return scope.Close(Integer::New(fd)); } else { struct sigaction saio;