欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

周立功上位机获取CAN通讯数据解析 V2.0

程序员文章站 2024-02-23 22:56:18
...

周立功上位机获取CAN通讯数据解析 V2.0

运行环境:
win7 、 python3.5.2
功能:
使用周立功can通讯上位机保存数据,保存数据格式为txt。将上位机获取的16位字符串解析位正常数据

效果:
周立功上位机获取CAN通讯数据解析 V2.0
转化之后的.xls文件,保存在after transfer文件夹内
周立功上位机获取CAN通讯数据解析 V2.0

代码如下:

import os
import xlwt
import sys
# from tkinter import *
import shutil

filedir = os.path.dirname(sys.argv[0])
# os.chdir(filedir)
wdir = os.getcwd()
print("当前工作目录为:{}\n".format(wdir))
buffer = []
satu = [0]
bit = []


def inverse_16(dst):
    bu = 0
    bit.clear()

    for z in range(0, 16):
        turn = dst % 2
        bit.append(turn)
        dst = dst // 2
    # print(bit)
    if bit[15] == 1:
        for z in range(16):
            if bu == 1:
                if bit[z] == 1:
                    bit[z] = 0
                # if bit[z] == 0:
                elif bit[z] == 0:
                    bit[z] = 1
            if bit[z] == 1:
                bu = 1
        dst1_un = bit[0] + bit[1] * 2 + bit[2] * 4 + bit[3] * 8 + bit[4] * 16 + bit[5] * 32 + bit[6] * 64 + bit[
            7] * 128 + bit[8] * 256 + bit[9] * 512 + bit[10] * 1024 + bit[11] * 2048 + bit[12] * 4096 + bit[13] * 8192 + \
                  bit[14] * 16384
        dst1 = dst1_un * (-1)
        return dst1
        # print(bit)
    elif bit[15] == 0:
        dst2 = bit[0] + bit[1] * 2 + bit[2] * 4 + bit[3] * 8 + bit[4] * 16 + bit[5] * 32 + bit[6] * 64 + bit[7] * 128 + \
               bit[8] * 256 + bit[9] * 512 + bit[10] * 1024 + bit[11] * 2048 + bit[12] * 4096 + bit[13] * 8192 + bit[
                   14] * 16384
        return dst2

if os.path.exists('after transfer'):
    pass
else:
    os.makedirs('after transfer')

for parent, dirs, files in os.walk(wdir):
    for file in files:
        print(file)
        workbook_760 = xlwt.Workbook()  # 初始化xlwt
        sheet_760 = workbook_760.add_sheet("760_Car_Speed", cell_overwrite_ok=True)
        sheet_767 = workbook_760.add_sheet('767_C1', cell_overwrite_ok=True)
        sheet_766 = workbook_760.add_sheet('766_left_C0_C2', cell_overwrite_ok=True)
        sheet_768 = workbook_760.add_sheet('768_Right_C0_C2', cell_overwrite_ok=True)
        sheet_470 = workbook_760.add_sheet('470_Tor_Ang_Rotation', cell_overwrite_ok=True)
        sheet_207 = workbook_760.add_sheet('207_Tor_Sign', cell_overwrite_ok=True)
        i_760 = 0  # 存放760数据时,按顺序摆放
        i_767 = 0
        i_766 = 0
        i_766_zxd = 0
        i_768 = 0
        i_768_zxd = 0
        i_470 = 0
        i_207 = 0
        i_207_1 = 0

        if file.split('.')[-1] == "txt":
            new_name = file.split('.')[0] + '.' + 'xls'
            df = open(file, 'r')
            lines = df.readlines()
            for line in lines:
                if line.strip() == '':
                    continue
                if line.strip() == '"':
                    continue
                id_old = line.split('          ')[-1]
                id1 = id_old.split('  ')[0]
                if id1 == '0x00000760':
                    time_760 = line.split('          ')[0]  # 报文中提取时间
                    time_760 = time_760.split('   ')[-1]
                    data_760 = line.split('       ')[-1]  # 报文中提取ID总的数据
                    cs_hex = data_760.split(' ')[2]  # 从报文中提取车速的16进制
                    cs_dec = int(cs_hex, 16)  # 将车速转化为十进制
                    sheet_760.write(i_760, 0, time_760)  # 保存时间
                    sheet_760.write(i_760, 1, cs_dec)  # 将10进制车速信息写入到xls文件
                    i_760 = i_760 + 1  # 对760数据进行排序

                elif id1 == '0x00000766':
                    time_766 = line.split('          ')[0]
                    time_766 = time_766.split('   ')[-1]
                    data_766 = line.split('       ')[-1]
                    posi_hex_l = data_766.split(' ')[1]
                    posi_hex_h = data_766.split(' ')[2]
                    position_l = int(posi_hex_l, 16)
                    position_h = int(posi_hex_h, 16)
                    position = position_h * 256 + position_l
                    c2_l_766 = int(data_766.split(' ')[3], 16)
                    c2_h_766 = int(data_766.split(' ')[4], 16)
                    c2_mid = ((c2_h_766 * 256 + c2_l_766) - 32767) / 1024
                    c2 = c2_mid / 1000
                    c3_l_766 = int(data_766.split(' ')[5], 16)
                    c3_h_766 = int(data_766.split(' ')[6], 16)
                    c3 = ((c3_h_766 * 256 + c3_l_766) - 32767) / 268435456
                    left_position = (inverse_16(position)) / 256
                    zxd_766 = int(data_766.split(' ')[0], 16)  # type: str
                    LKA_confidence_766 = (zxd_766 >> 4) & 0x03
                    c0_ltype0 = int(data_766.split(' ')[0], 16)
                    c0_ltype_l = (c0_ltype0 & 0x0F)


                    sheet_766.write(i_766, 0, time_766)
                    sheet_766.write(i_766, 1, left_position)
                    sheet_766.write(i_766, 2, c2)
                    sheet_766.write(i_766, 3, c3)
                    sheet_766.write(i_766, 4, LKA_confidence_766)
                    sheet_766.write(i_766, 5, c0_ltype_l)

                    i_766 = i_766 + 1

                elif id1 == '0x00000767':
                    time_767 = line.split('          ')[0]
                    time_767 = time_767.split('   ')[-1]
                    data_767 = line.split('       ')[-1]
                    az_hex_l = data_767.split(' ')[0]
                    az_hex_h = data_767.split(' ')[1]
                    az_dec_l = int(az_hex_l, 16)
                    az_dec_h = int(az_hex_h, 16)
                    az_dec = ((az_dec_h * 256 + az_dec_l) - 32767) / 1024
                    sheet_767.write(i_767, 0, time_767)
                    sheet_767.write(i_767, 1, str(az_dec))
                    i_767 = i_767 + 1

                elif id1 == '0x00000768':
                    time_768 = line.split('          ')[0]
                    time_768 = time_768.split('   ')[-1]
                    data_768 = line.split('       ')[-1]
                    position_l = int(data_768.split(' ')[1], 16)
                    position_h = int(data_768.split(' ')[2], 16)
                    position = position_h * 256 + position_l
                    right_position = inverse_16(position) / 256
                    c2_l_768 = int(data_768.split(' ')[3], 16)
                    c2_h_766 = int(data_768.split(' ')[4], 16)
                    c2_mid_768 = ((c2_h_766 * 256 + c2_l_768) - 32767) / 1024
                    c2_768 = c2_mid_768 / 1000
                    c3_l_768 = int(data_768.split(' ')[5], 16)
                    c3_h_768 = int(data_768.split(' ')[6], 16)
                    c3_768 = ((c3_h_768 * 256 + c3_l_768) - 32767) / 268435456
                    zxd_768 = int(data_768.split(' ')[0], 16)  # type: str
                    LKA_confidence_768 = (zxd_768 >> 4) & 0x03
                    c0_ltype1 = int(data_768.split(' ')[0], 16)
                    c0_ltype_r = (c0_ltype1 & 0x0F)

                    sheet_768.write(i_768, 0, time_768)
                    sheet_768.write(i_768, 1, right_position)
                    sheet_768.write(i_768, 2, c2_768)
                    sheet_768.write(i_768, 3, c3_768)
                    sheet_768.write(i_768, 4, LKA_confidence_768)
                    sheet_768.write(i_768, 5, c0_ltype_r)
                    i_768 = i_768 + 1

                elif id1 == '0x00000470':
                    time_470 = line.split('          ')[0]
                    time_470 = time_470.split('   ')[-1]
                    data_470 = line.split('       ')[-1]
                    torque_dec = int(data_470.split(' ')[3], 16)
                    torque = torque_dec * 0.07 - 8.96
                    sheet_470.write(i_470, 0, time_470)
                    sheet_470.write(i_470, 1, torque)

                    angle_dec_l = int(data_470.split(' ')[6], 16)
                    angle_dec_h = int(data_470.split(' ')[5], 16)
                    angle = (angle_dec_h * 256 + angle_dec_l) * 0.1 - 780
                    sheet_470.write(i_470, 2, angle)  # 存储方向盘转角

                    speed_dec = int(data_470.split(' ')[1], 16)
                    speed = speed_dec * 4
                    sheet_470.write(i_470, 3, speed)  # 存储方向盘转su

                    i_470 = i_470 + 1

                elif id1 == '0x00000207':
                    time_207 = line.split('          ')[0]
                    time_207 = time_207.split('   ')[-1]
                    data_207 = line.split('       ')[-1]
                    torque_dec_L = int(data_207.split(' ')[6], 16)
                    torque_dec_H = int(data_207.split(' ')[5], 16)
                    torque_dec_207 = torque_dec_H * 256 + torque_dec_L
                    torque_207 = torque_dec_207 * 0.02 - 20.48
                    sheet_207.write(i_207, 0, time_207)
                    sheet_207.write(i_207, 1, torque_207)

                    sign = int(data_207.split(' ')[3], 16)
                    for i in range(8 - 1):
                        sign_L = sign % 2
                        buffer.append(sign_L)
                        sign = sign // 2
                    sheet_207.write(i_207, 2, buffer[4])
                    buffer.clear()

                    i_207 = i_207 + 1

            workbook_760.save(new_name)
            shutil.move(new_name, 'after transfer')

        # os.remove(filename)
        satu.append(1)
        print("文件已转换完成,请查收!")

上一篇: 软件模拟IIC

下一篇: