3
3
//! [`embedded-hal`]: https://docs.rs/embedded-hal
4
4
5
5
use nb;
6
- use serial_core;
7
- use serial_unix:: TTYPort ;
6
+ use serialport:: { SerialPortBuilder , TTYPort } ;
8
7
use std:: io:: { ErrorKind as IoErrorKind , Read , Write } ;
9
- use std:: path:: Path ;
10
8
11
- /// Newtype around [`serial_unix ::TTYPort`] that implements
9
+ /// Newtype around [`serialport ::TTYPort`] that implements
12
10
/// the `embedded-hal` traits.
13
11
pub struct Serial ( pub TTYPort ) ;
14
12
15
13
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 ( ) ?) )
19
22
}
20
23
}
21
24
@@ -81,8 +84,6 @@ impl embedded_hal::serial::Error for SerialError {
81
84
82
85
#[ cfg( test) ]
83
86
mod test {
84
- use std:: path:: Path ;
85
-
86
87
use embedded_hal_nb:: serial:: { Read , Write } ;
87
88
use std:: io:: { Read as IoRead , Write as IoWrite } ;
88
89
@@ -91,10 +92,18 @@ mod test {
91
92
fn create_pty_and_serial ( ) -> ( std:: fs:: File , Serial ) {
92
93
let ( master, _slave, name) =
93
94
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" ) ;
95
96
( master, serial)
96
97
}
97
98
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
+
98
107
#[ test]
99
108
fn test_empty_read ( ) {
100
109
let ( mut _master, mut serial) = create_pty_and_serial ( ) ;
0 commit comments