Skip to content

Commit

Permalink
Set DTR when opening a serial port by default
Browse files Browse the repository at this point in the history
  • Loading branch information
sirhcel committed Jan 4, 2025
1 parent 22d69ba commit aacadc4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ pub struct SerialPortBuilder {
stop_bits: StopBits,
/// Amount of time to wait to receive data before timing out
timeout: Duration,
/// The state to set DTR to when opening the device
dtr_on_open: Option<bool>,
}

impl SerialPortBuilder {
Expand Down Expand Up @@ -393,6 +395,20 @@ impl SerialPortBuilder {
self
}

/// Set data terminal ready (DTR) to the given state when opening the device
#[must_use]
pub fn dtr_on_open(mut self, state: bool) -> Self {
self.dtr_on_open = Some(state);
self
}

/// Don't touch data terminal ready (DTR) when opening the device
#[must_use]
pub fn ignore_dtr_on_open(mut self) -> Self {
self.dtr_on_open = None;
self
}

/// Open a cross-platform interface to the port with the specified settings
pub fn open(self) -> Result<Box<dyn SerialPort>> {
#[cfg(unix)]
Expand Down Expand Up @@ -837,6 +853,11 @@ pub fn new<'a>(path: impl Into<std::borrow::Cow<'a, str>>, baud_rate: u32) -> Se
parity: Parity::None,
stop_bits: StopBits::One,
timeout: Duration::from_millis(0),
// By default, set DTR when opening the device. There are USB devices performing "wait for
// DTR" before sending any data and users stumbled over this multiple times (see issues #29
// and #204). We are expecting little to no negative consequences from setting DTR by
// default but less hassle for users.
dtr_on_open: Some(true),
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/posix/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,23 @@ impl TTYPort {
termios::set_termios(fd.0, &termios)?;

// Return the final port object
Ok(TTYPort {
let mut port = TTYPort {
fd: fd.into_raw(),
timeout: builder.timeout,
exclusive: true,
port_name: Some(builder.path.clone()),
#[cfg(any(target_os = "ios", target_os = "macos"))]
baud_rate: builder.baud_rate,
})
};

// Ignore setting DTR for pseudo terminals (indicated by baud_rate == 0).
if builder.baud_rate > 0 {
if let Some(dtr) = builder.dtr_on_open {
port.write_data_terminal_ready(dtr)?;
}
}

Ok(port)
}

/// Returns the exclusivity of the port
Expand Down
4 changes: 4 additions & 0 deletions src/windows/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ impl COMPort {
dcb::set_flow_control(&mut dcb, builder.flow_control);
dcb::set_dcb(handle, dcb)?;

if let Some(dtr) = builder.dtr_on_open {
com.write_data_terminal_ready(dtr)?;
}

com.set_timeout(builder.timeout)?;
com.port_name = Some(builder.path.clone());
Ok(com)
Expand Down

0 comments on commit aacadc4

Please sign in to comment.