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)