Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Sep 6, 2024
1 parent 0aa41e7 commit 4953fc1
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/drivers/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::io::{self, Read, Write};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast;
use tokio_serial;
use tokio::io::AsyncReadExt;
use tokio_serial::{self, SerialPort};
use tracing::*;

use crate::drivers::{Driver, DriverExt, DriverInfo};
Expand All @@ -25,15 +26,15 @@ impl Serial {

#[instrument(level = "debug", skip(port))]
async fn serial_receive_task(
mut port: Box<dyn tokio_serial::SerialPort>,
hub_sender: Arc<broadcast::Sender<Protocol>>,
mut port: Box<tokio_serial::SerialStream>,
hub_sender: broadcast::Sender<Protocol>,
) -> Result<()> {
let mut buf = vec![0; 1024];
let port_name = port.name().unwrap_or("unknown".to_string());

loop {
tokio::time::sleep(tokio::time::Duration::from_micros(1)).await;
match port.read(&mut buf) {
match tokio::io::AsyncReadExt::read(&mut port, &mut buf).await {
// We got something
Ok(bytes_received) if bytes_received > 0 => {
read_all_messages("serial", &mut buf, |message| async {
Expand All @@ -60,7 +61,7 @@ impl Serial {

#[instrument(level = "debug", skip(port))]
async fn serial_send_task(
mut port: Box<dyn tokio_serial::SerialPort>,
mut port: Box<tokio_serial::SerialStream>,
mut hub_receiver: broadcast::Receiver<Protocol>,
) -> Result<()> {
loop {
Expand All @@ -86,9 +87,11 @@ impl Driver for Serial {
async fn run(&self, hub_sender: broadcast::Sender<Protocol>) -> Result<()> {
let port_name = self.port_name.clone();

let port = match tokio_serial::new(&port_name, self.baud_rate)
.timeout(Duration::from_millis(1000))
.open()
let port_builder = tokio_serial::new(&port_name, self.baud_rate).timeout(Duration::from_millis(1000));

debug!("Serial successfully opened port {port_name:?}");

let port = match tokio_serial::SerialStream::open(&port_builder)
{
Ok(port) => port,
Err(error) => {
Expand All @@ -97,19 +100,19 @@ impl Driver for Serial {
}
};

debug!("Serial successfully opened port {port_name:?}");

let (port_send, port_receive) = port.pair().unwrap();
// let port = Box::new(port);
loop {
let hub_sender = Arc::new(hub_sender.clone());
let hub_sender = hub_sender.clone();
let hub_receiver = hub_sender.subscribe();

tokio::select! {
result = Serial::serial_send_task(port.try_clone()?, hub_receiver) => {
result = Serial::serial_send_task(port_send, hub_receiver) => {
if let Err(e) = result {
error!("Error in serial receive task for {port_name}: {e:?}");
}
}
result = Serial::serial_receive_task(port.try_clone()?, hub_sender) => {
result = Serial::serial_receive_task(port_receive, hub_sender) => {
if let Err(e) = result {
error!("Error in serial send task for {port_name}: {e:?}");
}
Expand Down

0 comments on commit 4953fc1

Please sign in to comment.