Append s-meter value to file with placeholders for time, lat, long.
This commit is contained in:
@@ -1 +1,2 @@
|
||||
/target
|
||||
/out
|
||||
|
||||
Generated
+34
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
+46
-6
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user