Skip to content

Commit 7b272f7

Browse files
authored
Merge branch 'development' into eds_fix
2 parents 514418c + fc2cf58 commit 7b272f7

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/canmatrix/formats/dbc.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -762,14 +762,14 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
762762
db.ecus.append(canmatrix.Ecu(ele))
763763

764764
elif decoded.startswith("VAL_ "):
765-
regexp = re.compile(r"^VAL_ +(\S+) +(\S+) +(.*) *;")
765+
regexp = re.compile(r"^VAL_ +(\d+)? *(\S+) +(.*) *;")
766766
temp = regexp.match(decoded)
767767
if temp:
768768
frame_id = temp.group(1)
769769
signal_name = temp.group(2)
770770
temp_list = list(canmatrix.utils.escape_aware_split(temp.group(3), '"'))
771771

772-
if frame_id.isnumeric(): # value for Frame
772+
if frame_id: # value for Frame
773773
try:
774774
frame = get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(int(frame_id)))
775775
sg = frame.signal_by_name(signal_name)
@@ -780,8 +780,15 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
780780
sg.add_values(temp_list[i * 2], val)
781781
except:
782782
logger.error("Error with Line: " + str(temp_list))
783-
else:
784-
logger.info("Warning: environment variables currently not supported")
783+
else:
784+
try:
785+
values = db.env_vars[signal_name]['values']
786+
for i in range(math.floor(len(temp_list) / 2)):
787+
val = temp_list[i * 2 + 1]
788+
val = val.replace('\\"', '"')
789+
values[temp_list[i * 2].strip()]=val
790+
except:
791+
logger.error("Error with Line: " + str(temp_list))
785792

786793
elif decoded.startswith("VAL_TABLE_ "):
787794
regexp = re.compile(r"^VAL_TABLE_ +(\S+) +(.*) *;")
@@ -925,7 +932,7 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
925932
access_nodes = temp.group(9).split(",")
926933
db.add_env_var(var_name, {"varType": var_type, "min": min_value, "max": max_value, "unit": unit,
927934
"initialValue": initial_value, "evId": ev_id, "accessType": access_type,
928-
"accessNodes": access_nodes})
935+
"accessNodes": access_nodes, "values": {}})
929936

930937
# else:
931938
except:

src/canmatrix/formats/json.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,14 @@ def load(f, **_options):
233233
new_frame.pdu_name = frame[key]
234234

235235
new_frame.arbitration_id.extended = frame.get("is_extended_frame", False)
236+
237+
if "attributes" in frame:
238+
for k, v in frame["attributes"].items():
239+
new_frame.add_attribute(k, v)
240+
236241
if "transmitters" in frame:
237242
new_frame.transmitters = frame["transmitters"]
243+
238244
for signal in frame["signals"]:
239245
is_little_endian = not signal.get("is_big_endian", False)
240246
is_float = signal.get("is_float", False)

tests/test_dbc.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,24 @@ def test_int_attribute_zero():
642642
assert 'BO_ 0 0' in outdbc.getvalue().decode('utf8')
643643
assert 'TestEcu 1' in outdbc.getvalue().decode('utf8')
644644
assert 'TestEcu 0' in outdbc.getvalue().decode('utf8')
645+
646+
def test_env_var_with_val():
647+
648+
dbc = io.BytesIO(textwrap.dedent(u'''\
649+
EV_ XYZ2_RADAR__LDWwarningStatusFl11: 0 [1|3] "km/h" 2 1 DUMMY_NODE_VECTOR0 Vector__XXX;
650+
BA_ "SystemEnvVarLongSymbol" EV_ XYZ2_RADAR__LDWwarningStatusFl11 "XYZ2_RADAR__LDWwarningStatus__RADAR_XYZ2";
651+
VAL_ XYZ2_RADAR__LDWwarningStatusFl11 0 "on" 1 "off" 2 "reserved" 3 "error" ;
652+
''').encode('utf-8'))
653+
654+
matrix = canmatrix.formats.dbc.load(dbc)
655+
key, var = next(iter(matrix.env_vars.items()))
656+
assert key == 'XYZ2_RADAR__LDWwarningStatus__RADAR_XYZ2'
657+
assert var['min'] == '1'
658+
assert var['max'] == '3'
659+
assert var['initialValue'] == '2'
660+
assert var['unit'] == 'km/h'
661+
assert len(var['values']) == 4
662+
assert var['values']['0'] == 'on'
663+
assert var['values']['1'] == 'off'
664+
665+

0 commit comments

Comments
 (0)