How to use the cantools.db function in cantools

To help you get started, we’ve selected a few cantools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eerimoq / cantools / tests / test_database.py View on Github external
def test_performance_big_endian_signals(self):
        """Test encode/decode performance of a frame with big endian signals.

        """

        iterations = 10000

        signals = [
            cantools.db.Signal('S0',  7, 4,  'big_endian'),
            cantools.db.Signal('S1',  3, 4,  'big_endian'),
            cantools.db.Signal('S2', 15, 4,  'big_endian'),
            cantools.db.Signal('S3', 11, 8,  'big_endian'),
            cantools.db.Signal('S4', 19, 1,  'big_endian'),
            cantools.db.Signal('S5', 17, 17, 'big_endian'),
            cantools.db.Signal('S6', 47, 15, 'big_endian')
        ]

        message = cantools.db.Message(frame_id=1,
                                      name='M0',
                                      length=8,
                                      signals=signals)

        # Encode.
        def encode():
            message.encode({
github eerimoq / cantools / tests / test_diagnostics_database.py View on Github external
def test_example_cdd(self):
        db = cantools.db.load_file('tests/files/cdd/example.cdd',
                                   encoding='iso-8859-1')
        self.assertEqual(len(db.dids), 15)
        self.assertEqual([did.name for did in db.dids],
                         [
                             'DEFAULT_SESSION',
                             'ProgrammingSession',
                             'ECU_Identification',
                             'Development_Data',
                             'Serial_Number',
                             'REQUEST_SEED_SERVICE',
                             'SUBMIT_KEY_SERVICE',
                             'A_D_Werte',
                             'SawTooth',
                             'Sine',
                             'FaultMemory_identified',
                             'FaultMemory_supported',
github eerimoq / cantools / tests / test_tester.py View on Github external
def setup_tester(dut_name,
                 on_message=None,
                 decode_choices=False,
                 scaling=False):
    database = cantools.db.load_file('tests/files/kcd/tester.kcd')
    can_bus = CanBus()
    tester = cantools.tester.Tester(dut_name,
                                    database,
                                    can_bus,
                                    'Bus1',
                                    on_message=on_message,
                                    decode_choices=decode_choices,
                                    scaling=scaling)

    return tester, can_bus
github eerimoq / cantools / tests / test_database.py View on Github external
def test_little_endian_no_decode_choices(self):
        """Decode a little endian signal with `decode_choices` set to False.

        """

        db = cantools.db.Database()
        filename = os.path.join('tests', 'files', 'socialledge.dbc')
        db.add_dbc_file(filename)

        decoded_message = {
            'DRIVER_HEARTBEAT_cmd': 1
        }
        encoded_message = b'\x01\x00\x00\x00\x00\x00\x00\x00'

        decoded = db.decode_message(100,
                                    encoded_message,
                                    decode_choices=False)
        self.assertEqual(decoded, decoded_message)

        decoded_message = {
            'DRIVER_HEARTBEAT_cmd': 'DRIVER_HEARTBEAT_cmd_SYNC'
        }
github eerimoq / cantools / tests / test_diagnostics_database.py View on Github external
def test_cdd_add(self):
        db = cantools.db.diagnostics.Database()
        db.add_cdd_file('tests/files/cdd/example.cdd', encoding='iso-8859-1')
        self.assertEqual(len(db.dids), 15)
github eerimoq / cantools / tests / test_database.py View on Github external
def test_foobar(self):
        db = cantools.db.Database()
        filename = os.path.join('tests', 'files', 'foobar.dbc')
        db.add_dbc_file(filename)

        self.assertEqual(len(db.nodes), 4)
        self.assertEqual(db.version, '2.0')
        self.assertEqual(
            repr(db),
            "version('2.0')\n"
            "\n"
            "node('FOO', None)\n"
            "node('BAR', 'fam \"1\"')\n"
            "node('FIE', None)\n"
            "node('FUM', None)\n"
            "\n"
            "message('Foo', 0x12330, True, 8, 'Foo.')\n"
            "  signal('Foo', 0, 12, 'big_endian', True, 0.01, "
github eerimoq / cantools / tests / test_database.py View on Github external
def test_little_endian_no_decode_choices(self):
        """Decode a little endian signal with `decode_choices` set to False.

        """

        db = cantools.db.Database()
        filename = os.path.join('tests', 'files', 'socialledge.dbc')
        db.add_dbc_file(filename)

        decoded_message = {
            'DRIVER_HEARTBEAT_cmd': 1
        }
        encoded_message = b'\x01\x00\x00\x00\x00\x00\x00\x00'

        decoded = db.decode_message(100,
                                    encoded_message,
                                    decode_choices=False)
        self.assertEqual(decoded, decoded_message)

        decoded_message = {
            'DRIVER_HEARTBEAT_cmd': 'DRIVER_HEARTBEAT_cmd_SYNC'
        }
github pixmoving-moveit / civic-hack / honda_controlling_scripts / scripts / can_listener_node_civic.py View on Github external
def __init__(self):
        rospy.loginfo("Initializing TwistFromCan")
        rp = RosPack()
        pkg_path = rp.get_path('panda_bridge_ros')
        path_to_dbc = pkg_path + '/config/honda_civic_touring_2016_can_for_cantools.dbc'
        self.can_db = cantools.db.load_file(path_to_dbc)
        self.wheel_speed = None
        self.wheel_angle = None
        self.sub = rospy.Subscriber("can_frame_msgs", Frame,
                                    self.callback, queue_size=10)
        self.pub = rospy.Publisher("twist_stamped",
                                   TwistStamped, queue_size=10)
        rospy.loginfo("Done")
github eerimoq / cantools / examples / diagnostics / did.py View on Github external
from __future__ import print_function
import os
from binascii import hexlify
import cantools


SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
EXAMPLE_PATH = os.path.join(SCRIPT_DIR,
                            '..',
                            '..',
                            'tests',
                            'files',
                            'cdd',
                            'example.cdd')

database = cantools.db.load_file(EXAMPLE_PATH)

data = {
    'Country_variant': 'Europe',
    'Vehicle_type': 'Sedan',
    'Special_setting': 3
}

did = database.get_did_by_name('Coding')
encoded = did.encode(data)
decoded = did.decode(encoded)

print('Data:   ', data)
print('Encoded:', hexlify(encoded).decode('ascii'))
print('Decoded:', decoded)