From 2ab663da7ea3a58b57205ff3db2cb2aca1e34018 Mon Sep 17 00:00:00 2001 From: Chris Williams Date: Mon, 20 Sep 2010 14:36:47 -0400 Subject: [PATCH] First run of read thanks to mranney --- sampleport | 1 - serialport.js | 35 +++++++------------------- serialport_native/serialport_native.cc | 23 +++++++++++++++++ 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/sampleport b/sampleport index 30d74d2..e69de29 100644 --- a/sampleport +++ b/sampleport @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/serialport.js b/serialport.js index 8d8c699..c53477e 100644 --- a/serialport.js +++ b/serialport.js @@ -6,7 +6,7 @@ var Buffer = require('buffer').Buffer; var events = require('events'); var fs = require('fs'); var serialport_native = require('./serialport_native'); - +// var IOWatcher = process.binding('io_watcher').IOWatcher; // - for the future! var BAUDRATES = [115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 120,, 600, 300, 200, 150, 134, 110, 75, 50]; var DATABITS = [8, 7, 6, 5]; @@ -39,20 +39,9 @@ function SerialPort(path) { this.fd = serialport_native.open(path, this.baudrate, this.databits, this.stopbits, this.parity); this.active = true; - // - // this.readStream = fs.createReadStream(path); - // this.readStream.on('data', function(data) { - // me.emit('data', data); - // }); - // this.readStream.on('close', function() { - // me.emit('close'); - // }); - // this.readStream.on('error', function(err) { - // me.emit('error', err); - // }); - // this.readStream.resume() - + this.readWatcher = new process.IOWatcher(); + // this.readWatcher = new IOWatcher(); this.empty_reads = 0; this.buf = new Buffer(65535); @@ -61,18 +50,12 @@ function SerialPort(path) { this.readWatcher.callback = function () { sys.puts("read callback"); if (me.fd) { - data_read = fs.read(me.fd, me.buf, 0, 65535, function (err, bytesRead) { - sys.puts(bytesRead); - if (err) { - sys.puts('error'); - me.emit('error',err); - } else if (bytesRead > 0) { - sys.puts('data: '+ me.buf); - me.emit('data', me.buf); - } - sys.puts("truncate") - fs.truncate(me.fd, bytesRead); - }); + data_read = serialport_native.read(me.fd, me.buf); + if (data_read > 0) { + sys.puts("Read some data: " + data_read + " bytes"); + sys.puts("Here is the data: " + me.buf.toString('utf8', 0, data_read)); + me.emit('data', me.buf); + } } }; this.readWatcher.set(this.fd, true, false); diff --git a/serialport_native/serialport_native.cc b/serialport_native/serialport_native.cc index 575e4da..fde5612 100644 --- a/serialport_native/serialport_native.cc +++ b/serialport_native/serialport_native.cc @@ -19,6 +19,7 @@ #include #include +#include #include @@ -38,6 +39,27 @@ namespace node { return e; } + + static Handle Read(const Arguments& args) { + HandleScope scope; + if (args.Length() != 2) { + return ThrowException(Exception::TypeError(String::New("Read takes exactly two arguments"))); + } + if (!args[0]->IsInt32()) { + return ThrowException(Exception::TypeError(String::New("First argument must be an fd"))); + } + if (!Buffer::HasInstance(args[1])) { + return ThrowException(Exception::TypeError(String::New("Second argument must be a buffer"))); + } + + int fd = args[0]->Int32Value(); + Buffer *buffer = ObjectWrap::Unwrap(args[1]->ToObject()); + int buffer_length = buffer->length(); + char *buffer_data = (char*)buffer->data(); + int bytes_read = read(fd, buffer_data, buffer_length); + return scope.Close(Integer::NewFromUnsigned(fd)); + } + static Handle Open(const Arguments& args) { HandleScope scope; @@ -233,6 +255,7 @@ namespace node { void SerialPort::Initialize(Handle target) { HandleScope scope; NODE_SET_METHOD(target, "open", Open); + NODE_SET_METHOD(target, "read", Read); }