新能源汽车自动化测试(一)

车载自动化介绍:

 

世界现在是越来越智能化了,为了提升效率和节约时间,就得不断学习和提升。所以来聊聊如何用Python调用canoe来实现车载自动化测试


车载自动化应用:

 首先是安装win32库

  1. 打开cmd
  2. pip install pypiwin32
  3. 为了节约时间可以使用国内镜像  pip -i http://mirrors.aliyun.com/pypi/simple/ pypiwin32

再来聊聊为什么要用win32的库

     windows系统上的软件,基于COM标准,开发各种组件Component,这些组件提供各种接口,其他组件通过接口使用它们,
组件需要经过注册才能被发现和使用注册后的组件作为服务端(COM Server),其他想要调用组件接口的组件作为客户端(COM Client)
CANoe在安装完成后已经windows系统中注册了COM组件

小案例测试代码

import os
import time
import sys
from win32com.client import *   #引入win32库
from win32com.client.connect import  *  #引入win32库
class CANoe:
    def __init__(self):
        self.application = None
        self.application = Dispatch("CANoe.Application")  # 实例化canoe,获得一个canoe进程对象,如果想再得到一个,用DispatchEx
        self.application.Configuration.Modified = False  # 启动时设置不可更改
        self.ver = self.application.Version  # 获得canoe的版本信息
        self.Running = self.application.Masurement.Running  # Measurement里面最常用的就是running,判断canoe是否在running  # 提取出版本号,类似于14.2.43(SP2)
        print('Loaded CANoe version',
              self.ver.major, '.',
              self.ver.minor, '.',
              self.ver.Build, '...')

        # 打开canoe的cfg文件

    def Open_cfg(self, cfgname):
        if self.application != None:
            self.application.open(cfgname)
            print("Opening: " + cfgname)
        else:
            raise RuntimeError("CANoe Application is missing,unable to open cfg")

    def OpenTestSetup(self, testsetup):
        self.TestSetup = self.application.Configuration.TestSetup
        path = os.path.join(self.ConfigPath, testsetup)
        testenv = self.TestSetup.TestEnvironments.Add(path)
        testenv = CastTo(testenv, "TestEnvironment")
        self.TestModules = []
        self.TraverseTestItem(testenv, lambda tm: self.TestModules.append(CanoeTestModule(tm)))


        # 关闭canoe的cfg文件

    def Close_cfg(self):
        if self.application != None:
            self.application.Quit(self)
            self.application = None

            # 运行canoe工程

    def Start(self):
        if not self.Running:
            self.application.Masurement.Start()

            # 停止canoe工程

    def Stop(self):
        if self.Running:
            self.application.Masurement.Stop()

            # 拿到Signal value

    def Get_SigVal(self, chanel_num, msg_name, sig_name, bus_type="CAN"):
        if (self.application != None):
            result = self.application.GetBus(bus_type).GetSignal(chanel_num, msg_name, sig_name)
            val = result.Value
            return val
        else:
            raise RuntimeError("CANoe is not Open, unable to get signal value")

            # 设置Signal value

    def Set_SigVal(self, chanel_num, msg_name, sig_name, bus_type, setValue):
        if (self.application != None):
            result = self.application.GetBus(bus_type).GetSignal(chanel_num, msg_name, sig_name)
            result.Value = setValue
        else:
            raise RuntimeError("CANoe is not Open, unable to set signal value")

            # 得到系统变量System value

    def Get_Sysvar(self, ns_name, sysvar_name):
        if (self.application != None):
            SysCAN = self.application.System.Namespaces
            sys_namespace = SysCAN(ns_name)
            sys_value = sys_namespace.Variables(sysvar_name)
            return sys_value.Value
        else:
            raise RuntimeError("CANoe is not Open, unable to get system value")

            # 设置环境变量System value

    def Set_Sysvar(self, ns_name, sysvar_name, var):
        if (self.application != None):
            SysCAN = self.application.System.Namespaces
            sys_namespace = SysCAN(ns_name)
            sys_value = sys_namespace.Variables(sysvar_name)
            sys_value.Value = var
        else:
            raise RuntimeError("CANoe is not Open, unable to set system value")


class CanoeTestModule:
    def __init__(self, tm):
        self.tm = tm
        self.Events = DispatchWithEvents(tm, CanoeTestEvents)
        self.Name = tm.Name
        self.IsDone = lambda: self.Events.stopped
        self.Enabled = tm.Enabled

    def Start(self):
        if self.tm.Enabled:
            self.tm.Start()
            self.Events.WaitForStart()


class CanoeTestEvents:
    def __init__(self):
        self.started = False
        self.stopped = False
        self.WaitForStart = lambda: self.started
        self.WaitForStop = lambda: self.stopped

    def OnStart(self):
        self.started = True
        self.stopped = False
        print("<", self.Name, " started >")

    def OnStop(self, reason):
        self.started = False
        self.stopped = True
        print("<", self.Name, " stopped >")


app = CANoe()  # 定义canoe为app
app.Open_cfg('J:\EasyTest\Easy\Easy.cfg')  # 定义要打开的canoe的cfg文件
time.sleep(5)  # 等待了5s
app.Close_cfg()

  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百里鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值