Start adding parameters.
This commit is contained in:
Generated
+19
@@ -157,6 +157,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "473c7e07f409a8d772161724aa8db6a765a2532a70f9667eeb7b49d3d02fbdca"
|
checksum = "473c7e07f409a8d772161724aa8db6a765a2532a70f9667eeb7b49d3d02fbdca"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
|
"clap_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -171,6 +172,18 @@ dependencies = [
|
|||||||
"strsim",
|
"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]]
|
[[package]]
|
||||||
name = "clap_lex"
|
name = "clap_lex"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@@ -643,6 +656,12 @@ version = "0.17.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hex"
|
name = "hex"
|
||||||
version = "0.4.3"
|
version = "0.4.3"
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = "4.6.6"
|
clap = { version = "4", features = ["derive"] }
|
||||||
csv = "1.4.0"
|
csv = "1.4.0"
|
||||||
io = "0.0.2"
|
io = "0.0.2"
|
||||||
iter = "0.1.0"
|
iter = "0.1.0"
|
||||||
|
|||||||
+20
@@ -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
|
||||||
|
+52
-10
@@ -7,6 +7,24 @@ use std::io::Write;
|
|||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{error::Error};
|
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<String>,
|
||||||
|
|
||||||
|
/// Format
|
||||||
|
#[arg(short = 'f', long, default_value = "result.csv")]
|
||||||
|
file: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
fn get_smeter(comport: &str) -> String {
|
fn get_smeter(comport: &str) -> String {
|
||||||
let mut port = serialport::new(comport, 4800)
|
let mut port = serialport::new(comport, 4800)
|
||||||
@@ -26,7 +44,6 @@ fn get_smeter(comport: &str) -> String {
|
|||||||
|
|
||||||
match port.write(output.as_bytes()) {
|
match port.write(output.as_bytes()) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
//print!("{}", &output);
|
|
||||||
std::io::stdout().flush().unwrap();
|
std::io::stdout().flush().unwrap();
|
||||||
}
|
}
|
||||||
Err(ref e) if e.kind() == ErrorKind::TimedOut => (),
|
Err(ref e) if e.kind() == ErrorKind::TimedOut => (),
|
||||||
@@ -55,8 +72,9 @@ fn get_smeter(comport: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write row to a file
|
// Write row to a file
|
||||||
fn write_row(str_arr: [String; 4], file: &str) -> Result<(), Box<dyn Error>> {
|
fn write_row(str_arr: [String; 4], file: &String, fmt: &str) -> Result<(), Box<dyn Error>> {
|
||||||
// From https://docs.rs/csv/latest/csv/tutorial/index.html#writing-csv
|
// From https://docs.rs/csv/latest/csv/tutorial/index.html#writing-csv
|
||||||
|
if fmt == "csv" {
|
||||||
let file = OpenOptions::new()
|
let file = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
@@ -69,25 +87,49 @@ fn write_row(str_arr: [String; 4], file: &str) -> Result<(), Box<dyn Error>> {
|
|||||||
// header record. A header record is written the same way that other
|
// header record. A header record is written the same way that other
|
||||||
// records are written.
|
// records are written.
|
||||||
// wtr.write_record(["City", "State", "Population", "Latitude", "Longitude"])?;
|
// wtr.write_record(["City", "State", "Population", "Latitude", "Longitude"])?;
|
||||||
// wtr.write_record(["Davidsons Landing", "AK", "", "65.2419444", "-165.2716667"])?;
|
let _ = wtr.write_record(str_arr);
|
||||||
// wtr.write_record(["Kenai", "AK", "7610", "60.5544444", "-151.2583333"])?;
|
|
||||||
// wtr.write_record(["Oakman", "AL", "", "33.7133333", "-87.3886111"])?;
|
|
||||||
wtr.write_record(str_arr);
|
|
||||||
|
|
||||||
// A CSV writer maintains an internal buffer, so it's important
|
// A CSV writer maintains an internal buffer, so it's important
|
||||||
// to flush the buffer when you're done.
|
// to flush the buffer when you're done.
|
||||||
wtr.flush()?;
|
wtr.flush()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
} else {
|
||||||
|
println!("Only accepts csv format for now, not recording to file.");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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
|
// Simple test loop to make sure we are getting values each time
|
||||||
for _i in 1..11 {
|
for _i in 1..11 {
|
||||||
let s_val = get_smeter(port);
|
let s_val = get_smeter(&com);
|
||||||
//println!("{a}");
|
//println!("{a}");
|
||||||
match s_val.parse::<i32>() {
|
match s_val.parse::<i32>() {
|
||||||
|
// Simple graph as we go. Eventually this will be replaced.
|
||||||
Ok(n) => println!("{} {}", s_val, "#".repeat(n.try_into().unwrap())),
|
Ok(n) => println!("{} {}", s_val, "#".repeat(n.try_into().unwrap())),
|
||||||
Err(_e) => todo!(),
|
Err(_e) => todo!(),
|
||||||
}
|
}
|
||||||
@@ -99,10 +141,10 @@ fn main() {
|
|||||||
"lon".to_string(),
|
"lon".to_string(),
|
||||||
s_val.to_string(),
|
s_val.to_string(),
|
||||||
],
|
],
|
||||||
"out/test.csv",
|
&file_out,
|
||||||
|
"csv"
|
||||||
) {
|
) {
|
||||||
println!("{}", err);
|
println!("{}", err);
|
||||||
// process::exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user