使用Python解析充电桩报文GBT-27930

        之前都是使用CANape解析充电桩报文,同时canape的function功能进行数据分析还挺好用的,可以统计充电桩功率和车载功率,还能积分算出充电桩输出电量和车的输入电量。于是琢磨着自己用Python去解析充电桩的报文。在次记录一下解析的弯弯绕绕,防止踩坑。

1、安装python环境及依赖库

        安装python就不必多说了推荐使用anaconda,重点是需要哪些库,这里需要使用pip安装:

pip install python-can
pip install j1939
pip install cantools

1、读取DBC文件

:CANoe-Configurations/SmartCharging/GBT27930/Simulation/GBT_27930_2015.dbc at master · sebastianwilczek/CANoe-Configurations

dbc文件使用canoe自带的27930文件,可以通过以上链接下载。通过cantools读取dbc文件

import cantools
import can
dbc = cantools.database.load_file(R'D:\GBT_27930_2015.dbc',encoding="gbk")

3、将原始CAN报文转换为J1939报文。

如果直接使用原始报文的话,有些J1939的多帧报文是无法解析出来的。因此需要使用J1939库,把多帧报文转换为单帧,从而可以使用cantools工具将其解析出信号。

import cantools
import can
import sys
import j1939
import logging
logging.getLogger('j1939').setLevel(logging.DEBUG)

j1939msg = []
ecu = j1939.ElectronicControlUnit()
ecu.connect(bustype='virtual', bitrate=250000)
blf_file = r"D:\聊天数据\企业微信\WXWork\1688856102462474\Cache\File\2024-10\0914_2.blf"
logdata = list(can.BLFReader(blf_file))
decoded = {}
# sys.stdout = open('output.txt', 'w')
def on_message56(priority, pgn, sa,timestamp, data):
    canid = (cantools.j1939.frame_id_pack(priority,0,0,int(pgn/256) ,0x56,sa))
    j1939msg.append([timestamp,canid,data])
def on_messagef4(priority, pgn, sa,timestamp, data):
    canid = (cantools.j1939.frame_id_pack(priority,0,0,int(pgn/256) ,0xF4,sa))
    j1939msg.append([timestamp,canid,data])
ecu.subscribe(on_message56,0x56)
ecu.subscribe(on_messagef4,0xf4)
for msg in logdata:
    dec = ecu.notify(msg.arbitration_id, msg.data,msg.timestamp)
# ecu.disconnect()

4、使用DBC解析J1939报文

decoded = {}
idlist = []
for msg in j1939msg:
#     try:
    if msg[1] in dbc._frame_id_to_message:
        if msg[1] not in idlist:
            idlist.append(msg[1])
        dec = dbc.decode_message(msg[1], msg[2], allow_truncated=True, scaling=True)
        if dec:
            for key ,data in dec.items():
                if key not in decoded:
                    decoded[key] = []
                decoded[key].append([msg[0],data])

至此,就可以获取完整的充电数据,接下计划可能是用pyechart进行数据可视化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值