better pli error propagation

This commit is contained in:
Alex Janka 2024-01-12 09:32:43 +11:00
parent af67a1e678
commit 8de6c8db61
4 changed files with 8 additions and 6 deletions

2
Cargo.lock generated
View file

@ -2256,7 +2256,7 @@ dependencies = [
[[package]] [[package]]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "0.1.18" version = "0.1.19"
dependencies = [ dependencies = [
"async-channel", "async-channel",
"chrono", "chrono",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "0.1.18" version = "0.1.19"
edition = "2021" edition = "2021"
license = "MITNFA" license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data" description = "Controls Tesla charge rate based on solar charge data"

View file

@ -60,6 +60,8 @@ pub enum RequestError {
pub enum PliError { pub enum PliError {
#[error("read error")] #[error("read error")]
ReadError(u8), ReadError(u8),
#[error("io error")]
StdioErr(#[from] std::io::Error),
} }
#[derive(Error, Debug)] #[derive(Error, Debug)]

View file

@ -206,10 +206,10 @@ impl Pli {
.expect("failed to write to serial port"); .expect("failed to write to serial port");
} }
fn receive<const LENGTH: usize>(&mut self) -> [u8; LENGTH] { fn receive<const LENGTH: usize>(&mut self) -> Result<[u8; LENGTH], PliError> {
let mut buf = [0; LENGTH]; let mut buf = [0; LENGTH];
let _ = self.port.read_exact(&mut buf).some_or_print(); self.port.read_exact(&mut buf)?;
buf Ok(buf)
} }
fn read_ram<T>(&mut self, address: T) -> Result<u8, PliError> fn read_ram<T>(&mut self, address: T) -> Result<u8, PliError>
@ -217,7 +217,7 @@ impl Pli {
T: Into<u8>, T: Into<u8>,
{ {
self.send_command(command(20, address.into(), 0)); self.send_command(command(20, address.into(), 0));
let buf: [u8; 2] = self.receive(); let buf: [u8; 2] = self.receive()?;
if buf[0] == 200 { if buf[0] == 200 {
Ok(buf[1]) Ok(buf[1])
} else { } else {