Example #1
0
def test_canmatrix_del_frame_by_instance(empty_matrix):
    f1 = canmatrix.Frame(name="F1")
    f2 = canmatrix.Frame(name="F2")
    empty_matrix.add_frame(f1)
    empty_matrix.add_frame(f2)
    empty_matrix.del_frame(f1)
    assert empty_matrix.frames == [f2]
Example #2
0
def test_arbitration_id_is_instance():
    frame1 = canmatrix.Frame(name="Frame1")
    frame2 = canmatrix.Frame(name="Frame1")

    frame1.arbitration_id.id = 42

    assert frame1.arbitration_id.id == 42
    assert frame2.arbitration_id.id == 0
Example #3
0
def test_canmatrix_del_ecu_by_glob(empty_matrix):
    ecu1 = canmatrix.Ecu(name="ecu1")
    ecu2 = canmatrix.Ecu(name="ecu2")
    frame = canmatrix.Frame(transmitters=["ecu2", "ecu3"])
    empty_matrix.add_ecu(ecu1)
    empty_matrix.add_ecu(ecu2)
    frame.add_signal(canmatrix.Signal(receivers=["ecu1", "ecu2"]))
    empty_matrix.add_frame(frame)
    empty_matrix.del_ecu("*2")
    assert empty_matrix.ecus == [ecu1]
    assert frame.receivers == ["ecu1"]
    assert frame.transmitters == ["ecu3"]
Example #4
0
def test_canmatrix_rename_frame_by_name(empty_matrix):
    f = canmatrix.Frame(name="F1")
    empty_matrix.add_frame(f)
    empty_matrix.rename_frame("F1", "F2")
    assert f.name == "F2"
    empty_matrix.rename_frame("X*", "G")
    assert f.name == "F2"
    empty_matrix.rename_frame("F*", "G")
    assert f.name == "G2"
    empty_matrix.rename_frame("*0", "9")
    assert f.name == "G2"
    empty_matrix.rename_frame("*2", "9")
    assert f.name == "G9"
Example #5
0
def test_canmatrix_del_obsolete_ecus(empty_matrix):
    empty_matrix.add_ecu(canmatrix.Ecu(name="Ecu1"))
    empty_matrix.add_ecu(canmatrix.Ecu(name="Ecu2"))
    frame1 = canmatrix.Frame(name="frame1", transmitters=["Ecu1"])
    frame1.add_signal(canmatrix.Signal("signal1", receivers=["Ecu2"]))
    empty_matrix.add_frame(frame1)
    empty_matrix.delete_obsolete_ecus()
    assert "Ecu1" in [ecu.name for ecu in empty_matrix.ecus]
    assert "Ecu2" in [ecu.name for ecu in empty_matrix.ecus]
    frame1.del_transmitter("Ecu1")
    empty_matrix.delete_obsolete_ecus()
    assert "Ecu1" not in [ecu.name for ecu in empty_matrix.ecus]
    assert "Ecu2" in [ecu.name for ecu in empty_matrix.ecus]
Example #6
0
    def export(self, filename):
        """Export current configuration to a database file.

        :param str filename:
            Filename to save to (e.g. DBC, DBF, ARXML, KCD etc)

        :return: The CanMatrix object created
        :rtype: canmatrix.canmatrix.CanMatrix
        """
        from canmatrix import canmatrix
        from canmatrix import formats

        db = canmatrix.CanMatrix()
        for pdo_maps in (self.rx, self.tx):
            for pdo_map in pdo_maps.values():
                if pdo_map.cob_id is None:
                    continue
                frame = canmatrix.Frame(pdo_map.name,
                                        Id=pdo_map.cob_id,
                                        extended=0)
                for var in pdo_map.map:
                    is_signed = var.od.data_type in objectdictionary.SIGNED_TYPES
                    is_float = var.od.data_type in objectdictionary.FLOAT_TYPES
                    min_value = var.od.min
                    max_value = var.od.max
                    if min_value is not None:
                        min_value *= var.od.factor
                    if max_value is not None:
                        max_value *= var.od.factor
                    name = var.name
                    name = name.replace(" ", "_")
                    name = name.replace(".", "_")
                    signal = canmatrix.Signal(name,
                                              startBit=var.offset,
                                              signalSize=var.length,
                                              is_signed=is_signed,
                                              is_float=is_float,
                                              factor=var.od.factor,
                                              min=min_value,
                                              max=max_value,
                                              unit=var.od.unit)
                    for value, desc in var.od.value_descriptions.items():
                        signal.addValues(value, desc)
                    frame.addSignal(signal)
                frame.calcDLC()
                db.frames.addFrame(frame)
        formats.dumpp({"": db}, filename)
        return db
Example #7
0
    def export(self, filename):
        """Export current configuration to a database file.

        :param str filename:
            Filename to save to (e.g. DBC, DBF, ARXML, KCD etc)
        """
        from canmatrix import canmatrix
        from canmatrix import formats

        db = canmatrix.CanMatrix()
        for pdo_maps in (self.rx, self.tx):
            for pdo_map in pdo_maps.values():
                if pdo_map.cob_id is None:
                    continue
                direction = "Tx" if pdo_map.cob_id & 0x80 else "Rx"
                map_id = pdo_map.cob_id >> 8
                name = "%sPDO%d_node%d" % (direction, map_id, self.parent.id)
                frame = canmatrix.Frame(name, Id=pdo_map.cob_id, extended=0)
                for var in pdo_map.map:
                    is_signed = var.od.data_type in objectdictionary.SIGNED_TYPES
                    is_float = var.od.data_type in objectdictionary.FLOAT_TYPES
                    min_value = var.od.min
                    max_value = var.od.max
                    if min_value is not None:
                        min_value *= var.od.factor
                    if max_value is not None:
                        max_value *= var.od.factor
                    signal = canmatrix.Signal(var.name.replace(".", "_"),
                                              startBit=var.offset,
                                              signalSize=len(var.od),
                                              is_signed=is_signed,
                                              is_float=is_float,
                                              factor=var.od.factor,
                                              min=min_value,
                                              max=max_value,
                                              unit=var.od.unit)
                    for value, desc in var.od.value_descriptions.items():
                        signal.addValues(value, desc)
                    frame.addSignal(signal)
                frame.calcDLC()
                db.frames.addFrame(frame)
        formats.dumpp({"": db}, filename)
Example #8
0
def test_effective_cycle_time():
    frame = canmatrix.Frame()
    sig1 = canmatrix.Signal(name="s1", cycle_time=1)
    sig2 = canmatrix.Signal(name="s2", cycle_time=0)
    frame.add_signal(sig1)
    frame.add_signal(sig2)
    assert frame.effective_cycle_time == 1

    sig2.cycle_time = 2
    assert frame.effective_cycle_time == 1

    sig1.cycle_time = 4
    assert frame.effective_cycle_time == 2

    sig1.cycle_time = 3
    assert frame.effective_cycle_time == 1

    frame.cycle_time = 1
    assert frame.effective_cycle_time == 1

    frame.cycle_time = 0
    sig1.cycle_time = 0
    sig2.cycle_time = 0
    assert frame.effective_cycle_time == 0
Example #9
0
    def __init__(
        self,
        matrix,
        frame_class=Frame,
        signal_class=Signal,
        rx_interval=0,
        bus=None,
        node_id_adjust=None,
        strip_summary=True,
        parent=None,
    ):
        super().__init__(self.message_received, parent=parent)

        self.bus = None

        self.frame_rx_timestamps = {}
        self.frame_rx_interval = rx_interval

        frames = []

        for frame in matrix.frames:
            if node_id_adjust is not None:
                frame.arbitration_id = canmatrix.canmatrix.ArbitrationId(
                    id=node_id_adjust(
                        message_id=frame.arbitration_id.id,
                        to_device=(frame.attributes["Receivable"].casefold() ==
                                   "false"),
                    ),
                    extended=frame.arbitration_id.extended,
                )
            multiplex_signal = None
            for signal in frame.signals:
                if signal.multiplex == "Multiplexor":
                    multiplex_signal = signal
                    break

            if multiplex_signal is None:
                neo_frame = frame_class(
                    frame=frame,
                    strip_summary=strip_summary,
                )
                # for signal in frame.signals:
                #     signal = signal_class(signal=signal, frame=neo_frame)
                #     signal.set_human_value(signal.default_value *
                #                            signal.factor[float])
                frames.append(neo_frame)
            else:
                # Make a frame with just the multiplexor entry for
                # parsing messages later
                multiplex_frame = canmatrix.Frame(
                    name=frame.name,
                    arbitration_id=frame.arbitration_id,
                    size=frame.size,
                    transmitters=list(frame.transmitters),
                    cycle_time=frame.cycle_time,
                )
                matrix_signal = canmatrix.Signal(
                    name=multiplex_signal.name,
                    start_bit=multiplex_signal.start_bit,
                    size=multiplex_signal.size,
                    is_little_endian=multiplex_signal.is_little_endian,
                    is_signed=multiplex_signal.is_signed,
                    factor=multiplex_signal.factor,
                    offset=multiplex_signal.offset,
                    min=multiplex_signal.min,
                    max=multiplex_signal.max,
                    unit=multiplex_signal.unit,
                    multiplex=multiplex_signal.multiplex,
                )
                multiplex_frame.add_signal(matrix_signal)
                multiplex_neo_frame = frame_class(
                    frame=multiplex_frame,
                    strip_summary=strip_summary,
                )
                multiplex_neo_frame.mux_frame = multiplex_neo_frame
                frames.append(multiplex_neo_frame)

                multiplex_neo_frame.multiplex_signal = multiplex_neo_frame.signals[
                    0]

                multiplex_neo_frame.multiplex_frames = {}

                for multiplex_value, multiplex_name in multiplex_signal.values.items(
                ):
                    # For each multiplexed frame, make a frame with
                    # just those signals.
                    matrix_frame = canmatrix.Frame(
                        name=frame.name,
                        arbitration_id=frame.arbitration_id,
                        size=frame.size,
                        transmitters=list(frame.transmitters),
                        cycle_time=frame.cycle_time,
                    )
                    matrix_frame.add_attribute("mux_name", multiplex_name)
                    matrix_frame.add_comment(
                        multiplex_signal.comments[int(multiplex_value)])
                    matrix_signal = canmatrix.Signal(
                        name=multiplex_signal.name,
                        start_bit=multiplex_signal.start_bit,
                        size=multiplex_signal.size,
                        is_little_endian=multiplex_signal.is_little_endian,
                        is_signed=multiplex_signal.is_signed,
                        factor=multiplex_signal.factor,
                        offset=multiplex_signal.offset,
                        min=multiplex_signal.min,
                        max=multiplex_signal.max,
                        unit=multiplex_signal.unit,
                        multiplex=multiplex_signal.multiplex,
                    )
                    # neo_signal = signal_class(signal=matrix_signal, frame=multiplex_neo_frame)
                    matrix_frame.add_signal(matrix_signal)

                    for signal in frame.signals:
                        if signal.multiplex == multiplex_value:
                            matrix_frame.add_signal(signal)

                    neo_frame = frame_class(
                        frame=matrix_frame,
                        mux_frame=multiplex_neo_frame,
                        strip_summary=strip_summary,
                    )
                    for signal in neo_frame.signals:
                        if signal.multiplex is True:
                            signal.set_value(multiplex_value)
                    frames.append(neo_frame)
                    multiplex_neo_frame.multiplex_frames[
                        multiplex_value] = neo_frame

        self.frames = tuple(frames)

        self.signal_from_uuid = {
            signal.parameter_uuid: signal
            for frame in self.frames for signal in frame.signals
            if signal.parameter_uuid is not None
        }

        if bus is not None:
            self.set_bus(bus=bus)
Example #10
0
    def __init__(self,
                 matrix,
                 frame_class=Frame,
                 signal_class=Signal,
                 rx_interval=0,
                 bus=None,
                 node_id_adjust=None,
                 strip_summary=True,
                 parent=None):
        super().__init__(self.message_received, parent=parent)

        self.bus = None

        self.frame_rx_timestamps = {}
        self.frame_rx_interval = rx_interval

        frames = []

        for frame in matrix.frames:
            if node_id_adjust is not None:
                frame.id = node_id_adjust(
                    message_id=frame.id,
                    to_device=(
                        frame.attributes['Receivable'].casefold() == 'false'),
                )
            multiplex_signal = None
            for signal in frame.signals:
                if signal.multiplex == 'Multiplexor':
                    multiplex_signal = signal
                    break

            if multiplex_signal is None:
                neo_frame = frame_class(
                    frame=frame,
                    strip_summary=strip_summary,
                )
                # for signal in frame.signals:
                #     signal = signal_class(signal=signal, frame=neo_frame)
                #     signal.set_human_value(signal.default_value *
                #                            signal.factor[float])
                frames.append(neo_frame)
            else:
                # Make a frame with just the multiplexor entry for
                # parsing messages later
                multiplex_frame = canmatrix.Frame(name=frame.name,
                                                  id=frame.id,
                                                  size=frame.size,
                                                  transmitters=list(
                                                      frame.transmitters))
                if 'GenMsgCycleTime' in frame.attributes:
                    multiplex_frame.addAttribute(
                        'GenMsgCycleTime', frame.attributes['GenMsgCycleTime'])
                multiplex_frame.extended = frame.extended
                matrix_signal = canmatrix.Signal(
                    name=multiplex_signal.name,
                    startBit=multiplex_signal.startBit,
                    size=multiplex_signal.size,
                    is_little_endian=multiplex_signal.is_little_endian,
                    is_signed=multiplex_signal.is_signed,
                    factor=multiplex_signal.factor,
                    offset=multiplex_signal.offset,
                    min=multiplex_signal.min,
                    max=multiplex_signal.max,
                    unit=multiplex_signal.unit,
                    multiplex=multiplex_signal.multiplex)
                multiplex_frame.addSignal(matrix_signal)
                multiplex_neo_frame = frame_class(
                    frame=multiplex_frame,
                    strip_summary=strip_summary,
                )
                multiplex_neo_frame.mux_frame = multiplex_neo_frame
                frames.append(multiplex_neo_frame)

                multiplex_neo_frame.multiplex_signal =\
                    multiplex_neo_frame.signals[0]

                multiplex_neo_frame.multiplex_frames = {}

                for multiplex_value, multiplex_name in multiplex_signal.values.items(
                ):
                    # For each multiplexed frame, make a frame with
                    # just those signals.
                    matrix_frame = canmatrix.Frame(name=frame.name,
                                                   id=frame.id,
                                                   size=frame.size,
                                                   transmitters=list(
                                                       frame.transmitters))
                    matrix_frame.extended = frame.extended
                    if 'GenMsgCycleTime' in frame.attributes:
                        matrix_frame.addAttribute(
                            'GenMsgCycleTime',
                            frame.attributes['GenMsgCycleTime'])
                    matrix_frame.addAttribute('mux_name', multiplex_name)
                    matrix_frame.addComment(
                        multiplex_signal.comments[int(multiplex_value)])
                    matrix_signal = canmatrix.Signal(
                        name=multiplex_signal.name,
                        startBit=multiplex_signal.startBit,
                        size=multiplex_signal.size,
                        is_little_endian=multiplex_signal.is_little_endian,
                        is_signed=multiplex_signal.is_signed,
                        factor=multiplex_signal.factor,
                        offset=multiplex_signal.offset,
                        min=multiplex_signal.min,
                        max=multiplex_signal.max,
                        unit=multiplex_signal.unit,
                        multiplex=multiplex_signal.multiplex)
                    # neo_signal = signal_class(signal=matrix_signal, frame=multiplex_neo_frame)
                    matrix_frame.addSignal(matrix_signal)

                    for signal in frame.signals:
                        if signal.multiplex == multiplex_value:
                            matrix_frame.addSignal(signal)

                    neo_frame = frame_class(
                        frame=matrix_frame,
                        mux_frame=multiplex_neo_frame,
                        strip_summary=strip_summary,
                    )
                    for signal in neo_frame.signals:
                        if signal.multiplex is True:
                            signal.set_value(multiplex_value)
                    frames.append(neo_frame)
                    multiplex_neo_frame.\
                        multiplex_frames[multiplex_value] = neo_frame

        self.frames = tuple(frames)

        if bus is not None:
            self.set_bus(bus=bus)
Example #11
0
def test_canmatrix_remove_frame(empty_matrix, empty_frame):
    empty_matrix.add_frame(empty_frame)
    empty_matrix.add_frame(canmatrix.Frame())
    empty_matrix.remove_frame(empty_frame)
    assert len(empty_matrix.frames) == 1
Example #12
0
def test_canmatrix_get_frame_by_glob(empty_matrix, empty_frame):
    empty_matrix.add_frame(empty_frame)
    f2 = canmatrix.Frame(name="nm_osek_esp")
    empty_matrix.add_frame(f2)
    assert empty_matrix.glob_frames("*osek*") == [f2]
Example #13
0
def test_canmatrix_rename_frame_by_instance(empty_matrix):
    f = canmatrix.Frame(name="F1")
    empty_matrix.add_frame(f)
    empty_matrix.rename_frame(f, "F2")
    assert f.name == "F2"