|
8 | 8 |
|
9 | 9 | def load_dbc_file(dbc_file): |
10 | 10 | """Load the DBC file and return the database object.""" |
11 | | - return cantools.db.load_file(dbc_file, strict=False) |
| 11 | + try: |
| 12 | + db = cantools.db.load_file(dbc_file, strict=False) |
| 13 | + return db |
| 14 | + except (FileNotFoundError, cantools.db.errors.DatabaseLoadError) as e: |
| 15 | + print(f"Error loading DBC file '{dbc_file}': {e}") |
| 16 | + raise |
12 | 17 |
|
13 | 18 |
|
14 | 19 | def extract_signals_from_dbc(db, dbc_file_name): |
15 | 20 | """Extract message names and signal names from the DBC database.""" |
16 | 21 | msg_list = [] |
17 | 22 | sgn_list = [] |
18 | 23 |
|
19 | | - messages_list = db.messages |
20 | | - for msg in tqdm(messages_list, desc=f"Reading signals in {os.path.basename(dbc_file_name)}"): |
21 | | - for sgn in msg.signal_tree: |
22 | | - msg_list.append(str(msg.name)) |
23 | | - sgn_list.append(str(sgn)) |
| 24 | + try: |
| 25 | + messages_list = db.messages |
| 26 | + for msg in tqdm( |
| 27 | + messages_list, desc=f"Reading signals in {os.path.basename(dbc_file_name)}" |
| 28 | + ): |
| 29 | + for sgn in msg.signal_tree: |
| 30 | + msg_list.append(str(msg.name)) |
| 31 | + sgn_list.append(str(sgn)) |
| 32 | + except AttributeError as e: |
| 33 | + print(f"Error extracting signals from DBC file '{dbc_file_name}': {e}") |
| 34 | + raise |
24 | 35 |
|
25 | 36 | return msg_list, sgn_list |
26 | 37 |
|
27 | 38 |
|
28 | 39 | def initialize_output_structure(sgn_list): |
29 | 40 | """Initialize the structure for storing the output CSV data and signal values.""" |
30 | | - output = [[] for _ in range(len(sgn_list) + 1)] |
31 | | - current_values = [0] * len(sgn_list) # Inizializziamo tutti i segnali a 0 |
32 | | - for i, sgn in enumerate(sgn_list): |
33 | | - output[i + 1].append(f"{sgn}") |
34 | | - return output, current_values |
| 41 | + try: |
| 42 | + output = [[] for _ in range(len(sgn_list) + 1)] |
| 43 | + current_values = [0] * len(sgn_list) # Initialize all signals to 0 |
| 44 | + for i, sgn in enumerate(sgn_list): |
| 45 | + output[i + 1].append(f"{sgn}") |
| 46 | + return output, current_values |
| 47 | + except Exception as e: |
| 48 | + print(f"Error initializing output structure: {e}") |
| 49 | + raise |
35 | 50 |
|
36 | 51 |
|
37 | 52 | def process_blf_file(db, blf_file, msg_list, sgn_list, output, current_values): |
38 | 53 | """Process the BLF file and fill in the output structure with decoded signal values.""" |
39 | | - can_log = can.BLFReader(blf_file) |
40 | | - first = True |
41 | | - previous_values = current_values.copy() # Memorizziamo i valori precedenti |
| 54 | + try: |
| 55 | + can_log = can.BLFReader(blf_file) |
| 56 | + except (FileNotFoundError, can.CanError) as e: |
| 57 | + print(f"Error opening BLF file '{blf_file}': {e}") |
| 58 | + raise |
42 | 59 |
|
43 | | - for msg in tqdm(can_log, desc=f"Reading signals in {os.path.basename(blf_file)}"): |
44 | | - try: |
45 | | - msg_name = db.get_message_by_frame_id(msg.arbitration_id).name |
46 | | - cur_frame = db.decode_message(msg.arbitration_id, msg.data, decode_choices=False) |
47 | | - |
48 | | - if first: |
49 | | - first = False |
50 | | - start_abs = msg.timestamp |
51 | | - output[0].append("Timestamp") |
52 | | - |
53 | | - timestamp = msg.timestamp - start_abs |
54 | | - row = [timestamp] # Start with timestamp |
55 | | - has_changes = False # Flag per tracciare i cambiamenti nei segnali |
56 | | - |
57 | | - # Aggiorniamo i valori correnti dei segnali e controlliamo se ci sono cambiamenti |
58 | | - for i in range(len(sgn_list)): |
59 | | - if sgn_list[i] in cur_frame and msg_name == msg_list[i]: |
60 | | - current_value = cur_frame[sgn_list[i]] |
61 | | - if current_value != previous_values[i]: |
62 | | - current_values[i] = current_value # Aggiorniamo solo se il valore è cambiato |
63 | | - has_changes = True # Flag indica cambiamento |
64 | | - row.append(current_values[i]) |
65 | | - |
66 | | - if has_changes: # Scriviamo solo se c'è stato un cambiamento nei valori dei segnali |
67 | | - previous_values = current_values.copy() # Aggiorniamo i valori precedenti |
68 | | - for i in range(len(row)): |
69 | | - output[i].append(row[i]) |
70 | | - |
71 | | - except KeyError: |
72 | | - pass |
| 60 | + first = True |
| 61 | + previous_values = current_values.copy() # Store previous signal values |
| 62 | + |
| 63 | + try: |
| 64 | + for msg in tqdm(can_log, desc=f"Reading signals in {os.path.basename(blf_file)}"): |
| 65 | + try: |
| 66 | + msg_name = db.get_message_by_frame_id(msg.arbitration_id).name |
| 67 | + cur_frame = db.decode_message( |
| 68 | + msg.arbitration_id, msg.data, decode_choices=False |
| 69 | + ) |
| 70 | + |
| 71 | + if first: |
| 72 | + first = False |
| 73 | + start_abs = msg.timestamp |
| 74 | + output[0].append("Timestamp") |
| 75 | + |
| 76 | + timestamp = msg.timestamp - start_abs |
| 77 | + row = [timestamp] # Start with timestamp |
| 78 | + has_changes = False # Flag to track changes in signals |
| 79 | + |
| 80 | + # Update current signal values and check for changes |
| 81 | + for i in range(len(sgn_list)): |
| 82 | + if sgn_list[i] in cur_frame and msg_name == msg_list[i]: |
| 83 | + current_value = cur_frame[sgn_list[i]] |
| 84 | + if current_value != previous_values[i]: |
| 85 | + current_values[i] = current_value # Update only if value changed |
| 86 | + has_changes = True # Flag indicates change |
| 87 | + row.append(current_values[i]) |
| 88 | + |
| 89 | + if has_changes: # Write row only if there were changes in signal values |
| 90 | + previous_values = current_values.copy() # Update previous values |
| 91 | + for i in range(len(row)): |
| 92 | + output[i].append(row[i]) |
| 93 | + |
| 94 | + except KeyError as e: |
| 95 | + print(f"Error decoding message: {e}") |
| 96 | + continue # Ignore undecodable messages |
| 97 | + except Exception as e: |
| 98 | + print(f"Unexpected error while processing message: {e}") |
| 99 | + continue # Ignore other general errors |
| 100 | + except Exception as e: |
| 101 | + print(f"Error processing BLF file '{blf_file}': {e}") |
| 102 | + raise |
73 | 103 |
|
74 | 104 | return output |
75 | 105 |
|
76 | 106 |
|
77 | 107 | def write_csv(output, dbc_file, blf_file): |
78 | 108 | """Write the output structure to a CSV file with a name based on the DBC and BLF filenames.""" |
79 | | - dbc_basename = os.path.splitext(os.path.basename(dbc_file))[0] |
80 | | - blf_basename = os.path.splitext(os.path.basename(blf_file))[0] |
81 | | - csv_filename = f"{dbc_basename}_{blf_basename}.csv" |
82 | | - |
83 | | - print(f"Creating {csv_filename}") |
84 | | - with open(csv_filename, "w", newline='') as f: |
85 | | - writer = csv.writer(f, delimiter=';') |
86 | | - writer.writerows(zip(*output)) |
| 109 | + try: |
| 110 | + dbc_basename = os.path.splitext(os.path.basename(dbc_file))[0] |
| 111 | + blf_basename = os.path.splitext(os.path.basename(blf_file))[0] |
| 112 | + csv_filename = f"{dbc_basename}_{blf_basename}.csv" |
| 113 | + |
| 114 | + print(f"Creating {csv_filename}") |
| 115 | + with open(csv_filename, "w", newline='') as f: |
| 116 | + writer = csv.writer(f, delimiter=';') |
| 117 | + writer.writerows(zip(*output)) |
| 118 | + except (OSError, IOError) as e: |
| 119 | + print(f"Error writing CSV file '{csv_filename}': {e}") |
| 120 | + raise |
| 121 | + except Exception as e: |
| 122 | + print(f"Unexpected error while writing CSV: {e}") |
| 123 | + raise |
87 | 124 |
|
88 | 125 |
|
89 | 126 | def convert_blf_to_csv(dbc_files, blf_file): |
90 | 127 | """Main function to convert a BLF file to multiple CSV files using multiple DBC files.""" |
91 | 128 | for dbc_file in dbc_files: |
92 | | - db = load_dbc_file(dbc_file) |
93 | | - msg_list, sgn_list = extract_signals_from_dbc(db, dbc_file) |
94 | | - output, current_values = initialize_output_structure(sgn_list) |
95 | | - output = process_blf_file(db, blf_file, msg_list, sgn_list, output, current_values) |
96 | | - write_csv(output, dbc_file, blf_file) |
| 129 | + try: |
| 130 | + db = load_dbc_file(dbc_file) |
| 131 | + msg_list, sgn_list = extract_signals_from_dbc(db, dbc_file) |
| 132 | + output, current_values = initialize_output_structure(sgn_list) |
| 133 | + output = process_blf_file( |
| 134 | + db, blf_file, msg_list, sgn_list, output, current_values |
| 135 | + ) |
| 136 | + write_csv(output, dbc_file, blf_file) |
| 137 | + except Exception as e: |
| 138 | + print(f"Error in conversion with DBC file '{dbc_file}': {e}") |
| 139 | + continue # If one DBC file fails, continue with the next one |
97 | 140 |
|
98 | 141 |
|
99 | 142 | if __name__ == "__main__": |
100 | | - parser = argparse.ArgumentParser(description="Convert BLF files to CSV using one or more DBC files.") |
101 | | - parser.add_argument("--blf", required=True, help="The path to the .blf file to convert.") |
102 | | - parser.add_argument("--dbc", required=True, nargs='+', |
103 | | - help="The path(s) to the .dbc file(s) used for interpreting CAN messages.") |
104 | | - |
105 | | - args = parser.parse_args() |
106 | | - |
107 | | - convert_blf_to_csv(args.dbc, args.blf) |
| 143 | + try: |
| 144 | + parser = argparse.ArgumentParser( |
| 145 | + description="Convert BLF files to CSV using one or more DBC files." |
| 146 | + ) |
| 147 | + parser.add_argument("--blf", required=True, help="The path to the .blf file to convert.") |
| 148 | + parser.add_argument( |
| 149 | + "--dbc", |
| 150 | + required=True, |
| 151 | + nargs='+', |
| 152 | + help="The path(s) to the .dbc file(s) used for interpreting CAN messages.", |
| 153 | + ) |
| 154 | + |
| 155 | + args = parser.parse_args() |
| 156 | + |
| 157 | + convert_blf_to_csv(args.dbc, args.blf) |
| 158 | + except Exception as e: |
| 159 | + print(f"Fatal error: {e}") |
0 commit comments