当前位置: 代码迷 >> 综合 >> 基于python对Autosar 标准文件(arxml)的fleray messagehe signal的提取
  详细解决方案

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

热度:88   发布时间:2023-09-20 14:20:13.0

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)
<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C958FDEAE8>
<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C9599A1098>
<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C95E2834F8>
<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C960600D68>
<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C960621188>
<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C960621F48>
<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C9619B8188>
root1 = root[0] # 第一个 AR-PACKAGE 标签

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

<AUTOSAR xmlns="http://autosar.org/schema/r4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://autosar.org/schema/r4.0 AUTOSAR_4-2-2.xsd"><AR-PACKAGES><AR-PACKAGE><SHORT-NAME>ECUExtractFLC</SHORT-NAME><AR-PACKAGES><AR-PACKAGE UUID="74a341ea-6fb6-4673-a778-ea0131d1e5eb-ECUExtractFLC-VehicleProject"><SHORT-NAME>VehicleProject</SHORT-NAME><ELEMENTS><SYSTEM><SHORT-NAME>XMA19011</SHORT-NAME><CATEGORY>ECU_EXTRACT</CATEGORY><ECU-EXTRACT-VERSION>0.0.0;19/04/17, ECU.Cfg.Ver: 0</ECU-EXTRACT-VERSION><FIBEX-ELEMENTS><FIBEX-ELEMENT-REF-CONDITIONAL><FIBEX-ELEMENT-REF DEST="ECU-INSTANCE">/EcuInstances/FLC2</FIBEX-ELEMENT-REF></FIBEX-ELEMENT-REF-CONDITIONAL>``````python

SHORT-NAME 标签

root1.find("./{http://autosar.org/schema/r4.0}SHORT-NAME")
<Element '{http://autosar.org/schema/r4.0}SHORT-NAME' at 0x000002C958FE9A98>
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")
<Element '{http://autosar.org/schema/r4.0}AR-PACKAGES' at 0x000002C958FE9AE8>
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/*")
[<Element '{http://autosar.org/schema/r4.0}SHORT-NAME' at 0x000002C958FE9C78>,<Element '{http://autosar.org/schema/r4.0}CATEGORY' at 0x000002C958FE9CC8>,<Element '{http://autosar.org/schema/r4.0}ECU-EXTRACT-VERSION' at 0x000002C958FE9D18>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENTS' at 0x000002C958FE9E08>,<Element '{http://autosar.org/schema/r4.0}PNC-VECTOR-LENGTH' at 0x000002C959999EA8>,<Element '{http://autosar.org/schema/r4.0}PNC-VECTOR-OFFSET' at 0x000002C959999EF8>,<Element '{http://autosar.org/schema/r4.0}SYSTEM-VERSION' at 0x000002C959999F98>]

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

root1.findall(".//{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL") # 注意 .// 才行;./ 是不行的
[<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C958FE9E58>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C958FE9EF8>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C958FE9F98>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C958FED098>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C958FED138>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C958FED1D8>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C958FED278>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C958FED318>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C958FED3B8>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C959121908>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C9591219A8>,<Element '{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL' at 0x000002C959121A48>,...]

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
[<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C9599A1188>,<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C959A84E08>,<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C959AA05E8>,<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C95C759D68>,<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C95CDD3F98>,<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C95CE63818>,<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C95CE74778>,<Element '{http://autosar.org/schema/r4.0}AR-PACKAGE' at 0x000002C95E27A228>]
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]

‘’’

<AR-PACKAGE UUID="74a341ea-6fb6-4673-a778-ea0131d1e5eb-Signal"><SHORT-NAME>Signal</SHORT-NAME><ELEMENTS><SYSTEM-SIGNAL UUID="133ab181-9b26-4d99-9a3e-bf45cc99d81d"><SHORT-NAME>ADataRawSafeALat</SHORT-NAME><DESC><L-2 L="FOR-ALL">IMU acceleration data Lateral acceleration</L-2></DESC><CATEGORY>VALUE</CATEGORY><DYNAMIC-LENGTH>false</DYNAMIC-LENGTH></SYSTEM-SIGNAL>

‘’’

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]

‘’’

<AR-PACKAGE UUID="74a341ea-6fb6-4673-a778-ea0131d1e5eb-SignalGroup"><SHORT-NAME>SignalGroup</SHORT-NAME><ELEMENTS><SYSTEM-SIGNAL-GROUP UUID="A9392304051EB45CEFAB85A722DD1729"><SHORT-NAME>ADataRawSafe</SHORT-NAME><SYSTEM-SIGNAL-REFS><SYSTEM-SIGNAL-REF DEST="SYSTEM-SIGNAL">/Signal/ADataRawSafeAVertQf</SYSTEM-SIGNAL-REF><SYSTEM-SIGNAL-REF DEST="SYSTEM-SIGNAL">/Signal/ADataRawSafeALgt</SYSTEM-SIGNAL-REF><SYSTEM-SIGNAL-REF DEST="SYSTEM-SIGNAL">/Signal/ADataRawSafeALat</SYSTEM-SIGNAL-REF><SYSTEM-SIGNAL-REF DEST="SYSTEM-SIGNAL">/Signal/ADataRawSafeChks</SYSTEM-SIGNAL-REF><SYSTEM-SIGNAL-REF DEST="SYSTEM-SIGNAL">/Signal/ADataRawSafeALat1Qf</SYSTEM-SIGNAL-REF><SYSTEM-SIGNAL-REF DEST="SYSTEM-SIGNAL">/Signal/ADataRawSafeCntr</SYSTEM-SIGNAL-REF><SYSTEM-SIGNAL-REF DEST="SYSTEM-SIGNAL">/Signal/ADataRawSafeALgt1Qf</SYSTEM-SIGNAL-REF><SYSTEM-SIGNAL-REF DEST="SYSTEM-SIGNAL">/Signal/ADataRawSafeAVert</SYSTEM-SIGNAL-REF></SYSTEM-SIGNAL-REFS></SYSTEM-SIGNAL-GROUP>

‘’’

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.textsignal=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))
  相关解决方案