Skip to content

Commit 817c3dd

Browse files
committed
backup
1 parent 77b6900 commit 817c3dd

File tree

1 file changed

+56
-40
lines changed

1 file changed

+56
-40
lines changed

src/serial.rs

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,86 @@ use crate::{
66
},
77
utils::log,
88
};
9-
use futures::channel::oneshot;
9+
use futures::{channel::mpsc, SinkExt};
10+
use futures::task::Spawn;
1011
use futures::{
11-
channel::oneshot::{Receiver, Sender},
12+
channel::mpsc::{Receiver, Sender},
1213
lock::Mutex,
1314
AsyncReadExt, AsyncWrite, AsyncWriteExt,
1415
};
1516
use serialport::SerialPort;
16-
use std::{future::Future, sync::Arc};
17-
use futures::task::Spawn;
17+
use std::{future::Future, sync::Arc, thread::JoinHandle};
1818

1919
pub struct SimpleSerialPort {
20-
read: Arc<Mutex<Box<dyn SerialPort>>>,
21-
write: Arc<Mutex<Box<dyn SerialPort>>>,
22-
tx: Sender<UnpiPacket<'static>>,
23-
rx: Receiver<UnpiPacket<'static>>,
20+
path: String,
21+
baud_rate: u32,
22+
// from the serial port to the coordinator
23+
serial_to: (
24+
Option<Sender<UnpiPacket<'static>>>,
25+
Option<Receiver<UnpiPacket<'static>>>,
26+
),
27+
// from the coordinator to the serial port
28+
to_serial: (
29+
Option<Sender<UnpiPacket<'static>>>,
30+
Option<Receiver<UnpiPacket<'static>>>,
31+
),
32+
read_thread: Option<JoinHandle<()>>,
33+
write_thread: Option<JoinHandle<()>>,
2434
}
2535

2636
impl SimpleSerialPort {
2737
pub fn new(path: &str, baud_rate: u32) -> Result<Self, CoordinatorError> {
28-
let (tx, rx) = oneshot::channel();
38+
let serial_to = mpsc::channel(10);
39+
let serial_to = (Some(serial_to.0), Some(serial_to.1));
40+
let to_serial = mpsc::channel(10);
41+
let to_serial = (Some(to_serial.0), Some(to_serial.1));
2942
Ok(SimpleSerialPort {
30-
read: Arc::new(Mutex::new(
31-
serialport::new(path, baud_rate)
32-
.timeout(std::time::Duration::from_millis(10))
33-
.open()
34-
.map_err(|_e| CoordinatorError::SerialOpen)?,
35-
)),
36-
write: Arc::new(Mutex::new(
37-
serialport::new(path, baud_rate)
38-
.timeout(std::time::Duration::from_millis(10))
39-
.open()
40-
.map_err(|_e| CoordinatorError::SerialOpen)?,
41-
)),
42-
tx,
43-
rx,
43+
path: path.to_string(),
44+
baud_rate,
45+
serial_to,
46+
to_serial,
47+
read_thread: None,
48+
write_thread: None,
4449
})
4550
}
4651

47-
pub async fn start(&self) -> Result<(), CoordinatorError> {
48-
let mut read = self.read.lock().await;
49-
let mut write = self.write.lock().await;
50-
let (tx, rx) = (self.tx, self.rx);
51-
let mut buffer = [0u8; 256];
52-
let receive = async {
52+
pub async fn start(&mut self) -> Result<(), CoordinatorError> {
53+
let mut read = serialport::new(self.path.clone(), self.baud_rate)
54+
.timeout(std::time::Duration::from_millis(10))
55+
.open()
56+
.map_err(|_e| CoordinatorError::SerialOpen)?;
57+
let mut write = serialport::new(self.path.clone(), self.baud_rate)
58+
.timeout(std::time::Duration::from_millis(10))
59+
.open()
60+
.map_err(|_e| CoordinatorError::SerialOpen)?;
61+
62+
let tx = self.serial_to.0.take().unwrap();
63+
let receive_from_serial_send_to_channel = move || {
64+
let tx = tx;
5365
loop {
54-
let len = read.read(&mut buffer).map_err(|_e| CoordinatorError::SerialRead)?;
66+
let mut buffer = [0u8; 256];
67+
let len = read
68+
.read(&mut buffer)
69+
.map_err(|_e| CoordinatorError::SerialRead)
70+
.unwrap();
5571
let packet = UnpiPacket::from_payload(
5672
(&buffer[..len], LenTypeInfo::OneByte),
5773
(MessageType::SREQ, Subsystem::Sys),
5874
0,
59-
)?;
75+
)
76+
.unwrap();
6077
log!("<<< {:?}", packet);
61-
tx.send(packet).map_err(|_e| CoordinatorError::SerialWrite)?;
78+
tx.send(packet)
79+
.map_err(|_e| CoordinatorError::SerialWrite)
80+
.unwrap();
6281
}
6382
};
64-
let send = async {
65-
loop {
66-
let packet = rx.await.map_err(|_e| CoordinatorError::SerialWrite)?;
67-
let mut buffer = [0u8; 256];
68-
packet.to_bytes(&mut buffer)?;
69-
write.write_all(buffer).map_err(|_e| CoordinatorError::SerialWrite)?;
70-
71-
}
83+
let rx = self.to_serial.1.take().unwrap();
84+
let receive_from_channel_send_to_serial = || loop {
85+
let packet = rx.recv().unwrap();
86+
todo!()
7287
};
88+
7389
todo!()
7490
}
7591
}

0 commit comments

Comments
 (0)