From dc272929006f9a84269490260e0341bf16d282a1 Mon Sep 17 00:00:00 2001 From: Maxine Alexander Date: Thu, 19 May 2022 16:20:58 -0400 Subject: [PATCH 1/2] add dtr control to serial::Serial constructor typedef enum `dtrcontrol_t` for specification in the constructor of `serial::Serial` and `serial::Serial::SerialImpl`. add `dtrcontrol_t` parameter to `serial::Serial` and `serial::Serial::SerialImpl` constructors for both windows and unix. `serial::Serial` constructor parameter defaults to `dtr_disable`. set dtr control in `serial::Serial::SerialImpl::reconfigurePort` with `ioctl`. change parameter for `setDTR` to `dtrcontrol_t` in both `serial::Serial` and `serial::Serial::SerialImpl` for both windows and unix. Changed default in `serial::Serial::setDTR` to `dtr_enable`. --- include/serial/impl/unix.h | 6 ++++-- include/serial/impl/win.h | 6 ++++-- include/serial/serial.h | 11 +++++++++-- src/impl/unix.cc | 19 ++++++++++++++----- src/impl/win.cc | 19 ++++++++++++++----- src/serial.cc | 8 ++++---- 6 files changed, 49 insertions(+), 20 deletions(-) diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h index 0fb38f24..14ccaeef 100644 --- a/include/serial/impl/unix.h +++ b/include/serial/impl/unix.h @@ -70,7 +70,8 @@ class serial::Serial::SerialImpl { bytesize_t bytesize, parity_t parity, stopbits_t stopbits, - flowcontrol_t flowcontrol); + flowcontrol_t flowcontrol, + dtrcontrol_t dtrcontrol); virtual ~SerialImpl (); @@ -117,7 +118,7 @@ class serial::Serial::SerialImpl { setRTS (bool level); void - setDTR (bool level); + setDTR (dtrcontrol_t dtrcontrol); bool waitForChange (); @@ -207,6 +208,7 @@ class serial::Serial::SerialImpl { bytesize_t bytesize_; // Size of the bytes stopbits_t stopbits_; // Stop Bits flowcontrol_t flowcontrol_; // Flow Control + dtrcontrol_t dtrcontrol_; // Data Terminal Ready Control // Mutex used to lock the read functions pthread_mutex_t read_mutex; diff --git a/include/serial/impl/win.h b/include/serial/impl/win.h index 2c0c6cde..29fd3afc 100644 --- a/include/serial/impl/win.h +++ b/include/serial/impl/win.h @@ -59,7 +59,8 @@ class serial::Serial::SerialImpl { bytesize_t bytesize, parity_t parity, stopbits_t stopbits, - flowcontrol_t flowcontrol); + flowcontrol_t flowcontrol, + dtrcontrol_t dtrcontrol); virtual ~SerialImpl (); @@ -106,7 +107,7 @@ class serial::Serial::SerialImpl { setRTS (bool level); void - setDTR (bool level); + setDTR (dtrcontrol_t dtrcontrol); bool waitForChange (); @@ -193,6 +194,7 @@ class serial::Serial::SerialImpl { bytesize_t bytesize_; // Size of the bytes stopbits_t stopbits_; // Stop Bits flowcontrol_t flowcontrol_; // Flow Control + dtrcontrol_t dtrcontrol_; // Data Terminal Ready Control // Mutex used to lock the read functions HANDLE read_mutex; diff --git a/include/serial/serial.h b/include/serial/serial.h index a1657852..7ba4dab4 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -89,6 +89,12 @@ typedef enum { flowcontrol_hardware } flowcontrol_t; +typedef enum { + dtr_disable = 0, + dtr_enable = 1, + dtr_handshake = 2 +} dtrcontrol_t; + /*! * Structure for setting the timeout of the serial port, times are * in milliseconds. @@ -183,7 +189,8 @@ class Serial { bytesize_t bytesize = eightbits, parity_t parity = parity_none, stopbits_t stopbits = stopbits_one, - flowcontrol_t flowcontrol = flowcontrol_none); + flowcontrol_t flowcontrol = flowcontrol_none, + dtrcontrol_t dtr = dtr_disable); /*! Destructor */ virtual ~Serial (); @@ -612,7 +619,7 @@ class Serial { /*! Set the DTR handshaking line to the given level. Defaults to true. */ void - setDTR (bool level = true); + setDTR (dtrcontrol_t dtrcontrol = dtr_enable); /*! * Blocks until CTS, DSR, RI, CD changes or something interrupts it. diff --git a/src/impl/unix.cc b/src/impl/unix.cc index a40b0fa0..78dfb171 100755 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -108,10 +108,12 @@ timespec_from_ms (const uint32_t millis) Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, - flowcontrol_t flowcontrol) + flowcontrol_t flowcontrol, + dtrcontrol_t dtrcontrol) : port_ (port), fd_ (-1), is_open_ (false), xonxoff_ (false), rtscts_ (false), baudrate_ (baudrate), parity_ (parity), - bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol) + bytesize_ (bytesize), stopbits_ (stopbits), + flowcontrol_ (flowcontrol), dtrcontrol_ (dtrcontrol) { pthread_mutex_init(&this->read_mutex, NULL); pthread_mutex_init(&this->write_mutex, NULL); @@ -376,6 +378,13 @@ Serial::SerialImpl::reconfigurePort () xonxoff_ = false; rtscts_ = true; } + // setup dtr control + int dtr_flag = TIOCM_DTR; + if (dtrcontrol_ == dtr_enable) { + ioctl(fd_, TIOCMBIS, &dtr_flag); + } else if (dtrcontrol_ == dtr_disable) { + ioctl(fd_, TIOCMBIC, &dtr_flag); + } // xonxoff #ifdef IXANY if (xonxoff_) @@ -893,7 +902,7 @@ Serial::SerialImpl::setRTS (bool level) } void -Serial::SerialImpl::setDTR (bool level) +Serial::SerialImpl::setDTR (dtrcontrol_t dtrcontrol) { if (is_open_ == false) { throw PortNotOpenedException ("Serial::setDTR"); @@ -901,14 +910,14 @@ Serial::SerialImpl::setDTR (bool level) int command = TIOCM_DTR; - if (level) { + if (dtrcontrol == dtr_enable) { if (-1 == ioctl (fd_, TIOCMBIS, &command)) { stringstream ss; ss << "setDTR failed on a call to ioctl(TIOCMBIS): " << errno << " " << strerror(errno); throw(SerialException(ss.str().c_str())); } - } else { + } else if (dtrcontrol == dtr_disable) { if (-1 == ioctl (fd_, TIOCMBIC, &command)) { stringstream ss; diff --git a/src/impl/win.cc b/src/impl/win.cc index 889e06f1..e4ae142d 100644 --- a/src/impl/win.cc +++ b/src/impl/win.cc @@ -34,10 +34,12 @@ _prefix_port_if_needed(const wstring &input) Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, - flowcontrol_t flowcontrol) + flowcontrol_t flowcontrol, + dtrcontrol_t dtrcontrol) : port_ (port.begin(), port.end()), fd_ (INVALID_HANDLE_VALUE), is_open_ (false), baudrate_ (baudrate), parity_ (parity), - bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol) + bytesize_ (bytesize), stopbits_ (stopbits), + flowcontrol_ (flowcontrol), dtrcontrol_ (dtrcontrol) { if (port_.empty () == false) open (); @@ -256,6 +258,13 @@ Serial::SerialImpl::reconfigurePort () dcbSerialParams.fInX = false; } + // setup dtr control + if (dtrcontrol_ == dtr_enable) { + dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE; + } else if (dtrcontrol_ == dtr_disable) { + dcbSerialParams.fDtrControl = DTR_CONTROL_DISABLE; + } + // activate settings if (!SetCommState(fd_, &dcbSerialParams)){ CloseHandle(fd_); @@ -519,14 +528,14 @@ Serial::SerialImpl::setRTS (bool level) } void -Serial::SerialImpl::setDTR (bool level) +Serial::SerialImpl::setDTR (dtrcontrol_t dtrcontrol) { if (is_open_ == false) { throw PortNotOpenedException ("Serial::setDTR"); } - if (level) { + if (dtrcontrol == dtr_enable) { EscapeCommFunction (fd_, SETDTR); - } else { + } else if (dtrcontrol == dtr_disable) { EscapeCommFunction (fd_, CLRDTR); } } diff --git a/src/serial.cc b/src/serial.cc index a9e6f84b..9415ba38 100755 --- a/src/serial.cc +++ b/src/serial.cc @@ -65,9 +65,9 @@ class Serial::ScopedWriteLock { Serial::Serial (const string &port, uint32_t baudrate, serial::Timeout timeout, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, - flowcontrol_t flowcontrol) + flowcontrol_t flowcontrol, dtrcontrol_t dtrcontrol) : pimpl_(new SerialImpl (port, baudrate, bytesize, parity, - stopbits, flowcontrol)) + stopbits, flowcontrol, dtrcontrol)) { pimpl_->setTimeout(timeout); } @@ -401,9 +401,9 @@ void Serial::setRTS (bool level) pimpl_->setRTS (level); } -void Serial::setDTR (bool level) +void Serial::setDTR (dtrcontrol_t dtrcontrol) { - pimpl_->setDTR (level); + pimpl_->setDTR (dtrcontrol); } bool Serial::waitForChange() From f08466accaa2f8fd49f1fdacfe15cc02e2e32fd3 Mon Sep 17 00:00:00 2001 From: Maxine Alexander Date: Thu, 19 May 2022 16:30:53 -0400 Subject: [PATCH 2/2] Update win.cc fix bug where parameter `dtrcontrol` isn't stored to `dtrcontrol_`. --- src/impl/win.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/impl/win.cc b/src/impl/win.cc index e4ae142d..6482315b 100644 --- a/src/impl/win.cc +++ b/src/impl/win.cc @@ -533,6 +533,7 @@ Serial::SerialImpl::setDTR (dtrcontrol_t dtrcontrol) if (is_open_ == false) { throw PortNotOpenedException ("Serial::setDTR"); } + dtrcontrol_ = dtrcontrol; if (dtrcontrol == dtr_enable) { EscapeCommFunction (fd_, SETDTR); } else if (dtrcontrol == dtr_disable) {