diff --git a/src/canmatrix/canmatrix.py b/src/canmatrix/canmatrix.py index 87d18fc3..f88f5a53 100644 --- a/src/canmatrix/canmatrix.py +++ b/src/canmatrix/canmatrix.py @@ -1671,6 +1671,17 @@ def compress(self): gap_found = True break + def multiplex_signals(self): + """Assign multiplexer to signals. When a multiplexor is in the frame.""" + multiplexor = self.get_multiplexer + if multiplexor is None: + return + + for signal in self.signals: + if signal.is_multiplexer or (signal.muxer_for_signal is not None): + continue + signal.muxer_for_signal = multiplexor.name + signal.mux_val = signal.multiplex def __str__(self): # type: () -> str """Represent the frame by its name only.""" diff --git a/src/canmatrix/formats/dbc.py b/src/canmatrix/formats/dbc.py index 28e2b227..965459e2 100644 --- a/src/canmatrix/formats/dbc.py +++ b/src/canmatrix/formats/dbc.py @@ -599,9 +599,11 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None multiplex = temp.group(2) # type: str is_complex_multiplexed = False + is_multiplexer = False if multiplex == 'M': multiplex = 'Multiplexor' + is_multiplexer = True elif multiplex.endswith('M'): is_complex_multiplexed = True multiplex = multiplex[:-1] @@ -632,7 +634,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None **extras ) - if is_complex_multiplexed: + if is_complex_multiplexed or is_multiplexer: temp_signal.is_multiplexer = True temp_signal.multiplex = 'Multiplexor' @@ -989,6 +991,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None db.enum_attribs_to_values() for frame in db.frames: + frame.multiplex_signals() if "_FD" in frame.attributes.get("VFrameFormat", ""): frame.is_fd = True if "J1939PG" in frame.attributes.get("VFrameFormat", ""): diff --git a/tests/test_dbc.py b/tests/test_dbc.py index 2f82602f..77db8fb5 100644 --- a/tests/test_dbc.py +++ b/tests/test_dbc.py @@ -339,6 +339,20 @@ def test_j1939_frametype(): matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8") assert matrix.frames[0].is_j1939 == False +def test_multiplex_frame(): + dbc = io.BytesIO(textwrap.dedent(u'''\ + BU_: someOtherEcu + + BO_ 123 someFrame: 8 someOtherEcu + SG_ someSignal m2 : 8|8@1+ (1,0) [0|9] "" CCL_TEST + SG_ someOtherSignal m1 : 8|8@0+ (1,0) [0|9] "" CCL_TEST + SG_ someMultiplexor M : 0|8@1+ (1,0) [0|2] "" CCL_TEST + ''').encode('utf-8')) + matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8") + assert matrix.frames[0].is_multiplexed + + assert matrix.frames[0].signal_by_name("someSignal").muxer_for_signal == "someMultiplexor" + def test_attributes_with_spaces_before_semicolumn(): dbc = io.BytesIO(textwrap.dedent(u'''\