|
| 1 | +"""API for setup/usage of Canoe COM Client interface. |
| 2 | +""" |
| 3 | +#-------------------------------------------------------------------------- |
| 4 | +# Standard library imports |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import subprocess |
| 8 | +import win32com.client |
| 9 | + |
| 10 | +import time |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +#Vector Canoe Class |
| 15 | +class CANoe: |
| 16 | + |
| 17 | + def __init__(self): |
| 18 | + self.application = None |
| 19 | + #check if there is any instance of CANoe process |
| 20 | + output = subprocess.check_output('tasklist',shell=True) |
| 21 | + #if CANoe process is still available, kill the process |
| 22 | + if "CANoe32.exe" in str(output): |
| 23 | + os.system("taskkill /im CANoe32.exe /f 2>nul >nul") |
| 24 | + |
| 25 | + #re-dispatch object for CANoe Application |
| 26 | + self.application = win32com.client.Dispatch("CANoe.Application") |
| 27 | + |
| 28 | + |
| 29 | + def open_simulation(self,cfgname): |
| 30 | + #open CANoe simulation |
| 31 | + if (self.application != None): |
| 32 | + #check for valid file and it is *.cfg file |
| 33 | + if os.path.isfile(cfgname) and (os.path.splitext(cfgname)[1]==".cfg"): |
| 34 | + self.application.Open(cfgname) |
| 35 | + else: |
| 36 | + raise FileNotFoundError("Can't find CANoe cfg file") |
| 37 | + else: |
| 38 | + raise RuntimeError("CANoe Application is missing,unable to open simulation") |
| 39 | + |
| 40 | + def close_simulation(self): |
| 41 | + #close CANoe simulation |
| 42 | + if (self.application != None): |
| 43 | + self.stop_Measurement() |
| 44 | + self.application.Quit() |
| 45 | + |
| 46 | + #make sure the CANoe is close properly, otherwise enforce taskkill |
| 47 | + output = subprocess.check_output('tasklist',shell=True) |
| 48 | + |
| 49 | + if "CANoe32.exe" in str(output): |
| 50 | + os.system("taskkill /im CANoe32.exe /f 2>nul >nul") |
| 51 | + |
| 52 | + self.application = None |
| 53 | + |
| 54 | + def start_Measurement(self): |
| 55 | + retry = 0 |
| 56 | + retry_counter = 5 |
| 57 | + #try to establish measurement within 20s timeout |
| 58 | + while not self.application.Measurement.Running and (retry < retry_counter): |
| 59 | + |
| 60 | + self.application.Measurement.Start() |
| 61 | + time.sleep(1) |
| 62 | + retry += 1 |
| 63 | + if (retry == retry_counter): |
| 64 | + raise RuntimeWarning("CANoe start measuremet failed, Please Check Connection!") |
| 65 | + |
| 66 | + |
| 67 | + def stop_Measurement(self): |
| 68 | + if self.application.Measurement.Running: |
| 69 | + self.application.Measurement.Stop() |
| 70 | + else: |
| 71 | + pass |
| 72 | + |
| 73 | + def get_EnvVar(self,var): |
| 74 | + |
| 75 | + if (self.application != None): |
| 76 | + result = self.application.Environment.GetVariable(var) |
| 77 | + return result.Value |
| 78 | + else: |
| 79 | + raise RuntimeError("CANoe is not open,unable to GetVariable") |
| 80 | + |
| 81 | + def set_EnvVar(self,var,value): |
| 82 | + result = None |
| 83 | + |
| 84 | + if (self.application != None): |
| 85 | + #set the environment varible |
| 86 | + result = self.application.Environment.GetVariable(var) |
| 87 | + result.Value = value |
| 88 | + |
| 89 | + checker = self.get_EnvVar(var) |
| 90 | + #check the environment varible is set properly? |
| 91 | + while (checker != value): |
| 92 | + checker = self.get_EnvVar(var) |
| 93 | + |
| 94 | + |
| 95 | + else: |
| 96 | + raise RuntimeError("CANoe is not open,unable to SetVariable") |
| 97 | + |
| 98 | + def get_SigVal(self, channel_num, msg_name, sig_name, bus_type = "CAN"): |
| 99 | + """ |
| 100 | + @summary Get the value of a raw CAN signal on the CAN simulation bus |
| 101 | + @param channel_num - Integer value to indicate from which channel we will read the signal, usually start from 1, |
| 102 | + Check with CANoe can channel setup. |
| 103 | + @param msg_name - String value that indicate the message name to which the signal belong. Check DBC setup. |
| 104 | + @param sig_name - String value of the signal to be read |
| 105 | + @param bus_type - String value of the bus type - e.g. "CAN", "LIN" and etc. |
| 106 | + @return The CAN signal value in floating point value. |
| 107 | + Even if the signal is of integer type, we will still return by |
| 108 | + floating point value. |
| 109 | + @exception None |
| 110 | + """ |
| 111 | + if (self.application != None): |
| 112 | + result = self.application.GetBus(bus_type).GetSignal(channel_num, msg_name, sig_name) |
| 113 | + return result.Value |
| 114 | + else: |
| 115 | + raise RuntimeError("CANoe is not open,unable to GetVariable") |
| 116 | + |
| 117 | + |
0 commit comments