Skip to content

Commit f060203

Browse files
khauerspebroecker
andauthored
Add in simple multiplexing for dbc (#816)
* test: add in two new unittests * test: remove unneeded test * feat: add in handling for simple multiplexing * feat: add in more logic * docs: update description of multiplex_signals --------- Co-authored-by: Eduard Bröcker <[email protected]>
1 parent 27822d2 commit f060203

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/canmatrix/canmatrix.py

+11
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,17 @@ def compress(self):
16711671
gap_found = True
16721672
break
16731673

1674+
def multiplex_signals(self):
1675+
"""Assign multiplexer to signals. When a multiplexor is in the frame."""
1676+
multiplexor = self.get_multiplexer
1677+
if multiplexor is None:
1678+
return
1679+
1680+
for signal in self.signals:
1681+
if signal.is_multiplexer or (signal.muxer_for_signal is not None):
1682+
continue
1683+
signal.muxer_for_signal = multiplexor.name
1684+
signal.mux_val = signal.multiplex
16741685

16751686
def __str__(self): # type: () -> str
16761687
"""Represent the frame by its name only."""

src/canmatrix/formats/dbc.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,11 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
599599
multiplex = temp.group(2) # type: str
600600

601601
is_complex_multiplexed = False
602+
is_multiplexer = False
602603

603604
if multiplex == 'M':
604605
multiplex = 'Multiplexor'
606+
is_multiplexer = True
605607
elif multiplex.endswith('M'):
606608
is_complex_multiplexed = True
607609
multiplex = multiplex[:-1]
@@ -632,7 +634,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
632634
**extras
633635
)
634636

635-
if is_complex_multiplexed:
637+
if is_complex_multiplexed or is_multiplexer:
636638
temp_signal.is_multiplexer = True
637639
temp_signal.multiplex = 'Multiplexor'
638640

@@ -989,6 +991,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
989991

990992
db.enum_attribs_to_values()
991993
for frame in db.frames:
994+
frame.multiplex_signals()
992995
if "_FD" in frame.attributes.get("VFrameFormat", ""):
993996
frame.is_fd = True
994997
if "J1939PG" in frame.attributes.get("VFrameFormat", ""):

tests/test_dbc.py

+14
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ def test_j1939_frametype():
339339
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
340340
assert matrix.frames[0].is_j1939 == False
341341

342+
def test_multiplex_frame():
343+
dbc = io.BytesIO(textwrap.dedent(u'''\
344+
BU_: someOtherEcu
345+
346+
BO_ 123 someFrame: 8 someOtherEcu
347+
SG_ someSignal m2 : 8|8@1+ (1,0) [0|9] "" CCL_TEST
348+
SG_ someOtherSignal m1 : 8|8@0+ (1,0) [0|9] "" CCL_TEST
349+
SG_ someMultiplexor M : 0|8@1+ (1,0) [0|2] "" CCL_TEST
350+
''').encode('utf-8'))
351+
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
352+
assert matrix.frames[0].is_multiplexed
353+
354+
assert matrix.frames[0].signal_by_name("someSignal").muxer_for_signal == "someMultiplexor"
355+
342356

343357
def test_attributes_with_spaces_before_semicolumn():
344358
dbc = io.BytesIO(textwrap.dedent(u'''\

0 commit comments

Comments
 (0)