Skip to content
Snippets Groups Projects
Commit da6af740 authored by Robert Hedman's avatar Robert Hedman
Browse files

readExact addition

parent a9caa438
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,7 @@ use serial::prelude::*; ...@@ -9,7 +9,7 @@ use serial::prelude::*;
fn main() { fn main() {
let device = String::from("/dev/cu.usbmodem1411"); let device = String::from("/dev/cu.usbmodem41");
let mut port = serial::open(&device).unwrap(); let mut port = serial::open(&device).unwrap();
interact(&mut port).unwrap(); interact(&mut port).unwrap();
...@@ -29,19 +29,37 @@ fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> { ...@@ -29,19 +29,37 @@ fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
//let mut buf: Vec<u8> = (0..255).collect(); //let mut buf: Vec<u8> = (0..255).collect();
let mut buf: [u8; 255] = [0; 255]; let mut buf: [u8; 8] = [0; 8];
let mut count: usize = 0; let mut count: usize = 0;
loop {
match port.read_exact(&mut buf){
Ok(_) => {
print!("A: {}, ", ((buf[0] as u32) << 24) + ((buf[1] as u32) << 16) + ((buf[2] as u32) << 8) + (buf[3] as u32));
println!("B: {}", ((buf[4] as u32) << 24) + ((buf[5] as u32) << 16) + ((buf[6] as u32) << 8) + (buf[7] as u32));
},
Err(e) => {
println!("Could not read all bytes: {:?}", e);
break;
},
}
}
for c in port.bytes() { for c in port.bytes() {
match c { match c {
Ok(c) => { Ok(c) => {
buf[count] = c; buf[count] = c;
count+=1; count+=1;
if c as char == '\n' { if count == 8 {
for i in 0..count {
print!("{}", buf[i] as char); print!("A: {}, ", ((buf[0] as u32) << 24) + ((buf[1] as u32) << 16) + ((buf[2] as u32) << 8) + (buf[3] as u32));
} println!("B: {}", ((buf[4] as u32) << 24) + ((buf[5] as u32) << 16) + ((buf[6] as u32) << 8) + (buf[7] as u32));
//for i in 0..count {
// print!("{} ", buf[i]);
//}
count = 0; count = 0;
//println!();
} }
}, },
Err(e) => println!("{:?}", e), Err(e) => println!("{:?}", e),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment