Skip to content

Commit bf7e591

Browse files
committed
add decodeFrame (#330)
1 parent 18ac6b7 commit bf7e591

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

examples/decodeFrame.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
3+
import canmatrix.formats
4+
import sys
5+
6+
# command line options...
7+
usage = """
8+
%prog [options] matrix frame
9+
10+
matrixX can be any of *.dbc|*.dbf|*.kcd|*.arxml
11+
frame is AAA#YYYYYYYYYYYYYYYY or
12+
BBBBB#YYYYYYYYYYYYYYYY or
13+
14+
15+
where AAA is standard ID and BBBBB is extended ID
16+
17+
"""
18+
19+
if len(sys.argv) < 3:
20+
print(usage)
21+
sys.exit(1)
22+
23+
# load matrix
24+
db = canmatrix.formats.loadp(sys.argv[1], flat_import=True)
25+
26+
# load frame data from argv
27+
frame_string = sys.argv[2]
28+
(arbitration_id_string, hexdata) = frame_string.split('#')
29+
30+
# set arbitration_id
31+
arbitration_id = canmatrix.ArbitrationId(int(arbitration_id_string, 16))
32+
arbitration_id.id = int(arbitration_id_string, 16)
33+
34+
# extended frame?
35+
if len(arbitration_id_string) == 3:
36+
arbitration_id.extended = False
37+
else:
38+
arbitration_id.extended = True
39+
40+
# find frame to given arbitration_id
41+
frame = db.frame_by_id(arbitration_id)
42+
can_data = bytearray([int(hexdata[index: index+2],16) for index in range(0,len(hexdata),2)])
43+
44+
# decode frame
45+
decoded = frame.decode(can_data)
46+
47+
#print decoded signals
48+
for (signal, value) in decoded.items():
49+
print (signal + "\t" + hex(value.raw_value) + "\t(" + str(value.phys_value)+ ")")
50+
51+

0 commit comments

Comments
 (0)