Start adding parameters.

This commit is contained in:
mattbk
2026-09-10 19:01:44 -05:00
parent f6089ebd5f
commit fb09318837
4 changed files with 92 additions and 11 deletions
+52 -10
View File
@@ -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<String>,
/// Format
#[arg(short = 'f', long, default_value = "result.csv")]
file: Option<String>,
}
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<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
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<dyn Error>> {
// 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::<i32>() {
// 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);
}
}
}