How to use the cantools.db.load_file 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_string_attribute_definition_dump(self):
        filename = os.path.join('tests', 'files', 'test_multiplex_dump.dbc')
        db = cantools.db.load_file(filename)
        dumped_db = cantools.db.load_string(db.as_dbc_string())
        attribute = dumped_db.dbc.attribute_definitions

        self.assertEqual(attribute['BusType'].type_name, "STRING")
github eerimoq / cantools / tests / test_database.py View on Github external
def test_the_homer_encode_decode_signed(self):
        filename = os.path.join('tests', 'files', 'the_homer.kcd')
        db = cantools.db.load_file(filename)

        datas = [
            (
                {
                    'TankLevel': 0,
                    'TankTemperature': -32768,
                    'FillingStatus': 0
                },
                b'\x00\x00\x00\x80\x00'
            ),
            (
                {
                    'TankLevel': 65535,
                    'TankTemperature': 32767,
                    'FillingStatus': 15
                },
github eerimoq / cantools / tests / test_database.py View on Github external
def test_get_bus_by_name(self):
        filename = os.path.join('tests', 'files', 'the_homer.kcd')
        db = cantools.db.load_file(filename)

        self.assertIs(db.get_bus_by_name('Comfort'), db.buses[2])

        with self.assertRaises(KeyError) as cm:
            db.get_bus_by_name('Missing')

        self.assertEqual(str(cm.exception), "'Missing'")
github eerimoq / cantools / tests / test_database.py View on Github external
def test_get_signal_by_name(self):
        filename = os.path.join('tests', 'files', 'foobar.dbc')
        db = cantools.db.load_file(filename)

        message = db.get_message_by_name('Foo')

        signal = message.get_signal_by_name('Foo')
        self.assertEqual(signal.name, 'Foo')

        signal = message.get_signal_by_name('Bar')
        self.assertEqual(signal.name, 'Bar')

        with self.assertRaises(KeyError) as cm:
            message.get_signal_by_name('Fum')

        self.assertEqual(str(cm.exception), "'Fum'")
github eerimoq / cantools / tests / test_database.py View on Github external
def test_multiplex_choices(self):
        filename = os.path.join('tests', 'files', 'multiplex_choices.dbc')
        db = cantools.db.load_file(filename)

        # With Multiplexor and BIT_L as strings.
        decoded_message = {
            'Multiplexor': 'MULTIPLEXOR_8',
            'BIT_C': 1, 'BIT_G': 1, 'BIT_J': 1, 'BIT_L': 'On'
        }
        encoded_message = b'\x20\x00\x8c\x01\x00\x00\x00\x00'

        message_1 = db.messages[0]

        encoded = message_1.encode(decoded_message)
        self.assertEqual(encoded, encoded_message)
        decoded = message_1.decode(encoded)
        self.assertEqual(decoded, decoded_message)

        # With Multiplexor and BIT_L as numbers.
github eerimoq / cantools / tests / test_database.py View on Github external
def test_multiplex(self):
        filename = os.path.join('tests', 'files', 'multiplex.dbc')
        db = cantools.db.load_file(filename)

        message_1 = db.messages[0]
        self.assertTrue(message_1.is_multiplexed())

        self.assertEqual(message_1.signal_tree,
                         [
                             {
                                 'Multiplexor': {
                                     8: [
                                         'BIT_J', 'BIT_C', 'BIT_G', 'BIT_L'
                                     ],
                                     16: [
                                         'BIT_J', 'BIT_C', 'BIT_G', 'BIT_L'
                                     ],
                                     24: [
                                         'BIT_J', 'BIT_C', 'BIT_G', 'BIT_L',
github eerimoq / cantools / tests / test_database.py View on Github external
def test_multiplex(self):
        filename = os.path.join('tests', 'files', 'multiplex.dbc')
        db = cantools.db.load_file(filename)

        message_1 = db.messages[0]
        self.assertTrue(message_1.is_multiplexed())

        self.assertEqual(message_1.signal_tree,
                         [
                             {
                                 'Multiplexor': {
                                     8: [
                                         'BIT_J', 'BIT_C', 'BIT_G', 'BIT_L'
                                     ],
                                     16: [
                                         'BIT_J', 'BIT_C', 'BIT_G', 'BIT_L'
                                     ],
                                     24: [
                                         'BIT_J', 'BIT_C', 'BIT_G', 'BIT_L',
github eerimoq / cantools / tests / test_database.py View on Github external
def test_timing(self):
        filename = os.path.join('tests', 'files', 'timing.dbc')
        db = cantools.db.load_file(filename)

        # Message cycle time is 200, as given by BA_.
        message = db.get_message_by_frame_id(1)
        self.assertEqual(message.cycle_time, 200)
        self.assertEqual(message.send_type, 'Cyclic')

        # Default message cycle time is 0, as given by BA_DEF_DEF_.
        message = db.get_message_by_frame_id(2)
        self.assertEqual(message.cycle_time, 0)
        self.assertEqual(message.send_type, 'NoMsgSendType')

        with open(filename, 'rb') as fin:
            self.assertEqual(db.as_dbc_string().encode('ascii'), fin.read())
github pixmoving-moveit / civic-hack / honda_controlling_scripts / scripts / set_braking.py View on Github external
#             signal('CRUISE_STATES', 38, 7, 'big_endian', False, 1, 0, 0, 1, 'None', False, None, None, None),
    #             signal('CHIME', 47, 3, 'big_endian', False, 1, 0, 0, 7, 'None', False, None, {4: 'double_chime', 3: 'single_chime', 2: 'continuous_chime', 1: 'repeating_chime', 0: 'no_chime'}, None),
    #             signal('ZEROS_BOH6', 44, 1, 'big_endian', False, 1, 0, 0, 1, 'None', False, None, None, None),
    #             signal('FCW', 43, 2, 'big_endian', False, 1, 0, 0, 3, 'None', False, None, {3: 'fcw', 2: 'fcw', 1: 'fcw', 0: 'no_fcw'}, None),
    #             signal('ZEROS_BOH4', 55, 8, 'big_endian', False, 1, 0, 0, 0, 'None', False, None, None, None),
    #             signal('COUNTER', 61, 2, 'big_endian', False, 1, 0, 0, 3, 'None', False, None, None, None),
    #             signal('CHECKSUM', 59, 4, 'big_endian', False, 1, 0, 0, 15, 'None', False, None, None, None)]}

    #                    apply_brake, pcm_override, pcm_cancel_cmd, chime, idx
    # idx refers to the COUNTER field we see
    brake_cmd = create_brake_command(True, True, True, 0, 0)
    # Looks like: [506, 0, '\x00A\x13\x80\x80\x00\x00\x05', 0]

    rp = RosPack()
    pkg_path = rp.get_path('panda_bridge_ros')
    can_msg_parser = load_dbc_file(pkg_path + '/config/' +
                                   'honda_civic_touring_2016_can_for_cantools.dbc')

    # Let's double check the brake_cmd
    decoded_brake_cmd = can_msg_parser.decode_message(brake_cmd[0],
                                                      brake_cmd[2])
    # {'BRAKE_LIGHTS': 1,
    # 'CHECKSUM': 5,
    # 'CHIME': 'no_chime',
    # 'COMPUTER_BRAKE': 1,
    # 'COMPUTER_BRAKE_REQUEST': 1,
    # 'COMPUTER_BRAKE_REQUEST_2': 1,
    # 'COUNTER': 0,
    # 'CRUISE_BOH2': 0,
    # 'CRUISE_BOH3': 0,
    # 'CRUISE_CANCEL_CMD': 1,
    # 'CRUISE_FAULT_CMD': 0,