Skip to content
Draft
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
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ pub struct SerialStream {
pipe: NamedPipe,
}

#[cfg(unix)]
fn map_nix_error(e: nix::Error) -> crate::Error {
crate::Error {
kind: crate::ErrorKind::Io(StdIoErrorKind::Other),
description: e.to_string(),
}
}

impl SerialStream {
/// Open a nonblocking serial port from the provided builder
///
Expand Down Expand Up @@ -525,11 +533,21 @@ impl TryFrom<NativeBlockingSerialPort> for SerialStream {
type Error = crate::Error;
#[cfg(unix)]
fn try_from(port: NativeBlockingSerialPort) -> std::result::Result<Self, Self::Error> {
// Set the O_NONBLOCK flag.
log::debug!(
"setting O_NONBLOCK for {}",
"switching {} to asynchronous mode",
port.name().unwrap_or_else(|| String::from("<UNKNOWN>"))
);
log::debug!("setting VMIN = 1");
use nix::sys::termios::{self, SetArg, SpecialCharacterIndices};
let port_fd = unsafe { BorrowedFd::borrow_raw(port.as_raw_fd()) };
let mut t = termios::tcgetattr(port_fd).map_err(map_nix_error)?;

// Set VMIN = 1 to block until at least one character is received.
t.control_chars[SpecialCharacterIndices::VMIN as usize] = 1;
termios::tcsetattr(port_fd, SetArg::TCSANOW, &t).map_err(map_nix_error)?;

// Set the O_NONBLOCK flag.
log::debug!("setting O_NONBLOCK flag");
let flags = unsafe { libc::fcntl(port.as_raw_fd(), libc::F_GETFL) };
if flags < 0 {
return Err(StdIoError::last_os_error().into());
Expand Down