python拆分CANLog

通过CANOE 导出的log通常有很多个ID的数据,如何才能找到某一个ID下的特殊的信号?利用python可以简单的进行这个步骤,代码如下:

说明:

  最终的效果是将log信息,分不同的ID进行拆分,并单独生成文件log_id.csv的文件夹。

  1. 需要输入输入文件夹

  2. 生成_out文件夹。

  3. 没有错误处理

  4. 采用"[A-F|\d]{3}"匹配CAN ID

 #log analy about CAN Signal for CANOE
import os
import re ID_Info = {}
fold_in = input("Please input Folder In:")
fold_out = fold_in + "_out"
os.mkdir(fold_out)
rep_id = r"[A-F|\d]{3}"
patt_id = re.compile(rep_id)
def Info_Report():
'''
CAN Log中的信息有固定的位数。
0:时间戳
1:1/2
2:ID
3:Rx/Tx
4: d
5: data Length
6: data length number info
'''
pass def Dev_Folder(folder_in):
for fi in os.listdir(folder_in):
#打开文件
with open(os.path.join(folder_in,fi),"r") as file:
#遍历文件行
for line in file:
#拆分文件,去掉空格,去掉前后逗号
a = re.sub('\s+',',',line).strip(",")
#判断List是否有3个以上的元素,并且第3个是否为ID.
if len(a.split(",")) > 2 and patt_id.match(a.split(",")[2]):
if a.split(",")[2] in ID_Info.keys():
#如果在字典中,直接将信息写入到该文件中
pass
else:
#新建一个文件,关联字典
ID_Info[a.split(",")[2]] = open((os.path.join(fold_out + "\\" + "Log_" + a.split(",")[2]) + ".csv"),"w")
ID_Info[a.split(",")[2]].write(a+"\n")
#print(a.split(",")[0])
for key in ID_Info:
ID_Info[key].close() if __name__ == '__main__':
Dev_Folder(fold_in)

以上拆分为单个的ID了,那么如何找到某一个信号的值呢?可以通过下面的代码进行计算:

 '''
CANOE Log
'''
''' '''
start_bit = 0
data_length = 0
can_mode = 0 def Info_Report():
'''
Need Input This information about CAN setting.
start bit: start_bit
Length : Data_Length
start bit mode : LSB(0)/MSB(1)
default:LSB(0)
'''
pass def SetBit(start,length,mode = 0):
'''
for Get Info or set info
'''
start_bit = start
data_length = length
can_mode = mode def Analyse_ID_File(File_Input_Name,File_Output_Name):
'''
Analyse id for Every ID Signal
'''
#open file
file_out = open(File_Output_Name,"w")
with open(File_Input_Name,"r") as file:
for line in file:
#拆分每一行,放入到list中
data_list = line.split(",")
data_length = int(data_list[5],16)
list_in = data_list[6:6+data_length]
data_signal = Find_FromList(list_in)
file_out.write(data_list[0]+","+str(data_signal)+"\n")
file_out.close() def Find_FromList(data_list):
dataMerge = ''
a,b,c = Find_StartEnd(start_bit,data_length,can_mode)
#print(data_list)
if len(data_list) > b:
for i in range(a,b+1):
#print(i)
dataMerge = dataMerge + data_list[i]
#print(dataMerge)
data_Digit = (int(dataMerge,16)>>c) & ((1<<(data_length))-1)
return (data_Digit)
#print(dataMerge) def Find_StartEnd(start,length,mode):
'''
This Function return value:
byte_little,byte_big,bigbyte_bit
'''
startbyte = start >> 3
startbit = start % 8
if mode == 0:
litByte = startbyte - ((startbit+length-1)>>3)
BigByte = startbyte
Endbit = startbit
else:
pass
return (litByte,BigByte,Endbit) if __name__ == '__main__':
#print(Find_StartEnd(32,20,1))
file_name = input("Please input file You need Analyse:")
file_out_name = input("Please input file You need Out(.csv):")
start_bit,data_length,can_mode = map(int,input("please input can info(start_bit,data_length,can_mode(0/1),PV为0):").split(","))
Analyse_ID_File(file_name,file_out_name)

以上,如果不通过导入每个信号信息含义的话,是可以快速的找到某个信号在整个log中的值得变化的。

  以上,只支持以LSB开始,计算长度的算法。如果以MSB需要在''Find_StartEnd"函数中增加''mode == 1''的处理。

上一篇:使用DrawerLayout实现QQ5.0侧拉菜单效果


下一篇:embedding与word2vec