车载自动化介绍:
世界现在是越来越智能化了,为了提升效率和节约时间,就得不断学习和提升。所以来聊聊如何用Python调用canoe来实现车载自动化测试
车载自动化应用:
首先是安装win32库
- 打开cmd
- pip install pypiwin32
- 为了节约时间可以使用国内镜像 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()