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》获取~

相关推荐
特立独行的猫A8 小时前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
吃我一窝jio11 小时前
Python与Go线程模型深度对比
java·开发语言·python
zwd200512 小时前
Manim 怎么放大画面的某一部分?
python·动画·可视化·manim·数学动画
屿华AI12 小时前
AUTOSAR 实战-诊断工具 | UDS 0x27 安全访问的配置化实现:种子密钥算法插件化设计
python·测试工具·汽车
RobinDevNotes13 小时前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
如夏倾城14 小时前
VRRP(虚拟路由器冗余协议Virtual Router Redundancy Protocol)
网络·python·网络协议·安全·智能路由器·负载均衡·信息与通信
IvanCodes14 小时前
LangChain(04):Runnable 与 LCEL 的执行与组合机制
python·langchain·agent
特创数字科技15 小时前
纯本地离线运行的文档脱敏工具(技术分享)
前端·驱动开发·python
东莞市云毅网络有限公司16 小时前
用 Playwright 搭一套问答平台引用监测的最小骨架
python·数据清洗·rag·企业知识库·文档解析
Thanks_ks16 小时前
【第 010 讲】全面掌握 Python 变量与常量:变量的定义与赋值 | 标识符与命名规范 | 变量的底层机制 | 常量与魔术数字
python·内存·变量·标识符·常量·赋值·命名规范