Convert the Datalogger record file to Canalyser to read the. asc file

Source: Internet
Author: User
Tags glob first row
Import OS
Import time
# New Add feature, use the ' 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 of 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 does all convert in one time.
Try
For File_selected_name in FileList:
# Create new folder for split data. Of couse, this folder name is with 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)
# (the necessity of this sentence) 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 the section you want split in one line.
Print ("Please wait a moment until for 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 Change: Wed APR 14 09:00:04 1999
line = File_data.readline () #读取090003. Mel's first row, [Header, Start time=wed Apr 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: Contents of the #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 0 0 05 00 10 5.377029 Time Difference
# will 5.377029 1 18eeff05x RX d 8 FF FF 1f 01 00 05 00 10 Write 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 are 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], #0x18eeff05中的eeff, viewing by J1939
#0x18eeff05中的05, Sa=source Address=engine ecm,rx=received the source address.
Msg_sa = Int ("0x" + msg_id[8:], 16)
#msg_p代表优先级priority, the 0x18 in 0x18eeff05 is converted to 10, and 2 digits to the right, the equivalent of dividing by 4. 6 on behalf of the engine signal, 8 on behalf of the gearbox signal
msg_p = (int (msg_id[0:4)) >> 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'll print several times! Homer 2014/8/11
Print (Canalyzer_asc_file_tail,file = file_converted)
# Here's 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: #此处有问题, if 090003.mel does not exist, will skip the try statement to perform this exception operation, then the following finally File_data, etc. will not be defined, error
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)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.