Appia의 IT세상

[Python]How to make CAN DBC module self-created,How to parse CAN DBC file - CAN DBC Class 본문

Python/Python 기본

[Python]How to make CAN DBC module self-created,How to parse CAN DBC file - CAN DBC Class

Appia 2020. 11. 25. 00:05
반응형

[Python]How to make CAN DBC module self-created,How to parse CAN DBC file.  - CAN DBC Class

[Python]How to make CAN DBC module self-created - CAN DBC Class

In this posting, I will talk about CAN Class that I mentioned earlier. I thought a lot about whether to use Dictionary or Class.

Of course, as many people know, Dictionary is very efficient, but we decided to use Class to create CAN-related verification procedures. However, we decided to use the Dictionary because it may be more efficient to use it for signal-related parts within the Class.

 

1. Message Class 
First, let's look at the class.

class Msg :
	#initialize
    def __init__(self,data):
        data.replace('\n','')
        data = data.split(' ')
        data[3].replace(':','')
        self._,self.MessageID,self.MessageName,self.SignalCount,self.TxNode = data
        self.signals=[]

	#function : Add signal 
    def insert_signal(self, i ):
        print(i)
        _,singalName,_,Temp0, Temp1,Temp2,Temp3, recevieNode = i.split()
        value_type = 'unsigned' if Temp0[-1] == '-' else 'signed'
        byte_order = 'little_endian' if Temp0[-2] == '0' else 'big_endian'
        Temp0 = re.split('\W+',Temp0)
        Temp1 = re.split('\W+', Temp1)
        Temp2 = re.split('\W+', Temp2)
        self.signals.append({"singalName":singalName,"value_type":value_type ,
                             "byte_order":byte_order,"start_bit":Temp0[0],
                             "signal_size":Temp0[1],"factor":Temp1[1],
                             "offset":Temp1[2],"minValue":Temp2[1], 
                             "maxValue" : Temp2[2], "unit":Temp3, 
                             "recevieNode":recevieNode})

Of course, as everyone knows, a single message can contain multiple signals. So we made the Singals part a list, and the attributes for each Signal were made to use Dictionary.

 

Import_DBC

Now that we have created a class, we will write a section that calls DBC through file input and output. This part was converted to a member of the List on a line basis.

def Import_DBC(File):
    DBCFile = []
    f = open(File,'r')
    Data = f.readlines()
    for i in Data :
        DBCFile.append(i)
    f.close()
    return DBCFile

 

Organize_Data

The imported DBC serves to distinguish between messages and signals in accordance with each structure.

def Organize_Data(DBCFile):
    Total = []
    eMsg = None
    for i in DBCFile :
        if "BO_ " == i[:4]:
            if eMsg != None :
                Total.append(eMsg)
                eMsg = None
            eMsg = Msg(i)
        elif " SG_ " == i[:5]:
            eMsg.insert_signal(i)
    return Total

 

Full Code : 

import re

class Msg :
    def __init__(self,data):
        data.replace('\n','')
        data = data.split(' ')
        data[3].replace(':','')
        self._,self.MessageID,self.MessageName,self.SignalCount,self.TxNode = data
        self.signals=[]

    def insert_signal(self, i ):
        print(i)
        _,singalName,_,Temp0, Temp1,Temp2,Temp3, recevieNode = i.split()
        value_type = 'unsigned' if Temp0[-1] == '-' else 'signed'
        byte_order = 'little_endian' if Temp0[-2] == '0' else 'big_endian'
        Temp0 = re.split('\W+',Temp0)
        Temp1 = re.split('\W+', Temp1)
        Temp2 = re.split('\W+', Temp2)
        """
        start_bit = Temp0[0]
        signal_size = Temp0[1]
        factor = Temp1[1]
        offset = Temp1[2]
        minValue = Temp2[1]
        manValue = Temp2[2]
        unit = Temp3
        recevieNode = recevieNode
        """
        self.signals.append({"singalName":singalName,"value_type":value_type ,
                             "byte_order":byte_order,"start_bit":Temp0[0],
                             "signal_size":Temp0[1],"factor":Temp1[1],
                             "offset":Temp1[2],"minValue":Temp2[1],
                             "maxValue" : Temp2[2], "unit":Temp3,
                             "recevieNode":recevieNode})



def Import_DBC(File):
    DBCFile = []
    f = open(File,'r')
    Data = f.readlines()
    for i in Data :
        DBCFile.append(i)
    f.close()
    return DBCFile

def Organize_Data(DBCFile):
    Total = []
    eMsg = None
    for i in DBCFile :
        if "BO_ " == i[:4]:
            if eMsg != None :
                Total.append(eMsg)
                eMsg = None
            eMsg = Msg(i)
        elif " SG_ " == i[:5]:
            eMsg.insert_signal(i)
    return Total

def Load_DBC(File):
    return Organize_Data(load_raw_file(File))

DBCFile = r'D:\Test\chassis.dbc'
Value = Load_DBC(DBCFile)
pass

 

In case of user-defined attributes, it is not supported but soon updated. If you have any questions or questions, please leave a comment or a guest book.

반응형
Comments