Skip to content

Commit

Permalink
fixed error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankLaterza committed Oct 11, 2023
1 parent 1646681 commit 98d5332
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 5 additions & 5 deletions config.json
Original file line number Diff line number Diff line change
@@ -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"
}

49 changes: 41 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -161,7 +173,7 @@ fn main() {
let mut byte_buffer: Vec<u8> = 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
Expand All @@ -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;
}
}
}
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
}
}
_ => {}
Expand All @@ -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
}
}
Expand Down

0 comments on commit 98d5332

Please sign in to comment.