Serialport Support

- allow communication to elm327 device via a serialport
- allow creation of elm327 device without requiring unwrap()
- added some extra documentation
- expose reset() functionality of device
- serial_comm and ftdi_comm abstractions added
This commit is contained in:
Nicholas Orlowsky 2025-05-23 18:41:49 -04:00
parent db4b4d990d
commit 821e80aaaf
No known key found for this signature in database
GPG key ID: A9F3BA4C0AA7A70B
14 changed files with 213 additions and 60 deletions

View file

@ -6,7 +6,6 @@ use super::{device::Obd2BaseDevice, Error, Obd2Device, Result};
///
/// Wraps an implementer of [Obd2BaseDevice] to allow for higher-level usage of the OBD-II
/// interface.
#[derive(Default)]
pub struct Obd2<T: Obd2BaseDevice> {
device: T,
}
@ -17,9 +16,11 @@ impl<T: Obd2BaseDevice> Obd2Device for Obd2<T> {
for response in result.iter() {
if response.first() != Some(&(0x40 | mode)) {
// mismatch of mode in response
todo!()
}
if response.get(1) != Some(&pid) {
// mismatch of PID in response
todo!()
}
}
@ -41,6 +42,18 @@ impl<T: Obd2BaseDevice> Obd2Device for Obd2<T> {
}
impl<T: Obd2BaseDevice> Obd2<T> {
/// Creates a new instance of an Obd device
pub fn new(dev: T) -> Result<Self> {
let device = Obd2 { device: dev };
Ok(device)
}
/// Resets the device
pub fn reset(&mut self) -> Result<()> {
Ok(self.device.reset()?)
}
fn command(&mut self, command: &[u8]) -> Result<Vec<Vec<u8>>> {
let response = self
.device
@ -109,7 +122,8 @@ impl<T: Obd2BaseDevice> Obd2<T> {
.filter_map(|l| l.split_once(':'))
.flat_map(|(idx, data)| {
if u8::from_str_radix(idx, 16) != Ok(n_idx) {
todo!()
// got an invalid hex code or values were not already in the correct order
todo!("Line index: {}, should be {:X}", idx, n_idx)
}
n_idx = (n_idx + 1) % 0x10;
data.split_whitespace().map(|s| s.to_owned())