当前位置:   article > 正文

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

python 解析arxml

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")


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

root下的所有 AR-PACKAGE 子标签

for child in root:#  root下面的所有子标签
    print(child)
  • 1
  • 2
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
root1 = root[0] # 第一个 AR-PACKAGE 标签
  • 1

下面是第一个 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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

SHORT-NAME 标签

root1.find("./{http://autosar.org/schema/r4.0}SHORT-NAME")
  • 1
<Element '{http://autosar.org/schema/r4.0}SHORT-NAME' at 0x000002C958FE9A98>
  • 1
root1.find("./{http://autosar.org/schema/r4.0}SHORT-NAME").text
  • 1
'ECUExtractFLC'
  • 1

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

root1.find("./{http://autosar.org/schema/r4.0}AR-PACKAGES")
  • 1
<Element '{http://autosar.org/schema/r4.0}AR-PACKAGES' at 0x000002C958FE9AE8>
  • 1
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/*")
  • 1
[<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>]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

root1.findall(".//{http://autosar.org/schema/r4.0}FIBEX-ELEMENT-REF-CONDITIONAL") # 注意 .// 才行;./ 是不行的
  • 1
[<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>,
 ...]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

  • 1
  • 2
Communication
  • 1
[<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>]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
for child in Communication:
    print(child.tag ,child.attrib)
  • 1
  • 2
{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'}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

获取所有的frame

ELEMENTS = Communication[0].findall(".//{http://autosar.org/schema/r4.0}ELEMENTS/*")
  • 1
for child in ELEMENTS:
    print(child[0].text)
  • 1
  • 2
ASDMCanFD2TimeSynchFr
ASDMFlcFlexTimeSynchFr
ASDMSafetyCANFD2Frame5
ASDMSafetyCANFD2Frame6
AsdmFLC_FlexrayFr00
AsdmFLC_FlexrayFr01
AsdmFLC_FlexrayFr02
AsdmFLC_FlexrayFr03
AsdmFLC_FlexrayNMFr
AsdmFlcFrDiagReqFrame1
AsdmFlcFrDiagReqFrame2
AsdmFlcFrDiagReqFrame3
AsdmFlcFrDiagReqFrame4
BbmAdRedundancyFr01
BbmAdRedundancyFr02
BbmAdRedundancyFr03
Pscm2ADRedundancyNMFr
PscmAdRedundancyFr01
PscmAdRedundancyFr02
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
frames = [child[0].text for child in ELEMENTS]
  • 1
frames
  • 1
['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']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
len(frames)
  • 1
227
  • 1

  • 1
'
运行

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

root_signal = root[5]
  • 1

‘’’

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

‘’’

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")
  • 1
len(siganls)
  • 1
7363
  • 1

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

root_signal_group = root[6]
  • 1

‘’’

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

‘’’

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")
  • 1
len(siganls_group)

  • 1
  • 2
576
  • 1
siganls_group_list = [i.text for i in siganls_group]#迭代取出signal——group的文本组成列表
  • 1
siganls_group_list
  • 1
['ADataRawSafe',
 'AdPSSGroupSafe0',
 'AdPSSGroupSafe1',
 'AgDataRawSafe',
 'AmbTRaw',
 'AsyALgtReqForCmft',
 'VisnObj21Msg1',
 'VisnObj21Msg2',
 'VisnObj21Msg3',
 'VisnObj22Msg1',
 'VisnObj22Msg2',
 'VisnObj22Msg3',
 'WhlSpdCircumlReForBkp',
 'WipgInfo']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
获取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")
  • 1
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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/1011005
推荐阅读
相关标签
  

闽ICP备14008679号