Skip to content

Commit

Permalink
optionally allow multiple singals with same name in frame #411 (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebroecker authored Oct 28, 2019
1 parent cf63b07 commit 69647af
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/canmatrix/cli/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def get_formats():
@click.option('--dbcImportCommentEncoding', 'dbcImportCommentEncoding', default="iso-8859-1", help="Import charset of Comments in dbc\ndefault iso-8859-1")
@click.option('--dbcExportEncoding', 'dbcExportEncoding', default="iso-8859-1", help="Export charset of dbc (relevant for units), maybe utf-8\ndefault iso-8859-1")
@click.option('--dbcExportCommentEncoding', 'dbcExportCommentEncoding', default="iso-8859-1", help="Export charset of comments in dbc\ndefault iso-8859-1")
@click.option('--dbcUniqueSignalNames/--no-dbcUniqueSignalNames', 'dbcUniqueSignalNames', default=True, help="Check if signal names are unique per frame")
# dbf switches
@click.option('--dbfImportEncoding', 'dbfImportEncoding', default="iso-8859-1", help="Import charset of dbf, maybe utf-8\ndefault iso-8859-1")
@click.option('--dbfExportEncoding', 'dbfExportEncoding', default="iso-8859-1", help="Export charset of dbf, maybe utf-8\ndefault iso-8859-1")
Expand Down
5 changes: 3 additions & 2 deletions src/canmatrix/formats/dbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ def dump(in_db, f, **options):

dbc_export_encoding = options.get("dbcExportEncoding", 'iso-8859-1')
dbc_export_comment_encoding = options.get("dbcExportCommentEncoding", dbc_export_encoding)
compatibility = options.get('compatibility', True)
dbc_unique_signal_names_per_frame = options.get("dbcUniqueSignalNames", compatibility)
ignore_encoding_errors= options.get("ignoreEncodingErrors", "")
write_val_table = options.get("writeValTable", True)
compatibility = options.get('compatibility', True)

whitespace_replacement = options.get("whitespaceReplacement", '_')
if whitespace_replacement in ['', None] or {' ', '\t'}.intersection(whitespace_replacement):
Expand Down Expand Up @@ -241,7 +242,7 @@ def dump(in_db, f, **options):
if name[0].isdigit():
name = whitespace_replacement + name
duplicate_signal_counter[name] += 1
if duplicate_signal_totals[name] > 1:
if dbc_unique_signal_names_per_frame and duplicate_signal_totals[name] > 1:
# TODO: pad to 01 in case of 10+ instances, for example?
name += str(duplicate_signal_counter[name] - 1)
output_names[frame][signal] = name
Expand Down
17 changes: 17 additions & 0 deletions src/canmatrix/tests/test_dbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,23 @@ def test_cycle_time_handling():
outdbc = io.BytesIO()
canmatrix.formats.dump({"aa":matrix}, outdbc, "kcd")

def test_unique_signal_names():
db = canmatrix.CanMatrix()
frame = canmatrix.Frame("some Frame")
frame.add_signal(canmatrix.Signal("signal_name", size=1, start_bit=1))
frame.add_signal(canmatrix.Signal("signal_name", size=2, start_bit=9))
db.add_frame(frame)
outdbc = io.BytesIO()
canmatrix.formats.dump(db, outdbc, "dbc")
assert "signal_name0" in outdbc.getvalue().decode('utf8')
assert "signal_name1" in outdbc.getvalue().decode('utf8')

outdbc = io.BytesIO()
canmatrix.formats.dump(db, outdbc, "dbc", dbcUniqueSignalNames=False)
assert "signal_name0" not in outdbc.getvalue().decode('utf8')
assert "signal_name1" not in outdbc.getvalue().decode('utf8')
assert "signal_name" in outdbc.getvalue().decode('utf8')

def test_signal_inital_value():
dbc = io.BytesIO(textwrap.dedent(u'''\
BO_ 17 Frame_1: 8 Vector__XXX
Expand Down

0 comments on commit 69647af

Please sign in to comment.