#13.0版本CANoe PYTHON 模板路径 C:\Users\Public\Documents\Vector\CANoe\Sample Configurations 13.0.155\Programming\Python# coding: utf-8"""API for setup/usage of Canoe COM Client interface.
"""# --------------------------------------------------------------------------# Standard library importsimport os
import sys
import subprocess
import time
import msvcrt
import serial
import requests #python 控制钉钉专用import json #python 控制钉钉专用from win32com.client import*from win32com.client.connect import*# Vector Canoe ClassclassCANoe:def__init__(self):
self.application =None
self.application = DispatchEx("CANoe.Application")
self.ver = self.application.Version
print('Loaded CANoe version ',
self.ver.major,'.',
self.ver.minor,'.',
self.ver.Build,'...')#, sep,''
self.Measurement = self.application.Measurement.Running
defopen_cfg(self, cfgname):# open CANoe simulationif(self.application !=None):# check for valid file and it is *.cfg fileif os.path.isfile(cfgname)and(os.path.splitext(cfgname)[1]==".cfg"):
self.application.Open(cfgname)print("opening..."+cfgname)else:raise RuntimeError("Can't find CANoe cfg file")else:raise RuntimeError("CANoe Application is missing,unable to open simulation")defclose_cfg(self):if(self.application !=None):# self.stop_Measurement()
self.application.Quit()
self.application =Nonedefstart_Measurement(self):
retry =0
retry_counter =5# try to establish measurement within 5s timeoutwhilenot self.application.Measurement.Running and(retry < retry_counter):
self.application.Measurement.Start()
time.sleep(1)
retry +=1if(retry == retry_counter):raise RuntimeWarning("CANoe start measuremet failed, Please Check Connection!")defstop_Measurement(self):if self.application.Measurement.Running:
self.application.Measurement.Stop()else:passdefget_SigVal(self, channel_num, msg_name, sig_name, bus_type="CAN"):"""
@summary Get the value of a raw CAN signal on the CAN simulation bus
@param channel_num - Integer value to indicate from which channel we will read the signal, usually start from 1,
Check with CANoe can channel setup.
@param msg_name - String value that indicate the message name to which the signal belong. Check DBC setup.
@param sig_name - String value of the signal to be read
@param bus_type - String value of the bus type - e.g. "CAN", "LIN" and etc.
@return The CAN signal value in floating point value.
Even if the signal is of integer type, we will still return by
floating point value.
@exception None
"""if(self.application !=None):
result = self.application.GetBus(bus_type).GetSignal(channel_num, msg_name, sig_name)return result.Value
else:raise RuntimeError("CANoe is not open,unable to GetVariable")defget_EnvVar(self, var):if(self.application !=None):
result = self.application.Environment.GetVariable(var)return result.Value
else:raise RuntimeError("CANoe is not open,unable to GetVariable")defset_EnvVar(self, var, value):
result =Noneif(self.application !=None):# set the environment varible
result = self.application.Environment.GetVariable(var)
result.Value = value
checker = self.get_EnvVar(var)# check the environment varible is set properly?while(checker != value):
checker = self.get_EnvVar(var)else:raise RuntimeError("CANoe is not open,unable to SetVariable")defget_SysVar(self, ns_name, sysvar_name):if(self.application !=None):
systemCAN = self.application.System.Namespaces
sys_namespace = systemCAN(ns_name)
sys_value = sys_namespace.Variables(sysvar_name)return sys_value.Value
else:raise RuntimeError("CANoe is not open,unable to GetVariable")defset_SysVar(self, ns_name, sysvar_name, var):if(self.application !=None):
systemCAN = self.application.System.Namespaces
sys_namespace = systemCAN(ns_name)
sys_value = sys_namespace.Variables(sysvar_name)
sys_value.Value = var
else:raise RuntimeError("CANoe is not open,unable to GetVariable")defDoEvents(self):#作用纯粹的阻塞进程
pythoncom.PumpWaitingMessages()
time.sleep(1)"""
portx="COM44"
bps=9600
timex=5
ser=serial.Serial(portx,bps,timeout=timex)
"""
app = CANoe()#定义CANoe为app
app.open_cfg(r"F://台架实验.cfg")#导入某个CANoe congif
time.sleep(5)
app.start_Measurement()#让CANoe 运行起来相当于点下了 闪电标志#def dingmessage():# 请求的URL,WebHook地址,,,发现bug时启动发送# webhook = "https://oapi.dingtalk.com/robot/send?access_token=2ca480540a81a8013c2be88680bebf560138f9dc93c80c8452d9ca"#构建请求头部# header = {# "Content-Type": "application/json",# "Charset": "UTF-8"#}#构建请求数据# tex0 = "远程测试+请注意发现了太网频繁linkdown的bug"# tex1 = "远程测试+请注意发现了someip无法建立的的bug"# tex2 = "远程测试+请注意发现了DUT不休眠的bug"# tex3 = "远程测试+请注意发现了busoff的bug"# tex4 = "远程测试+请注意发现了远程控制失败的bug"# tex5 = "远程测试+请注意发现了休眠唤醒后DUT起不来的bug"#message ={# "msgtype": "text",# "text": {# "content": tex# },# "at": {# "isAtAll": True# }# }#对请求的数据进行json封装#message_json = json.dumps(message)#发送请求#info = requests.post(url=webhook,data=message_json,headers=header)#打印返回的结果#print(info.text)#if __name__=="__main__":# dingmessage()# 需要实现如何在CAPL里监测linkdown, someip未建立的情况,本重点任务需要在CAPL里完成whilenot msvcrt.kbhit():#构建请求数据
webhook ="https://oapi.dingtalk.com/robot/send?access_token=2ca486e540a81a8013c2be88680bebf560138f9dc93c80c8452d9ca"
header ={"Content-Type":"application/json","Charset":"UTF-8"}
tex0 ="远程测试+请注意发现了太网频繁linkdown的bug"
tex1 ="远程测试+请注意发现了someip无法建立的的bug"
tex2 ="远程测试+请注意发现了DUT不休眠的bug"
tex3 ="远程测试+请注意发现了busoff的bug"
tex4 ="远程测试+请注意发现了远程控制失败的bug"
tex5 ="远程测试+请注意发现了休眠唤醒后DUT起不来的bug"
tex6 ="远程测试+请注意发现了某一时间点不能上网问题"
TGS_LEVER = app.get_SigVal(channel_num=1, msg_name="FD2", sig_name="TRS_LEVER", bus_type="CAN")#直接获取信号 print("代表报警信号数值",TRS_LEVER)if TGS_LEVER==1:#发生了频繁linkdown
tex=tex0
message ={"msgtype":"text","text":{"content": tex
},"at":{"isAtAll":True}}
message_json = json.dumps(message)
info = requests.post(url=webhook,data=message_json,headers=header)elif TRS_LEVER==2:#代表发生了Someip 无法建立问题
tex=tex1
message ={"msgtype":"text","text":{"content": tex
},"at":{"isAtAll":True}}
message_json = json.dumps(message)
info = requests.post(url=webhook,data=message_json,headers=header)
TRS_LEVER=0elif TRS_LEVER==3:#代表发生了wakeup 后 DUT不工作的问题
tex=tex5
message ={"msgtype":"text","text":{"content": tex
},"at":{"isAtAll":True}}
message_json = json.dumps(message)
info = requests.post(url=webhook,data=message_json,headers=header)
TRS_LEVER=0elif TRS_LEVER==4:#代表发生了DUT不休眠的bug
tex=tex2
message ={"msgtype":"text","text":{"content": tex
},"at":{"isAtAll":True}}
message_json = json.dumps(message)
info = requests.post(url=webhook,data=message_json,headers=header)
TRS_LEVER=0elif TRS_LEVER==6:#代表发生了DUT不休眠的bug
tex=tex6
message ={"msgtype":"text","text":{"content": tex
},"at":{"isAtAll":True}}
message_json = json.dumps(message)
info = requests.post(url=webhook,data=message_json,headers=header)
TGS_LEVER=0"""SPowerMode2 = app.get_SigVal(channel_num=1, msg_name="FD6", sig_name="SystemPowerMode", bus_type="CAN") #直接获取信号
print("车辆电源模式",SPowerMode2)
if SPowerMode==2:#代表电源模式ON
RelayOnData=[0x33,0x01,0x12,0x00,0x00,0x00,0x01,0x47]
result=ser.write(RelayOnData)
elif SPowerMode==0:#代表电源模式OFF
RelayOffData=[0x33,0x01,0x11,0x00,0x00,0x00,0x01,0x46]
result=ser.write(RelayOffData)
V_DriverDoorSts = app.get_EnvVar("Var_DoorSts")#获取环境变量
if(V_DriverDoorSts==1):
app.set_EnvVar("Var_DoorSts",1)#直接处理环境变量
Network_Switch = app.get_SysVar("huts","NetSwitch")#获取系统变量
if(Network_Switch==0):
app.set_SysVar("huts","NetSwitch",0)#直接处理系统变量
"""
app.DoEvents()
app.close_cfg()