mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
unix support for update baudrate
This commit is contained in:
parent
171377b55a
commit
7eba3f8d41
5 changed files with 164 additions and 9 deletions
|
|
@ -242,6 +242,38 @@ function SerialPortFactory(_spfOptions) {
|
|||
});
|
||||
};
|
||||
|
||||
//underlying code is written to update all options, but for now
|
||||
//only baud is respected as I dont want to duplicate all the option
|
||||
//verification code above
|
||||
SerialPort.prototype.update = function (options, callback) {
|
||||
var self = this;
|
||||
if (!this.fd) {
|
||||
debug('Update attempted, but serialport not available - FD is not set');
|
||||
var err = new Error('Serialport not open.');
|
||||
if (callback) {
|
||||
callback(err);
|
||||
} else {
|
||||
// console.log("write-fd");
|
||||
self.emit('error', err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.options.baudRate = options.baudRate || options.baudrate || _options.baudrate;
|
||||
|
||||
factory.SerialPortBinding.update(this.fd, this.options, function (err) {
|
||||
if (err) {
|
||||
if (callback) {
|
||||
callback(err);
|
||||
} else {
|
||||
self.emit('error', err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
self.emit('open');
|
||||
if (callback) { callback(); }
|
||||
});
|
||||
};
|
||||
|
||||
SerialPort.prototype.isOpen = function() {
|
||||
return (this.fd ? true : false);
|
||||
|
|
|
|||
|
|
@ -170,6 +170,90 @@ void EIO_AfterOpen(uv_work_t* req) {
|
|||
delete req;
|
||||
}
|
||||
|
||||
NAN_METHOD(Update) {
|
||||
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<v8::Object> options = args[1]->ToObject();
|
||||
|
||||
// callback
|
||||
if(!args[2]->IsFunction()) {
|
||||
NanThrowTypeError("Third argument must be a function");
|
||||
NanReturnUndefined();
|
||||
}
|
||||
v8::Local<v8::Function> callback = args[2].As<v8::Function>();
|
||||
|
||||
OpenBaton* baton = new OpenBaton();
|
||||
memset(baton, 0, sizeof(OpenBaton));
|
||||
baton->fd = fd;
|
||||
baton->baudRate = options->Get(NanNew<v8::String>("baudRate"))->ToInt32()->Int32Value();
|
||||
baton->dataBits = options->Get(NanNew<v8::String>("dataBits"))->ToInt32()->Int32Value();
|
||||
baton->bufferSize = options->Get(NanNew<v8::String>("bufferSize"))->ToInt32()->Int32Value();
|
||||
baton->parity = ToParityEnum(options->Get(NanNew<v8::String>("parity"))->ToString());
|
||||
baton->stopBits = ToStopBitEnum(options->Get(NanNew<v8::String>("stopBits"))->ToNumber()->NumberValue());
|
||||
baton->rtscts = options->Get(NanNew<v8::String>("rtscts"))->ToBoolean()->BooleanValue();
|
||||
baton->xon = options->Get(NanNew<v8::String>("xon"))->ToBoolean()->BooleanValue();
|
||||
baton->xoff = options->Get(NanNew<v8::String>("xoff"))->ToBoolean()->BooleanValue();
|
||||
baton->xany = options->Get(NanNew<v8::String>("xany"))->ToBoolean()->BooleanValue();
|
||||
|
||||
v8::Local<v8::Object> platformOptions = options->Get(NanNew<v8::String>("platformOptions"))->ToObject();
|
||||
baton->platformOptions = ParsePlatformOptions(platformOptions);
|
||||
|
||||
baton->callback = new NanCallback(callback);
|
||||
baton->dataCallback = new NanCallback(options->Get(NanNew<v8::String>("dataCallback")).As<v8::Function>());
|
||||
baton->disconnectedCallback = new NanCallback(options->Get(NanNew<v8::String>("disconnectedCallback")).As<v8::Function>());
|
||||
baton->errorCallback = new NanCallback(options->Get(NanNew<v8::String>("errorCallback")).As<v8::Function>());
|
||||
|
||||
uv_work_t* req = new uv_work_t();
|
||||
req->data = baton;
|
||||
|
||||
uv_queue_work(uv_default_loop(), req, EIO_Update, (uv_after_work_cb)EIO_AfterUpdate);
|
||||
|
||||
NanReturnUndefined();
|
||||
}
|
||||
|
||||
void EIO_AfterUpdate(uv_work_t* req) {
|
||||
NanScope();
|
||||
|
||||
OpenBaton* data = static_cast<OpenBaton*>(req->data);
|
||||
|
||||
v8::Handle<v8::Value> argv[2];
|
||||
if(data->errorString[0]) {
|
||||
argv[0] = v8::Exception::Error(NanNew<v8::String>(data->errorString));
|
||||
argv[1] = NanUndefined();
|
||||
// not needed for AfterOpenSuccess
|
||||
delete data->dataCallback;
|
||||
delete data->errorCallback;
|
||||
delete data->disconnectedCallback;
|
||||
} else {
|
||||
argv[0] = NanUndefined();
|
||||
argv[1] = NanNew<v8::Int32>(data->result);
|
||||
|
||||
int fd = argv[1]->ToInt32()->Int32Value();
|
||||
newQForFD(fd);
|
||||
|
||||
AfterOpenSuccess(data->result, data->dataCallback, data->disconnectedCallback, data->errorCallback);
|
||||
}
|
||||
|
||||
data->callback->Call(2, argv);
|
||||
|
||||
delete data->platformOptions;
|
||||
delete data->callback;
|
||||
delete data;
|
||||
delete req;
|
||||
}
|
||||
|
||||
NAN_METHOD(Write) {
|
||||
NanScope();
|
||||
|
||||
|
|
@ -603,6 +687,7 @@ extern "C" {
|
|||
NanScope();
|
||||
NODE_SET_METHOD(target, "set", Set);
|
||||
NODE_SET_METHOD(target, "open", Open);
|
||||
NODE_SET_METHOD(target, "update", Update);
|
||||
NODE_SET_METHOD(target, "write", Write);
|
||||
NODE_SET_METHOD(target, "close", Close);
|
||||
NODE_SET_METHOD(target, "list", List);
|
||||
|
|
|
|||
|
|
@ -34,6 +34,10 @@ void EIO_Open(uv_work_t* req);
|
|||
void EIO_AfterOpen(uv_work_t* req);
|
||||
void AfterOpenSuccess(int fd, NanCallback* dataCallback, NanCallback* disconnectedCallback, NanCallback* errorCallback);
|
||||
|
||||
NAN_METHOD(Update);
|
||||
void EIO_Update(uv_work_t* req);
|
||||
void EIO_AfterUpdate(uv_work_t* req);
|
||||
|
||||
NAN_METHOD(Write);
|
||||
void EIO_Write(uv_work_t* req);
|
||||
void EIO_AfterWrite(uv_work_t* req);
|
||||
|
|
@ -69,6 +73,7 @@ public:
|
|||
NanCallback* dataCallback;
|
||||
NanCallback* disconnectedCallback;
|
||||
NanCallback* errorCallback;
|
||||
int fd;
|
||||
int result;
|
||||
int baudRate;
|
||||
int dataBits;
|
||||
|
|
@ -190,4 +195,6 @@ public:
|
|||
char errorString[ERROR_STRING_SIZE];
|
||||
};
|
||||
|
||||
int setup(int fd, OpenBaton *data);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -128,8 +128,34 @@ int ToDataBitsConstant(int dataBits) {
|
|||
|
||||
|
||||
void EIO_Open(uv_work_t* req) {
|
||||
OpenBaton* data = static_cast<OpenBaton*>(req->data);
|
||||
UnixPlatformOptions* platformOptions = static_cast<UnixPlatformOptions*>(data->platformOptions);
|
||||
OpenBaton* data = static_cast<OpenBaton*>(req->data);
|
||||
|
||||
int flags = (O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC | O_SYNC);
|
||||
int fd = open(data->path, flags);
|
||||
|
||||
if(-1 == setup(fd, data)){
|
||||
return;
|
||||
}
|
||||
|
||||
data->result = fd;
|
||||
}
|
||||
|
||||
void EIO_Update(uv_work_t* req) {
|
||||
OpenBaton* data = static_cast<OpenBaton*>(req->data);
|
||||
|
||||
int fd = data->fd;
|
||||
|
||||
if(-1 == setup(fd, data)){
|
||||
return;
|
||||
}
|
||||
|
||||
data->result = fd;
|
||||
}
|
||||
|
||||
|
||||
int setup(int fd, OpenBaton *data) {
|
||||
|
||||
UnixPlatformOptions* platformOptions = static_cast<UnixPlatformOptions*>(data->platformOptions);
|
||||
|
||||
int baudRate = ToBaudConstant(data->baudRate);
|
||||
|
||||
|
|
@ -143,16 +169,15 @@ void EIO_Open(uv_work_t* req) {
|
|||
int dataBits = ToDataBitsConstant(data->dataBits);
|
||||
if(dataBits == -1) {
|
||||
snprintf(data->errorString, sizeof(data->errorString), "Invalid data bits setting %d", data->dataBits);
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int flags = (O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC | O_SYNC);
|
||||
int fd = open(data->path, flags);
|
||||
|
||||
|
||||
if (fd == -1) {
|
||||
snprintf(data->errorString, sizeof(data->errorString), "Cannot open %s", data->path);
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -276,7 +301,7 @@ void EIO_Open(uv_work_t* req) {
|
|||
default:
|
||||
snprintf(data->errorString, sizeof(data->errorString), "Invalid parity setting %d", data->parity);
|
||||
close(fd);
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch(data->stopBits) {
|
||||
|
|
@ -289,7 +314,7 @@ void EIO_Open(uv_work_t* req) {
|
|||
default:
|
||||
snprintf(data->errorString, sizeof(data->errorString), "Invalid stop bits setting %d", data->stopBits);
|
||||
close(fd);
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
options.c_cflag |= CLOCAL; //ignore status lines
|
||||
|
|
@ -321,7 +346,8 @@ void EIO_Open(uv_work_t* req) {
|
|||
}
|
||||
#endif
|
||||
|
||||
data->result = fd;
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
void EIO_Write(uv_work_t* req) {
|
||||
|
|
|
|||
|
|
@ -163,6 +163,11 @@ public:
|
|||
NanCallback* disconnectedCallback;
|
||||
};
|
||||
|
||||
void EIO_Update(uv_work_t* req) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
void EIO_Set(uv_work_t* req) {
|
||||
SetBaton* data = static_cast<SetBaton*>(req->data);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue