cleanup
This commit is contained in:
parent
ab604f3c51
commit
6effd4f1a8
5 changed files with 129 additions and 274 deletions
|
|
@ -4,9 +4,97 @@ use std::{
|
|||
io::{Read, Write},
|
||||
thread, time,
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
use super::{Error, Obd2BaseDevice, Obd2Reader, Result};
|
||||
|
||||
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
|
||||
///
|
||||
/// It communicates with the computer over UART using an FTDI FT232R USB-to-UART converter.
|
||||
|
|
@ -16,13 +104,13 @@ use super::{Error, Obd2BaseDevice, Obd2Reader, Result};
|
|||
///
|
||||
/// [Datasheet for v1.4b](https://github.com/rsammelson/obd2/blob/master/docs/ELM327DSH.pdf), and
|
||||
/// the [source](https://www.elmelectronics.com/products/dsheets/).
|
||||
pub struct Elm327 {
|
||||
device: ftdi::Device,
|
||||
pub struct Elm327<T: SerialDevice> {
|
||||
device: T,
|
||||
buffer: VecDeque<u8>,
|
||||
baud_rate: u32,
|
||||
}
|
||||
|
||||
impl Obd2BaseDevice for Elm327 {
|
||||
impl<T: SerialDevice> Obd2BaseDevice for Elm327<T> {
|
||||
fn reset(&mut self) -> Result<()> {
|
||||
self.flush_buffers()?;
|
||||
self.reset_ic()?;
|
||||
|
|
@ -42,7 +130,7 @@ impl Obd2BaseDevice for Elm327 {
|
|||
}
|
||||
}
|
||||
|
||||
impl Obd2Reader for Elm327 {
|
||||
impl<T: SerialDevice> Obd2Reader for Elm327<T> {
|
||||
fn get_line(&mut self) -> Result<Option<Vec<u8>>> {
|
||||
self.get_until(b'\n', false)
|
||||
}
|
||||
|
|
@ -58,22 +146,12 @@ impl Obd2Reader for Elm327 {
|
|||
}
|
||||
}
|
||||
|
||||
impl Elm327 {
|
||||
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)?;
|
||||
// device.set_latency_timer(2).unwrap();
|
||||
|
||||
ftdi_device.usb_reset()?;
|
||||
|
||||
impl<T: SerialDevice> Elm327<T> {
|
||||
pub fn new(serial_device: T) -> Result<Self> {
|
||||
let mut device = Elm327 {
|
||||
device: ftdi_device,
|
||||
device: serial_device,
|
||||
buffer: VecDeque::new(),
|
||||
baud_rate: 38400,
|
||||
baud_rate: 38_400,
|
||||
};
|
||||
|
||||
device.connect(false)?;
|
||||
|
|
@ -92,7 +170,7 @@ impl Elm327 {
|
|||
}
|
||||
|
||||
fn flush_buffers(&mut self) -> Result<()> {
|
||||
self.device.usb_purge_buffers()?;
|
||||
self.device.purge_buffers()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -117,9 +195,10 @@ impl Elm327 {
|
|||
fn reset_ic(&mut self) -> Result<()> {
|
||||
info!("Performing IC reset");
|
||||
self.send_serial_str("ATZ")?;
|
||||
let response = self.get_response()?;
|
||||
debug!(
|
||||
"reset_ic: got response {:?}",
|
||||
self.get_response()?
|
||||
response
|
||||
.as_ref()
|
||||
.map(|l| std::str::from_utf8(l.as_slice()))
|
||||
);
|
||||
|
|
@ -128,14 +207,18 @@ impl Elm327 {
|
|||
|
||||
fn reset_protocol(&mut self) -> Result<()> {
|
||||
info!("Performing protocol reset");
|
||||
let elm_response = self.serial_cmd("ATSP0")?;
|
||||
debug!(
|
||||
"reset_protocol: got response {:?}",
|
||||
self.serial_cmd("ATSP0")?
|
||||
elm_response
|
||||
);
|
||||
|
||||
let obd_response = self.cmd(&[0x01, 0x00])?;
|
||||
debug!(
|
||||
"reset_protocol: got OBD response {:?}",
|
||||
self.cmd(&[0x01, 0x00])?
|
||||
obd_response
|
||||
);
|
||||
|
||||
self.flush_buffers()?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -252,13 +335,18 @@ impl Elm327 {
|
|||
fn read_into_queue(&mut self) -> Result<()> {
|
||||
let mut buf = [0u8; 16];
|
||||
loop {
|
||||
let len = self.device.read(&mut buf)?;
|
||||
if len > 0 {
|
||||
self.buffer.extend(&buf[0..len]);
|
||||
trace!(
|
||||
"read_into_queue: values {:?}",
|
||||
std::str::from_utf8(&buf[0..len])
|
||||
);
|
||||
let len_res = self.device.read(&mut buf);
|
||||
if let Ok(len) = len_res {
|
||||
if len > 0 {
|
||||
self.buffer.extend(&buf[0..len]);
|
||||
trace!(
|
||||
"read_into_queue: values {:?}",
|
||||
std::str::from_utf8(&buf[0..len])
|
||||
);
|
||||
} else {
|
||||
trace!("read_into_queue: no values left to read");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
trace!("read_into_queue: no values left to read");
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue