@@ -62,34 +62,72 @@ def process_blf_file(db, blf_file, msg_list, sgn_list, output):
6262 return output
6363
6464
65- def write_csv (output , dbc_file , blf_file ):
66- """Write the output structure to a CSV file with a name based on the DBC and BLF filenames."""
65+ def process_trc_file (db , trc_file , msg_list , sgn_list , output ):
66+ """Process the TRC file and fill in the output structure with decoded signal values."""
67+ can_log = can .CanalyzerLogReader (trc_file )
68+ first = True
69+
70+ for msg in tqdm (can_log , desc = f"Reading signals in { os .path .basename (trc_file )} " ):
71+ try :
72+ msg_name = db .get_message_by_frame_id (msg .arbitration_id ).name
73+ cur_frame = db .decode_message (msg .arbitration_id , msg .data , decode_choices = False )
74+ if first :
75+ first = False
76+ start_abs = msg .timestamp
77+ output [0 ].append ("Timestamp" )
78+ else :
79+ output [0 ].append (msg .timestamp - start_abs )
80+
81+ for i in range (len (sgn_list )):
82+ if sgn_list [i ] in cur_frame and msg_name == msg_list [i ]:
83+ output [i + 1 ].append (cur_frame [sgn_list [i ]])
84+ else :
85+ output [i + 1 ].append ('' )
86+
87+ except KeyError :
88+ pass
89+
90+ return output
91+
92+
93+ def write_csv (output , dbc_file , log_file ):
94+ """Write the output structure to a CSV file with a name based on the DBC and log filenames."""
6795 dbc_basename = os .path .splitext (os .path .basename (dbc_file ))[0 ]
68- blf_basename = os .path .splitext (os .path .basename (blf_file ))[0 ]
69- csv_filename = f"{ dbc_basename } _{ blf_basename } .csv"
96+ log_basename = os .path .splitext (os .path .basename (log_file ))[0 ]
97+ csv_filename = f"{ dbc_basename } _{ log_basename } .csv"
7098
7199 print (f"Creating { csv_filename } " )
72100 with open (csv_filename , "w" , newline = '' ) as f :
73101 writer = csv .writer (f , delimiter = ';' )
74102 writer .writerows (zip (* output ))
75103
76104
77- def convert_blf_to_csv (dbc_files , blf_file ):
78- """Main function to convert a BLF file to multiple CSV files using multiple DBC files."""
105+ def convert_log_to_csv (dbc_files , log_file ):
106+ """Main function to convert a log file (.blf or .trc) to multiple CSV files using multiple DBC files."""
107+ file_ext = os .path .splitext (log_file )[1 ].lower ()
108+
79109 for dbc_file in dbc_files :
80110 db = load_dbc_file (dbc_file )
81111 msg_list , sgn_list = extract_signals_from_dbc (db , dbc_file )
82112 output = initialize_output_structure (sgn_list )
83- output = process_blf_file (db , blf_file , msg_list , sgn_list , output )
84- write_csv (output , dbc_file , blf_file )
113+
114+ if file_ext == ".blf" :
115+ output = process_blf_file (db , log_file , msg_list , sgn_list , output )
116+ elif file_ext == ".trc" :
117+ output = process_trc_file (db , log_file , msg_list , sgn_list , output )
118+ else :
119+ print (f"Unsupported file format: { file_ext } " )
120+ return
121+
122+ write_csv (output , dbc_file , log_file )
85123
86124
87125if __name__ == "__main__" :
88- parser = argparse .ArgumentParser (description = "Convert BLF files to CSV using one or more DBC files." )
89- parser .add_argument ("--blf " , required = True , help = "The path to the .blf file to convert." )
126+ parser = argparse .ArgumentParser (description = "Convert log files (.blf or .trc) to CSV using one or more DBC files." )
127+ parser .add_argument ("--log " , required = True , help = "The path to the .blf or .trc file to convert." )
90128 parser .add_argument ("--dbc" , required = True , nargs = '+' ,
91129 help = "The path(s) to the .dbc file(s) used for interpreting CAN messages." )
92130
93131 args = parser .parse_args ()
94132
95- convert_blf_to_csv (args .dbc , args .blf )
133+ convert_log_to_csv (args .dbc , args .log )
0 commit comments