mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-28 11:57:30 +00:00
Can now write buffers and strings.
This commit is contained in:
parent
3c68d2d733
commit
edf70c33da
3 changed files with 22 additions and 43 deletions
|
|
@ -1,6 +1,4 @@
|
|||
// Copyright 2010 Chris Williams <chris@iterativedesigns.com>
|
||||
#include "serialport_native.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
|
|
@ -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<SerialPort>(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<Buffer>(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<Buffer>(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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +0,0 @@
|
|||
// // Copyright 2010 Chris Williams <chris@iterativedesigns.com>
|
||||
// #ifndef SRC_FILE_H_
|
||||
// #define SRC_FILE_H_
|
||||
//
|
||||
// #include <node.h>
|
||||
// #include <node_events.h>
|
||||
// #include <v8.h>
|
||||
//
|
||||
// namespace node {
|
||||
// //
|
||||
// // class SerialPort {
|
||||
// // public:
|
||||
// // static void Initialize(v8::Handle<v8::Object> target);
|
||||
// // };
|
||||
//
|
||||
// } // namespace node
|
||||
// #endif // SRC_FILE_H_
|
||||
10
test.js
10
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();
|
||||
|
||||
/*
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue