일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- Windows11
- 엑셀
- win32com
- 파워포인트
- VBA
- 안드로이드
- pythongui
- Windows10
- 윈도우10
- 파이썬GUI
- Android
- matlab
- 깃
- python3
- 아웃룩
- pyqt5
- 비주얼베이직
- 오피스
- git
- 문자열
- 파이썬3
- html
- Outlook
- Excel
- 윈도우11
- python
- windows
- 파이썬
- pandas
- office
Appia의 IT세상
[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,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
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.
'Python > Python 기본' 카테고리의 다른 글
파이썬(Python)설치 없이 온라인에서 코딩하기(Jupyter, 주피터노트북) (0) | 2020.11.28 |
---|---|
Python[파이썬] 데이터 시각화00 matplotlib 설치하기 (0) | 2020.11.26 |
머신러닝 관련 GPU와 파이썬(Python)설치 없이 코딩하기, TensorFlow사용하기(구글 Google Colab) (0) | 2020.11.24 |
파이썬[Python] 환율 변환 모듈(CurrencyConvertor) 설치 및 달러-원, 유로-원으로 실시간 변환하기 (2) | 2020.11.07 |
파이썬[Python] 변수의 Null인지 아닌지 확인하는 방법 (0) | 2020.10.20 |