- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVcanDecoder.py
More file actions
Latest commit
95 lines (91 loc) · 3.46 KB
/
Copy pathVcanDecoder.py
File metadata and controls
95 lines (91 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
importcan
importcantools
classVcanDecoder:
messages= []
engineDataset= {}
engineInfoDecoded= {}
UseDBC=False
MyDBCFile='EngineSimCanDecoder/engine_ecu.dbc'
DBCFile=None
# initialize class stuff
def__init__(self, channel='vcan0'):
self.canbus=can.interface.Bus(channel=channel, interface='socketcan')
self.use_dbc_file(self=self, dbc_file_name=self.MyDBCFile)
# Get data
defgrab_data(self):
whileTrue:
try:
message=self.canbus.recv()
exceptExceptionase:
print(f"Error receiving CAN message: {e}")
continue
ifmessageisnotNone:
self.messages.append(message)
iflen(self.messages) >=100:
break
self.write_to_file('engine_data.txt')
self.shutdown()
# Use to decipher and reverse engineer your own dbc
@staticmethod
defprint_messages(messages):
formsginmessages:
print(msg)
defgetKeys(self):
returnself.engineDataset.keys()
# Save logs
defwrite_to_file(self, filename):
try:
withopen(filename, 'w') asfile:
forkey, valueinself.engineDataset.items():
byte_strings= [str(byte) forbyteinvalue]
line=f"{key}: {','.join(byte_strings)}\n"
file.write(line)
exceptExceptionase:
print(f"Error writing to file: {e}")
defget_engine_dataset(self):
formsginself.messages:
self.engineDataset[msg.arbitration_id] =msg.data
defget_engine_byteinfo(self):
forkey, valueinself.engineDataset.items():
byte_strings= [str(byte) forbyteinvalue]
self.engineInfoDecoded[key] =",".join(byte_strings)
print(self.engineInfoDecoded)
# When dbc is available, use it to decode messages
@staticmethod
defuse_dbc_file(self, dbc_file_name):
ifnotdbc_file_name.endswith('.dbc'):
print("Invalid DBC file format.")
else:
try:
self.DBCFile=cantools.database.load_file(filename=dbc_file_name)
ifself.DBCFileisNone:
print("DBC support is not available in the current CAN library.")
self.UseDBC=False
else:
print(f"DBC file '{dbc_file_name}' loaded successfully.")
self.UseDBC=True
exceptExceptionase:
print(f"Error loading DBC file: {e}")
self.UseDBC=False
except:
print("An unexpected error occurred while loading the DBC file.")
self.UseDBC=False
defdecode_message(self, message):
ifself.UseDBC:
try:
decoded=self.DBCFile.decode_message(message.arbitration_id, message.data)
returndecoded
exceptExceptionase:
print(f"Error decoding message with DBC: {e}")
returnNone
else:
print("DBC file not loaded. Cannot decode message.")
returnNone
# shutdown
defshutdown(self):
self.canbus.shutdown()
VcanDecoder().grab_data()
VcanDecoder().get_engine_dataset()
VcanDecoder().get_engine_byteinfo()
VcanDecoder().print_messages(VcanDecoder().decode_message(msg) formsginVcanDecoder().messages)
VcanDecoder().shutdown()