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..6482315b 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,15 @@ 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) { + dtrcontrol_ = dtrcontrol; + 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()