0

I am trying to automate canoe and I am getting some error while trying to create an object for testmodules in method "load_testenvironment". I am refering canoe-> help-> Technical reference -> COM interface but not much help.

Can anyone please tell me what I am doing wrong here?

Error : raise AttributeError( AttributeError: '<win32com.gen_py.CANoe 14.0 Type Library.ITestEnvironment instance at 0x1262718134176>' object has no attribute 'TestModules'

Code :

import time, os, msvcrt
from win32com.client import *
from win32com.client.connect import *

import win32com.client
import pythoncom

def DoEvents():
    pythoncom.PumpWaitingMessages()
    time.sleep(.1)


def DoEventsUntil(cond):
    while not cond():
        DoEvents()


class CanoeSync(object):
    """Wrapper class for CANoe Application object"""
    Started = False
    Stopped = False
    ConfigPath = ""
    CANOE_APP_NAME = "CANoe.Application"
    def __init__(self):
       # app = win32com.client.Dispatch(self.CANOE_APP_NAME)
        app = DispatchEx('CANoe.Application')
        app.Configuration.Modified = False
        ver = app.Version
        print('Loaded CANoe version ',
              ver.major, '.',
              ver.minor, '.',
              ver.Build, '...', sep='')
        self.App = app
        #self.Measurement = win32com.client.Dispatch(self.App.Measurement)
        self.Measurement  = self.App.Measurement

        self.Networks = None
        self.Devices = None
        self.Device = None
        self.Diagnostic = None

        self.Running = lambda: self.Measurement.Running
        #self.Networks = win32com.client.Dispatch(self.App.Networks)
        #self.network = None

        self.Testmodules = None
        self.environment_obj = dict()
        self.testmodule_obj = dict()

        self.WaitForStart = lambda: DoEventsUntil(lambda: CanoeSync.Started)
        self.WaitForStop = lambda: DoEventsUntil(lambda: CanoeSync.Stopped)
        WithEvents(self.App.Measurement, CanoeMeasurementEvents)

    def load_testenvironment(self):
        self.Testmodules =  self.App.Configuration.TestSetup.TestEnvironments.Item(1).TestModules

    def run_testenviroment(self,environment_name):
        pass


    def Load(self, cfgPath):
        # current dir must point to the script file
        cfg = os.path.join(os.curdir, cfgPath)
        cfg = os.path.abspath(cfg)
        print('Opening: ', cfg)
        self.ConfigPath = os.path.dirname(cfg)
        self.Configuration = self.App.Configuration
        self.App.Open(cfg)


    def Start(self):
        if not self.Running():
            self.Measurement.Start()
            self.WaitForStart()

    def Stop(self):
        if self.Running():
            self.Measurement.Stop()
            self.WaitForStop()



    def diagnostic_setup(self):
        self.Networks = self.App.Networks
        self.Devices = self.Networks.Item(1).Devices   #set only for one
        self.Device = self.Devices.Item(1)
        self.Diagnostic = self.Devices.Item(1).Diagnostic
        print(self.Device.Name)

    def tester_presentstatus(self):
        print(self.Diagnostic.TesterPresentStatus)


    def diag_request(self,request):
        diareq = self.Diagnostic.CreateRequest(request)
        diareq.Send()
        # wait until ECU response is pending
        while diareq.Pending:
            time.sleep(0.1)
        if diareq.Responses.Count == 0:
            return False
        else :
            response = list()
            print('count of resp:',diareq.Responses.Count)
            for num_of_resp in range(0, diareq.Responses.Count):
                resp = diareq.Responses(num_of_resp + 1)
                if resp.Positive :
                    for data in resp.Stream:
                        response.append(hex(data))
                else :
                    print(111,resp.ResponseCode)
            print(response)
            return response


    def diag_response(self):
        pass

class CanoeMeasurementEvents(object):
    """Handler for CANoe measurement events"""

    def OnStart(self):
        CanoeSync.Started = True
        CanoeSync.Stopped = False
        print("< measurement started >")

    def OnStop(self):
        CanoeSync.Started = False
        CanoeSync.Stopped = True
        print("< measurement stopped >")

2 Answers 2

0

Try this:

def load_testenvironment(self):
    testEnv = CastTo(self.App.Configuration.TestSetup.TestEnvironments.Item(1),"ITestEnvironment2")
    self.Testmodules = testEnv.TestModules

Explanation: Looking at the COM dll (Vector.CANoe.Interop.dll) the interface ITestEnvironment indeed does not have a property TestModules. However, the interface ITestEnvironment2 does: enter image description here

-2

There are many versions of the com interface, and the best way to do this is to use CastTo() to force the interface to the latest one, which you can see in canoe15\Exec32\COMdev\CANoe.h.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.