Skip to content

Commit

Permalink
Add in simple multiplexing for dbc (#816)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
khauersp and ebroecker authored Oct 14, 2024
1 parent 27822d2 commit f060203
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/canmatrix/canmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
5 changes: 4 additions & 1 deletion src/canmatrix/formats/dbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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'

Expand Down Expand Up @@ -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", ""):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_dbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'''\
Expand Down

0 comments on commit f060203

Please sign in to comment.