1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 11:44:53 +08:00

Fix ERROR_INVALID_PARAMETER and undefined WINBOOL

This commit is contained in:
chinaheyu 2023-09-05 09:36:57 +08:00
parent 310b473868
commit a196d9fd68
2 changed files with 32 additions and 31 deletions

View File

@ -195,12 +195,12 @@ private:
flowcontrol_t flowcontrol_; // Flow Control flowcontrol_t flowcontrol_; // Flow Control
// Mutex used to lock the read functions // Mutex used to lock the read functions
HANDLE read_mutex; HANDLE read_mutex_;
// Mutex used to lock the write functions // Mutex used to lock the write functions
HANDLE write_mutex; HANDLE write_mutex_;
OVERLAPPED ov_read; // OVERLAPPED read OVERLAPPED ov_read_; // OVERLAPPED read
OVERLAPPED ov_write; // OVERLAPPED write OVERLAPPED ov_write_; // OVERLAPPED write
}; };
} }

View File

@ -37,23 +37,24 @@ Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
flowcontrol_t flowcontrol) flowcontrol_t flowcontrol)
: port_ (port.begin(), port.end()), fd_ (INVALID_HANDLE_VALUE), is_open_ (false), : port_ (port.begin(), port.end()), fd_ (INVALID_HANDLE_VALUE), is_open_ (false),
baudrate_ (baudrate), parity_ (parity), baudrate_ (baudrate), parity_ (parity),
bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol) bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol),
ov_read_{}, ov_write_{}
{ {
if (port_.empty () == false) if (port_.empty () == false)
open (); open ();
read_mutex = CreateMutex(NULL, false, NULL); read_mutex_ = CreateMutex(NULL, false, NULL);
write_mutex = CreateMutex(NULL, false, NULL); write_mutex_ = CreateMutex(NULL, false, NULL);
ov_read.hEvent = CreateEvent(NULL, 1, 0, NULL); ov_read_.hEvent = CreateEvent(NULL, 1, 0, NULL);
ov_write.hEvent = CreateEvent(NULL, 0, 0, NULL); ov_write_.hEvent = CreateEvent(NULL, 0, 0, NULL);
} }
Serial::SerialImpl::~SerialImpl () Serial::SerialImpl::~SerialImpl ()
{ {
this->close(); this->close();
CloseHandle(read_mutex); CloseHandle(read_mutex_);
CloseHandle(write_mutex); CloseHandle(write_mutex_);
CloseHandle(ov_read.hEvent); CloseHandle(ov_read_.hEvent);
CloseHandle(ov_write.hEvent); CloseHandle(ov_write_.hEvent);
} }
void void
@ -284,18 +285,18 @@ Serial::SerialImpl::close ()
if (is_open_) { if (is_open_) {
// Cancel a blocking operation. // Cancel a blocking operation.
DWORD rc; DWORD rc;
WINBOOL err; BOOL err;
err = GetOverlappedResult(fd_, &ov_read, &rc, FALSE); err = GetOverlappedResult(fd_, &ov_read_, &rc, FALSE);
if (!err) { if (!err) {
rc = GetLastError(); rc = GetLastError();
if (rc == ERROR_IO_PENDING || rc == ERROR_IO_INCOMPLETE) if (rc == ERROR_IO_PENDING || rc == ERROR_IO_INCOMPLETE)
CancelIoEx(fd_, &ov_read); CancelIoEx(fd_, &ov_read_);
} }
err = GetOverlappedResult(fd_, &ov_write, &rc, FALSE); err = GetOverlappedResult(fd_, &ov_write_, &rc, FALSE);
if (!err) { if (!err) {
rc = GetLastError(); rc = GetLastError();
if (rc == ERROR_IO_PENDING || rc == ERROR_IO_INCOMPLETE) if (rc == ERROR_IO_PENDING || rc == ERROR_IO_INCOMPLETE)
CancelIoEx(fd_, &ov_write); CancelIoEx(fd_, &ov_write_);
} }
if (fd_ != INVALID_HANDLE_VALUE) { if (fd_ != INVALID_HANDLE_VALUE) {
@ -350,14 +351,14 @@ Serial::SerialImpl::waitReadable (uint32_t timeout)
} }
msk = 0; msk = 0;
SetCommMask(fd_, EV_RXCHAR | EV_ERR); SetCommMask(fd_, EV_RXCHAR | EV_ERR);
if (!WaitCommEvent(fd_, &msk, &ov_read)) { if (!WaitCommEvent(fd_, &msk, &ov_read_)) {
if (GetLastError() == ERROR_IO_PENDING) { if (GetLastError() == ERROR_IO_PENDING) {
if (WaitForSingleObject(ov_read.hEvent, (DWORD)timeout) == WAIT_TIMEOUT) { if (WaitForSingleObject(ov_read_.hEvent, (DWORD)timeout) == WAIT_TIMEOUT) {
SetCommMask(fd_, old_msk); SetCommMask(fd_, old_msk);
return false; return false;
} }
GetOverlappedResult(fd_, &ov_read, &length, TRUE); GetOverlappedResult(fd_, &ov_read_, &length, TRUE);
ResetEvent(ov_read.hEvent); ResetEvent(ov_read_.hEvent);
} else { } else {
ClearCommError(fd_, &error, &cs); ClearCommError(fd_, &error, &cs);
SetCommMask(fd_, old_msk); SetCommMask(fd_, old_msk);
@ -392,9 +393,9 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
throw PortNotOpenedException ("Serial::read"); throw PortNotOpenedException ("Serial::read");
} }
if (size > 0) { if (size > 0) {
ResetEvent(ov_read.hEvent); ResetEvent(ov_read_.hEvent);
DWORD bytes_read; DWORD bytes_read;
WINBOOL read_ok = ReadFile(fd_, buf, static_cast<DWORD>(size), &bytes_read, &ov_read); BOOL read_ok = ReadFile(fd_, buf, static_cast<DWORD>(size), &bytes_read, &ov_read_);
if (!read_ok) { if (!read_ok) {
DWORD error = GetLastError(); DWORD error = GetLastError();
if ((error != ERROR_SUCCESS) && (error != ERROR_IO_PENDING)) { if ((error != ERROR_SUCCESS) && (error != ERROR_IO_PENDING)) {
@ -403,7 +404,7 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
THROW (IOException, ss.str().c_str()); THROW (IOException, ss.str().c_str());
} }
} }
WINBOOL result_ok = GetOverlappedResult(fd_, &ov_read, &bytes_read, TRUE); BOOL result_ok = GetOverlappedResult(fd_, &ov_read_, &bytes_read, TRUE);
if (!result_ok) { if (!result_ok) {
DWORD error = GetLastError(); DWORD error = GetLastError();
if (error != ERROR_OPERATION_ABORTED) { if (error != ERROR_OPERATION_ABORTED) {
@ -425,7 +426,7 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
throw PortNotOpenedException ("Serial::write"); throw PortNotOpenedException ("Serial::write");
} }
DWORD bytes_written; DWORD bytes_written;
int success = WriteFile(fd_, data, static_cast<DWORD>(length), &bytes_written, &ov_write); int success = WriteFile(fd_, data, static_cast<DWORD>(length), &bytes_written, &ov_write_);
DWORD error = success ? ERROR_SUCCESS : GetLastError(); DWORD error = success ? ERROR_SUCCESS : GetLastError();
if (timeout_.write_timeout_constant != 0) { if (timeout_.write_timeout_constant != 0) {
if ((error != ERROR_SUCCESS) && (error != ERROR_IO_PENDING)) { if ((error != ERROR_SUCCESS) && (error != ERROR_IO_PENDING)) {
@ -433,7 +434,7 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
ss << "WriteFile failed: " << error; ss << "WriteFile failed: " << error;
THROW (IOException, ss.str().c_str()); THROW (IOException, ss.str().c_str());
} }
GetOverlappedResult(fd_, &ov_write, &bytes_written, TRUE); GetOverlappedResult(fd_, &ov_write_, &bytes_written, TRUE);
error = GetLastError(); error = GetLastError();
if (error == ERROR_OPERATION_ABORTED) { if (error == ERROR_OPERATION_ABORTED) {
return bytes_written; return bytes_written;
@ -715,7 +716,7 @@ Serial::SerialImpl::getCD()
void void
Serial::SerialImpl::readLock() Serial::SerialImpl::readLock()
{ {
if (WaitForSingleObject(read_mutex, INFINITE) != WAIT_OBJECT_0) { if (WaitForSingleObject(read_mutex_, INFINITE) != WAIT_OBJECT_0) {
THROW (IOException, "Error claiming read mutex."); THROW (IOException, "Error claiming read mutex.");
} }
} }
@ -723,7 +724,7 @@ Serial::SerialImpl::readLock()
void void
Serial::SerialImpl::readUnlock() Serial::SerialImpl::readUnlock()
{ {
if (!ReleaseMutex(read_mutex)) { if (!ReleaseMutex(read_mutex_)) {
THROW (IOException, "Error releasing read mutex."); THROW (IOException, "Error releasing read mutex.");
} }
} }
@ -731,7 +732,7 @@ Serial::SerialImpl::readUnlock()
void void
Serial::SerialImpl::writeLock() Serial::SerialImpl::writeLock()
{ {
if (WaitForSingleObject(write_mutex, INFINITE) != WAIT_OBJECT_0) { if (WaitForSingleObject(write_mutex_, INFINITE) != WAIT_OBJECT_0) {
THROW (IOException, "Error claiming write mutex."); THROW (IOException, "Error claiming write mutex.");
} }
} }
@ -739,7 +740,7 @@ Serial::SerialImpl::writeLock()
void void
Serial::SerialImpl::writeUnlock() Serial::SerialImpl::writeUnlock()
{ {
if (!ReleaseMutex(write_mutex)) { if (!ReleaseMutex(write_mutex_)) {
THROW (IOException, "Error releasing write mutex."); THROW (IOException, "Error releasing write mutex.");
} }
} }