基于python对Autosar 标准文件(arxml)的fleray messagehe signal的提取

arxml文件定义:

ARXML文件是以AUTOSAR XML (ARXML)格式保存的配置文件。它被AUTOSAR使用,AUTOSAR是2003年由汽车制造商和供应商组成的一个项目,用于为汽车电子控制单元(ECUs)建立软件体系结构。ARXML文件包含ECU的XML格式的配置和规范信息,ECU用于控制引擎组件,以确保引擎达到最佳性能。

需知:

XPATH 语法

xml.etree.ElementTree 语法

重要!!!:

查找元素需要前面加"{ http://autosar.org/schema/r4.0}"

比如如果要查找“AR-PACKAGE”标签 findall("./{ http://autosar.org/schema/r4.0}AR-PACKAGE")


import re
import xml.etree.ElementTree as ET
tree = ET.parse('FLC2_SystemExtract_19011.arxml')
root = tree.getroot()
root.tag
'{http://autosar.org/schema/r4.0}AUTOSAR'
root = root[0] 

root下的所有 AR-PACKAGE 子标签

for child in root:#  root下面的所有子标签
    print(child)







root1 = root[0] # 第一个 AR-PACKAGE 标签

下面是第一个 AR-PACKAGE的数据格式


  
    
      ECUExtractFLC
      
        
          VehicleProject
          
            
              XMA19011
              ECU_EXTRACT
              0.0.0;19/04/17, ECU.Cfg.Ver: 0
              
                
                  /EcuInstances/FLC2
                ```


```python

SHORT-NAME 标签

root1.find("./{http://autosar.org/schema/r4.0}SHORT-NAME")

root1.find("./{http://autosar.org/schema/r4.0}SHORT-NAME").text
'ECUExtractFLC'

AR-PACKAGES标签(SHORT-NAME的兄弟标签)

root1.find("./{http://autosar.org/schema/r4.0}AR-PACKAGES")

root1.findall("./{http://autosar.org/schema/r4.0}AR-PACKAGES/{http://autosar.org/schema/r4.0}AR-PACKAGE/{http://autosar.org/schema/r4.0}ELEMENTS/{http://autosar.org/schema/r4.0}SYSTEM/*")
[,
 ,
 ,
 ,
 ,
 ,
 ]

找到这个标签 FIBEX-ELEMENT-REF-CONDITIONAL ;但是数据包含FRAME,PDU,GROUP,SIGNAL暂时不进一步处理了

root1.findall(".//{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL") # 注意 .// 才行;./ 是不行的
[,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ,
 ...]

root2 = root[1] # 第2个 AR-PACKAGE 标签
Communication = root2.findall("./{http://autosar.org/schema/r4.0}AR-PACKAGES/{http://autosar.org/schema/r4.0}AR-PACKAGE")

Communication
[,
 ,
 ,
 ,
 ,
 ,
 ,
 ]
for child in Communication:
    print(child.tag ,child.attrib)
{http://autosar.org/schema/r4.0}AR-PACKAGE {'UUID': '74a341ea-6fb6-4673-a778-ea0131d1e5eb-Communication-Frame'}
{http://autosar.org/schema/r4.0}AR-PACKAGE {'UUID': '74a341ea-6fb6-4673-a778-ea0131d1e5eb-Communication-Gateway'}
{http://autosar.org/schema/r4.0}AR-PACKAGE {'UUID': '74a341ea-6fb6-4673-a778-ea0131d1e5eb-Communication-ISignal'}
{http://autosar.org/schema/r4.0}AR-PACKAGE {'UUID': '74a341ea-6fb6-4673-a778-ea0131d1e5eb-Communication-ISignalGroup'}
{http://autosar.org/schema/r4.0}AR-PACKAGE {'UUID': '74a341ea-6fb6-4673-a778-ea0131d1e5eb-Communication-ISignalPduGroup'}
{http://autosar.org/schema/r4.0}AR-PACKAGE {'UUID': '74a341ea-6fb6-4673-a778-ea0131d1e5eb-Communication-NmConfig'}
{http://autosar.org/schema/r4.0}AR-PACKAGE {'UUID': '74a341ea-6fb6-4673-a778-ea0131d1e5eb-Communication-Pdu'}
{http://autosar.org/schema/r4.0}AR-PACKAGE {'UUID': '74a341ea-6fb6-4673-a778-ea0131d1e5eb-Communication-TpConfig'}

获取所有的frame

ELEMENTS = Communication[0].findall(".//{http://autosar.org/schema/r4.0}ELEMENTS/*")
for child in ELEMENTS:
    print(child[0].text)
ASDMCanFD2TimeSynchFr
ASDMFlcFlexTimeSynchFr
ASDMSafetyCANFD2Frame5
ASDMSafetyCANFD2Frame6
AsdmFLC_FlexrayFr00
AsdmFLC_FlexrayFr01
AsdmFLC_FlexrayFr02
AsdmFLC_FlexrayFr03
AsdmFLC_FlexrayNMFr
AsdmFlcFrDiagReqFrame1
AsdmFlcFrDiagReqFrame2
AsdmFlcFrDiagReqFrame3
AsdmFlcFrDiagReqFrame4
BbmAdRedundancyFr01
BbmAdRedundancyFr02
BbmAdRedundancyFr03
Pscm2ADRedundancyNMFr
PscmAdRedundancyFr01
PscmAdRedundancyFr02
frames = [child[0].text for child in ELEMENTS]
frames
['ASDMCanFD2TimeSynchFr',
 'ASDMFlcFlexTimeSynchFr',
 'ASDMSafetyCANFD2Frame5',
 'ASDMSafetyCANFD2Frame6',

 'FlcFLC_FlexrayFr300',
 'FlcFLC_FlexrayFr303',
 'FlcFLC_FlexrayFr306',
 'FlcFLC_FlexrayFr309',
 'FlcFLC_FlexrayFr341',
 'FlcFLC_FlexrayFr344',
 'FlcFLC_FlexrayFr347',
 'FlcFLC_FlexrayFr35',
 'FlcFLC_FlexrayFr350',
 'FlrSafetyCanFD2NmFr',
 'IMUPrivateSafeADCanFr01',
 'IMUPrivateSafeADCanFr02',
 'Pscm2ADRedundancyNMFr',
 'PscmAdRedundancyFr01',
 'PscmAdRedundancyFr02']
len(frames)
227

抓取所有的signals :实际7363 ,vector autosar中是7456

root_signal = root[5]

‘’’


  Signal
  
    
      ADataRawSafeALat
      
        IMU acceleration data Lateral acceleration
      
      VALUE
      false
    

‘’’

arxml中的signal数据结构

siganls = root_signal.findall("./{http://autosar.org/schema/r4.0}ELEMENTS/{http://autosar.org/schema/r4.0}SYSTEM-SIGNAL/{http://autosar.orcg/schema/r4.0}SHORT-NAME")
len(siganls)
7363

抓取所有的 siganl_group :实际抓取576;vector autosar软件中读到595

root_signal_group = root[6]

‘’’


  SignalGroup
  
    
      ADataRawSafe
      
        /Signal/ADataRawSafeAVertQf
        /Signal/ADataRawSafeALgt
        /Signal/ADataRawSafeALat
        /Signal/ADataRawSafeChks
        /Signal/ADataRawSafeALat1Qf
        /Signal/ADataRawSafeCntr
        /Signal/ADataRawSafeALgt1Qf
        /Signal/ADataRawSafeAVert
      
    

‘’’

arxml中signal_group的数据结构

获取signal_group 名字

siganls_group = root_signal_group.findall("./{http://autosar.org/schema/r4.0}ELEMENTS/{http://autosar.org/schema/r4.0}SYSTEM-SIGNAL-GROUP/{http://autosar.org/schema/r4.0}SHORT-NAME")
len(siganls_group)

576
siganls_group_list = [i.text for i in siganls_group]#迭代取出signal——group的文本组成列表
siganls_group_list
['ADataRawSafe',
 'AdPSSGroupSafe0',
 'AdPSSGroupSafe1',
 'AgDataRawSafe',
 'AmbTRaw',
 'AsyALgtReqForCmft',
 'VisnObj21Msg1',
 'VisnObj21Msg2',
 'VisnObj21Msg3',
 'VisnObj22Msg1',
 'VisnObj22Msg2',
 'VisnObj22Msg3',
 'WhlSpdCircumlReForBkp',
 'WipgInfo']

获取signal_group 下的信号名字

siganls_group_ref = root_signal_group.findall("./{http://autosar.org/schema/r4.0}ELEMENTS/{http://autosar.org/schema/r4.0}SYSTEM-SIGNAL-GROUP/{http://autosar.org/schema/r4.0}SYSTEM-SIGNAL-REFS")
all_signal_list=[]
for signal_each_group in siganls_group_ref: 
    signal_list =[]
    for content_signal in signal_each_group:     
        signal_text = content_signal.text
        signal=signal_text.split(r"/")[-1] #根据“/”c拆分获取到的包含信号的字符串“/Signal/ADataRawSafeAVertQf”,通过split 拆分成列表,最后一就是信号名字
        signal_list.append(signal)
    all_signal_list.append(signal_list)
print(all_signal_list)
print(len(all_signal_list))

你可能感兴趣的:(python,python,;arxml)