Append s-meter value to file with placeholders for time, lat, long.

This commit is contained in:
mattbk
2026-09-10 18:01:57 -05:00
parent ccf554c8a4
commit f6089ebd5f
4 changed files with 83 additions and 7 deletions
+47 -7
View File
@@ -1,11 +1,12 @@
use serialport::DataBits;
use serialport::Parity;
use serialport::StopBits;
use std::fs::OpenOptions;
use std::io::ErrorKind;
use std::io::Write;
use std::thread::sleep;
use std::time::Duration;
use core::iter::repeat;
use std::{error::Error};
fn get_smeter(comport: &str) -> String {
let mut port = serialport::new(comport, 4800)
@@ -53,16 +54,55 @@ fn get_smeter(comport: &str) -> String {
}
}
// Write row to a file
fn write_row(str_arr: [String; 4], file: &str) -> Result<(), Box<dyn Error>> {
// From https://docs.rs/csv/latest/csv/tutorial/index.html#writing-csv
let file = OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(file)
.unwrap();
let mut wtr = csv::Writer::from_writer(file);
// Since we're writing records manually, we must explicitly write our
// 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);
// A CSV writer maintains an internal buffer, so it's important
// to flush the buffer when you're done.
wtr.flush()?;
Ok(())
}
fn main() {
let port: &str = "/dev/tty.usbmodem1101";
// Simple test loop to make sure we are getting values each time
for _i in 1..101 {
let a = get_smeter(port);
for _i in 1..11 {
let s_val = get_smeter(port);
//println!("{a}");
match a.parse::<i32>() {
Ok(n) => println!("{} {}", a, "#".repeat(n.try_into().unwrap())),
Err(e) => todo!(),
}
match s_val.parse::<i32>() {
Ok(n) => println!("{} {}", s_val, "#".repeat(n.try_into().unwrap())),
Err(_e) => todo!(),
}
if let Err(err) = write_row(
[
"time".to_string(),
"lat".to_string(),
"lon".to_string(),
s_val.to_string(),
],
"out/test.csv",
) {
println!("{}", err);
// process::exit(1);
}
}
}