diff --git a/Cargo.lock b/Cargo.lock index 5c5827d..5260ae1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -157,6 +157,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "473c7e07f409a8d772161724aa8db6a765a2532a70f9667eeb7b49d3d02fbdca" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -171,6 +172,18 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_derive" +version = "4.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d012d2b9d65aca7f18f4d9878a045bc17899bba951561ba5ec3c2ba1eed9a061" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 3.0.5", +] + [[package]] name = "clap_lex" version = "1.1.0" @@ -643,6 +656,12 @@ version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" diff --git a/Cargo.toml b/Cargo.toml index e2fd2fd..89f5328 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -clap = "4.6.6" +clap = { version = "4", features = ["derive"] } csv = "1.4.0" io = "0.0.2" iter = "0.1.0" diff --git a/result.csv b/result.csv new file mode 100644 index 0000000..4731251 --- /dev/null +++ b/result.csv @@ -0,0 +1,20 @@ +time,lat,lon,000 +time,lat,lon,004 +time,lat,lon,008 +time,lat,lon,004 +time,lat,lon,010 +time,lat,lon,011 +time,lat,lon,002 +time,lat,lon,003 +time,lat,lon,009 +time,lat,lon,010 +time,lat,lon,006 +time,lat,lon,006 +time,lat,lon,008 +time,lat,lon,010 +time,lat,lon,007 +time,lat,lon,004 +time,lat,lon,003 +time,lat,lon,006 +time,lat,lon,006 +time,lat,lon,005 diff --git a/src/main.rs b/src/main.rs index 1c7f966..898d280 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,24 @@ use std::io::Write; use std::thread::sleep; use std::time::Duration; use std::{error::Error}; +use clap::Parser; + +/// Command-line arguments using Clap. +#[derive(Parser, Debug)] +#[command( + author, + version, + about = "A tool for measuring S-meter readings at different locations." +)] +struct Cli { + /// COM port + #[arg(short = 'c', long)] + com: Option, + + /// Format + #[arg(short = 'f', long, default_value = "result.csv")] + file: Option, +} fn get_smeter(comport: &str) -> String { let mut port = serialport::new(comport, 4800) @@ -26,7 +44,6 @@ fn get_smeter(comport: &str) -> String { match port.write(output.as_bytes()) { Ok(_) => { - //print!("{}", &output); std::io::stdout().flush().unwrap(); } Err(ref e) if e.kind() == ErrorKind::TimedOut => (), @@ -55,8 +72,9 @@ fn get_smeter(comport: &str) -> String { } // Write row to a file -fn write_row(str_arr: [String; 4], file: &str) -> Result<(), Box> { +fn write_row(str_arr: [String; 4], file: &String, fmt: &str) -> Result<(), Box> { // From https://docs.rs/csv/latest/csv/tutorial/index.html#writing-csv + if fmt == "csv" { let file = OpenOptions::new() .write(true) .create(true) @@ -69,25 +87,49 @@ fn write_row(str_arr: [String; 4], file: &str) -> Result<(), Box> { // header record. A header record is written the same way that other // records are written. // wtr.write_record(["City", "State", "Population", "Latitude", "Longitude"])?; - // wtr.write_record(["Davidsons Landing", "AK", "", "65.2419444", "-165.2716667"])?; - // wtr.write_record(["Kenai", "AK", "7610", "60.5544444", "-151.2583333"])?; - // wtr.write_record(["Oakman", "AL", "", "33.7133333", "-87.3886111"])?; - wtr.write_record(str_arr); + let _ = wtr.write_record(str_arr); // A CSV writer maintains an internal buffer, so it's important // to flush the buffer when you're done. wtr.flush()?; Ok(()) + } else { + println!("Only accepts csv format for now, not recording to file."); + Ok(()) + } } fn main() { - let port: &str = "/dev/tty.usbmodem1101"; + // Arguments + let cli = Cli::parse(); + + let com = match cli.com { + Some(com) => com, + None => { + // https://github.com/serialport/serialport-rs#listing-available-ports + let ports = serialport::available_ports().expect("No ports found!"); + println!("Available Ports:"); + for p in ports { + println!("{}", p.port_name); + } + println!("\nMissing `-c` port argument, use one listed above."); + return; + } //todo!() + }; + + let file_out = match cli.file { + Some(file_out) => file_out, + None => { + return; + } //todo!() + }; // Simple test loop to make sure we are getting values each time for _i in 1..11 { - let s_val = get_smeter(port); + let s_val = get_smeter(&com); //println!("{a}"); match s_val.parse::() { + // Simple graph as we go. Eventually this will be replaced. Ok(n) => println!("{} {}", s_val, "#".repeat(n.try_into().unwrap())), Err(_e) => todo!(), } @@ -99,10 +141,10 @@ fn main() { "lon".to_string(), s_val.to_string(), ], - "out/test.csv", + &file_out, + "csv" ) { println!("{}", err); - // process::exit(1); } } }