Python脚本开发-统计Rte中未连接的Port

文章目录

前言

项目开发过程中,经常会遇到应用层中Rte接口未连接导致的信号传递异常,本文介绍利用Python脚本对未连接的Rte port进行提取并导出到excel中

Autosar规范

此处我们主要针对于Rport,且是Explicit 类型的,RTE规范中定义如下:

对于未连接的Read Port,接口会返回RTE_E_UNCONNECTED,后面我们通过该特征来对此类接口进行提取

Python脚本开发

思路:

1.利用正则表达式提取文件中含RTE_E_UNCONNECTED的字符,并匹配对应的Rte接口名

复制代码
def find_rte_unconnected_in_file(file_path):
    """
    在单个文件中查找RTE_E_UNCONNECTED接口
    """
    interfaces = []
    
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
            
        # 查找RTE_E_UNCONNECTED相关的定义
        pattern = r'#define\s+(\w+).*RTE_E_UNCONNECTED'
        matches = re.findall(pattern, content)
        
        interfaces.extend(matches)
        
    except Exception as e:
        print(f"处理文件 {file_path} 时出错: {e}")
        
    return interfaces

2.遍历对应Rte文件夹下的.h文件

复制代码
def scan_directory_for_rte_unconnected(directory_path, file_extensions=None):
    """
    扫描目录中所有文件,查找RTE_E_UNCONNECTED接口
    """
    if file_extensions is None:
        file_extensions = ['.h']
        
    unconnected_interfaces = {}
    
    # 遍历目录中的所有文件
    for root, dirs, files in os.walk(directory_path):
        for file in files:
            if any(file.endswith(ext) for ext in file_extensions):
                file_path = os.path.join(root, file)
                interfaces = find_rte_unconnected_in_file(file_path)
                if interfaces:
                    unconnected_interfaces[file_path] = interfaces
                    
    return unconnected_interfaces

3.将匹配的结果导出到csv中

复制代码
def export_to_csv(results, output_file="unconnected_interfaces.csv"):
    """
    将未连接的接口导出到CSV文件,按不同文件分类
    """
    with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
        fieldnames = ['File', 'Interface']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        
        writer.writeheader()
        
        # 按文件分组写入
        for file_path, interfaces in results.items():
            for interface in interfaces:
                writer.writerow({
                    'File': file_path,
                    'Interface': interface
                })
    
    print(f"结果已导出到 {output_file}")

使用示例:

复制代码
# 使用示例
if __name__ == "__main__":
    directory_path = "./BasicSoftware/src/rte/gen"
    results = scan_directory_for_rte_unconnected(directory_path)
    
    # 打印结果到控制台
    print("未连接的接口:")
    for file_path, interfaces in results.items():
        print(f"\n文件: {file_path}")
        for interface in interfaces:
            print(f"  - {interface}")
    
    # 导出到CSV文件
    if results:
        export_to_csv(results)
    else:
        print("未找到未连接的接口")

实现效果

导出到csv中的文件路径及Port:

总结

对于未连接的port,需要充分评估,避免因port未连而导致的信号传递不正常,导致实际功能失效

提示:完整脚本可以通过《汽车电子学习笔记》-回复《 PY RTE》获取~

相关推荐
打工仔折腾 AI33 分钟前
System Prompt 替代 Few-shot:用规则约束大模型输出的省钱实践
java·人工智能·python·spring·langchain·prompt·ai agent 实战
打工仔折腾 AI37 分钟前
从零写一个CAD 05:以鼠标为中心的滚轮缩放,招法能复用但顺序不能反
开发语言·后端·python·性能优化·计算机外设·swift·ai agent 实战
for_ever_love__1 小时前
控制流——if、for、while 与可迭代对象
python·大模型开发·循环·控制流
for_ever_love__1 小时前
PyTorch 张量与 autograd——自动求导怎么工作
pytorch·python·深度学习·自动求导
小小张说故事1 小时前
Python 里 `is` 和 `==` 到底差在哪?5 个让你怀疑人生的案例 + 对照表
python
XLYcmy2 小时前
PDF 论文处理器 — 改进方向与建议文档
python·pdf·llm·agent·dify·rag·harness
青柠之夏cc2 小时前
前端拖拽功能原生实现,不引入拖拽库完成业务
开发语言·前端·python
曾晓森2 小时前
OpenAI 兼容 API 接入山猫云:先核对分组和价格,再发最小请求
网络·python
OKkankan2 小时前
Python 高阶语法(一):高阶函数、闭包、lambda 与 functools——从底层真正理解装饰器
开发语言·python
智鸟科技GemeOpen开发者智能设备3 小时前
平安校园 AI 智能实时告警系统 智鸟科技·GemeOpen + 谷华科技·融合创新方案白皮书
java·开发语言·python·物联网·智能家居