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

相关推荐
weixin_4684668524 分钟前
机器学习之决策树新手实战指南
人工智能·python·算法·决策树·机器学习·ai
Hesionberger33 分钟前
巧用异或找出唯一数字(多解)
java·数据结构·python·算法·leetcode
hef28836 分钟前
Python内置函数从入门到实战:list、open等核心用法全解析
python
七老板的blog38 分钟前
【Agent智能体】 任务规划工作流
python·学习·ai·开源
weixin1997010801638 分钟前
[特殊字符] 【性能提升300%】仿1688首页的Webpack优化全记录(附构建分析Python脚本)
前端·python·webpack
代码小书生38 分钟前
getpass,一个安全输入的 Python 库!
开发语言·python·安全
其实防守也摸鱼1 小时前
告别单个变量,用列表和字典批量管理你的 Python 数据
开发语言·网络·软件测试·python·web安全·数据结构,编程教程
海鸥-w1 小时前
前端学习python第二天手敲笔记整理
前端·python·学习
MageGojo1 小时前
10 种主题随机诗词:一个 API 解决小程序的诗词内容源
python·小程序·古诗词·api 接入
cooldream20091 小时前
使用 uv 管理 Python 虚拟环境:现代 Python 开发的高效实践
python·uv·mcp