From 31a5cf13418c22ce62b3cd868a12dfda22da6579 Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Tue, 14 Mar 2023 21:38:33 +0100 Subject: [PATCH 1/2] serial: Replace the serial-rs crate with serialport-rs --- CHANGELOG.md | 2 ++ Cargo.toml | 3 +-- src/lib.rs | 3 +-- src/serial.rs | 29 +++++++++++++++++++---------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4aacb4..23203a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +- [breaking-change] Replace serial-rs with the serialport-rs crate. + ## [v0.4.0-alpha.3] - 2022-08-04 ### Added diff --git a/Cargo.toml b/Cargo.toml index 4c9f57c..6dd1dac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 4de0d92..cb68e5a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/serial.rs b/src/serial.rs index 55fcf75..75cc946 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -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) -> Result { - 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 { + 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 { + Ok(Serial(builder.open_native()?)) } } @@ -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}; @@ -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(); From d8a411cc99e81fa9d3f5a725815bbfcebb800296 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Mon, 20 Mar 2023 10:05:14 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23203a7..3370f9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] -- [breaking-change] Replace serial-rs with the serialport-rs crate. +### 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