Skip to content

Commit

Permalink
adds README and LICENSE for serial-unix
Browse files Browse the repository at this point in the history
  • Loading branch information
dcuddeback committed Jul 2, 2017
1 parent 7f9ec8f commit 1275bb4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions serial-unix/LICENSE
32 changes: 32 additions & 0 deletions serial-unix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Unix Serial Ports

The `serial-unix` crate provides a serial port implementation for Unix operating systems.

* [Documentation](http://dcuddeback.github.io/serial-rs/serial_unix/)

## Compatibility

The `serial-unix` crate is compatible with 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)

## Usage

In general, one shouldn't need to use the `serial-unix` library directly. The implementation
provided by `serial-unix` is also exposed through a cross-platform API in the [`serial`
crate](../serial/).

The serial port type defined in `serial-unix` works with any Unix TTY device. In addition to
implementing the standard serial port traits, it also implements `std::os::unix::io::AsRawFd`, which
can be used for integrating with other I/O libraries. See [`examples/poll.rs`](examples/poll.rs) for
an example of using `AsRawFd` for event-driven I/O.

## License

Copyright © 2015 David Cuddeback

Distributed under the [MIT License](LICENSE).
33 changes: 33 additions & 0 deletions serial-unix/examples/poll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
extern crate serial_unix;
extern crate libc;

use std::io;
use std::path::Path;

use std::io::prelude::*;
use std::os::unix::prelude::*;

fn main() {
let mut port = serial_unix::TTYPort::open(Path::new("/dev/ttyUSB0")).unwrap();

let mut fds = vec![libc::pollfd {
fd: port.as_raw_fd(),
events: libc::POLLIN,
revents: 0,
}];

loop {
let retval = unsafe { libc::poll(fds.as_mut_ptr(), fds.len() as libc::nfds_t, 100) };

if retval < 0 {
panic!("{:?}", io::Error::last_os_error());
}

if retval > 0 && fds[0].revents & libc::POLLIN != 0 {
let mut buffer = Vec::<u8>::new();
port.read_to_end(&mut buffer).unwrap();

println!("{:?}", buffer);
}
}
}

0 comments on commit 1275bb4

Please sign in to comment.