Skip to content

Commit 214c3fa

Browse files
authored
chore: Update async example after embedded io fix/rework. (#89)
1 parent 660aa4c commit 214c3fa

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

examples/local_async.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use embedded_io_adapters::tokio_1::FromTokio;
22
use std::env;
3+
use std::io::ErrorKind;
34
use std::process::exit;
45
use std::time::Duration;
56

@@ -8,21 +9,36 @@ use uf_crsf::async_io::AsyncCrsfReader;
89

910
#[tokio::main]
1011
async fn main() {
11-
let path = env::args().nth(1).unwrap_or_else(|| {
12-
println!("No serial port specified. Usage: local_async <PATH>");
12+
let ports = match serialport::available_ports() {
13+
Ok(ports) => ports,
14+
Err(e) => {
15+
eprintln!("Failed to enumerate serial ports: {}", e);
16+
exit(1);
17+
}
18+
};
1319

14-
let ports = match serialport::available_ports() {
15-
Ok(ports) => ports,
16-
Err(e) => {
17-
eprintln!("Failed to enumerate serial ports: {}", e);
18-
exit(1);
20+
if ports.is_empty() {
21+
eprintln!("No serial ports found.");
22+
eprintln!("Please specify a serial port path as an argument.");
23+
exit(1);
24+
}
25+
26+
let path = env::args().nth(1).unwrap_or_else(|| {
27+
const DEFAULT_PORT: &str = "/dev/tty.usbmodem00000000001B1";
28+
if ports.iter().any(|p| p.port_name == DEFAULT_PORT) {
29+
println!(
30+
"No serial port specified. Using default port: {}",
31+
DEFAULT_PORT
32+
);
33+
DEFAULT_PORT.to_string()
34+
} else {
35+
println!("No serial port specified. Available ports:");
36+
for p in &ports {
37+
println!(" {}", p.port_name);
1938
}
20-
};
21-
println!("Available ports:");
22-
for p in &ports {
23-
println!(" {}", p.port_name);
39+
println!("\nUsing first available port: {}", ports[0].port_name);
40+
ports[0].port_name.clone()
2441
}
25-
exit(1);
2642
});
2743

2844
let mut port = tokio_serial::new(&path, 420_000)
@@ -44,7 +60,6 @@ async fn main() {
4460
}
4561
Err(e) => {
4662
eprintln!("Error reading from serial port: {:?}", e);
47-
break;
4863
}
4964
}
5065
}

justfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ example_raw:
5454
example_simple:
5555
cargo run --example=simple
5656

57+
# Run example local_simple that parses hard coded buffer.
58+
[group('examples')]
59+
example_async:
60+
cargo run --example=async --all-features
61+
5762
# Lint source code with strict linter
5863
[group('lint')]
5964
pedantic:

0 commit comments

Comments
 (0)