Code Monkey home page Code Monkey logo

j2534-rs's Introduction

J2534

Low-level SAE J2534 (PassThru) support for rust.

J2534 Introduction

SAE J2534 PassThru defines a standard library interface for communicating with vehicle control modules. All automakers in the US are required to provide J2534-compatible service software. J2534 provides access to the communication layer required for accessing vehicle diagnostics services as well as downloading and reflashing control modules.

Usage

The J2534 specification requires PassThru libraries to be compiled for 32 bit, so any program using this crate must be compiled as 32 bit.

Example

use j2534::{Interface, PassThruMsg, Protocol, ConnectFlags, RxStatus, TxFlags};

fn main() -> j2534::Result<()> {
    // Open the library and connect to a device
    let interface = Interface::new("C:\\device.dll")?;
    let device = interface.open_any()?;
    
    // Create a CAN channel
    let channel = device
        .connect(Protocol::CAN, ConnectFlags::NONE, 500000)
        .unwrap();
    // Create a new message with an arbitration id of `8` and payload of `[0, 1, 2, 3]`.
    let message = PassThruMsg::new_can(8, &[0, 1, 2, 3]);
    channel.write(&mut [message], 1000)?;
    Ok(())
}

j2534-rs's People

Contributors

const-volatile avatar jcmnn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

j2534-rs's Issues

Read Timeout Error

When using the channel.read function, the function will return a timeout error if the size of the buffer is larger than the number of actually received messages.

For instance, if the size of the buffer is 32, and 2 messages are received, eventually the function will wait timeout miliseconds and then return back a timeout error while waiting for 30 more messages.

Recommend handling the timeout error such that the buffer does not need to be <= the number of expected response messages.

Is J2534 the same for all cars?

What kind of cable is needed to read J2534? I'm aware of OBD-II to DB9, OBD-II to DB25, OBD-II to USB, OBD-II to Ethernet.

Also, are there protection methods at play that prevent J2534 over CAN access in the wild?

Access Violation due to incorrect calling convention being used (__cdecl instead of __stdcall)

J2534 uses the WinAPI convention. It is defined in Windows header files as WINAPI, which is in turn defined using the Win32 declarator __stdcall. To make this crate work correctly the function pointer definitions in "j2534-0.1.2\src\cpp\j2534.cpp" need to be modified accordingly.

i.e. typedef int32_t (*PassThruOpen_t)(void *, uint32_t *); need to be changed to
typedef int32_t (WINAPI *PassThruOpen_t)(void *, uint32_t *);

Use libloading

Use libloading for loading PassThru DLLs instead of a C wrapper.

Unaligned References Complier Error [E0793]

Seems like the latest Rust complier does not like how the references in impl Debug for PassThruMsg are unaligned.

Here's an error for &self.protocol_id specifically:

error[E0793]: reference to packed field is unaligned
   --> <redacted>\.cargo\registry\src\index.crates.io-1cd66030c949c28d\j2534-0.3.1\src\lib.rs:377:39
    |
377 |                 .field("protocol_id", &self.protocol_id)
    |                                       ^^^^^^^^^^^^^^^^^
    |
    = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
    = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
    = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

I was able to clear the compiler error by changing this code:

impl Debug for PassThruMsg {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        unsafe {
            f.debug_struct("PassThruMsg")
                .field("protocol_id", &self.protocol_id)
                .field("rx_status", &self.rx_status)
                .field("tx_flags", &self.tx_flags)
                .field("timestamp", &self.timestamp)
                .field("extra_data_index", &self.extra_data_index)
                .field("data", &&self.data[..self.data_size as usize])
                .finish()
        }
    }
}

to:

impl Debug for PassThruMsg {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let protocol_id = self.protocol_id;
        let rx_status = self.rx_status;
        let tx_flags = self.tx_flags;
        let timestamp = self.timestamp;
        let extra_data_index = self.extra_data_index;
        unsafe {
            f.debug_struct("PassThruMsg")
                .field("protocol_id", &protocol_id)
                .field("rx_status", &rx_status)
                .field("tx_flags", &tx_flags)
                .field("timestamp", &timestamp)
                .field("extra_data_index", &extra_data_index)
                .field("data", &&self.data[..self.data_size as usize])
                .finish()
        }
    }
}

Use extern "stdcall" fn

Hi there,

I'm the author of OVD (https://github.com/rnd-ash/OpenVehicleDiag), and wrote a similar J2534 API based on some of your work (Except I'm also unofficially supporting UNIX / OSX).

I found that using extern "C" fn and extern "system" fn causes issues with the i686-pc-windows-msvc target (ACCESS_VIOLATION error being triggered). Changing the function calls to extern "stdcall" fn fixes the issue

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.