Skip to content

Commit

Permalink
Add simple uds read data by identifier example
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelbuesing committed Aug 28, 2021
1 parent e05cd91 commit 0c329ed
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions examples/uds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! UDS (unified diagnostic protocol) example for reading a data by identifer.
//! Run the following server for testing.
//! https://github.com/zombieCraig/uds-server

use socketcan_isotp::{self, IsoTpSocket, StandardId};
use std::sync::mpsc;

fn main() -> Result<(), socketcan_isotp::Error> {
let (tx, rx) = mpsc::channel();

// Reader
let mut reader_tp_socket = IsoTpSocket::open(
"vcan0",
StandardId::new(0x7E8).expect("Invalid dst CAN ID"),
StandardId::new(0x77A).expect("Invalid src CAN ID"),
)?;
std::thread::spawn(move || loop {
let buffer = reader_tp_socket.read().expect("Failed to read from socket");
tx.send(buffer.to_vec()).expect("Receiver deallocated");
});

let tp_socket = IsoTpSocket::open(
"vcan0",
StandardId::new(0x77A).expect("Invalid src CAN ID"),
StandardId::new(0x7E0).expect("Invalid dst CAN ID"),
)?;

// 0x22 - Service Identifier for "Read data by identifier" request
// 0xF189 - Data identifer - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier
tp_socket.write(&[0x22, 0xF1, 0x89])?;

println!("Sent read data by identifier 0xF189 - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier");

loop {
let recv_buffer = rx.recv().expect("Failed to receive");
// 0x62 - Service Identifier for "Read data by identifier" response
// 0xF189 - Data identifer - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier
if recv_buffer[0..=2] != [0x62, 0xF1, 0x89] {
println!("Skipping: {:X?}", recv_buffer);
} else {
println!("Response: {:X?}", &recv_buffer[3..]);
}
}
}

0 comments on commit 0c329ed

Please sign in to comment.