Skip to content
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 @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
- [breaking-change] Replace serial-rs with the serialport-rs crate. `Serial::open` now needs a baud-rate argument as well.

## [v0.4.0-alpha.3] - 2022-08-04

### Added
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ gpio-cdev = { version = "0.5.1", optional = true }
sysfs_gpio = { version = "0.6.1", optional = true }
i2cdev = { version = "0.5.1", optional = true }
nb = "1"
serial-core = "0.4.0"
serial-unix = "0.4.0"
serialport = { version = "4.2.0", default-features = false }
spidev = { version = "0.5.1", optional = true }
nix = "0.23.1"

Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
#[cfg(feature = "i2c")]
pub use i2cdev;
pub use nb;
pub use serial_core;
pub use serial_unix;
pub use serialport;
#[cfg(feature = "spi")]
pub use spidev;

Expand Down
29 changes: 19 additions & 10 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
//! [`embedded-hal`]: https://docs.rs/embedded-hal

use nb;
use serial_core;
use serial_unix::TTYPort;
use serialport::{SerialPortBuilder, TTYPort};
use std::io::{ErrorKind as IoErrorKind, Read, Write};
use std::path::Path;

/// Newtype around [`serial_unix::TTYPort`] that implements
/// Newtype around [`serialport::TTYPort`] that implements
/// the `embedded-hal` traits.
pub struct Serial(pub TTYPort);

impl Serial {
/// Wrapper for `serial_unix::TTYPort::open`
pub fn open(path: impl AsRef<Path>) -> Result<Serial, serial_core::Error> {
Ok(Serial(TTYPort::open(path.as_ref())?))
/// Open a `serialport::TTYPort` by providing the port path and baud rate
pub fn open(path: String, baud_rate: u32) -> Result<Serial, serialport::Error> {
Ok(Serial(serialport::new(path, baud_rate).open_native()?))
}

/// Open a `serialport::TTYPort` by providing `serialport::SerialPortBuilder`
pub fn open_from_builder(builder: SerialPortBuilder) -> Result<Serial, serialport::Error> {
Ok(Serial(builder.open_native()?))
}
}

Expand Down Expand Up @@ -81,8 +84,6 @@ impl embedded_hal::serial::Error for SerialError {

#[cfg(test)]
mod test {
use std::path::Path;

use embedded_hal_nb::serial::{Read, Write};
use std::io::{Read as IoRead, Write as IoWrite};

Expand All @@ -91,10 +92,18 @@ mod test {
fn create_pty_and_serial() -> (std::fs::File, Serial) {
let (master, _slave, name) =
openpty::openpty(None, None, None).expect("Creating pty failed");
let serial = Serial::open(Path::new(&name)).expect("Creating TTYPort failed");
let serial = Serial::open(name, 9600).expect("Creating TTYPort failed");
(master, serial)
}

#[test]
fn create_serial_from_builder() {
let (_master, _slave, name) =
openpty::openpty(None, None, None).expect("Creating pty failed");
let builder = serialport::new(name, 9600);
let _serial = Serial::open_from_builder(builder).expect("Creating TTYPort failed");
}

#[test]
fn test_empty_read() {
let (mut _master, mut serial) = create_pty_and_serial();
Expand Down