#dbc #can #automotive #ecu

can-dbc

A parser for the DBC format. The DBC format is used to exchange CAN network data.

15 stable releases (4 major)

5.0.0 Jun 22, 2022
4.0.0 Nov 15, 2021
3.0.2 Oct 28, 2020
3.0.1 May 17, 2020
1.0.0 Dec 9, 2018

#40 in Parser implementations

Download history 637/week @ 2022-03-17 403/week @ 2022-03-24 410/week @ 2022-03-31 398/week @ 2022-04-07 358/week @ 2022-04-14 415/week @ 2022-04-21 535/week @ 2022-04-28 708/week @ 2022-05-05 944/week @ 2022-05-12 785/week @ 2022-05-19 655/week @ 2022-05-26 853/week @ 2022-06-02 561/week @ 2022-06-09 796/week @ 2022-06-16 920/week @ 2022-06-23 698/week @ 2022-06-30

3,130 downloads per month
Used in 4 crates (3 directly)

MIT license

78KB
2K SLoC

can-dbc

LICENSE VERSION Actions Status codecov docs Cargo Deny Status

A CAN-dbc format parser written with Rust's nom parser combinator library.

1. Example

Read dbc file and generate Rust structs based on the messages/signals defined in the dbc.

use can_dbc::DBC;
use codegen::Scope;

use std::fs::File;
use std::io;
use std::io::prelude::*;

fn main() -> io::Result<()> {
    let mut f = File::open("./examples/sample.dbc")?;
    let mut buffer = Vec::new();
    f.read_to_end(&mut buffer)?;

    let dbc = can_dbc::DBC::from_slice(&buffer).expect("Failed to parse dbc file");

    let mut scope = Scope::new();
    for message in dbc.messages() {
        for signal in message.signals() {

            let mut scope = Scope::new();
            let message_struct = scope.new_struct(message.message_name());
            for signal in message.signals() {
                message_struct.field(signal.name().to_lowercase().as_str(), "f64");
            }
        }
    }

    println!("{}", scope.to_string());
    Ok(())
}

For a proper implementation for reading or writing CAN frames according to the DBC, I recommend you take a look at dbc-codegen.

2. Example

The file parser simply parses a dbc input file and prints the parsed content.

cargo test && ./target/debug/examples/file_parser -i examples/sample.dbc

Installation

can-dbc is available on crates.io and can be included in your Cargo enabled project like this:

[dependencies]
can-dbc = "3.0"

Implemented DBC parts

  • version
  • new_symbols
  • bit_timing (deprecated but mandatory)
  • nodes
  • value_tables
  • messages
  • message_transmitters
  • environment_variables
  • environment_variables_data
  • signal_types
  • comments
  • attribute_definitions
  • sigtype_attr_list (format missing documentation)
  • attribute_defaults
  • attribute_values
  • value_descriptions
  • category_definitions (deprecated)
  • categories (deprecated)
  • filter (deprecated)
  • signal_type_refs
  • signal_groups
  • signal_extended_value_type_list

Deviating from standard

  • multispace between parsers instead of single space allowing e.g. (two spaces) SIG_GROUP 13.
  • VAL_ suffix may be ; or ;

Alternatives

Credits

Test dbcs files were copied from the cantools project.

License Checks

This project uses cargo-deny for checking the licenses of dependencies. To run the check locally run the following:

cargo install cargo-deny
cargo deny check

Dependencies

~1–1.6MB
~34K SLoC