Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Cancel" Implementation(windows only): Cancels all pending read and w… #257

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions include/serial/impl/win.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class serial::Serial::SerialImpl {
void
open ();

void
cancel();

void
close ();

Expand Down
4 changes: 4 additions & 0 deletions include/serial/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ class Serial {
bool
isOpen () const;

/*! Cancels all pending read and write operation. */
void
cancel();

/*! Closes the serial port. */
void
close ();
Expand Down
18 changes: 18 additions & 0 deletions src/impl/win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,27 @@ Serial::SerialImpl::reconfigurePort ()
}
}

void
Serial::SerialImpl::cancel()
{
if (is_open_ == true) {
if (fd_ != INVALID_HANDLE_VALUE) {
int ret;
ret = CancelIoEx(fd_, NULL);
DWORD last_error = GetLastError();
if (ret == 0 && last_error != ERROR_NOT_FOUND) {
stringstream ss;
ss << "Error while canceling serial port: " << last_error;
THROW(IOException, ss.str().c_str());
}
}
}
}

void
Serial::SerialImpl::close ()
{
this->cancel();
if (is_open_ == true) {
if (fd_ != INVALID_HANDLE_VALUE) {
int ret;
Expand Down
6 changes: 6 additions & 0 deletions src/serial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ Serial::open ()
pimpl_->open ();
}

void
Serial::cancel()
{
pimpl_->cancel();
}

void
Serial::close ()
{
Expand Down