將datalogger記錄文件轉換爲canalyser可讀取.asc文件

import os
import time
# New add feature, use this to list all *.mel file in current folder, Ver 0.80 2014-1-21 by Homer
import glob


# Redirection the work space path.
path_cwd = os.getcwd()
os.chdir(path_cwd)


# Use a list content all *.mel, Ver 0.80 2014-1-21 by Homer
filelist = glob.glob("090003.mel")


# Need new for each_line in filelist .......... Do it!
print(filelist)


# Use the same CANalyzer ASC file head & tail
# Modify the routine to get the real measurement time. Homer 2014/8/11
CANalyzer_ASC_file_time = ""
CANalyzer_ASC_file_Head1 = "date "
CANalyzer_ASC_file_Head2 = "base hex  timestamps absolute"
CANalyzer_ASC_file_Head3 = "internal events logged"
CANalyzer_ASC_file_Head4 = "// version 7.2.0"
CANalyzer_ASC_file_Head5 = "Begin Triggerblock "
CANalyzer_ASC_file_Tail = "End TriggerBlock"


# Start get the PGN list
# New modify here, ergodic whole folder do all convert in one time.
try:
    for file_selected_name in filelist:
        # Create new folder for split data. Of couse, this folder name use system time as part of it.
        folder_curr = time.strftime("%Y%m%d_%H%M%S")+"_Split_Data_"+os.path.splitext(file_selected_name)[0]
        # Define new sub-folder "Split_Data" if it is not exist
        if not os.path.exists(folder_curr):
            os.mkdir(folder_curr)
        
        # Delete every file in sub-folder "Split_Data"
        path_cwd = os.path.join(path_cwd, folder_curr)
        print(path_cwd)
        #(這一句的必要性)os.chdir(path_cwd)
        #刪除新建文件夾裏面的文件
        if os.path.isfile(path_cwd):
            try:
                os.remove(path_cwd)
            except:
                pass
        #刪除新建文件夾裏面的文件夾目錄
        elif os.path.isdir(path_cwd):
            for each_item in os.listdir(path_cwd):
                os.remove(each_item)


        # Return to father folder
        path_cwd = os.path.split(path_cwd)[0]
        os.chdir(path_cwd)
        #file_data代表090003mel內容
        file_data = open(file_selected_name, "r")
        lines_seen = set()
    
    # Get in sub-folder to save the splited data file which base on the PGN
        path_cwd = os.path.join(path_cwd, folder_curr)
        os.chdir(path_cwd)


    # Define mid-way temp file
        file_msgid = open("result_MsgID.txt", "w")
        file_msgid_split = open("result_MsgID_Split.txt", "w")
    
    # Define the handle succeed flag, 0 mean fail, 1 mean succeed
        hnd_succeed = 0


    # Very useful, the split function has a parameter can define how many part you want split in one line.
        print("Please wait a moment until you see a line of '+' or some failure information! Wait with patience & good lucky! ^_*")


    # Add CANalyzer ASC file Head in the converted file
        file_converted = open(os.path.splitext(file_selected_name)[0]+".asc","a")


        #Try to get the real measurement time of data file. Homer 2014/8/11
        #將09003中的Start Time=Wed Apr 14 09:00:04 1999變爲:Wed Apr 14 09:00:04 1999
        line = file_data.readline()#讀取090003.mel的第一行,[Header, Start Time=Wed Apr 14 09:00:04 1999, TimeStamp=3.677354]
        print(line)
        tmp = line.split(",",3)#3代表最多劃分4個元祖
        CANalyzer_ASC_file_time = tmp[1]
        CANalyzer_ASC_file_time = CANalyzer_ASC_file_time[12:]
        #print(CANalyzer_ASC_file_time)
        CANalyzer_ASC_file_Head1 = CANalyzer_ASC_file_Head1+CANalyzer_ASC_file_time
        CANalyzer_ASC_file_Head5 = CANalyzer_ASC_file_Head5+CANalyzer_ASC_file_time


        #將擡頭相關信息寫入090003.asc
        print(CANalyzer_ASC_file_Head1,file = file_converted)#file_converted = open(os.path.splitext(file_selected_name)[0]+".asc","a")
        print(CANalyzer_ASC_file_Head2,file = file_converted)
        print(CANalyzer_ASC_file_Head3,file = file_converted)
        print(CANalyzer_ASC_file_Head4,file = file_converted)
        print(CANalyzer_ASC_file_Head5,file = file_converted)


    # Start convert the data to CANalyzer ASC format
        #file_data代表090003mel內容
        for each_line in file_data:#file_data=090003.mel的內容
            try:
                (msg_id, msg_channal, msg_dlc, msg_data1,msg_data2,msg_data3,msg_data4,msg_data5,msg_data6,msg_data7,msg_data8,msg_timestamp,msg_timedelta) = each_line.split(",", 12)
            #  18eeff05x       1                  8            ff                 ff                 1f              01              00               05              00              10          5.377029              時間差
            #  將 5.377029 1 18eeff05x RX d 8 ff ff 1f 01 00 05 00 10寫入090003.asc   
                print(msg_timestamp, msg_channal, msg_id[2:]+"x", "RX", "d", msg_dlc, msg_data1[2:],msg_data2[2:],msg_data3[2:],msg_data4[2:],msg_data5[2:],msg_data6[2:],msg_data7[2:],msg_data8[2:], file = file_converted)
            # here below is used for find out the repeated value!!
                if msg_id not in lines_seen:#lines_seen = set()
                    # Write the original msg_id into file_msgid
                        print(msg_id, file = file_msgid)
                        #lines_seen = set()
                        lines_seen.add(msg_id)
                    
                    # Write the Dec format PGN, SA & P into file_msgid_split
                    
                        
                        msg_PGN = int("0x" + msg_id[4:8], 16)#0x18eeff05中的eeff,通過J1939查看
                        #0x18eeff05中的05,SA=source address=engine ECM,RX=received from the source address。
                        msg_SA = int("0x" + msg_id[8:], 16)
                        #msg_p代表優先級priority,0x18eeff05中的0x18轉化爲10進制數後,右移2位,相當於除以4。6代表發動機發出信號,8代表變速箱發出信號
                        msg_P = (int(msg_id[0:4], 16)) >> 2  #it's work, I can split the Priority of Msg and I can translate it to int, also. 2013-7-16
                        print(msg_PGN, msg_SA, msg_P, file=file_msgid_split)


                    # Print split line and show user the route still alive ^_^
                        print("+", end="")
            except ValueError:
                pass
            #print(CANalyzer_ASC_file_Tail,file = file_converted)
# Some logic wrong, otherwise end of file will print several times! Homer 2014/8/11
        print(CANalyzer_ASC_file_Tail,file = file_converted)
        # Here try to return father folder, 2014-1-22 Homer
        print("")
        path_cwd = os.path.split(path_cwd)[0]
        os.chdir(path_cwd)    
        hnd_succeed = 1
except  IOError:#此處有問題,若090003.mel不存在,將跳過try語句執行此異常操作,那下面finally中的file_data等將未定義,報錯
    print("")
    print(IOError)
    print("File not excist or something wrong!")    
finally:
    file_data.close()
    file_msgid.close()
    file_msgid_split.close()
    file_converted.close()


    # Totally finished, show some information to indicate the status of route running
    if hnd_succeed == 1:
        print("")
        print("Succeed execute the command!")


    # Return to father level dir
    path_cwd = os.path.split(path_cwd)[0]
    os.chdir(path_cwd)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章