1
1
//! Implementation of [`Serial`](https://docs.rs/embedded-hal/0.2.1/embedded_hal/serial/index.html)
2
2
3
- use hal:: serial:: { Read , Write } ;
3
+ use std:: io:: { Error as IoError , Read , Write } ;
4
+
4
5
use nb;
5
- use serial;
6
6
7
- /// Newtype around [`serial::SystemPort`] that implements the `embedded-hal` traits
8
- pub struct Serial ( pub serial:: SystemPort ) ;
7
+ use hal;
8
+ use serial_unix:: TTYPort ;
9
+
10
+ /// Newtype around [`serial_unix::TTYPort`] that implements
11
+ /// the `embedded-hal` traits.
12
+ pub struct Serial ( pub TTYPort ) ;
9
13
10
- impl Read < u8 > for Serial {
11
- type Error = serial :: Error ;
14
+ impl hal :: serial :: Read < u8 > for Serial {
15
+ type Error = IoError ;
12
16
13
17
fn read ( & mut self ) -> nb:: Result < u8 , Self :: Error > {
14
- use std:: io:: Read ;
15
18
let mut buffer = [ 0 ; 1 ] ;
16
19
let bytes_read = self
17
20
. 0
@@ -25,19 +28,17 @@ impl Read<u8> for Serial {
25
28
}
26
29
}
27
30
28
- impl Write < u8 > for Serial {
29
- type Error = serial :: Error ;
31
+ impl hal :: serial :: Write < u8 > for Serial {
32
+ type Error = IoError ;
30
33
31
34
fn write ( & mut self , word : u8 ) -> nb:: Result < ( ) , Self :: Error > {
32
- use std:: io:: Write ;
33
35
self . 0
34
36
. write ( & [ word] )
35
37
. map_err ( |err| nb:: Error :: Other ( Self :: Error :: from ( err) ) ) ?;
36
38
Ok ( ( ) )
37
39
}
38
40
39
41
fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
40
- use std:: io:: Write ;
41
42
self . 0
42
43
. flush ( )
43
44
. map_err ( |err| nb:: Error :: Other ( Self :: Error :: from ( err) ) )
@@ -58,7 +59,7 @@ mod test {
58
59
let ( mut master, _slave, name) =
59
60
openpty:: openpty ( None , None , None ) . expect ( "Creating pty failed" ) ;
60
61
println ! ( "{:?}" , name) ;
61
- let port = serial :: open ( Path :: new ( & name) ) . unwrap ( ) ;
62
+ let port = TTYPort :: open ( Path :: new ( & name) ) . unwrap ( ) ;
62
63
let mut serial = Serial ( port) ;
63
64
master. write ( & [ 1 ] ) . unwrap ( ) ;
64
65
serial. read ( ) . unwrap ( ) ;
0 commit comments