Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer wrapping around rather than negative value #25

Open
JulianWgs opened this issue Oct 17, 2020 · 1 comment
Open

Integer wrapping around rather than negative value #25

JulianWgs opened this issue Oct 17, 2020 · 1 comment

Comments

@JulianWgs
Copy link

A minimal dbc-file:

VERSION "A version string"

BA_ "SingleFrame" BO_ 2364539904 0;

BO_ 643 Message: 8 Vector__XXX
 SG_ Signal : 32|16@1- (1,0) [0|0] "" Vector__XXX

Rust code:

extern crate canparse;
// use std::env;

use canparse::pgn::{PgnLibrary, SpnDefinition, ParseMessage};

fn main() {
    let dbc_filename = "test.dbc";
    let msg_name = "Signal";

    // Parse dbc file into PgnLibrary
    let lib = PgnLibrary::from_dbc_file(dbc_filename).unwrap();

    // Pull signal definition for engine speed
    let enginespeed_def: &SpnDefinition = lib
        .get_spn(msg_name).unwrap();

    // Parse frame containing engine speed
    let msg: [u8; 8] = [0x00, 0x01, 0x00, 0x00, 0xcb, 0xff, 0x02, 0x00];
    let engine_speed = enginespeed_def.parse_message(&msg).unwrap();

    println!("Engine speed: {}", engine_speed);
}

Output:

Engine speed: 65483

But the correct value should be 65483 - 2**16=-53, which cantools correctly decodes.

@jmagnuson
Copy link
Owner

Ugh, thanks. I should really write more unit tests 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants