Read asynchronous from serial
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Use tokio to read asynchronous from serial port.
This commit is contained in:
finga 2022-04-23 20:34:36 +02:00
parent 1d5f5fd0f3
commit e820273c89
3 changed files with 98 additions and 3 deletions

74
Cargo.lock generated
View file

@ -177,6 +177,7 @@ dependencies = [
"env_logger",
"log",
"serialport",
"tokio",
]
[[package]]
@ -192,12 +193,46 @@ dependencies = [
"memoffset",
]
[[package]]
name = "num_cpus"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "pin-project-lite"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c"
[[package]]
name = "pkg-config"
version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]]
name = "proc-macro2"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.5.5"
@ -232,6 +267,17 @@ dependencies = [
"winapi",
]
[[package]]
name = "syn"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "termcolor"
version = "1.1.3"
@ -241,6 +287,34 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "tokio"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2af73ac49756f3f7c01172e34a23e5d0216f6c32333757c2c61feb2bbff5a5ee"
dependencies = [
"num_cpus",
"pin-project-lite",
"tokio-macros",
]
[[package]]
name = "tokio-macros"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "winapi"
version = "0.3.9"

View file

@ -13,3 +13,4 @@ anyhow = "1.0"
serialport = "4.1"
log = "0.4"
env_logger = "0.9"
tokio = { version = "1.17", features = ["rt-multi-thread", "macros"] }

View file

@ -5,6 +5,7 @@ use std::{
str::FromStr,
time::Duration,
};
use tokio::{spawn, task::JoinHandle, try_join};
#[derive(Debug)]
enum Speed {
@ -50,9 +51,8 @@ fn parse(input: &str) -> Result<(usize, usize, f32, Speed)> {
))
}
fn main() -> Result<()> {
env_logger::init();
#[allow(clippy::unused_async, clippy::similar_names)]
async fn listen_serial() -> Result<()> {
let port_name = "/dev/serial0";
let baud_rate = 9600;
@ -69,3 +69,23 @@ fn main() -> Result<()> {
let (_cps, _cpm, _radiation, _mode) = parse(line)?;
}
}
async fn flatten<T>(handle: JoinHandle<Result<T>>) -> Result<T>
where
T: Send,
{
match handle.await {
Ok(Ok(result)) => Ok(result),
Ok(Err(err)) => bail!(err),
Err(err) => bail!(err),
}
}
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
try_join!(flatten(spawn(listen_serial())))?;
Ok(())
}