Package: can-decoder
CAN Decoder - DBC decode CAN Bus DataThis package lets you DBC decode raw CAN data from theCANedgeto human-readable form (physical values) in Python.Key features1. Easily decode raw CAN bus data via DBC files
2. Support for regular CAN, OBD2 and J1939
3. Very fast data conversion and minimal external dependencies
4. Output to pandas dataframe for easy use of the data
5. Conversion can be done iteratively (from iterator) or in bulk (from DataFrame) 
6. Can be used together with our mdf_iter and canedge_browserInstallationUse pip to install thecan_decodermodule:pip install can_decoderOptionally installcanmatrixandpandasto load DBC files and enable conversion of pandas dataframes:pip install canmatrix pandasDependenciesnumpy(required)canmatrix(optional)pandas(optional)Module usage exampleBelow we load a log file viamdf_iterand usecan_decoderto DBC decode it:import can_decoder
import mdf_iter

mdf_path = "00000001.MF4"
dbc_path = "j1939.dbc"

db = can_decoder.load_dbc(dbc_path)
df_decoder = can_decoder.DataFrameDecoder(db)

with open(mdf_path, "rb") as handle:
    mdf_file = mdf_iter.MdfFile(handle)
    df_raw = mdf_file.get_data_frame()

df_phys = df_decoder.decode_frame(df_raw)
print(df_phys)Further examples are included in the repo.DocumentationSupplying decoding rulesData decoding is based on a set of signals which can be grouped together in frames. The frames in turn are grouped together in a single database. The list of rules can be crafted by hand, using the primitivesSignal,FrameandSignalDB- or generated from a DBC file.From a DBC fileIfcanmatrixis installed, the library can load the conversion rules from a DBC file:db = can_decoder.load_dbc(dbc_path)By default, the output will distinguish signals by the signal name (e.g. EngineSpeed). It is possible to switch from the primary signal name to another signal attribute in the DBC file by supplying the optionaluse_custom_attributekeyword. This takes the form of a string, and can e.g. be used to select SPNs instead of signal names in a J1939 DBC file. If no valid attribute is found, the signal name is used instead.db = can_decoder.load_dbc(dbc_path, use_custom_attribute="SPN")Data conversionThe library supports two methods of decoding data:IterativelyBulkData conversion (iterator)For iterative decoding (frame-by-frame), the library uses theIteratorDecoderclass. This class takes a set of conversion rules (e.g. from a DBC file) and an iterable object (e.g. a MDF file):decoder = can_decoder.IteratorDecoder(mdf_file, db)

for record in decoder:
    ...This method expects an iterator structure like that ofmdf_iter- incl. the following fields:ID- integer specifying the 11 or 29 bit CAN IDIDE- boolean specifying if the record uses a regular 11 bit ID or an extended 29 bit IDDataBytes- A bytearray, in the order the data bytes appear on the CAN bus.TimeStamp- A floating point number, representing seconds passed since epochIn the case multiple signals are defined from a single ID, the library iterator will queue them internally, deferring the request for more data until all signals have been consumed from the iterator.The output is of the formdecoded_signal, which is anamedtuplewith the following fields:TimeStamp- timestamp of the record as regular Python datetimeCanID- CAN ID from the sending frameSignal- name of the decoded signalSignalValueRaw- raw value of the decoded signalSignalValuePhysical- physical value of the decoded signalData conversion (DataFrame)For batch conversion of messages, the library uses theDataFrameDecoderclass. This is constructed with the conversion rules as a parameter and can be re-used several times from the same set of parameters:df_decoder = can_decoder.DataFrameDecoder(db)

df_phys_1 = df_decoder.decode_frame(df_raw_1)
df_phys_2 = df_decoder.decode_frame(df_raw_2)The data supplied should be similar to that of the iterator method, but as a DataFrame. See also the initial example. Unlike the iterator component, this method does not require the presence of a time stamp entry. Instead, the index of the DataFrame passed to the decoder will be used as the index in the resulting DataFrame.The output is a dataframe with the same index as the input dataframe, containing decoded results for the frames matched by the loaded DBC file.DataFrame output columnsThe available signals in the output depends on the type of conversion. For generic CAN data (incl. OBD2), the following output columns are included:CAN ID- CAN ID of the frame, with the extended flag set as the most significant bitSignal- signal name stringRaw Value- the raw value used as input in the decodingPhysical Value- the physical value (after scaling and offset correction)When decoding data using a J1939 DBC, the output includes the following extra columns:PGN- the PGN of the CAN frameSource Address- the source of the dataSignal- the signal nameTo remove columns from the output you can use the keywordcolumns_to_drop:df_phys = df_decoder.decode_frame(df_raw, columns_to_drop=["CAN ID", "Raw Value"])