Compare commits
3 commits
fuel_level
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
b581c1e4f8 | ||
|
1769d2a84b | ||
|
11e87eafaf |
|
@ -8,6 +8,11 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
env_logger = "0.10"
|
env_logger = "0.10"
|
||||||
ftdi = "0.1.3"
|
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
thiserror = "1.0.15"
|
thiserror = "1.0.15"
|
||||||
|
ftdi = { version = "0.1.3", optional = true }
|
||||||
|
serialport= { version = "=4.6.1", optional = true }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
ftdi_comm = [ "dep:ftdi" ]
|
||||||
|
serialport_comm = [ "dep:serialport" ]
|
||||||
|
|
11
README.md
11
README.md
|
@ -9,13 +9,20 @@ vehicle.
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```rs
|
```rs
|
||||||
use obd2::{commands::Obd2DataRetrieval, device::Elm327, Obd2};
|
use obd2::{commands::Obd2DataRetrieval, device::{Elm327, FTDIDevice}, Obd2};
|
||||||
|
|
||||||
fn main() -> Result<(), obd2::Error> {
|
fn main() -> Result<(), obd2::Error> {
|
||||||
let mut device = Obd2::<Elm327>::default();
|
let mut device = Obd2::<Elm327::<FTDIDevice>>::new(Elm327::new(FTDIDevice::new()?)?)?;
|
||||||
println!("VIN: {}", device.get_vin()?);
|
println!("VIN: {}", device.get_vin()?);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
alternatively, you could use a serial port provided by your operating system such as
|
||||||
|
/dev/ttyUSB0 on unix-like systems
|
||||||
|
|
||||||
|
```rs
|
||||||
|
let mut device = Obd2::<Elm327::<SerialPort>>::new(Elm327::new(SerialPort::new("/dev/ttyUSB0")?)?)?;
|
||||||
|
```
|
||||||
|
|
||||||
See the docs for more: https://docs.rs/obd2/
|
See the docs for more: https://docs.rs/obd2/
|
||||||
|
|
|
@ -2,15 +2,16 @@ use obd2::commands::Obd2DataRetrieval;
|
||||||
|
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), obd2::Error> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let mut device: obd2::Obd2<obd2::device::Elm327> = obd2::Obd2::default();
|
let mut device: obd2::Obd2<obd2::device::Elm327<obd2::device::FTDIDevice>> =
|
||||||
|
obd2::Obd2::new(obd2::device::Elm327::new(obd2::device::FTDIDevice::new()?)?)?;
|
||||||
|
|
||||||
println!("VIN: {:?}", device.get_vin());
|
println!("VIN: {:?}", device.get_vin());
|
||||||
for s in device.get_service_1_pid_support_1().unwrap().iter() {
|
for s in device.get_service_1_pid_support_1()?.iter() {
|
||||||
println!("PID support ($01-$20): {:08X}", s);
|
println!("PID support ($01-$20): {:08X}", s);
|
||||||
}
|
}
|
||||||
for s in device.get_service_1_pid_support_2().unwrap().iter() {
|
for s in device.get_service_1_pid_support_2()?.iter() {
|
||||||
println!("PID support ($21-$40): {:08X}", s);
|
println!("PID support ($21-$40): {:08X}", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,4 +48,6 @@ fn main() {
|
||||||
device.get_throttle_position()
|
device.get_throttle_position()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -220,7 +220,4 @@ func! {
|
||||||
|
|
||||||
/// Get service 1 PID support for $21 to $40
|
/// Get service 1 PID support for $21 to $40
|
||||||
fn get_service_1_pid_support_2(0x01, 0x20) -> u32;
|
fn get_service_1_pid_support_2(0x01, 0x20) -> u32;
|
||||||
|
|
||||||
// Get the fuel level (out of 255)
|
|
||||||
fn get_fuel_level(0x01, 0x2F) -> u8;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +1,24 @@
|
||||||
use log::{debug, info, trace};
|
use log::{debug, info, trace};
|
||||||
use std::{
|
use std::{collections::VecDeque, thread, time};
|
||||||
collections::VecDeque,
|
|
||||||
io::{Read, Write},
|
|
||||||
thread, time,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{Error, Obd2BaseDevice, Obd2Reader, Result};
|
use super::{serial_comm::SerialComm, Error, Obd2BaseDevice, Obd2Reader, Result};
|
||||||
|
|
||||||
/// 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 {
|
pub struct Elm327<T: SerialComm> {
|
||||||
device: ftdi::Device,
|
device: T,
|
||||||
buffer: VecDeque<u8>,
|
buffer: VecDeque<u8>,
|
||||||
baud_rate: u32,
|
baud_rate: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Elm327 {
|
impl<T: SerialComm> Obd2BaseDevice for Elm327<T> {
|
||||||
/// Create a Elm327 device
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
/// If the device cannot be initialized. Use [Self::new] for a panic-free API.
|
|
||||||
fn default() -> Self {
|
|
||||||
Elm327::new().unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Obd2BaseDevice for Elm327 {
|
|
||||||
fn reset(&mut self) -> Result<()> {
|
fn reset(&mut self) -> Result<()> {
|
||||||
self.flush_buffers()?;
|
self.flush_buffers()?;
|
||||||
self.reset_ic()?;
|
self.reset_ic()?;
|
||||||
|
@ -52,7 +38,7 @@ impl Obd2BaseDevice for Elm327 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Obd2Reader for Elm327 {
|
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)
|
||||||
}
|
}
|
||||||
|
@ -68,22 +54,14 @@ impl Obd2Reader for Elm327 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Elm327 {
|
impl<T: SerialComm> Elm327<T> {
|
||||||
fn new() -> Result<Self> {
|
/// Creates a new Elm327 adapter with the given
|
||||||
let mut ftdi_device = ftdi::find_by_vid_pid(0x0403, 0x6001)
|
/// unserlying Serial Communication device
|
||||||
.interface(ftdi::Interface::A)
|
pub fn new(serial_device: T) -> Result<Self> {
|
||||||
.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()?;
|
|
||||||
|
|
||||||
let mut device = Elm327 {
|
let mut device = Elm327 {
|
||||||
device: ftdi_device,
|
device: serial_device,
|
||||||
buffer: VecDeque::new(),
|
buffer: VecDeque::new(),
|
||||||
baud_rate: 38400,
|
baud_rate: 38_400,
|
||||||
};
|
};
|
||||||
|
|
||||||
device.connect(false)?;
|
device.connect(false)?;
|
||||||
|
@ -102,7 +80,7 @@ impl Elm327 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush_buffers(&mut self) -> Result<()> {
|
fn flush_buffers(&mut self) -> Result<()> {
|
||||||
self.device.usb_purge_buffers()?;
|
self.device.purge_buffers()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,11 +105,10 @@ impl Elm327 {
|
||||||
fn reset_ic(&mut self) -> Result<()> {
|
fn reset_ic(&mut self) -> Result<()> {
|
||||||
info!("Performing IC reset");
|
info!("Performing IC reset");
|
||||||
self.send_serial_str("ATZ")?;
|
self.send_serial_str("ATZ")?;
|
||||||
|
let response = self.get_response()?;
|
||||||
debug!(
|
debug!(
|
||||||
"reset_ic: got response {:?}",
|
"reset_ic: got response {:?}",
|
||||||
self.get_response()?
|
response.as_ref().map(|l| std::str::from_utf8(l.as_slice()))
|
||||||
.as_ref()
|
|
||||||
.map(|l| std::str::from_utf8(l.as_slice()))
|
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -140,16 +117,12 @@ impl Elm327 {
|
||||||
info!("Performing protocol reset");
|
info!("Performing protocol reset");
|
||||||
|
|
||||||
// set to use automatic protocol selection
|
// set to use automatic protocol selection
|
||||||
debug!(
|
let elm_response = self.serial_cmd("ATSP0")?;
|
||||||
"reset_protocol: got response {:?}",
|
debug!("reset_protocol: got response {:?}", elm_response);
|
||||||
self.serial_cmd("ATSP0")?
|
|
||||||
);
|
|
||||||
|
|
||||||
// perform the search
|
// perform the search for ECUs
|
||||||
debug!(
|
let obd_response = self.cmd(&[0x01, 0x00])?;
|
||||||
"reset_protocol: got OBD response {:?}",
|
debug!("reset_protocol: got OBD response {:?}", obd_response);
|
||||||
self.cmd(&[0x01, 0x00])?
|
|
||||||
);
|
|
||||||
|
|
||||||
// get rid of extra data hanging around in the buffer
|
// get rid of extra data hanging around in the buffer
|
||||||
self.flush_buffers()?;
|
self.flush_buffers()?;
|
||||||
|
@ -269,13 +242,18 @@ impl Elm327 {
|
||||||
fn read_into_queue(&mut self) -> Result<()> {
|
fn read_into_queue(&mut self) -> Result<()> {
|
||||||
let mut buf = [0u8; 16];
|
let mut buf = [0u8; 16];
|
||||||
loop {
|
loop {
|
||||||
let len = self.device.read(&mut buf)?;
|
let len_res = self.device.read(&mut buf);
|
||||||
if len > 0 {
|
if let Ok(len) = len_res {
|
||||||
self.buffer.extend(&buf[0..len]);
|
if len > 0 {
|
||||||
trace!(
|
self.buffer.extend(&buf[0..len]);
|
||||||
"read_into_queue: values {:?}",
|
trace!(
|
||||||
std::str::from_utf8(&buf[0..len])
|
"read_into_queue: values {:?}",
|
||||||
);
|
std::str::from_utf8(&buf[0..len])
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
trace!("read_into_queue: no values left to read");
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
trace!("read_into_queue: no values left to read");
|
trace!("read_into_queue: no values left to read");
|
||||||
break;
|
break;
|
||||||
|
|
42
src/device/ftdi_comm.rs
Normal file
42
src/device/ftdi_comm.rs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
use super::serial_comm::{SerialComm, DEFAULT_BAUD_RATE};
|
||||||
|
use super::Result;
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
|
||||||
|
/// Communicate with a USB to Serial FTDI device
|
||||||
|
/// with the FTDI library
|
||||||
|
pub struct FTDIDevice {
|
||||||
|
device: ftdi::Device,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FTDIDevice {
|
||||||
|
/// Creates a new instance of an FTDIDevice
|
||||||
|
pub fn new() -> Result<Self> {
|
||||||
|
let mut device = ftdi::find_by_vid_pid(0x0404, 0x6001)
|
||||||
|
.interface(ftdi::Interface::A)
|
||||||
|
.open()?;
|
||||||
|
|
||||||
|
device.set_baud_rate(DEFAULT_BAUD_RATE)?;
|
||||||
|
device.configure(ftdi::Bits::Eight, ftdi::StopBits::One, ftdi::Parity::None)?;
|
||||||
|
device.usb_reset()?;
|
||||||
|
|
||||||
|
Ok(Self { device })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SerialComm 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()?)
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,18 @@
|
||||||
mod elm327;
|
mod elm327;
|
||||||
pub use elm327::Elm327;
|
pub use elm327::Elm327;
|
||||||
|
|
||||||
|
mod serial_comm;
|
||||||
|
|
||||||
|
#[cfg(feature = "ftdi_comm")]
|
||||||
|
mod ftdi_comm;
|
||||||
|
#[cfg(feature = "ftdi_comm")]
|
||||||
|
pub use ftdi_comm::FTDIDevice;
|
||||||
|
|
||||||
|
#[cfg(feature = "serialport_comm")]
|
||||||
|
mod serialport_comm;
|
||||||
|
#[cfg(feature = "serialport_comm")]
|
||||||
|
pub use serialport_comm::SerialPort;
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, Error>;
|
type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
/// A lower-level API for using an OBD-II device
|
/// A lower-level API for using an OBD-II device
|
||||||
|
@ -51,9 +63,15 @@ pub trait Obd2Reader {
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// An error with the underlying [FTDI device](ftdi::Device)
|
/// An error with the underlying [FTDI device](ftdi::Device)
|
||||||
|
#[cfg(feature = "ftdi_comm")]
|
||||||
#[error("FTDI error: `{0:?}`")]
|
#[error("FTDI error: `{0:?}`")]
|
||||||
Ftdi(ftdi::Error),
|
Ftdi(ftdi::Error),
|
||||||
|
|
||||||
|
/// An error with the underlying [serialport device](serialport::SerialPort)
|
||||||
|
#[cfg(feature = "serialport_comm")]
|
||||||
|
#[error("Serialport error: `{0:?}`")]
|
||||||
|
Serialport(serialport::Error),
|
||||||
|
|
||||||
/// An I/O error in a low-level [std::io] stream operation
|
/// An I/O error in a low-level [std::io] stream operation
|
||||||
#[error("IO error: `{0:?}`")]
|
#[error("IO error: `{0:?}`")]
|
||||||
IO(std::io::Error),
|
IO(std::io::Error),
|
||||||
|
@ -63,12 +81,20 @@ pub enum Error {
|
||||||
Communication(String),
|
Communication(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "ftdi_comm")]
|
||||||
impl From<ftdi::Error> for Error {
|
impl From<ftdi::Error> for Error {
|
||||||
fn from(e: ftdi::Error) -> Self {
|
fn from(e: ftdi::Error) -> Self {
|
||||||
Error::Ftdi(e)
|
Error::Ftdi(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "serialport_comm")]
|
||||||
|
impl From<serialport::Error> for Error {
|
||||||
|
fn from(e: serialport::Error) -> Self {
|
||||||
|
Error::Serialport(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<std::io::Error> for Error {
|
impl From<std::io::Error> for Error {
|
||||||
fn from(e: std::io::Error) -> Self {
|
fn from(e: std::io::Error) -> Self {
|
||||||
Error::IO(e)
|
Error::IO(e)
|
||||||
|
|
11
src/device/serial_comm.rs
Normal file
11
src/device/serial_comm.rs
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
use super::Result;
|
||||||
|
|
||||||
|
pub const DEFAULT_BAUD_RATE: u32 = 38_400;
|
||||||
|
|
||||||
|
/// An API to communicate with a serial device
|
||||||
|
pub trait SerialComm {
|
||||||
|
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<()>;
|
||||||
|
}
|
46
src/device/serialport_comm.rs
Normal file
46
src/device/serialport_comm.rs
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
use super::serial_comm::{SerialComm, DEFAULT_BAUD_RATE};
|
||||||
|
use super::Result;
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
/// Communicate with a serial device using the
|
||||||
|
/// serialport library
|
||||||
|
///
|
||||||
|
/// /dev/tty* or similar on unix-like systems
|
||||||
|
/// COM devices on Windows systems
|
||||||
|
pub struct SerialPort {
|
||||||
|
device: Box<dyn serialport::SerialPort>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SerialPort {
|
||||||
|
/// Creates a new instance of a SerialPort
|
||||||
|
pub fn new(path: &str) -> Result<Self> {
|
||||||
|
let device = serialport::new(path, DEFAULT_BAUD_RATE)
|
||||||
|
.timeout(Duration::from_millis(10))
|
||||||
|
.parity(serialport::Parity::None)
|
||||||
|
.data_bits(serialport::DataBits::Eight)
|
||||||
|
.stop_bits(serialport::StopBits::One)
|
||||||
|
.path(path)
|
||||||
|
.open()?;
|
||||||
|
|
||||||
|
Ok(Self { device })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SerialComm 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)?)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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(crate::device::Error);
|
pub struct DeviceError(crate::device::Error);
|
||||||
|
|
||||||
|
|
|
@ -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
|
/// Wraps an implementer of [Obd2BaseDevice] to allow for higher-level usage of the OBD-II
|
||||||
/// interface.
|
/// interface.
|
||||||
#[derive(Default)]
|
|
||||||
pub struct Obd2<T: Obd2BaseDevice> {
|
pub struct Obd2<T: Obd2BaseDevice> {
|
||||||
device: T,
|
device: T,
|
||||||
}
|
}
|
||||||
|
@ -43,6 +42,18 @@ impl<T: Obd2BaseDevice> Obd2Device for Obd2<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Obd2BaseDevice> 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>>> {
|
fn command(&mut self, command: &[u8]) -> Result<Vec<Vec<u8>>> {
|
||||||
let response = self
|
let response = self
|
||||||
.device
|
.device
|
||||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -6,14 +6,20 @@
|
||||||
//!
|
//!
|
||||||
//! # Usage
|
//! # Usage
|
||||||
//! ```
|
//! ```
|
||||||
//! use obd2::{commands::Obd2DataRetrieval, device::Elm327, Obd2};
|
//! use obd2::{commands::Obd2DataRetrieval, device::{Elm327, FTDIDevice}, Obd2};
|
||||||
//!
|
//!
|
||||||
//! fn main() -> Result<(), obd2::Error> {
|
//! fn main() -> Result<(), obd2::Error> {
|
||||||
//! let mut device = Obd2::<Elm327>::default();
|
//! let mut device = Obd2::<Elm327::<FTDIDevice>>::new(Elm327::new(FTDIDevice::new()?)?)?;
|
||||||
//! println!("VIN: {}", device.get_vin()?);
|
//! println!("VIN: {}", device.get_vin()?);
|
||||||
//! Ok(())
|
//! Ok(())
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
//!
|
||||||
|
//! alternatively, you could use a serial port provided by your operating system such as
|
||||||
|
//! /dev/ttyUSB0 on unix-like systems
|
||||||
|
//! ```
|
||||||
|
//! let mut device = Obd2::<Elm327::<SerialPort>>::new(Elm327::new(SerialPort::new("/dev/ttyUSB0")?)?)?;
|
||||||
|
//! ```
|
||||||
|
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
#![warn(missing_docs, clippy::panic)]
|
#![warn(missing_docs, clippy::panic)]
|
||||||
|
@ -22,7 +28,7 @@ pub mod commands;
|
||||||
|
|
||||||
pub mod device;
|
pub mod device;
|
||||||
|
|
||||||
mod error;
|
pub mod error;
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
use error::Result;
|
use error::Result;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue