prefmt
This commit is contained in:
parent
6effd4f1a8
commit
e941cf8bc8
|
@ -12,4 +12,3 @@ ftdi = "0.1.3"
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
thiserror = "1.0.15"
|
thiserror = "1.0.15"
|
||||||
serialport="=4.6.1"
|
serialport="=4.6.1"
|
||||||
anyhow = "1.0.97"
|
|
||||||
|
|
|
@ -1,116 +1,27 @@
|
||||||
use log::{debug, info, trace};
|
use log::{debug, info, trace};
|
||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
io::{Read, Write},
|
|
||||||
thread, time,
|
thread, time,
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use super::{Error, Obd2BaseDevice, Obd2Reader, Result};
|
use super::{Error, Obd2BaseDevice, Obd2Reader, Result, serial_comm::SerialComm};
|
||||||
|
|
||||||
trait SerialDevice {
|
|
||||||
fn write_all(&mut self, data: &[u8]) -> Result<()>;
|
|
||||||
fn read(&mut self, data: &mut [u8]) -> Result<usize>;
|
|
||||||
fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()>;
|
|
||||||
fn purge_buffers(&mut self) -> Result<()>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SerialPort {
|
|
||||||
device: Box<dyn serialport::SerialPort>
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SerialPort {
|
|
||||||
pub fn new(path: &str) -> Result<Self> {
|
|
||||||
let port = serialport::new(path, 38_400)
|
|
||||||
.timeout(Duration::from_millis(10))
|
|
||||||
.parity(serialport::Parity::None)
|
|
||||||
.data_bits(serialport::DataBits::Eight)
|
|
||||||
.stop_bits(serialport::StopBits::One)
|
|
||||||
.path(path)
|
|
||||||
.open()?;
|
|
||||||
|
|
||||||
let device = SerialPort {
|
|
||||||
device: port
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(device)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SerialDevice for SerialPort {
|
|
||||||
fn write_all(&mut self, data: &[u8]) -> Result<()> {
|
|
||||||
Ok(self.device.write_all(data)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read(&mut self, data: &mut [u8]) -> Result<usize> {
|
|
||||||
Ok(self.device.read(data)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()> {
|
|
||||||
Ok(self.device.set_baud_rate(baud_rate)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn purge_buffers(&mut self) -> Result<()> {
|
|
||||||
Ok(self.device.clear(serialport::ClearBuffer::All)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FTDIDevice {
|
|
||||||
device: ftdi::Device
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FTDIDevice {
|
|
||||||
pub fn new() -> Result<Self> {
|
|
||||||
let mut ftdi_device = ftdi::find_by_vid_pid(0x0404, 0x6001)
|
|
||||||
.interface(ftdi::Interface::A)
|
|
||||||
.open()?;
|
|
||||||
|
|
||||||
ftdi_device.set_baud_rate(38400)?;
|
|
||||||
ftdi_device.configure(ftdi::Bits::Eight, ftdi::StopBits::One, ftdi::Parity::None)?;
|
|
||||||
ftdi_device.usb_reset()?;
|
|
||||||
|
|
||||||
let device = FTDIDevice {
|
|
||||||
device: ftdi_device
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(device)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SerialDevice for FTDIDevice {
|
|
||||||
fn write_all(&mut self, data: &[u8]) -> Result<()> {
|
|
||||||
Ok(self.device.write_all(data)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read(&mut self, data: &mut [u8]) -> Result<usize> {
|
|
||||||
Ok(self.device.read(data)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_baud_rate(&mut self, baud_rate: u32) -> Result<()> {
|
|
||||||
Ok(self.device.set_baud_rate(baud_rate)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn purge_buffers(&mut self) -> Result<()> {
|
|
||||||
Ok(self.device.usb_purge_buffers()?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An ELM327 OBD-II adapter
|
/// An ELM327 OBD-II adapter
|
||||||
///
|
///
|
||||||
/// It communicates with the computer over UART using an FTDI FT232R USB-to-UART converter.
|
/// It communicates with the computer or UART using an FTDI FT232R USB-to-UART converter.
|
||||||
/// Commands to the device itself are indicated by sending "AT" followed by the command, while
|
/// Commands to the device itself are indicated by sending "AT" followed by the command, while
|
||||||
/// plain strings of hex data indicate OBD-II requests to be sent to the vehicle. The responses of
|
/// plain strings of hex data indicate OBD-II requests to be sent to the vehicle. The responses of
|
||||||
/// the vehicle are echoed back as hex characters. Capitalization and spaces are always ignored.
|
/// the vehicle are echoed back as hex characters. Capitalization and spaces are always ignored.
|
||||||
///
|
///
|
||||||
/// [Datasheet for v1.4b](https://github.com/rsammelson/obd2/blob/master/docs/ELM327DSH.pdf), and
|
/// [Datasheet for v1.4b](https://github.com/rsammelson/obd2/blob/master/docs/ELM327DSH.pdf), and
|
||||||
/// the [source](https://www.elmelectronics.com/products/dsheets/).
|
/// the [source](https://www.elmelectronics.com/products/dsheets/).
|
||||||
pub struct Elm327<T: SerialDevice> {
|
pub struct Elm327<T: SerialComm> {
|
||||||
device: T,
|
device: T,
|
||||||
buffer: VecDeque<u8>,
|
buffer: VecDeque<u8>,
|
||||||
baud_rate: u32,
|
baud_rate: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: SerialDevice> Obd2BaseDevice for Elm327<T> {
|
impl<T: SerialComm> Obd2BaseDevice for Elm327<T> {
|
||||||
fn reset(&mut self) -> Result<()> {
|
fn reset(&mut self) -> Result<()> {
|
||||||
self.flush_buffers()?;
|
self.flush_buffers()?;
|
||||||
self.reset_ic()?;
|
self.reset_ic()?;
|
||||||
|
@ -130,7 +41,7 @@ impl<T: SerialDevice> Obd2BaseDevice for Elm327<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: SerialDevice> Obd2Reader for Elm327<T> {
|
impl<T: SerialComm> Obd2Reader for Elm327<T> {
|
||||||
fn get_line(&mut self) -> Result<Option<Vec<u8>>> {
|
fn get_line(&mut self) -> Result<Option<Vec<u8>>> {
|
||||||
self.get_until(b'\n', false)
|
self.get_until(b'\n', false)
|
||||||
}
|
}
|
||||||
|
@ -146,7 +57,9 @@ impl<T: SerialDevice> Obd2Reader for Elm327<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: SerialDevice> Elm327<T> {
|
impl<T: SerialComm> Elm327<T> {
|
||||||
|
/// Creates a new Elm327 adapter with the given
|
||||||
|
/// unserlying Serial Communication device
|
||||||
pub fn new(serial_device: T) -> Result<Self> {
|
pub fn new(serial_device: T) -> Result<Self> {
|
||||||
let mut device = Elm327 {
|
let mut device = Elm327 {
|
||||||
device: serial_device,
|
device: serial_device,
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
//! Lower level OBD-II interfacing structures
|
//! Lower level OBD-II interfacing structures
|
||||||
|
|
||||||
mod elm327;
|
mod elm327;
|
||||||
pub use elm327::{Elm327, SerialPort, FTDIDevice};
|
pub use elm327::Elm327;
|
||||||
|
|
||||||
|
mod serial_comm;
|
||||||
|
pub use serial_comm::{SerialPort, FTDIDevice};
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, Error>;
|
type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
@ -54,6 +57,7 @@ pub enum Error {
|
||||||
#[error("FTDI error: `{0:?}`")]
|
#[error("FTDI error: `{0:?}`")]
|
||||||
Ftdi(ftdi::Error),
|
Ftdi(ftdi::Error),
|
||||||
|
|
||||||
|
/// An error with the underlying [serialport device](serialport::SerialPort)
|
||||||
#[error("Serialport error: `{0:?}`")]
|
#[error("Serialport error: `{0:?}`")]
|
||||||
Serialport(serialport::Error),
|
Serialport(serialport::Error),
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
//! Error types for OBD-II related errors
|
||||||
|
|
||||||
|
/// Result type defaulted with this library's error type
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
/// An error with OBD-II communication
|
/// An error with OBD-II communication
|
||||||
|
@ -16,6 +19,7 @@ pub enum Error {
|
||||||
Other(String),
|
Other(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An error with the ELM327 device
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct DeviceError(pub crate::device::Error);
|
pub struct DeviceError(pub crate::device::Error);
|
||||||
|
|
||||||
|
@ -27,12 +31,12 @@ impl From<super::device::Error> for Error {
|
||||||
|
|
||||||
impl From<std::num::ParseIntError> for Error {
|
impl From<std::num::ParseIntError> for Error {
|
||||||
fn from(e: std::num::ParseIntError) -> Self {
|
fn from(e: std::num::ParseIntError) -> Self {
|
||||||
Error::Other(format!("invalid data recieved: {:?}", e))
|
Error::Other(format!("invalid data received: {:?}", e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<std::string::FromUtf8Error> for Error {
|
impl From<std::string::FromUtf8Error> for Error {
|
||||||
fn from(e: std::string::FromUtf8Error) -> Self {
|
fn from(e: std::string::FromUtf8Error) -> Self {
|
||||||
Error::Other(format!("invalid string recieved: {:?}", e))
|
Error::Other(format!("invalid string received: {:?}", e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,8 @@ impl<T: Obd2BaseDevice> Obd2Device for Obd2<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Obd2BaseDevice> Obd2<T> {
|
impl<T: Obd2BaseDevice> Obd2<T> {
|
||||||
pub fn new(dev: T) -> ::anyhow::Result<Self> {
|
/// Creates a new instance of an Obd device
|
||||||
|
pub fn new(dev: T) -> Result<Self> {
|
||||||
let device = Obd2 {
|
let device = Obd2 {
|
||||||
device: dev
|
device: dev
|
||||||
};
|
};
|
||||||
|
@ -48,6 +49,7 @@ impl<T: Obd2BaseDevice> Obd2<T> {
|
||||||
Ok(device)
|
Ok(device)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resets the device
|
||||||
pub fn reset(&mut self) -> Result<()> {
|
pub fn reset(&mut self) -> Result<()> {
|
||||||
Ok(self.device.reset()?)
|
Ok(self.device.reset()?)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue