serial-0.4.0/Cargo.toml01006440000765000002400000002221131260451060013134 0ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "serial" version = "0.4.0" authors = ["David Cuddeback "] description = "Rust library for accessing serial ports." homepage = "https://github.com/dcuddeback/serial-rs" documentation = "https://dcuddeback.github.io/serial-rs/serial/" readme = "README.md" keywords = ["serial", "hardware", "system", "RS232"] categories = ["hardware-support", "os"] license = "MIT" repository = "https://github.com/dcuddeback/serial-rs" [dependencies.serial-core] version = "=0.4.0" [target."cfg(unix)".dependencies.serial-unix] version = "=0.4.0" [target."cfg(windows)".dependencies.serial-windows] version = "=0.4.0" serial-0.4.0/Cargo.toml.orig01006440000765000002400000001347131260451060014103 0ustar0000000000000000[package] name = "serial" version = "0.4.0" authors = ["David Cuddeback "] description = "Rust library for accessing serial ports." homepage = "https://github.com/dcuddeback/serial-rs" repository = "https://github.com/dcuddeback/serial-rs" documentation = "https://dcuddeback.github.io/serial-rs/serial/" license = "MIT" readme = "README.md" keywords = ["serial", "hardware", "system", "RS232"] categories = ["hardware-support", "os"] [dependencies] serial-core = { version = "=0.4.0", path = "../serial-core" } [target.'cfg(unix)'.dependencies] serial-unix = { version = "=0.4.0", path = "../serial-unix" } [target.'cfg(windows)'.dependencies] serial-windows = { version = "=0.4.0", path = "../serial-windows" } serial-0.4.0/examples/open_port.rs01006440000765000002400000000550131260451060015400 0ustar0000000000000000extern crate serial; #[cfg(unix)] fn main() { use std::path::Path; serial::open("/dev/ttyUSB0").unwrap(); serial::open(Path::new("/dev/ttyUSB0")).unwrap(); serial::unix::TTYPort::open(Path::new("/dev/ttyUSB0")).unwrap(); } #[cfg(windows)] fn main() { serial::open("COM1").unwrap(); serial::windows::COMPort::open("COM1").unwrap(); } serial-0.4.0/examples/probe_pins.rs01006440000765000002400000002566131260451060015544 0ustar0000000000000000extern crate serial; use std::env; use std::thread; use std::time::Duration; use serial::prelude::*; const SETTINGS: serial::PortSettings = serial::PortSettings { baud_rate: serial::Baud9600, char_size: serial::Bits8, parity: serial::ParityNone, stop_bits: serial::Stop1, flow_control: serial::FlowNone, }; fn main() { for arg in env::args_os().skip(1) { let mut port = serial::open(&arg).unwrap(); println!("opened device {:?}", arg); probe_pins(&mut port).unwrap(); } } fn probe_pins(port: &mut T) -> serial::Result<()> { try!(port.configure(&SETTINGS)); try!(port.set_timeout(Duration::from_millis(100))); try!(port.set_rts(false)); try!(port.set_dtr(false)); let mut rts = false; let mut dtr = false; let mut toggle = true; loop { thread::sleep(Duration::from_secs(1)); if toggle { rts = !rts; try!(port.set_rts(rts)); } else { dtr = !dtr; try!(port.set_dtr(dtr)); } println!("RTS={:5?} DTR={:5?} CTS={:5?} DSR={:5?} RI={:5?} CD={:?}", rts, dtr, try!(port.read_cts()), try!(port.read_dsr()), try!(port.read_ri()), try!(port.read_cd())); toggle = !toggle; } } serial-0.4.0/examples/read_write.rs01006440000765000002400000001602131260451060015517 0ustar0000000000000000extern crate serial; use std::env; use std::time::Duration; use std::io::prelude::*; use serial::prelude::*; const SETTINGS: serial::PortSettings = serial::PortSettings { baud_rate: serial::Baud9600, char_size: serial::Bits8, parity: serial::ParityNone, stop_bits: serial::Stop1, flow_control: serial::FlowNone, }; fn main() { for arg in env::args_os().skip(1) { println!("opening port: {:?}", arg); let mut port = serial::open(&arg).unwrap(); interact(&mut port).unwrap(); } } fn interact(port: &mut T) -> serial::Result<()> { try!(port.configure(&SETTINGS)); try!(port.set_timeout(Duration::from_secs(1))); let mut buf: Vec = (0..255).collect(); println!("writing bytes"); try!(port.write(&buf[..])); println!("reading bytes"); try!(port.read(&mut buf[..])); Ok(()) } serial-0.4.0/README.md01006440000765000002400000004617131260451060012476 0ustar0000000000000000# Serial The `serial` crate provides Rust programs with access to serial ports. Serial ports are defined as traits to support extension through custom implementations. Unix TTY devices and Windows COM ports are supported out of the box. * [Documentation](http://dcuddeback.github.io/serial-rs/serial/) ## Compatibility The `serial` crate is compatible with Windows and any Unix operating system that implements the termios API. The following platforms are confirmed to be compatible: * Linux (x86_64, armv6l) * OS X (x86_64) * FreeBSD (amd64) * OpenBSD (amd64) * Windows (x86_64) ## Usage Add `serial` as a dependency in `Cargo.toml`: ```toml [dependencies] serial = "0.4" ``` Import the `serial` crate and everything from the `serial::prelude` module. The traits in the `serial::prelude` module are are useful to have in scope when working with serial ports, and they are unlikely to conflict with other crates. To open a serial port, call `serial::open()` with any type that's convertable to `OsStr`. With an open serial port, you can interact with it using the `SerialPort` trait. By depending on the traits, your code will support future implementations of serial ports, including custom implementations. ```rust extern crate serial; use std::env; use std::io; use std::time::Duration; use std::io::prelude::*; use serial::prelude::*; fn main() { for arg in env::args_os().skip(1) { let mut port = serial::open(&arg).unwrap(); interact(&mut port).unwrap(); } } fn interact(port: &mut T) -> io::Result<()> { try!(port.reconfigure(&|settings| { try!(settings.set_baud_rate(serial::Baud9600)); settings.set_char_size(serial::Bits8); settings.set_parity(serial::ParityNone); settings.set_stop_bits(serial::Stop1); settings.set_flow_control(serial::FlowNone); Ok(()) })); try!(port.set_timeout(Duration::from_millis(1000))); let mut buf: Vec = (0..255).collect(); try!(port.write(&buf[..])); try!(port.read(&mut buf[..])); Ok(()) } ``` ### Cross-Compiling Cross-compiling the `serial` crate requires only that the `--target` option is provided to `cargo build`. The following is an example of cross-compiling for `arm-unknown-linux-gnueabihf` (Raspberry Pi): ``` cargo build --target=arm-unknown-linux-gnueabihf ``` ## License Copyright © 2015 David Cuddeback Distributed under the [MIT License](LICENSE). serial-0.4.0/src/lib.rs01006440000765000002400000006512131260451060013116 0ustar0000000000000000pub extern crate serial_core as core; #[cfg(unix)] pub extern crate serial_unix as unix; #[cfg(windows)] pub extern crate serial_windows as windows; use std::ffi::OsStr; #[doc(no_inline)] pub use core::prelude; #[doc(no_inline)] pub use core::{Result, Error, ErrorKind}; #[doc(no_inline)] pub use core::{PortSettings, BaudRate, CharSize, Parity, StopBits, FlowControl}; #[doc(no_inline)] pub use core::{SerialPort, SerialPortSettings}; pub use core::BaudRate::*; pub use core::CharSize::*; pub use core::Parity::*; pub use core::StopBits::*; pub use core::FlowControl::*; /// A convenience type alias for the system's native serial port type. #[cfg(unix)] pub type SystemPort = unix::TTYPort; /// A convenience type alias for the system's native serial port type. #[cfg(windows)] pub type SystemPort = windows::COMPort; /// A convenience function for opening a native serial port. /// /// The argument must be one that's understood by the target operating system to identify a serial /// port. On Unix systems, it should be a path to a TTY device file. On Windows, it should be the /// name of a COM port. /// /// ## Errors /// /// This function returns an error if the device could not be opened and initialized: /// /// * `NoDevice` if the device could not be opened. This could indicate that the device is /// already in use. /// * `InvalidInput` if `port` is not a valid device name. /// * `Io` for any other error while opening or initializing the device. /// /// ## Examples /// /// Provide a system-specific string that identifies a serial port: /// /// ```no_run /// let port = serial::open("/dev/ttyUSB0").unwrap(); /// ``` /// /// Hard-coding the device name dimishes the cross-platform utility of `serial::open()`. To /// preserve cross-platform functionality, device names should come from external sources: /// /// ```no_run /// use std::env; /// /// for arg in env::args_os().skip(1) { /// let port = serial::open(&arg).unwrap(); /// } /// ``` #[cfg(unix)] pub fn open + ?Sized>(port: &T) -> ::core::Result { use std::path::Path; unix::TTYPort::open(Path::new(port)) } /// A convenience function for opening a native serial port. /// /// The argument must be one that's understood by the target operating system to identify a serial /// port. On Unix systems, it should be a path to a TTY device file. On Windows, it should be the /// name of a COM port. /// /// ## Errors /// /// This function returns an error if the device could not be opened and initialized: /// /// * `NoDevice` if the device could not be opened. This could indicate that the device is /// already in use. /// * `InvalidInput` if `port` is not a valid device name. /// * `Io` for any other error while opening or initializing the device. /// /// ## Examples /// /// Provide a system-specific string that identifies a serial port: /// /// ```no_run /// let port = serial::open("COM1").unwrap(); /// ``` /// /// Hard-coding the device name dimishes the cross-platform utility of `serial::open()`. To /// preserve cross-platform functionality, device names should come from external sources: /// /// ```no_run /// use std::env; /// /// for arg in env::args_os().skip(1) { /// let port = serial::open(&arg).unwrap(); /// } /// ``` #[cfg(windows)] pub fn open + ?Sized>(port: &T) -> ::core::Result { windows::COMPort::open(port) }