1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

"Cancel" Implementation(windows only): Cancels all pending read and write operation from an other thread.

Unix also need add something like this.
This commit is contained in:
Morteza Aghazamani 2021-10-03 12:52:34 +03:30
parent 33e5a31ab7
commit b520457b14
4 changed files with 31 additions and 0 deletions

View File

@ -66,6 +66,9 @@ public:
void void
open (); open ();
void
cancel();
void void
close (); close ();

View File

@ -211,6 +211,10 @@ public:
bool bool
isOpen () const; isOpen () const;
/*! Cancels all pending read and write operation. */
void
cancel();
/*! Closes the serial port. */ /*! Closes the serial port. */
void void
close (); close ();

View File

@ -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 void
Serial::SerialImpl::close () Serial::SerialImpl::close ()
{ {
this->cancel();
if (is_open_ == true) { if (is_open_ == true) {
if (fd_ != INVALID_HANDLE_VALUE) { if (fd_ != INVALID_HANDLE_VALUE) {
int ret; int ret;

View File

@ -83,6 +83,12 @@ Serial::open ()
pimpl_->open (); pimpl_->open ();
} }
void
Serial::cancel()
{
pimpl_->cancel();
}
void void
Serial::close () Serial::close ()
{ {