def test_export_all_native(): matrix = canmatrix.canmatrix.CanMatrix() frame = canmatrix.canmatrix.Frame(name="test_frame", size=6, arbitration_id=10) signal = canmatrix.Signal(name="test_sig", size=40, is_float=True, min="-4.2", max=42, factor="0.123", offset=1) frame.add_signal(signal) matrix.add_frame(frame) out_file = io.BytesIO() canmatrix.formats.dump(matrix, out_file, "json", jsonExportAll=True, jsonNativeTypes=True) data = json.loads(out_file.getvalue().decode("utf-8")) assert (data['messages'][0]['signals'][0]['min'] == -4.2) assert (data['messages'][0]['signals'][0]['max'] == 42) assert (data['messages'][0]['signals'][0]['factor'] == 0.123) assert (data['messages'][0]['signals'][0]['offset'] == 1)
def test_signal_range_type_int(): signal = canmatrix.Signal(is_float=False) min, max = signal.calculateRawRange() min_is = isinstance(min, int) max_is = isinstance(max, int) assert (min_is, max_is) == (True, True), str((type(min), type(max)))
def test_signal_range_type_float(): signal = canmatrix.Signal(is_float=True) min, max = signal.calculateRawRange() factory_type = type(signal.float_factory()) min_is = isinstance(min, factory_type) max_is = isinstance(max, factory_type) assert (min_is, max_is) == (True, True), str((type(min), type(max)))
def test_export_min_max(): matrix = canmatrix.canmatrix.CanMatrix() frame = canmatrix.canmatrix.Frame(name="test_frame", size=6, id=10) signal = canmatrix.Signal(name="someSigName", size=40, min=-5, max=42) frame.addSignal(signal) matrix.addFrame(frame) out_file = io.BytesIO() canmatrix.formats.dump(matrix, out_file, "cmjson", jsonAll=True) data = json.loads(out_file.getvalue().decode("utf-8")) data['messages'][0]['signals'][0]['min'] == -5 data['messages'][0]['signals'][0]['max'] == 42
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"]
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]
def test_signal_multiplexer_value_in_range(): # test multiplexer ranges (complex multiplex) signal = canmatrix.Signal() signal.mux_val_grp.append([1, 2]) signal.mux_val_grp.append([4, 5]) assert signal.multiplexer_value_in_range(0) == False assert signal.multiplexer_value_in_range(1) == True assert signal.multiplexer_value_in_range(2) == True assert signal.multiplexer_value_in_range(3) == False assert signal.multiplexer_value_in_range(4) == True assert signal.multiplexer_value_in_range(5) == True assert signal.multiplexer_value_in_range(6) == False # test standard multiplexer signal2 = canmatrix.Signal() signal2.multiplex_setter(1) assert signal2.multiplexer_value_in_range(1) == True assert signal2.multiplexer_value_in_range(0) == False signal3 = canmatrix.Signal() signal3.multiplex_setter("Multiplexor") assert signal3.multiplexer_value_in_range(1) == False assert signal3.multiplexer_value_in_range(0) == False
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
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
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)
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)
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)
def test_signal_max_specified_respects_calc_for_max_none_true(): signal = canmatrix.Signal(max=42, calc_max_for_none=True) assert signal.max == 42
def test_signal_max_unspecified_respects_calc_for_max_none_true(): signal = canmatrix.Signal(size=8, is_signed=True, calc_max_for_none=True) assert signal.max == 127
def test_signal_max_unspecified_respects_calc_for_max_none_false(): signal = canmatrix.Signal(calc_max_for_none=False) assert signal.max is None
def test_signal_min_specified_respects_calc_for_min_none_false(): signal = canmatrix.Signal(min=42, calc_min_for_none=False) assert signal.min == 42