Using asammdf in Python to decode CAN frames

1.6k views Asked by At

I might be misunderstanding something about asammdf in general, but I'm not really sure how to go about decoding a CAN frame.

Supposing I have a dbc with relevant messages defined, I get that I can use mdf.extract_bus_logging() to parse the signals, but I'm not really sure what to do from there.

I'm sending a raw payload + can frame ID, and I think I could get away with using cantools to parse the raw data (and then passing that data into an asammdf signal), but it feels like there's some degree of support here within asammdf.

I have an example here along the lines of what I want to do (using this motohawk.dbc file from the cantools examples here https://cantools.readthedocs.io/en/latest/)

from asammdf import MDF
import cantools

filename = "~/mdf_dbc_play/tmp.dbc"
db = cantools.database.load_file(filename)

msg_bytes = [0,0,0xff,0x01,0x02,0x03,0x04,0x05]
# Temporary bogus timestamp
timestamps = [0.1]

# Use cantools to decode the raw bytes
msg = db.get_message_by_name("EEC1")
unpacked_signals = msg.decode(msg_bytes)

# Using version 3.10 just in case we have some versioning issues with old tooling.

with asammdf.MDF(version='3.10') as mdf:
    # I assume there's some way of getting the actual value we care about from an unpacked message.
    samples = unpacked_signals.get_values()

    sig = asammdf.Signal(name="EEC1", samples=samples, timestamps=timestamps)
    mdf.append(sig)
    mdf.save("~/mdf_dbc_play/output.mdf")


1

There are 1 answers

0
danielhrisca On

For logging CAN traffic to an MF4 file you can try this code from python-can

https://github.com/hardbyte/python-can/blob/mf4-io/can/io/mf4.py