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

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`.
This commit is contained in:
Maxine Alexander 2022-05-19 16:20:58 -04:00
parent 69e0372cf0
commit dc27292900
6 changed files with 49 additions and 20 deletions

View File

@ -70,7 +70,8 @@ public:
bytesize_t bytesize,
parity_t parity,
stopbits_t stopbits,
flowcontrol_t flowcontrol);
flowcontrol_t flowcontrol,
dtrcontrol_t dtrcontrol);
virtual ~SerialImpl ();
@ -117,7 +118,7 @@ public:
setRTS (bool level);
void
setDTR (bool level);
setDTR (dtrcontrol_t dtrcontrol);
bool
waitForChange ();
@ -207,6 +208,7 @@ private:
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;

View File

@ -59,7 +59,8 @@ public:
bytesize_t bytesize,
parity_t parity,
stopbits_t stopbits,
flowcontrol_t flowcontrol);
flowcontrol_t flowcontrol,
dtrcontrol_t dtrcontrol);
virtual ~SerialImpl ();
@ -106,7 +107,7 @@ public:
setRTS (bool level);
void
setDTR (bool level);
setDTR (dtrcontrol_t dtrcontrol);
bool
waitForChange ();
@ -193,6 +194,7 @@ private:
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;

View File

@ -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 @@ public:
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 @@ public:
/*! 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.

View File

@ -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;

View File

@ -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);
}
}

View File

@ -65,9 +65,9 @@ private:
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()