Expand description
§can-dbc
A CAN-dbc format parser written with Rust’s Pest grammar library. CAN databases are used to exchange details about a CAN network, e.g. what messages are being send over the CAN bus and what data do they contain.
§Usage example
Read dbc file and generate Rust structs based on the messages/signals defined in the dbc.
use std::fs;
use can_dbc::Dbc;
use codegen::Scope;
fn main() {
let data = fs::read_to_string("./examples/sample.dbc").expect("Unable to read input file");
let dbc = Dbc::try_from(data.as_str()).expect("Failed to parse dbc file");
let mut scope = Scope::new();
for msg in dbc.messages {
let msg_struct = scope.new_struct(&msg.name);
for signal in msg.signals {
msg_struct.field(&signal.name.to_lowercase(), "f64");
}
}
println!("{}", scope.to_string());
}For a proper implementation for reading or writing CAN frames according to the DBC, I recommend you take a look at dbc-codegen.
§Running Parser Example
The file parser simply parses a dbc input file and prints the parsed content.
cargo run --example file_parser -- --input examples/sample.dbc§Structure
§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.
§Development
- This project is easier to develop with just, a modern alternative to
make. - To get a list of available commands, run
just. - To run tests, use
just test. - This project uses insta for snapshot testing. To update the snapshots run
just bless
§License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT) at your option.
§Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual-licensed as above, without any additional terms or conditions.
Re-exports§
pub use encoding_rs as encodings;
Structs§
- Attr
Default - Attribute
Default - Attribute
Value ForDatabase - Attribute
Value ForEnv Variable - Attribute
Value ForMessage - Attribute
Value ForNode - Attribute
Value ForRelation - An attribute attached to the relation between a node and a signal
- Attribute
Value ForSignal - Baudrate
- Baudrate of network in KBit/s
- Dbc
- Environment
Variable - Environment
Variable Data - Extended
Multiplex - Mapping between multiplexors and multiplexed signals
- Extended
Multiplex Mapping - Message
- CAN message (frame) details including signal details
- Message
Transmitter - Node
- CAN network nodes, names must be unique
- Signal
- One or multiple signals are the payload of a CAN frame.
To determine the actual value of a signal the following fn applies:
let value = |can_signal_value| can_signal_value * factor + offset; - Signal
Extended Value Type List - Signal
Groups - Signal groups define a group of signals within a message
- Signal
Type - Signal
Type Ref - Symbol
- ValDescription
- Value
Table - Global value table
- Version
- Version identifier of the DBC file.
Enums§
- Access
Node - Access
Type - Attribute
Definition - Attribute
Value - Attribute
Value ForRelation Type - Attribute
Value Type - Byte
Order - Comment
- Object comments
- DbcError
- Error type for DBC parsing operations
- EnvType
env_var_type = '0' | '1' | '2' ; (* 0=integer, 1=float, 2=string *)- Message
Id - CAN id in header of CAN frame. Must be unique in DBC file.
- Multiplex
Indicator - Numeric
Value - Signal
Attribute Value - Signal
Extended Value Type - Transmitter
- Value
Description - Encoding for signal raw values.
- Value
Type
Functions§
- decode_
cp1252 - A helper function to decode cp1252 bytes, as DBC files are often encoded in cp1252.