From f6089ebd5f1e79509c7a31a97746e40078efb5f6 Mon Sep 17 00:00:00 2001 From: mattbk Date: Thu, 10 Sep 2026 18:01:57 -0500 Subject: [PATCH] Append s-meter value to file with placeholders for time, lat, long. --- .gitignore | 1 + Cargo.lock | 34 +++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 83 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..0a25f36 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/out diff --git a/Cargo.lock b/Cargo.lock index a0bfaa0..5c5827d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -251,6 +251,27 @@ dependencies = [ "typenum", ] +[[package]] +name = "csv" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52cd9d68cf7efc6ddfaaee42e7288d3a99d613d4b50f76ce9827ae0c6e14f938" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde_core", +] + +[[package]] +name = "csv-core" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704a3c26996a80471189265814dbc2c257598b96b8a7feae2d31ace646bb9782" +dependencies = [ + "memchr", +] + [[package]] name = "darling" version = "0.20.11" @@ -698,6 +719,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3b2e3c8e73c68d760d0c421025a2b740727f1e807bd4a3d6de2ceb886c5dcb9" +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + [[package]] name = "konst" version = "0.2.20" @@ -894,6 +921,7 @@ name = "rfim-rust" version = "0.1.0" dependencies = [ "clap", + "csv", "io", "iter", "serialport", @@ -920,6 +948,12 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + [[package]] name = "scale-bits" version = "0.7.0" diff --git a/Cargo.toml b/Cargo.toml index 1d1b537..e2fd2fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2024" [dependencies] clap = "4.6.6" +csv = "1.4.0" io = "0.0.2" iter = "0.1.0" serialport = "4.10.0" diff --git a/src/main.rs b/src/main.rs index 322b989..1c7f966 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { + // 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::() { - Ok(n) => println!("{} {}", a, "#".repeat(n.try_into().unwrap())), - Err(e) => todo!(), - } + match s_val.parse::() { + 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); + } } }