Skip to content

Commit 1f97fd4

Browse files
bors[bot]qrasmonteldruin
authored
Merge #89
89: serial: Replace the serial-rs crate with serialport-rs r=eldruin a=qrasmont Replace the unmaintained serial-rs crate with serialport-rs. Closes #86 Co-authored-by: Quentin Rasmont <qrasmont@gmail.com> Co-authored-by: Diego Barrios Romero <eldruin@gmail.com>
2 parents 0a65cd1 + d8a411c commit 1f97fd4

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Changed
11+
- [breaking-change] Replace serial-rs with the serialport-rs crate. `Serial::open` now needs a baud-rate argument as well.
12+
1013
## [v0.4.0-alpha.3] - 2022-08-04
1114

1215
### Added

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ gpio-cdev = { version = "0.5.1", optional = true }
2828
sysfs_gpio = { version = "0.6.1", optional = true }
2929
i2cdev = { version = "0.5.1", optional = true }
3030
nb = "1"
31-
serial-core = "0.4.0"
32-
serial-unix = "0.4.0"
31+
serialport = { version = "4.2.0", default-features = false }
3332
spidev = { version = "0.5.1", optional = true }
3433
nix = "0.23.1"
3534

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#[cfg(feature = "i2c")]
1616
pub use i2cdev;
1717
pub use nb;
18-
pub use serial_core;
19-
pub use serial_unix;
18+
pub use serialport;
2019
#[cfg(feature = "spi")]
2120
pub use spidev;
2221

src/serial.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
55
use nb;
6-
use serial_core;
7-
use serial_unix::TTYPort;
6+
use serialport::{SerialPortBuilder, TTYPort};
87
use std::io::{ErrorKind as IoErrorKind, Read, Write};
9-
use std::path::Path;
108

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

1513
impl Serial {
16-
/// Wrapper for `serial_unix::TTYPort::open`
17-
pub fn open(path: impl AsRef<Path>) -> Result<Serial, serial_core::Error> {
18-
Ok(Serial(TTYPort::open(path.as_ref())?))
14+
/// Open a `serialport::TTYPort` by providing the port path and baud rate
15+
pub fn open(path: String, baud_rate: u32) -> Result<Serial, serialport::Error> {
16+
Ok(Serial(serialport::new(path, baud_rate).open_native()?))
17+
}
18+
19+
/// Open a `serialport::TTYPort` by providing `serialport::SerialPortBuilder`
20+
pub fn open_from_builder(builder: SerialPortBuilder) -> Result<Serial, serialport::Error> {
21+
Ok(Serial(builder.open_native()?))
1922
}
2023
}
2124

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

8285
#[cfg(test)]
8386
mod test {
84-
use std::path::Path;
85-
8687
use embedded_hal_nb::serial::{Read, Write};
8788
use std::io::{Read as IoRead, Write as IoWrite};
8889

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

99+
#[test]
100+
fn create_serial_from_builder() {
101+
let (_master, _slave, name) =
102+
openpty::openpty(None, None, None).expect("Creating pty failed");
103+
let builder = serialport::new(name, 9600);
104+
let _serial = Serial::open_from_builder(builder).expect("Creating TTYPort failed");
105+
}
106+
98107
#[test]
99108
fn test_empty_read() {
100109
let (mut _master, mut serial) = create_pty_and_serial();

0 commit comments

Comments
 (0)