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

Set DTR by default when opening a serial port #239

Merged
merged 2 commits into from
Jan 13, 2025
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ project adheres to [Semantic Versioning](https://semver.org/).

* Enumerate ports from more subsystems on Linux without libudev.
[#238](https://github.com/serialport/serialport-rs/pull/238)
* Set data terminal ready (DTR) signal when opening a port by default and allow
to customize this behavior through the builder.
[#239](https://github.com/serialport/serialport-rs/pull/239)

### Fixed
### Removed
Expand Down
23 changes: 23 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,22 @@ 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
}

/// Preserve the state of data terminal ready (DTR) when opening the device. Your outcome may
/// vary depending on the operation system. For example, Linux sets DTR by default and Windows
/// doesn't.
#[must_use]
pub fn preserve_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 +855,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
Loading