-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (64 loc) · 2.92 KB
/
main.py
File metadata and controls
70 lines (64 loc) · 2.92 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
import can_receiver
import Parser
import argparse
import os
def main():
# User inputs defined parameters to get to desired mode: auto/manual
arg_parse = argparse.ArgumentParser(description="Process, log, and display CAN data")
arg_parse.add_argument("--manual", action="store_true", help="Manual CAN line input on command line")
arg_parse.add_argument("--status", action="store_true", help="Determine pCAN connection status")
arg_parse.add_argument("--file", action="store_true", help="Read CAN data from file")
arg_parse.add_argument("--debug" , action="store_true", help="Debug mode for CAN data")
arg_parse.add_argument("--log", action="store_true", help="Logs data in log.txt is local directory")
arg_parse.add_argument("--setup", action="store_true", help="Sets CAN0 interface to active")
# Determine what the users arguments are
args = arg_parse.parse_args()
is_debug: bool = args.debug
is_log: bool = args.log
if args.setup:
# Automatically set CAN0 port to activate for data transmission
try:
os.system("sudo ip link set can0 up type can bitrate 500000")
# Interesting bug where it makes it through even without sudo privilages
except Exception as e:
print(f"MAIN::can_set_up::error {e}")
# Manual User Input for CAN data
if args.manual:
running: bool = True
while running:
data: str = input("Enter CAN data line: ")
Parser.parse_can_line(data, is_debug)
running = input("Continue? (y/n) ") == 'y'
# Determine CAN network status
if args.status:
if not can_receiver.get_status():
return
# Automatically take in process and display data in the terminal
elif args.file:
# Get the input file path
input_file_path = input("Enter the input file path: ")
# Get the output file path
output_file_path = input("Enter the output file path: ")
with open(output_file_path, 'w') as file_handle:
with open(input_file_path, 'r') as file:
for line in file:
file_handle.write(Parser.parse_can_line(line, is_debug))
else:
# Initialize Socket CAN to read from the correct bus
bus = can_receiver.get_data_bus()
# Continuously dump the data into parser
while True:
# retrieve data from the CAN network
message = can_receiver.get_can_line(bus)
# print("TYPE: ", type(message), ' ', str(message))
# failed to receive message break from loop
if message == None:
break
# print and log the data
if is_log:
file = open("log.txt", "a")
file.write(str(message) + '\n')
# message = can_receiver.clean_message(message)
Parser.parse_can_line(message, is_debug)
if __name__ == '__main__':
main()