Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ double getDoubleFromObject(Napi::Object options, std::string key) {
return getValueFromObject(options, key).ToNumber().DoubleValue();
}

bool hasKeyWithDefinedValue(Napi::Object options, std::string key) {
if ((options).Has(key)) {
Napi::Value val = getValueFromObject(options, key);
if (val.IsUndefined() || val.IsNull()) {
return false;
}
return true;
}
return false;
}

Napi::Value Open(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// path
Expand Down Expand Up @@ -184,11 +195,26 @@ Napi::Value Set(const Napi::CallbackInfo& info) {

SetBaton* baton = new SetBaton(callback);
baton->fd = fd;
baton->brk = getBoolFromObject(options, "brk");
baton->rts = getBoolFromObject(options, "rts");
baton->cts = getBoolFromObject(options, "cts");
baton->dtr = getBoolFromObject(options, "dtr");
baton->dsr = getBoolFromObject(options, "dsr");
if (hasKeyWithDefinedValue(options, "brk")) {
baton->brkToSet = true;
baton->brk = getBoolFromObject(options, "brk");
}
if (hasKeyWithDefinedValue(options, "rts")) {
baton->rtsToSet = true;
baton->rts = getBoolFromObject(options, "rts");
}
if (hasKeyWithDefinedValue(options, "cts")) {
baton->ctsToSet = true;
baton->cts = getBoolFromObject(options, "cts");
}
if (hasKeyWithDefinedValue(options, "dtr")) {
baton->dtrToSet = true;
baton->dtr = getBoolFromObject(options, "dtr");
}
if (hasKeyWithDefinedValue(options, "dsr")) {
baton->rtsToSet = true;
baton->dsr = getBoolFromObject(options, "dsr");
}
baton->lowLatency = getBoolFromObject(options, "lowLatency");

baton->Queue();
Expand Down
6 changes: 6 additions & 0 deletions src/serialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ struct SetBaton : public Napi::AsyncWorker {
bool brk = false;
bool lowLatency = false;

bool rtsToSet = false;
bool dtrToSet = false;
bool brkToSet = false;
bool ctsToSet = false;
bool dsrToSet = false;

void Execute() override;

void OnOK() override {
Expand Down
34 changes: 22 additions & 12 deletions src/serialport_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,32 @@ void ConnectionOptionsBaton::Execute() {
}

void SetBaton::Execute() {
if (rts) {
EscapeCommFunction(int2handle(fd), SETRTS);
} else {
EscapeCommFunction(int2handle(fd), CLRRTS);
if (rtsToSet) {
if (rts) {
EscapeCommFunction(int2handle(fd), SETRTS);
} else {
EscapeCommFunction(int2handle(fd), CLRRTS);
}
}

if (dtr) {
EscapeCommFunction(int2handle(fd), SETDTR);
} else {
EscapeCommFunction(int2handle(fd), CLRDTR);
if (dtrToSet) {
if (dtr) {
EscapeCommFunction(int2handle(fd), SETDTR);
} else {
EscapeCommFunction(int2handle(fd), CLRDTR);
}
}

if (brk) {
EscapeCommFunction(int2handle(fd), SETBREAK);
} else {
EscapeCommFunction(int2handle(fd), CLRBREAK);
if (brkToSet) {
if (brk) {
EscapeCommFunction(int2handle(fd), SETBREAK);
} else {
EscapeCommFunction(int2handle(fd), CLRBREAK);
}
}

if (!ctsToSet && !dsrToSet) {
return;
}

DWORD bits = 0;
Expand Down