How to use the cantools.database.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_message_layout(self):
        filename = os.path.join('tests', 'files', 'message_layout.kcd')
        db = cantools.database.load_file(filename, strict=False)

        # Message 1.
        expected_lines = [
            '                      Bit',
            '',
            '         7   6   5   4   3   2   1   0',
            '       +---+---+---+---+---+---+---+---+',
            ' B',
            ' y',
            ' t',
            ' e'
        ]

        actual = db.get_message_by_name('Message1').layout_string()
        self.assertEqual(actual, '\n'.join(expected_lines))
github eerimoq / cantools / tests / test_database.py View on Github external
def test_message_layout(self):
        filename = os.path.join('tests', 'files', 'message_layout.kcd')
        db = cantools.database.load_file(filename, strict=False)

        # Message 1.
        expected_lines = [
            '                      Bit',
            '',
            '         7   6   5   4   3   2   1   0',
            '       +---+---+---+---+---+---+---+---+',
            ' B',
            ' y',
            ' t',
            ' e'
        ]

        actual = db.get_message_by_name('Message1').layout_string()
        self.assertEqual(actual, '\n'.join(expected_lines))
github eerimoq / cantools / tests / test_database.py View on Github external
def test_the_homer_encode_decode_choice_scaling(self):
        """Verify a label/enum matches the raw value, and is not affected by
        scaling.

        """

        filename = os.path.join('tests', 'files', 'the_homer.kcd')
        db = cantools.database.load_file(filename)

        messages = [
            (0x900, {'EnumTest': 'one'}, b'\x80\x00\x00\x00\x00\x00\x00\x00'),
            (0x900, {'EnumTest': 'two'}, b'\xff\x00\x00\x00\x00\x00\x00\x00'),
            (0x901, {'EnumTestFloat': 'one'}, b'\x00\x00\x00\x43\x00\x00\x00\x00'),
            (0x901, {'EnumTestFloat': 'two'}, b'\x00\x00\x7f\x43\x00\x00\x00\x00'),

            # Verify encode/decode using int/float to verify scaling
            # still works.
            (0x900, {'EnumTest': 4}, b'\x02\x00\x00\x00\x00\x00\x00\x00'),
            (0x901, {'EnumTestFloat': 4}, b'\x00\x00\x00\x40\x00\x00\x00\x00'),
            (0x900, {'EnumTest': 4.0}, b'\x02\x00\x00\x00\x00\x00\x00\x00'),
            (0x901, {'EnumTestFloat': 4.0}, b'\x00\x00\x00\x40\x00\x00\x00\x00')
        ]

        for message_id, decoded_message, encoded_message in messages:
github eerimoq / cantools / tests / test_database.py View on Github external
def test_issue_63(self):
        """Test issue 63.

        """

        filename = os.path.join('tests', 'files', 'issue_63.dbc')

        with self.assertRaises(cantools.database.errors.Error) as cm:
            cantools.database.load_file(filename)

        self.assertEqual(
            str(cm.exception),
            'The signals HtrRes and MaxRes are overlapping in message '
            'AFT1PSI2.')

        db = cantools.database.load_file(filename, strict=False)
        self.assertEqual(
            db.messages[0].layout_string(),
            '                      Bit\n'
            '\n'
github eerimoq / cantools / tests / test_database.py View on Github external
def test_issue_63(self):
        """Test issue 63.

        """

        filename = os.path.join('tests', 'files', 'issue_63.dbc')

        with self.assertRaises(cantools.database.errors.Error) as cm:
            cantools.database.load_file(filename)

        self.assertEqual(
            str(cm.exception),
            'The signals HtrRes and MaxRes are overlapping in message '
            'AFT1PSI2.')

        db = cantools.database.load_file(filename, strict=False)
        self.assertEqual(
            db.messages[0].layout_string(),
            '                      Bit\n'
            '\n'
github eerimoq / cantools / tests / test_database.py View on Github external
filenames = [
            os.path.join('tests', 'files', 'bad_message_length.kcd'),
            os.path.join('tests', 'files', 'bad_message_length.dbc'),
            os.path.join('tests', 'files', 'bad_message_length.sym')
        ]

        for filename in filenames:
            # Strict true.
            with self.assertRaises(cantools.database.Error) as cm:
                cantools.database.load_file(filename, strict=True)

            self.assertEqual(str(cm.exception),
                             'The signal Signal1 does not fit in message Message1.')

            # Strict false.
            db = cantools.database.load_file(filename, strict=False)

            # Sanity checks.
            message_1 = db.get_message_by_frame_id(1)
            self.assertEqual(message_1.length, 1)
            self.assertEqual(message_1.signals[0].start, 8)
            self.assertEqual(message_1.signals[0].length, 1)
github eerimoq / cantools / tests / test_database.py View on Github external
def test_encode_signal_strict(self):
        """Test signal out of range errors.

        """

        filename = os.path.join('tests', 'files', 'signal_range.kcd')
        db = cantools.database.load_file(filename)

        # Values in range.
        datas = [
            ('Message2', 3),
            ('Message3', 0)
        ]

        for message_name, signal_value in datas:
            db.encode_message(message_name, {'Signal1': signal_value})

        # Values out of range.
        datas = [
            (
                'Message1',
                0,
                "Expected signal 'Signal1' value greater than or equal to 1 in "
github eerimoq / cantools / cantools / subparsers / monitor.py View on Github external
def __init__(self, stdscr, args):
        self._stdscr = stdscr
        self._dbase = database.load_file(args.database,
                                         encoding=args.encoding,
                                         frame_id_mask=args.frame_id_mask,
                                         strict=not args.no_strict)
        self._single_line = args.single_line
        self._filtered_sorted_message_names = []
        self._filter = ''
        self._compiled_filter = None
        self._formatted_messages = {}
        self._playing = True
        self._modified = True
        self._show_filter = False
        self._queue = Queue()
        self._nrows, self._ncols = stdscr.getmaxyx()
        self._received = 0
        self._discarded = 0
        self._basetime = None
github eerimoq / cantools / examples / database_cache.py View on Github external
def load_database(filename):
    # Use the full file contents as database key.
    with open(filename, 'rb') as fin:
        key = fin.read()

    try:
        # Try to load the database for given file from the cache.
        database = DATABASE_CACHE[key]

        print('  Loaded cached database.')
    except KeyError:
        # The database for given file was not found in the cache. Load
        # it using cantools.
        database = cantools.database.load_file(filename)
        DATABASE_CACHE[key] = database

        print('  Loaded database using cantools.')

    return database