-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_can_data.py
62 lines (51 loc) · 1.82 KB
/
mock_can_data.py
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
# This script automatically generates and sends CAN messages based off of the DBC file
# Ensure that you have can and cantools installed, which can be accomplished by running
# pip install -r requirements.txt
# Alternatively, you could also just run
# pip install can && pip install cantools
# If you are running this script with virtual CAN ensure that it is set up first
# You can run the below commands
# sudo modprobe vcan
# sudo ip link add dev vcan0 type vcan
# sudo ip link set up vcan0
import cantools
import can
import time
import random
# A negative value for num_messages will cause the script to send CAN messages forever
num_messages = -1
sleep_time_s = 1
can_messages = []
try:
db = cantools.database.load_file('system_can.dbc')
except:
print("Must generate DBC file first")
print("Run make gen && make gen-dbc")
# This can be edited depending on the CAN interface
can_bus = can.interface.Bus('vcan0', bustype='socketcan')
def main():
get_messages()
iterate_message_and_signal()
def get_messages():
for msg in db.messages:
can_messages.append(msg)
def iterate_message_and_signal():
num_messages_sent = 0
while num_messages < 0 or num_messages > num_messages_sent:
try:
send_message()
num_messages_sent +=1
time.sleep(sleep_time_s)
except KeyboardInterrupt:
break
print("\n" + str(num_messages_sent) + " CAN messages have been sent")
def send_message():
msg = random.choice(can_messages)
data = {}
for signal in msg.signals:
data[signal.name]=random.randint(0, pow(2,signal.length)-1)
new_data = msg.encode(data)
message = can.Message(arbitration_id=msg.frame_id, data=new_data)
can_bus.send(message)
if __name__ == "__main__":
main()