-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple uds read data by identifier example
- Loading branch information
1 parent
e05cd91
commit 0c329ed
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..]); | ||
} | ||
} | ||
} |