32 lines
788 B
Rust
32 lines
788 B
Rust
#![feature(async_closure)]
|
|
|
|
use anyhow::{Context, Result};
|
|
use common::{Command, Response, BAUDRATE};
|
|
use framed_codec::FramedCodec;
|
|
use futures::{SinkExt, StreamExt};
|
|
use tokio_serial::SerialPortBuilderExt;
|
|
use tokio_util::codec::Framed;
|
|
|
|
mod framed_codec;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
let mut serial = tokio_serial::new("/dev/ttyAMA0", BAUDRATE).open_native_async()?;
|
|
|
|
serial.set_exclusive(false)?;
|
|
|
|
let (mut write, read) = Framed::new(serial, FramedCodec::new()).split();
|
|
|
|
let mut read = read.map(|data| -> Result<Response> {
|
|
Ok(postcard::from_bytes::<Response>(&data.ok().context("decode err")?)?)
|
|
});
|
|
|
|
|
|
while let Some(data) = read.next().await {
|
|
dbg!(data?);
|
|
|
|
write.send(Command::Enable).await?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|