Skip to content

Commit

Permalink
fix slicer
Browse files Browse the repository at this point in the history
  • Loading branch information
jmmaa committed Mar 5, 2023
1 parent 17efcff commit ed5d816
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ fn open_port(b: u32, t: u64) -> Result<Box<dyn serialport::SerialPort>, Box<dyn
match find_port() {
Ok(path) => {
let port = serialport::new(path, b)
.timeout(Duration::from_millis(t))
.flow_control(serialport::FlowControl::None)
.data_bits(serialport::DataBits::Eight)
.stop_bits(serialport::StopBits::One)
.timeout(Duration::from_millis(t))
.parity(serialport::Parity::None)
.open();

Expand Down Expand Up @@ -160,15 +161,15 @@ fn write_reading(wtr: &mut Writer<File>, parsed: &ParsedData) -> Result<(), Box<

fn process_data(port: &mut Box<dyn serialport::SerialPort>, wtr: &mut Writer<File>) {
let mut to_resolve: Vec<u8> = Vec::new();
let mut buf = [0; 16];
let mut buf = [0; 4];

loop {
match port.read(&mut buf) {
Ok(num) => {
if num > 0 {
let slice = Slicer::new(&buf);
let slice = Slicer::new(Slicer::new(&buf).to_before(num));

let marker_pos = slice.from(0).to_before(num).position(|b| b == b'$');
let marker_pos = slice.to_end().position(|b| b == b'$');

if let Some(marker_index) = marker_pos {
to_resolve.extend(slice.to_before(marker_index));
Expand All @@ -184,7 +185,7 @@ fn process_data(port: &mut Box<dyn serialport::SerialPort>, wtr: &mut Writer<Fil
to_resolve.clear();
to_resolve.extend(slice.from_after(marker_index).to_end());
} else {
to_resolve.extend(slice.from(0).to_before(num));
to_resolve.extend(slice.to_before(num));
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl<'a, V> Slicer<'a, V, NoStartIndex> {
pub fn to_before(&self, n: usize) -> &'a [V] {
&self.v[..n]
}

pub fn to_end(&self) -> &'a [V] {
&self.v[..]
}
}

impl<'a, V> Slicer<'a, V> {
Expand Down
2 changes: 2 additions & 0 deletions tests/slicer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ fn test_slicer() {
assert_eq!(slice.from_after(0).to_end(), &[2, 3, 4, 5]);

assert_eq!(slice.from(1).to_before(5), &[2, 3, 4, 5]);

assert_eq!(slice.from(1).to(4), &[2, 3, 4, 5]);
}

0 comments on commit ed5d816

Please sign in to comment.