From 98d5332d8f426f7a2507084659e28fb58f6e2f32 Mon Sep 17 00:00:00 2001 From: fank Date: Wed, 11 Oct 2023 09:58:07 -0400 Subject: [PATCH] fixed error handling --- README.md | 6 +++++- config.json | 10 +++++----- src/main.rs | 49 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0e1e649..b0b3640 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # serial-cli -A command line interface built in rust +A command line interface built in rust designed to decode binary data from byte, int, and float types. Program runs with a configuration file called config.json. + + +## How to start +To start you can run the program in the main directory with ```cargo run```. This will read the config file in the main directory as well. Its recommended to run the program from the binary compiled with ```cargo build --release``` where the executable can be found in target/release. This comes with its own config file in the same directory. \ No newline at end of file diff --git a/config.json b/config.json index 597c6d8..7449bc8 100644 --- a/config.json +++ b/config.json @@ -1,11 +1,11 @@ { "baud": 9600, - "start": "A", + "start": "ยช", "body": [ - {"data_type": "float", "count": 2}, - {"data_type": "int", "count": 1}, - {"data_type": "byte", "count": 4} + {"data_type": "float", "count": 19}, + {"data_type": "byte", "count": 1}, + {"data_type": "float", "count": 2} ], - "end": "Z" + "end": "U" } diff --git a/src/main.rs b/src/main.rs index b5c90a9..3d59d54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,9 +85,21 @@ fn main() { println!(); print!("**********************************************\n"); print!("** **\n"); - print!("** Welcome to Frank's Serial Monitor! **\n"); + print!("** Welcome to ETA SPACE's Binary Decoder! **\n"); print!("** **\n"); print!("**********************************************\n"); + + // printing commands an operation + print!("Exit the program with ESC\n"); + print!("\n"); + + // print loading config + print!("Loading Config:\n"); + print!("{}", config_data); + + print!("********************START*********************\n"); + + print!(""); println!(); io::stdout().flush().unwrap(); @@ -161,7 +173,7 @@ fn main() { let mut byte_buffer: Vec = vec![0; 1]; let mut state: State = State::Start; - + loop { if thread2_terminate_flag.load(Ordering::Relaxed) { break; // Exit the loop when the termination flag is set @@ -182,6 +194,9 @@ fn main() { } Err(e) => { eprint!("{}", e); + // thread flag not read fast enough (kill) + thread2_terminate_flag.store(true, Ordering::Relaxed); + break; } } } @@ -201,11 +216,15 @@ fn main() { float_byte_count += 1; } } - Err(_) => {} + Err(_) => { + // thread flag not read fast enough (kill) + thread2_terminate_flag.store(true, Ordering::Relaxed); + break; + } } } byte_found_count += 1; - float_buffer.reverse(); + // float_buffer.reverse(); let float_value = f32::from_ne_bytes(float_buffer); print!("\r\x1B[K"); print!("Float: {}\r\n", float_value); @@ -223,7 +242,11 @@ fn main() { int_byte_count += 1; } } - Err(_) => {} + Err(_) => { + // thread flag not read fast enough (kill) + thread2_terminate_flag.store(true, Ordering::Relaxed); + break; + } } } byte_found_count += 1; @@ -247,7 +270,11 @@ fn main() { io::stdout().flush().unwrap(); } } - Err(_) => {} + Err(_) => { + // thread flag not read fast enough (kill) + thread2_terminate_flag.store(true, Ordering::Relaxed); + break; + } } } _ => {} @@ -269,15 +296,21 @@ fn main() { state = State::Start; } else { print!("\r\x1B[K"); - print!("End not found!\r\n"); + print!("End not found: {}\r\n", byte_buffer[0]); print!(">{}", thread2.lock().unwrap()); } io::stdout().flush().unwrap(); } } - Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), + Err(ref e) if e.kind() == io::ErrorKind::TimedOut => { + // thread flag not read fast enough (kill) + thread2_terminate_flag.store(true, Ordering::Relaxed); + break; + } Err(e) => { eprintln!("{:?}", e); + // thread flag not read fast enough (kill) + thread2_terminate_flag.store(true, Ordering::Relaxed); break; // Exit the loop on error } }