From da6af740becf3dd8e3ca68c83070b2a7d33d7eec Mon Sep 17 00:00:00 2001 From: Robert Hedman <robert.hedman@mac.com> Date: Sun, 26 Nov 2017 22:24:40 +0100 Subject: [PATCH] readExact addition --- src/main.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 97b3fb9..07835cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use serial::prelude::*; fn main() { - let device = String::from("/dev/cu.usbmodem1411"); + let device = String::from("/dev/cu.usbmodem41"); let mut port = serial::open(&device).unwrap(); interact(&mut port).unwrap(); @@ -29,19 +29,37 @@ fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> { //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; + 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() { match c { Ok(c) => { buf[count] = c; count+=1; - if c as char == '\n' { - for i in 0..count { - print!("{}", buf[i] as char); - } + if count == 8 { + + 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; + //println!(); } }, Err(e) => println!("{:?}", e), -- GitLab