Init.
This commit is contained in:
+61
@@ -0,0 +1,61 @@
|
||||
use serialport::DataBits;
|
||||
use serialport::Parity;
|
||||
use serialport::StopBits;
|
||||
use std::io::ErrorKind;
|
||||
use std::io::Write;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
||||
fn get_smeter(comport: &str) -> String {
|
||||
let mut port = serialport::new(comport, 4800)
|
||||
.data_bits(DataBits::Eight)
|
||||
.stop_bits(StopBits::Two)
|
||||
.parity(Parity::None)
|
||||
.timeout(Duration::from_millis(100))
|
||||
.open()
|
||||
.expect("Failed to open port");
|
||||
|
||||
// Duh, you need the ";" on the end!
|
||||
// let output = "FA14075000;";
|
||||
let output = "SM;";
|
||||
// What are we actually sending?
|
||||
//dbg!(output.as_bytes());
|
||||
// port.write(output).expect("Write failed!");
|
||||
|
||||
match port.write(output.as_bytes()) {
|
||||
Ok(_) => {
|
||||
//print!("{}", &output);
|
||||
std::io::stdout().flush().unwrap();
|
||||
}
|
||||
Err(ref e) if e.kind() == ErrorKind::TimedOut => (),
|
||||
Err(e) => eprintln!("{:?}", e),
|
||||
}
|
||||
|
||||
// Sleep to allow the device to switch from read to write
|
||||
sleep(Duration::from_millis(50));
|
||||
|
||||
let mut buf = [0u8; 1024];
|
||||
loop {
|
||||
match port.read(&mut buf) {
|
||||
Ok(n) => {
|
||||
let data = String::from_utf8_lossy(&buf[..n]).to_string();
|
||||
//dbg!(data.as_bytes());
|
||||
//let value = data;
|
||||
let value = &data[2..5];
|
||||
return format!("{}", value);
|
||||
// return;
|
||||
}
|
||||
Err(_) => {} // lol that I can just not put a return in?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
println!("{a}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user