# This is a sample Python script.
import os
import sys
import subprocess
import time
import msvcrt
from win32com.client import *
from win32com.client.connect import *
from openpyxl import load_workbook
DDM_High = []
DDM_Low = []
PDM_High = []
PDM_Low = []
RLDM_High = []
RLDM_Low = []
RRDM_High = []
RRDM_Low = []
DDM_High_D= {'DU':'','HWSD':'','SXBL':'','SXDI-SXBL':'','SWBL':'','SXDI-SWBL':'','SWLM':'','SXDI-SWLM':'','SWP1':''}
DDM_Low_D= {'DU':'','HWSD':'','SXBL':'','SXDI-SXBL':'','SWBL':'','SXDI-SWBL':'','SWLM':'','SXDI-SWLM':'','SWP1':''}
PDM_High_D= {'DU':'','HWSD':'','SXBL':'','SXDI-SXBL':'','SWBL':'','SXDI-SWBL':'','SWLM':'','SXDI-SWLM':'','SWP1':''}
PDM_Low_D= {'DU':'','HWSD':'','SXBL':'','SXDI-SXBL':'','SWBL':'','SXDI-SWBL':'','SWLM':'','SXDI-SWLM':'','SWP1':''}
RLDM_High_D= {'DU':'','HWSD':'','SXBL':'','SXDI-SXBL':'','SWBL':'','SXDI-SWBL':'','SWLM':'','SXDI-SWLM':'','SWP1':''}
RLDM_Low_D= {'DU':'','HWSD':'','SXBL':'','SXDI-SXBL':'','SWBL':'','SXDI-SWBL':'','SWLM':'','SXDI-SWLM':'','SWP1':''}
RRDM_High_D= {'DU':'','HWSD':'','SXBL':'','SXDI-SXBL':'','SWBL':'','SXDI-SWBL':'','SWLM':'','SXDI-SWLM':'','SWP1':''}
RRDM_Low_D= {'DU':'','HWSD':'','SXBL':'','SXDI-SXBL':'','SWBL':'','SXDI-SWBL':'','SWLM':'','SXDI-SWLM':'','SWP1':''}
def versioncheck(versionstr):
versionstr=str(hex(ord(versionstr)))
versionstrL=versionstr.split('x')
return versionstrL[1]
def applydata(versionD,versionL):
versionD['DU']=versionL[0]
versionD['HWSD'] = versionL[1]
versionD['SXBL'] = versionL[2]
versionD['SXDI-SXBL'] = versionL[3]
versionD['SWBL'] = versionL[4]
versionD['SXDI-SWBL'] = versionL[5]
versionD['SWLM'] = versionL[6]
versionD['SXDI-SWLM'] = versionL[7]
def GetData():
wb=load_workbook(r"C:\Users\uic75642\Desktop\Python run CANOE\CX1EEX1E DCU SW part number list-202308016.xlsx")
worksheet=wb[input('please input the variant to test')]
R=int(input('please input the target row number'))
C=int(input('please input the target column number'))
CV=C+1
i=worksheet.max_column
n=worksheet.max_row
for row in range(R, n):
cellvalue = worksheet.cell(row, C)
cellvalueV = worksheet.cell(row, CV)
partnumber = str(cellvalue.value) + '20' + '20' + versioncheck(cellvalueV.value)
if row in range(3, 12):
DDM_Low.append(partnumber)
if row in range(12, 21):
DDM_High.append(partnumber)
if row in range(21, 30):
PDM_Low.append(partnumber)
if row in range(30, 39):
PDM_High.append(partnumber)
if row in range(39, 48):
RLDM_Low.append(partnumber)
if row in range(48, 57):
RLDM_High.append(partnumber)
if row in range(57, 66):
RRDM_Low.append(partnumber)
if row in range(66, 75):
RRDM_High.append(partnumber)
class CANoe:
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
def open_cfg(self, cfgname):
# open CANoe simulation
if (self.application != None):
# check for valid file and it is *.cfg file
if 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")
def close_cfg(self):
# close CANoe simulation
if (self.application != None):
print("close cfg ...")
# self.stop_Measurement()
self.application.Quit()
self.application = None
def start_Measurement(self):
retry = 0
retry_counter = 5
# try to establish measurement within 5s timeout
while not self.application.Measurement.Running and (retry < retry_counter):
self.application.Measurement.Start()
time.sleep(1)
retry += 1
if (retry == retry_counter):
raise RuntimeWarning("CANoe start measuremet failed, Please Check Connection!")
def stop_Measurement(self):
if self.application.Measurement.Running:
self.application.Measurement.Stop()
else:
pass
def get_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")
def get_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")
def set_EnvVar(self, var, value):
result = None
if (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")
def get_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")
def set_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")
def DoEvents(self):
pythoncom.PumpWaitingMessages()
time.sleep(1)
def ApplyDtaToCANOE(self):
if self.get_SysVar("TestOrder", "DDM") == 1:
if self.get_SysVar("TestOrder", "High") == 1:
self.set_SysVar("TestOrder", "DU", DDM_High_D.get('DU'))
self.set_SysVar("TestOrder", "HWSD", DDM_High_D.get('HWSD'))
self.set_SysVar("TestOrder", "SXBL", DDM_High_D.get('SXBL'))
self.set_SysVar("TestOrder", "SXDISXBL", DDM_High_D.get('SXDI-SXBL'))
self.set_SysVar("TestOrder", "SWBL", DDM_High_D.get('SWBL'))
self.set_SysVar("TestOrder", "SXDISWBL", DDM_High_D.get('SXDI-SWBL'))
self.set_SysVar("TestOrder", "SWLM", DDM_High_D.get('SWLM'))
self.set_SysVar("TestOrder", "SXDISWLM", DDM_High_D.get('SXDI-SWLM'))
self.set_SysVar("TestOrder", "SWP1", DDM_High_D.get('SWP1'))
if self.get_SysVar("TestOrder", "Low") == 1:
self.set_SysVar("TestOrder", "DU", DDM_Low_D.get('DU'))
self.set_SysVar("TestOrder", "HWSD", DDM_Low_D.get('HWSD'))
self.set_SysVar("TestOrder", "SXBL", DDM_Low_D.get('SXBL'))
self.set_SysVar("TestOrder", "SXDISXBL", DDM_Low_D.get('SXDI-SXBL'))
self.set_SysVar("TestOrder", "SWBL", DDM_Low_D.get('SWBL'))
self.set_SysVar("TestOrder", "SXDISWBL", DDM_Low_D.get('SXDI-SWBL'))
self.set_SysVar("TestOrder", "SWLM", DDM_Low_D.get('SWLM'))
self.set_SysVar("TestOrder", "SXDISWLM", DDM_Low_D.get('SXDI-SWLM'))
self.set_SysVar("TestOrder", "SWP1", DDM_Low_D.get('SWP1'))
if self.get_SysVar("TestOrder", "PDM") == 1:
if self.get_SysVar("TestOrder", "High") == 1:
self.set_SysVar("TestOrder", "DU", PDM_High_D.get('DU'))
self.set_SysVar("TestOrder", "HWSD", PDM_High_D.get('HWSD'))
self.set_SysVar("TestOrder", "SXBL", PDM_High_D.get('SXBL'))
self.set_SysVar("TestOrder", "SXDISXBL", PDM_High_D.get('SXDI-SXBL'))
self.set_SysVar("TestOrder", "SWBL", PDM_High_D.get('SWBL'))
self.set_SysVar("TestOrder", "SXDISWBL", PDM_High_D.get('SXDI-SWBL'))
self.set_SysVar("TestOrder", "SWLM", PDM_High_D.get('SWLM'))
self.set_SysVar("TestOrder", "SXDISWLM", PDM_High_D.get('SXDI-SWLM'))
self.set_SysVar("TestOrder", "SWP1", PDM_High_D.get('SWP1'))
if self.get_SysVar("TestOrder", "Low") == 1:
self.set_SysVar("TestOrder", "DU", PDM_Low_D.get('DU'))
self.set_SysVar("TestOrder", "HWSD", PDM_Low_D.get('HWSD'))
self.set_SysVar("TestOrder", "SXBL", PDM_Low_D.get('SXBL'))
self.set_SysVar("TestOrder", "SXDISXBL", PDM_Low_D.get('SXDI-SXBL'))
self.set_SysVar("TestOrder", "SWBL", PDM_Low_D.get('SWBL'))
self.set_SysVar("TestOrder", "SXDISWBL", PDM_Low_D.get('SXDI-SWBL'))
self.set_SysVar("TestOrder", "SWLM", PDM_Low_D.get('SWLM'))
self.set_SysVar("TestOrder", "SXDISWLM", PDM_Low_D.get('SXDI-SWLM'))
self.set_SysVar("TestOrder", "SWP1", PDM_Low_D.get('SWP1'))
if self.get_SysVar("TestOrder", "RLDM") == 1:
if self.get_SysVar("TestOrder", "High") == 1:
self.set_SysVar("TestOrder", "DU", RLDM_High_D.get('DU'))
self.set_SysVar("TestOrder", "HWSD", RLDM_High_D.get('HWSD'))
self.set_SysVar("TestOrder", "SXBL", RLDM_High_D.get('SXBL'))
self.set_SysVar("TestOrder", "SXDISXBL", RLDM_High_D.get('SXDI-SXBL'))
self.set_SysVar("TestOrder", "SWBL", RLDM_High_D.get('SWBL'))
self.set_SysVar("TestOrder", "SXDISWBL", RLDM_High_D.get('SXDI-SWBL'))
self.set_SysVar("TestOrder", "SWLM", RLDM_High_D.get('SWLM'))
self.set_SysVar("TestOrder", "SXDISWLM", RLDM_High_D.get('SXDI-SWLM'))
self.set_SysVar("TestOrder", "SWP1", RLDM_High_D.get('SWP1'))
if self.get_SysVar("TestOrder", "Low") == 1:
self.set_SysVar("TestOrder", "DU", RLDM_Low_D.get('DU'))
self.set_SysVar("TestOrder", "HWSD", RLDM_Low_D.get('HWSD'))
self.set_SysVar("TestOrder", "SXBL", RLDM_Low_D.get('SXBL'))
self.set_SysVar("TestOrder", "SXDISXBL", RLDM_Low_D.get('SXDI-SXBL'))
self.set_SysVar("TestOrder", "SWBL", RLDM_Low_D.get('SWBL'))
self.set_SysVar("TestOrder", "SXDISWBL", RLDM_Low_D.get('SXDI-SWBL'))
self.set_SysVar("TestOrder", "SWLM", RLDM_Low_D.get('SWLM'))
self.set_SysVar("TestOrder", "SXDISWLM", RLDM_Low_D.get('SXDI-SWLM'))
self.set_SysVar("TestOrder", "SWP1", RLDM_Low_D.get('SWP1'))
if self.get_SysVar("TestOrder", "RRDM") == 1:
if self.get_SysVar("TestOrder", "High") == 1:
self.set_SysVar("TestOrder", "DU", RRDM_High_D.get('DU'))
self.set_SysVar("TestOrder", "HWSD", RRDM_High_D.get('HWSD'))
self.set_SysVar("TestOrder", "SXBL", RRDM_High_D.get('SXBL'))
self.set_SysVar("TestOrder", "SXDISXBL", RRDM_High_D.get('SXDI-SXBL'))
self.set_SysVar("TestOrder", "SWBL", RRDM_High_D.get('SWBL'))
self.set_SysVar("TestOrder", "SXDISWBL", RRDM_High_D.get('SXDI-SWBL'))
self.set_SysVar("TestOrder", "SWLM", RRDM_High_D.get('SWLM'))
self.set_SysVar("TestOrder", "SXDISWLM", RRDM_High_D.get('SXDI-SWLM'))
self.set_SysVar("TestOrder", "SWP1", RRDM_High_D.get('SWP1'))
if self.get_SysVar("TestOrder", "Low") == 1:
self.set_SysVar("TestOrder", "DU", RRDM_Low_D.get('DU'))
self.set_SysVar("TestOrder", "HWSD", RRDM_Low_D.get('HWSD'))
self.set_SysVar("TestOrder", "SXBL", RRDM_Low_D.get('SXBL'))
self.set_SysVar("TestOrder", "SXDISXBL", RRDM_Low_D.get('SXDI-SXBL'))
self.set_SysVar("TestOrder", "SWBL", RRDM_Low_D.get('SWBL'))
self.set_SysVar("TestOrder", "SXDISWBL", RRDM_Low_D.get('SXDI-SWBL'))
self.set_SysVar("TestOrder", "SWLM", RRDM_Low_D.get('SWLM'))
self.set_SysVar("TestOrder", "SXDISWLM", RRDM_Low_D.get('SXDI-SWLM'))
self.set_SysVar("TestOrder", "SWP1", RRDM_Low_D.get('SWP1'))
GetData()
applydata(DDM_High_D,DDM_High)
applydata(DDM_Low_D,DDM_Low)
applydata(PDM_High_D,PDM_High)
applydata(PDM_Low_D,PDM_Low)
applydata(RLDM_High_D,RLDM_High)
applydata(RLDM_Low_D,RLDM_Low)
applydata(RRDM_High_D,RRDM_High)
applydata(RRDM_Low_D,RRDM_Low)
app = CANoe() #定义CANoe为app
app.open_cfg(r"C:\Users\uic75642\Desktop\Python run CANOE\ZEEKR_DCU_EX1E_V1.3\ZEEKR_DCU_EX1E_V1.3\ZEEKR_DCU_EX1E_CANFD_withBoday_VTS_CANoe10_V1.3.cfg") #导入某个CANoe congif
time.sleep(5)
app.start_Measurement()
while not msvcrt.kbhit():
app.ApplyDtaToCANOE()
app.DoEvents()
#app.co