Skip to content

Commit

Permalink
Merge pull request #1 from ndusart/serialport-2
Browse files Browse the repository at this point in the history
match new API of serialport-rs 2.0.0
  • Loading branch information
berkowski committed Jul 30, 2017
2 parents af438f8 + dcd3047 commit 73fe754
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Expand Up @@ -13,11 +13,10 @@ categories = ["asynchronous", "hardware-support"]

[dependencies]
mio = "0.6"
serialport = "1.0"
serialport = { git = "https://gitlab.com/susurrus/serialport-rs" }

[target.'cfg(unix)'.dependencies]
libc = "0.2"
termios = "0.2"
nix = { git = "https://github.com/nix-rust/nix" }

[[example]]
name = "serial_printline"
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Expand Up @@ -14,9 +14,7 @@ extern crate serialport;
extern crate mio;

#[cfg(unix)]
extern crate libc;
#[cfg(unix)]
extern crate termios;
extern crate nix;

// Enums, Structs, and Traits from the serialport crate
pub use serialport::{// Traits
Expand All @@ -33,11 +31,8 @@ pub use serialport::{// Traits
BaudRate,
FlowControl};

// The serialport Result type, used in SerialPort trait.
pub use serialport::Result as SerialResult;

// Some enumeration functions from the serialport crate
pub use serialport::{available_baud_rates, available_ports};
pub use serialport::available_ports;

#[cfg(unix)]
pub mod unix;
Expand All @@ -47,3 +42,8 @@ pub mod windows;

#[cfg(unix)]
pub use unix::Serial;


/// A type for results generated by interacting with serial ports.
pub type Result<T> = serialport::Result<T>;
pub use serialport::{Error, ErrorKind};
36 changes: 22 additions & 14 deletions src/unix.rs
Expand Up @@ -5,15 +5,17 @@ use std::path::Path;
use std::convert::AsRef;
use std::time::Duration;

use libc;
use mio::{Evented, PollOpt, Token, Poll, Ready};
use mio::unix::EventedFd;

use serialport;
use serialport::posix::TTYPort;
use serialport::prelude::*;

use termios;
use nix;
use nix::libc;
use nix::sys::termios;
use nix::sys::termios::{SetArg, SpecialCharacterIndices};


/// *nix serial port using termios
Expand All @@ -35,7 +37,7 @@ impl Serial {
///
/// let serial = Serial::from_path(tty_name, &SerialPortSettings::default()).unwrap();
/// ```
pub fn from_path<T: AsRef<Path>>(path: T, settings: &SerialPortSettings) -> io::Result<Self> {
pub fn from_path<T: AsRef<Path>>(path: T, settings: &SerialPortSettings) -> ::Result<Self> {
let port = TTYPort::open(path.as_ref(), settings)?;
Serial::from_serial(port)
}
Expand All @@ -58,24 +60,24 @@ impl Serial {
/// let serial = Serial::from_serial(blocking_serial).unwrap();
/// # fn main() {}
/// ```
pub fn from_serial(port: TTYPort) -> io::Result<Self> {
pub fn from_serial(port: TTYPort) -> ::Result<Self> {

// Get the termios structure
let mut t = termios::Termios::from_fd(port.as_raw_fd())?;
let mut t = termios::tcgetattr(port.as_raw_fd())?;

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

// Set the O_NONBLOCK flag.
let flags = unsafe { libc::fcntl(port.as_raw_fd(), libc::F_GETFL) };
if flags < 0 {
return Err(io::Error::last_os_error());
return Err(io::Error::last_os_error().into());
}

match unsafe { libc::fcntl(port.as_raw_fd(), libc::F_SETFL, flags | libc::O_NONBLOCK) } {
0 => Ok(Serial { inner: port }),
_ => Err(io::Error::last_os_error()),
_ => Err(io::Error::last_os_error().into()),
}

}
Expand All @@ -96,7 +98,7 @@ impl Serial {
///
/// let (master, slave) = Serial::pair().unwrap();
/// ```
pub fn pair() -> ::SerialResult<(Self, Self)> {
pub fn pair() -> ::Result<(Self, Self)> {
let (master, slave) = TTYPort::pair()?;

let master = Self::from_serial(master)?;
Expand All @@ -115,8 +117,8 @@ impl Serial {
/// ## Errors
///
/// * `Io` for any error while setting exclusivity for the port.
pub fn set_exclusive(&mut self, exclusive: bool) -> ::SerialResult<()> {
self.inner.set_exclusive(exclusive)
pub fn set_exclusive(&mut self, exclusive: bool) -> ::Result<()> {
self.inner.set_exclusive(exclusive).map_err(|e| e.into())
}

/// Returns the exclusivity of the port
Expand Down Expand Up @@ -359,7 +361,10 @@ impl Write for Serial {
}

fn flush(&mut self) -> io::Result<()> {
termios::tcdrain(self.inner.as_raw_fd())
termios::tcdrain(self.inner.as_raw_fd()).map_err(|e| {
let e : ::Error = e.into();
e.into()
})
//self.inner.flush()
}
}
Expand Down Expand Up @@ -390,7 +395,10 @@ impl<'a> Write for &'a Serial {
}

fn flush(&mut self) -> io::Result<()> {
termios::tcdrain(self.inner.as_raw_fd())
termios::tcdrain(self.inner.as_raw_fd()).map_err(|e| {
let e : ::Error = e.into();
e.into()
})
}
}

Expand Down

0 comments on commit 73fe754

Please sign in to comment.