diff --git a/serialport_native/serialport_native.cc b/serialport_native/serialport_native.cc index d440dc1..941c5e5 100644 --- a/serialport_native/serialport_native.cc +++ b/serialport_native/serialport_native.cc @@ -1,6 +1,4 @@ // Copyright 2010 Chris Williams -#include "serialport_native.h" - #include #include #include @@ -197,7 +195,7 @@ public: return true; } - int Write(char* buf, size_t length) { + int Write(char* buf, ssize_t length) { ssize_t written = write(fd_, buf, length); return written; } @@ -235,27 +233,21 @@ protected: SerialPort *serial_port = ObjectWrap::Unwrap(args.This()); - - if (!args[0]->IsString()) { - return scope.Close(ThrowException(Exception::Error(String::New("first argument must be a string")))); + int written = 0; + if (args[0]->IsString()) { + String::Utf8Value buffer(args[0]->ToString()); + written = serial_port->Write(*buffer, buffer.length()); + } else if (Buffer::HasInstance(args[0])) { + Buffer * buffer = ObjectWrap::Unwrap(args[0]->ToObject()); + ssize_t buffer_length = buffer->length(); + char * buf = (char*)buffer->data(); + if (buffer_length < 0) { + return ThrowException(Exception::TypeError(String::New("Bad argument"))); + } + written = serial_port->Write(buf, buffer_length); + } else { + return scope.Close(ThrowException(Exception::Error(String::New("First argument must be a string or buffer.")))); } - - String::Utf8Value buffer(args[0]->ToString()); - // - // if (!Buffer::HasInstance(args[0])) { - // return ThrowException(Exception::Error(String::New("First argument needs to be a buffer"))); - // } - // - // Buffer * buffer = ObjectWrap::Unwrap(args[1]->ToObject()); - // int buffer_length = buffer->length(); - // char * buf = (char*)buffer->data(); - // if (buffer_length < 0) { - // return ThrowException(Exception::TypeError(String::New("Bad argument"))); - // } - - - - int written = serial_port->Write(*buffer, buffer.length()); if (written < 0) return ThrowException(Exception::Error(String::NewSymbol(strerror(errno)))); return scope.Close(Integer::New(written)); } diff --git a/serialport_native/serialport_native.h b/serialport_native/serialport_native.h deleted file mode 100644 index 211512a..0000000 --- a/serialport_native/serialport_native.h +++ /dev/null @@ -1,17 +0,0 @@ -// // Copyright 2010 Chris Williams -// #ifndef SRC_FILE_H_ -// #define SRC_FILE_H_ -// -// #include -// #include -// #include -// -// namespace node { -// // -// // class SerialPort { -// // public: -// // static void Initialize(v8::Handle target); -// // }; -// -// } // namespace node -// #endif // SRC_FILE_H_ diff --git a/test.js b/test.js index e0d656b..c804d77 100644 --- a/test.js +++ b/test.js @@ -1,10 +1,14 @@ var sys = require("sys"); var SerialPort = require("./serialport_native").SerialPort; sp = new SerialPort; -sp.open("/dev/tty"); +sp.open("./sampleport"); + +var str = "TEST"; +sp.write(str); + +var buf = new Buffer([0x02, 0x03, 0x05, 0x02, 0x01]); +sp.write(buf); -var d = "TEST"; -sp.write(d); sp.close(); /*