Skip to content

Commit 6f0042f

Browse files
authored
maybe fix for issue 661 (#666)
* maybe fix for issue 661 * create safe start values again
1 parent 92d8c38 commit 6f0042f

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

Diff for: src/canmatrix/formats/dbc.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,13 @@ def dump(in_db, f, **options):
237237
for signal in frame.signals:
238238
if signal.cycle_time != 0:
239239
signal.add_attribute("GenSigCycleTime", signal.cycle_time)
240-
if signal.phys2raw(None) != 0:
241-
signal.add_attribute("GenSigStartValue", signal.phys2raw(None))
240+
if "GenSigStartValue" in db.signal_defines:
241+
if signal.phys2raw(None) != 0:
242+
if db.signal_defines["GenSigStartValue"].defaultValue is not None and \
243+
float(signal.initial_value) != float(db.signal_defines["GenSigStartValue"].defaultValue):
244+
signal.add_attribute("GenSigStartValue", signal.phys2raw(float(db.signal_defines["GenSigStartValue"].defaultValue)))
245+
elif db.signal_defines["GenSigStartValue"].defaultValue is None:
246+
signal.add_attribute("GenSigStartValue", signal.phys2raw(None))
242247

243248
name = normalized_names[signal]
244249
if compatibility:
@@ -956,7 +961,12 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
956961
# frame.extended = 1
957962

958963
for signal in frame.signals:
959-
gen_sig_start_value = float_factory(signal.attributes.get("GenSigStartValue", "0"))
964+
if "GenSigStartValue" in db.signal_defines \
965+
and db.signal_defines["GenSigStartValue"].defaultValue is not None:
966+
default_value = signal.phys2raw(float_factory(db.signal_defines["GenSigStartValue"].defaultValue))
967+
else:
968+
default_value = signal.phys2raw(None)
969+
gen_sig_start_value = float_factory(signal.attributes.get("GenSigStartValue", default_value))
960970
signal.initial_value = (gen_sig_start_value * signal.factor) + signal.offset
961971
signal.cycle_time = int(signal.attributes.get("GenSigCycleTime", 0))
962972
if signal.attribute("SystemSignalLongSymbol") is not None:

Diff for: src/canmatrix/tests/test_dbc.py

+42
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def test_create_comment_string():
5353
test_string = canmatrix.formats.dbc.create_comment_string("BO_", "ident", "some comment", "utf8", "utf8", "")
5454
assert test_string == b'CM_ BO_ ident "some comment";\n'
5555

56+
5657
def test_parse_comment_from_dbc():
5758
dbc = io.BytesIO(textwrap.dedent(u'''\
5859
BO_ 1 someFrame: 1 someEcu
@@ -64,6 +65,7 @@ def test_parse_comment_from_dbc():
6465
matrix = canmatrix.formats.dbc.load(dbc)
6566
assert matrix.frames[0].signals[0].comment == "resistance setting (0-100%)"
6667

68+
6769
def test_parse_multi_line_comment():
6870
dbc = io.BytesIO(textwrap.dedent(u'''\
6971
BO_ 1 someFrame: 1 someEcu
@@ -240,6 +242,7 @@ def test_export_of_unknown_defines():
240242
if line.startswith("BA_ "):
241243
assert line.endswith('";')
242244

245+
243246
def test_braces_in_attributes():
244247
dbc = io.BytesIO(textwrap.dedent(u'''\
245248
BO_ 20 frameName: 1 someEcu
@@ -249,6 +252,7 @@ def test_braces_in_attributes():
249252
''').encode('utf-8'))
250253
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
251254

255+
252256
def test_defines_with_spaces():
253257
dbc = io.BytesIO(textwrap.dedent(u'''\
254258
BU_: someOtherEcu
@@ -276,6 +280,7 @@ def test_defines_with_spaces():
276280
assert matrix.env_vars["someEnvVar"]["attributes"]["some attrib"] == '"some space"'
277281
assert matrix.ecus[0].attributes["Description X"] == "Some Some Text"
278282

283+
279284
def test_writing_complex_multiplex():
280285
db = canmatrix.CanMatrix()
281286
frame = canmatrix.Frame("someFrame")
@@ -307,6 +312,7 @@ def test_defines_with_special_cars():
307312
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
308313
assert matrix.frames[0].signals[0].attributes["Accuracy"] == "+/- 10.2 at 55.1%"
309314

315+
310316
def test_j1939_frametype():
311317
dbc = io.BytesIO(textwrap.dedent(u'''\
312318
BU_: someOtherEcu
@@ -346,6 +352,7 @@ def test_attributes_with_spaces_before_semicolumn():
346352
assert matrix.frames[0].attributes["someAttribute"] == 'str'
347353
assert matrix.frames[1].attribute("someAttribute", matrix) == 'asd'
348354

355+
349356
def test_cycle_time_handling():
350357
dbc = io.BytesIO(textwrap.dedent(u'''\
351358
BO_ 17 Frame_1: 8 Vector__XXX
@@ -381,6 +388,7 @@ def test_cycle_time_handling():
381388
outdbc = io.BytesIO()
382389
canmatrix.formats.dump({"aa":matrix}, outdbc, "kcd")
383390

391+
384392
def test_keep_cycle_time_defines():
385393
dbc = io.BytesIO(textwrap.dedent(u'''\
386394
BO_ 17 Frame_1: 8 Vector__XXX
@@ -396,6 +404,7 @@ def test_keep_cycle_time_defines():
396404
assert 'BA_DEF_ BO_ "GenMsgCycleTime" INT 0 50000' in outdbc.getvalue().decode('utf8')
397405
assert 'BA_DEF_DEF_ "GenMsgCycleTime" 0' in outdbc.getvalue().decode('utf8')
398406

407+
399408
def test_unique_signal_names():
400409
db = canmatrix.CanMatrix()
401410
frame = canmatrix.Frame("some Frame")
@@ -413,6 +422,7 @@ def test_unique_signal_names():
413422
assert "signal_name1" not in outdbc.getvalue().decode('utf8')
414423
assert "signal_name" in outdbc.getvalue().decode('utf8')
415424

425+
416426
def test_signal_inital_value():
417427
dbc = io.BytesIO(textwrap.dedent(u'''\
418428
BO_ 17 Frame_1: 8 Vector__XXX
@@ -480,6 +490,7 @@ def test_missing_space():
480490
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
481491
assert matrix.frames[0].signals[0].name == "sig1"
482492

493+
483494
def test_escaped_quotes():
484495
dbc = io.BytesIO(textwrap.dedent(r'''
485496
BO_ 17 Frame_1: 8 Vector__XXX
@@ -518,6 +529,37 @@ def test_without_ecu():
518529
matrix.frames[0].signals[0].name == "A_B_C_D_E"
519530

520531

532+
def test_default_initial_value():
533+
dbc = io.BytesIO(textwrap.dedent(u'''\
534+
BO_ 560 ECU1_Message: 1 ECU1
535+
SG_ ECU2_Signal : 0|8@0+ (1,-5) [-2|250] "g" ECU2
536+
537+
BA_DEF_ SG_ "GenSigStartValue" FLOAT 0.0 100.0;
538+
539+
BA_DEF_DEF_ "GenSigStartValue" 10.0;
540+
''').encode('utf-8'))
541+
542+
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
543+
assert matrix.frames[0].signals[0].initial_value == 10
544+
# outdbc = io.BytesIO()
545+
# canmatrix.formats.dump(matrix, outdbc, "dbc")
546+
547+
548+
def test_no_initial_value():
549+
dbc = io.BytesIO(textwrap.dedent(u'''\
550+
BO_ 560 ECU1_Message: 1 ECU1
551+
SG_ ECU2_Signal : 0|8@0+ (1,-5) [-2|250] "g" ECU2
552+
553+
BA_DEF_ SG_ "GenSigStartValue" FLOAT 0.0 100.0;
554+
''').encode('utf-8'))
555+
556+
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
557+
outdbc = io.BytesIO()
558+
canmatrix.formats.dump(matrix, outdbc, "dbc")
559+
assert 'BA_ "GenSigStartValue" SG_ 560 ECU2_Signal 5;' in outdbc.getvalue().decode('utf8')
560+
# assert matrix.frames[0].signals[0].initial_value == 10
561+
562+
521563
def test_int_attribute_zero():
522564
db = canmatrix.CanMatrix()
523565
frame = canmatrix.Frame("some Frame")

0 commit comments

Comments
 (0)