Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dtr control to Serial constructor #266

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/serial/impl/unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();

Expand Down Expand Up @@ -117,7 +118,7 @@ class serial::Serial::SerialImpl {
setRTS (bool level);

void
setDTR (bool level);
setDTR (dtrcontrol_t dtrcontrol);

bool
waitForChange ();
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions include/serial/impl/win.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();

Expand Down Expand Up @@ -106,7 +107,7 @@ class serial::Serial::SerialImpl {
setRTS (bool level);

void
setDTR (bool level);
setDTR (dtrcontrol_t dtrcontrol);

bool
waitForChange ();
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions include/serial/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ();
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 14 additions & 5 deletions src/impl/unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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_)
Expand Down Expand Up @@ -893,22 +902,22 @@ 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");
}

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;
Expand Down
20 changes: 15 additions & 5 deletions src/impl/win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down Expand Up @@ -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_);
Expand Down Expand Up @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/serial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()
Expand Down