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 4ce025c..b604a17 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: @@ -47,6 +48,10 @@ function SerialPortFactory() { xon: false, xoff: false, xany: false, + rts: false, + cts: false, + dtr: false, + dts: false, databits: 8, stopbits: 1, buffersize: 256, @@ -520,6 +525,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 8f05706..7506e84 100644 --- a/src/serialport.cpp +++ b/src/serialport.cpp @@ -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(); @@ -431,6 +492,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 011520b..1589d47 100644 --- a/src/serialport.h +++ b/src/serialport.h @@ -47,6 +47,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); @@ -127,6 +131,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 42a52b2..87a2351 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -680,6 +680,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);