python对采集的CAN数据进行整理

	采集的CAN数据比较大,有几万行.如果手动处理起来就很费时间,而且容易出错.而且单帧和多帧的数据处理起来又不一样.多帧处理起来更麻烦,所以花费了一些时间写了一个处理CAN数据的脚本.
	代码有点乱,还是贴出代码来.作为记录和收藏

处理之前的数据如下所示:
70302 Req: 07 56 22 F1 90 cnt: 1

70821 Req: 07 56 22 F1 90 cnt: 1

71177 Req: 07 26 22 F1 90 cnt: 1
71235 Ans: 07 2E 62 F1 90 53 41 4C 46 41 32 42 42 35 41 48 31 36 39 31 37 30 00 00 00 00 00 00 00

90912 Req: 07 26 22 F1 14 cnt: 1
91524 Ans: 07 2E 62 F1 14 57 00 31 81 38 44 30 31 38 0D 0A 53 41 4C 46 41 32 42 42 35 41 48 31 36 39 31 37 30 0D 0A 30 30 30 30 30 30 30 30 30 0D 0A 30 30 30 0D 0A 30 30 30 30 30 30 0D 0A 30 30 0D 0A 30 30 30 30 30 30 0D 0A 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0D 0A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0D 0A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

91524 Req: 07 26 22 F1 06 cnt: 1
91923 Ans: 07 2E 62 F1 06 FC 07 08 02 02 01 31 02 02 01 04 02 04 03 00 03 02 02 01 02 02 01 01 02 02 02 02 01 02 01 02 02 02 02 01 02 01 02 01 00 01 01 01 01 01 01 04 01 01 01 01 02 55 16 06 01 01 01 03 02 03 02 01 03 24 01 01 02 00 00 12 34 00 00 00 01 06 01 00 00 00 00 01 00 02 01 03 01 02 02 02 02 02 02 04 01 01 01 01 01 01 02 01 02 02 00 00 01 01 01 02 03 00 00 01 00 00 00 00 00 03 01 02 01 02 04 01 02 01 00 02 00 02 02 02 02 02 02 00 00 00 00 00 00 00 02 02 00 02 00 02 01 00 01 01 01 01 00 02 01 01 01 00 02 04 00 00 02 00 00 00 00 01 01 01 01 02 00 01 01 01 00 01 01 01 02 01 00 02 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 02 00 07 01 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

92045 Req: 07 20 22 F1 14 cnt: 1
92659 Ans: 07 28 62 F1 14 57 00 31 81 38 44 30 31 38 0D 0A 53 41 4C 46 41 32 42 42 35 41 48 31 36 39 31 37 30 0D 0A 30 30 30 30 30 30 30 30 30 0D 0A 30 30 30 0D 0A 30 30 30 30 30 30 0D 0A 30 30 0D 0A 30 30 30 30 30 30 0D 0A 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0D 0A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0D 0A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

92659 Req: 07 20 22 F1 06 cnt: 1
93069 Ans: 07 28 62 F1 06 FC 07 08 02 02 01 31 02 02 01 04 02 04 03 00 03 02 02 01 02 02 01 01 02 02 02 02 01 02 01 02 02 02 02 01 02 01 02 01 00 01 01 01 01 01 01 04 01 01 01 01 02 55 16 06 01 01 01 03 02 03 02 01 03 24 01 01 02 00 00 12 34 00 00 00 01 06 01 00 00 00 00 01 00 02 01 03 01 02 02 02 02 02 02 04 01 01 01 01 01 01 02 01 02 02 00 00 01 01 01 02 03 00 00 01 00 00 00 00 00 03 01 02 01 02 04 01 02 01 00 02 00 02 02 02 02 02 02 00 00 00 00 00 00 00 02 02 00 02 00 02 01 00 01 01 01 01 00 02 01 01 01 00 02 04 00 00 02 00 00 00 00 01 01 01 01 02 00 01 01 01 00 01 01 01 02 01 00 02 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 02 00 07 01 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

96802 Req: 05 07 E0 03 22 F1 03 cnt: 1
97026 Ans: 07 E8 7F 22 31

97027 Req: 07 E0 22 F1 11 cnt: 1
97074 Ans: 07 E8 62 F1 11 41 48 35 32 2D 31 32 41 36 35 30 2D 41 41 00 00 00 00 00 00 00 00 00 00

97076 Req: 07 E0 19 02 AF cnt: 1
97364 Ans: 07 E8 59 02 FF 02 E9 73 39

97564 Req: 07 E1 22 F1 03 cnt: 1
97818 Ans: 07 E9 62 F1 03 31 35 30 37 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

97820 Req: 07 E1 19 02 AF cnt: 1
97868 Ans: 07 E9 59 02 FF

98037 Req: 07 60 22 F1 03 cnt: 1
98310 Ans: 07 68 62 F1 03 30 35 45 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

这里只是贴出一部分数据,懂CAN的都知道,回复的数据超过13个字节就会当做长帧处理,所以要对长短进行判断.然后根据模拟器的格式处理成不同的数据

处理后的数据
基本数据还算整齐,能跑.但是还有些细节处理不是很好,也不打算修改了,就这样吧

import re
reqbyte = []
def DealTxt():
    fLandRead = open("C:\\Users\\admin\\Desktop\\1234.txt","r")
    fLandWrite = open("C:\\Users\\admin\\Desktop\\LandRovernew.txt","w")

    readlines = fLandRead.readlines()

    for line in readlines:
        if line.__contains__("Req"):
            arr = re.split(r"\t",line)
            reqbyte = re.split(r" ",arr[2])
            reqbyteLen = len(reqbyte)
            bytestr = "{:02x} ".format(11)

            for i in range(len(reqbyte)):
                bytestr += reqbyte[i]+" "
                if i == 1:
                    bytestr += "{:02x} ".format(8)+ "{:02x} ".format(reqbyteLen - 2-1)

            for k in range(11 - len(reqbyte) - 1):
                bytestr +="00 "

            newReqLine = arr[1] + bytestr + "\n"
            fLandWrite.writelines(newReqLine)
        elif line.__contains__("Ans"):
            arrAns = re.split(r"\t", line)
            ansbyte = re.split(r" ", arrAns[2].strip())
            ansbyteLen = len(ansbyte)
            if ansbyteLen > 0x0B and ansbyteLen <= 0xff:
                print("回长帧处理")
                bytestr = arrAns[1] + "0B "
                for i in range(0x08):
                    bytestr += ansbyte[i] +' '
                    if i == 1:
                        bytestr += "08 " +"10 " +"{:02x} ".format(ansbyteLen - 5)
                bytestr += '\n'
                fLandWrite.writelines(bytestr)
                fLandWrite.writelines("Req:" + "06 " + reqbyte[0] + " " + reqbyte[1] + " " + "03 30 00 00 " + '\n')
                for j in range(int((ansbyteLen - 5)/7) ):
                    if j == int((ansbyteLen - 5)/7) :
                        bytestr = arrAns[1] + "{:02x} ".format(ansbyteLen - 8-j*7+3)+ ansbyte[0] + " " + ansbyte[1] + " {:02x} ".format(ansbyteLen - 8-j*7) + str(hex(0x21+j)).replace("0x","") + " "
                    else:
                        bytestr = arrAns[1] + "1N " + "0B " + ansbyte[0] + " " + ansbyte[1] + " 08 " + str(hex(0x21+j)).replace("0x","") + " "
                    for k in range(7):
                        try:
                             bytestr += ansbyte[j*7+k+8] + " "
                        except Exception as  e:
                            print(str(e))
                    bytestr += "\n"

                    fLandWrite.writelines(bytestr)
                fLandWrite.writelines("\n")
            elif ansbyteLen > 0xff:
                arrAns = re.split(r"\t", line)
                ansbyte = re.split(r" ", arrAns[2].strip())
                ansbyteLen = len(ansbyte)
                if ansbyteLen > 0x0B and ansbyteLen <= 0xff:
                    print("回长帧处理")
                    bytestr = arrAns[1] + "0B "
                    for i in range(0x08):
                        bytestr += ansbyte[i] + ' '
                        if i == 1:
                            bytestr += "08 " + "10 " + "{:02x} ".format((ansbyteLen - 5)>>8) + "{:02x} ".format((ansbyteLen - 5)&0xff)
                    bytestr += '\n'
                    fLandWrite.writelines(bytestr)
                    fLandWrite.writelines("Req:" + "06 " + reqbyte[0] + " " + reqbyte[1] + " " + "03 30 00 00 " + '\n')
                    for j in range(int((ansbyteLen - 4) / 7)):
                        if j == int((ansbyteLen - 4) / 7):
                            bytestr = arrAns[1] + "{:02x} ".format(ansbyteLen - 8 - j * 7 + 3) + ansbyte[0] + " " + \
                                      ansbyte[1] + " {:02x} ".format(ansbyteLen - 8 - j * 7) + str(
                                hex(0x21 + j)).replace("0x", "") + " "
                        else:
                            bytestr = arrAns[1] + "1N " + "0B " + ansbyte[0] + " " + ansbyte[1] + " 08 " + str(
                                hex(0x21 + j)).replace("0x", "") + " "
                        for k in range(7):
                            try:
                                bytestr += ansbyte[j * 7 + k + 8] + " "
                            except Exception as  e:
                                print(str(e))
                        bytestr += "\n"

                        fLandWrite.writelines(bytestr)
                    fLandWrite.writelines("\n")
            else:
                bytestr = "{:02x} ".format(ansbyteLen+2)
                for i in range(len(ansbyte)):
                    bytestr += ansbyte[i] + " "
                    if i == 1:
                        bytestr += "{:02x} ".format(ansbyteLen - 1 ) +"{:02x} ".format(ansbyteLen - 2 )
                newAnsLine = arrAns[1] + bytestr + "\n\n"
                fLandWrite.writelines(newAnsLine)





if __name__ == '__main__':
    DealTxt()












  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值