33//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
55use nb;
6- use serial_core;
7- use serial_unix:: TTYPort ;
6+ use serialport:: { SerialPortBuilder , TTYPort } ;
87use 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.
1311pub struct Serial ( pub TTYPort ) ;
1412
1513impl 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) ]
8386mod 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