1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 03:34:53 +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
open ();
void
cancel();
void
close ();

View File

@ -211,6 +211,10 @@ public:
bool
isOpen () const;
/*! Cancels all pending read and write operation. */
void
cancel();
/*! Closes the serial port. */
void
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
Serial::SerialImpl::close ()
{
this->cancel();
if (is_open_ == true) {
if (fd_ != INVALID_HANDLE_VALUE) {
int ret;

View File

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