Python cantools create dbc-file

Viewed 1534

I have little experience with can and cantools, so the answer to my question might be rather simple.

I would like to create my own dbc-file by using python cantools in order to build my own CAN-Sensor which I can connect to a CAN-Network. I would like to provide the appropriate dbc-file to the receiver on the network.

I tried the following:

import cantools

can_id = 0x100

can_db = cantools.db

can_db.Signal(name="Sig",
                start=0,
                length=8,
                byte_order="little_endian",
                is_signed=False,
                initial=None,
                scale=1,
                offset=0,
                minimum=None,
                maximum=None,
                unit="m/s2",
                comment="Comment",
                is_multiplexer=False,
                is_float=False,
                decimal=None)

can_db.Message(frame_id=can_id,
                 name="CAN_Message_Name",
                 length=1,
                 signals=["Sig"],
                 comment="Message_Comment",
                 is_extended_frame=False,
                 bus_name=None)

Somehow the signal object is not a can-database-type. I get the following error message in message.py:

   if data.byte_order == 'big_endian':
AttributeError: 'str' object has no attribute 'byte_order'

How do I declare the signal and the message structure? Where do I define the node and the can bitrate?

1 Answers

It appears you're trying to pass the literal string `"Sig" instead of your signal.

Try the following

import cantools

can_id = 0x100

can_db = cantools.db

sig = can_db.Signal(name="Sig",
            start=0,
            length=8,
            byte_order="little_endian",
            is_signed=False,
            initial=None,
            scale=1,
            offset=0,
            minimum=None,
            maximum=None,
            unit="m/s2",
            comment="Comment",
            is_multiplexer=False,
            is_float=False,
            decimal=None)

can_db.Message(frame_id=can_id,
             name="CAN_Message_Name",
             length=1,
             signals=[sig],
             comment="Message_Comment",
             is_extended_frame=False,
             bus_name=None)
Related