diff --git a/README.md b/README.md index 404a1b3..2bb38df 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,12 @@ [![Build Status](https://travis-ci.org/voodootikigod/node-serialport.png?branch=master)](https://travis-ci.org/voodootikigod/node-serialport) [![Gitter chat](https://badges.gitter.im/voodootikigod/node-serialport.png)](https://gitter.im/voodootikigod/node-serialport) +[![Dependency Status](https://david-dm.org/voodootikigod/node-serialport.png) For all discussions, designs, and clarifications, we recommend you join our Gitter Chat room: [https://gitter.im/voodootikigod/node-serialport](https://gitter.im/voodootikigod/node-serialport) + + Version: 1.4.6 - Released September 3, 2014 ***** diff --git a/examples/reset.js b/examples/reset.js new file mode 100644 index 0000000..96a70be --- /dev/null +++ b/examples/reset.js @@ -0,0 +1,41 @@ +//////////////////////////////////////////////////////// +// Use the cool library // +// git://github.com/voodootikigod/node-serialport.git // +// to reset an arduino . // +//////////////////////////////////////////////////////// +var com = require("serialport"); + +var serialPort = new com.SerialPort("/dev/tty.usbmodem1411", { + baudrate: 115200, + parser: com.parsers.readline('\r\n') + }); + +serialPort.on('open',function() { + console.log('Port open'); + setTimeout(asserting, 5000); +}); + +serialPort.on('data', function(data) { + console.log(data); +}); + + +function asserting() { + console.log('asserting'); + serialPort.set({rts:true, dtr:true}, function(err, something) { + console.log('asserted'); + setTimeout(clear, 250); + }); +} + +function clear() { + console.log('clearing'); + serialPort.set({rts:false, dtr:false}, function(err, something) { + console.log('clear'); + setTimeout(done, 50); + }); +} + +function done() { + console.log("done resetting"); +} diff --git a/serialport.js b/serialport.js index 57d31e7..4c3faaf 100644 --- a/serialport.js +++ b/serialport.js @@ -33,6 +33,7 @@ function SerialPortFactory() { var STOPBITS = [1, 1.5, 2]; var PARITY = ['none', 'even', 'mark', 'odd', 'space']; var FLOWCONTROLS = ["XON", "XOFF", "XANY", "RTSCTS"]; + var SETS = ["rts", "cts", "dtr", "dts"]; // Stuff from ReadStream, refactored for our usage: @@ -58,6 +59,10 @@ function SerialPortFactory() { xon: false, xoff: false, xany: false, + rts: false, + cts: false, + dtr: false, + dts: false, databits: 8, stopbits: 1, buffersize: 256, @@ -540,6 +545,41 @@ function SerialPortFactory() { }); }; + SerialPort.prototype.set = function (options, callback) { + var self = this; + var fd = self.fd; + + options = (typeof option !== 'function') && options || {}; + + // flush defaults, then update with provided details + options.rts = options.rts || options.rts || _options.rts; + options.cts = options.cts || options.cts || _options.cts; + options.dtr = options.dtr || options.dtr || _options.dtr; + options.dts = options.dts || options.dts || _options.dts; + + if (!fd) { + var err = new Error("Serialport not open."); + if (callback) { + callback(err); + } else { + self.emit('error', err); + } + return; + } + + factory.SerialPortBinding.set(fd, options, function (err, result) { + if (err) { + if (callback) { + callback(err, result); + } else { + self.emit('error', err); + } + } else { + callback(err, result); + } + }); + }; + SerialPort.prototype.drain = function (callback) { var self = this; var fd = this.fd; diff --git a/src/serialport.cpp b/src/serialport.cpp index 900278b..4647bef 100644 --- a/src/serialport.cpp +++ b/src/serialport.cpp @@ -166,7 +166,7 @@ void EIO_AfterWrite(uv_work_t* req) { // We're not done with this baton, so throw it right back onto the queue. // Don't re-push the write in the event loop if there was an error; because same error could occur again! // TODO: Add a uv_poll here for unix... - fprintf(stderr, "Write again...\n"); + //fprintf(stderr, "Write again...\n"); uv_queue_work(uv_default_loop(), req, EIO_Write, (uv_after_work_cb)EIO_AfterWrite); return; } @@ -341,6 +341,67 @@ void EIO_AfterFlush(uv_work_t* req) { delete req; } +NAN_METHOD(Set) { + NanScope(); + + // file descriptor + if(!args[0]->IsInt32()) { + NanThrowTypeError("First argument must be an int"); + NanReturnUndefined(); + } + int fd = args[0]->ToInt32()->Int32Value(); + + // options + if(!args[1]->IsObject()) { + NanThrowTypeError("Second argument must be an object"); + NanReturnUndefined(); + } + v8::Local options = args[1]->ToObject(); + + // callback + if(!args[2]->IsFunction()) { + NanThrowTypeError("Third argument must be a function"); + NanReturnUndefined(); + } + v8::Local callback = args[2].As(); + + SetBaton* baton = new SetBaton(); + memset(baton, 0, sizeof(SetBaton)); + baton->fd = fd; + baton->callback = new NanCallback(callback); + baton->rts = options->Get(NanNew("rts"))->ToBoolean()->BooleanValue(); + baton->cts = options->Get(NanNew("cts"))->ToBoolean()->BooleanValue(); + baton->dtr = options->Get(NanNew("dtr"))->ToBoolean()->BooleanValue(); + baton->dsr = options->Get(NanNew("dsr"))->ToBoolean()->BooleanValue(); + + uv_work_t* req = new uv_work_t(); + req->data = baton; + uv_queue_work(uv_default_loop(), req, EIO_Set, (uv_after_work_cb)EIO_AfterSet); + + NanReturnUndefined(); +} + +void EIO_AfterSet(uv_work_t* req) { + NanScope(); + + SetBaton* data = static_cast(req->data); + + v8::Handle argv[2]; + + if(data->errorString[0]) { + argv[0] = v8::Exception::Error(NanNew(data->errorString)); + argv[1] = NanUndefined(); + } else { + argv[0] = NanUndefined(); + argv[1] = NanNew(data->result); + } + data->callback->Call(2, argv); + + delete data->callback; + delete data; + delete req; +} + NAN_METHOD(Drain) { NanScope(); @@ -427,6 +488,7 @@ extern "C" { void init (v8::Handle target) { NanScope(); + NODE_SET_METHOD(target, "set", Set); NODE_SET_METHOD(target, "open", Open); NODE_SET_METHOD(target, "write", Write); NODE_SET_METHOD(target, "close", Close); diff --git a/src/serialport.h b/src/serialport.h index eb6599e..07c1652 100644 --- a/src/serialport.h +++ b/src/serialport.h @@ -46,6 +46,10 @@ NAN_METHOD(Flush); void EIO_Flush(uv_work_t* req); void EIO_AfterFlush(uv_work_t* req); +NAN_METHOD(Set); +void EIO_Set(uv_work_t* req); +void EIO_AfterSet(uv_work_t* req); + NAN_METHOD(Drain); void EIO_Drain(uv_work_t* req); void EIO_AfterDrain(uv_work_t* req); @@ -164,6 +168,19 @@ public: char errorString[ERROR_STRING_SIZE]; }; +struct SetBaton { +public: + int fd; + NanCallback* callback; + int result; + char errorString[ERROR_STRING_SIZE]; + bool rts; + bool cts; + bool dtr; + bool dsr; + +}; + struct DrainBaton { public: int fd; diff --git a/src/serialport_unix.cpp b/src/serialport_unix.cpp index 4858433..cb61848 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -698,6 +698,34 @@ void EIO_Flush(uv_work_t* req) { data->result = tcflush(data->fd, TCIFLUSH); } +void EIO_Set(uv_work_t* req) { + SetBaton* data = static_cast(req->data); + + int bits; + ioctl( data->fd, TIOCMGET, &bits ); + + bits &= ~(TIOCM_RTS | TIOCM_CTS | TIOCM_DTR | TIOCM_DSR); + + if (data->rts) { + bits |= TIOCM_RTS; + } + + if (data->cts) { + bits |= TIOCM_CTS; + } + + if (data->dtr) { + bits |= TIOCM_DTR; + } + + if (data->dsr) { + bits |= TIOCM_DSR; + } + + data->result = ioctl( data->fd, TIOCMSET, &bits ); + +} + void EIO_Drain(uv_work_t* req) { DrainBaton* data = static_cast(req->data);