vector.com|Contact
Search tips

apple banana
Find rows that contain at least one of the two words.

+apple +juice
Find rows that contain both words.

+apple macintosh
Find rows that contain the word 'apple', but rank rows higher if they also contain 'macintosh'.

+apple -macintosh
Find rows that contain the word 'apple' but not 'macintosh'.

+apple ~macintosh
Find rows that contain the word 'apple', but if the row also contains the word 'macintosh', rate it lower than if row does not. This is "softer" than a search for '+apple -macintosh', for which the presence of 'macintosh' causes the row not to be returned at all.

+apple +(>turnover >strudel)
Find rows that contain the words 'apple' and 'turnover', or 'apple' and 'strudel' (in any order), but rank 'apple turnover' higher than 'apple strudel'.

apple*
Find rows that contain words such as 'apple', 'apples', 'applesauce', or 'applet'.

"some words"
Find rows that contain the exact phrase 'some words' (for example, rows that contain 'some words of wisdom' but not "some noise words").

By continuing to use this site you agree to the use of cookies. For more information and to find out how to change this click here. Accept Cookies
Please enable cookies in your browser for this website.
Search:
Advanced search

CANoe/CANalyzer COM API with Python – Common Errors and Solutions

Last updated: 2020-04-16
Introduction

This document showcases some common issues and solutions, that can occur when trying to access CANoe/CANalyzer via a COM API Python client.

The following topics will be discussed in this article:

  1. Import errors after the installation of the pywin32 package
  2. Attribute errors when using CANoe COM components
  3. How to set the number of hardware channels in a CANoe configuration via COM
  4. How to use constants from the CANoe type library

Please note that this documentation refers to the makepy.py module from the pywin32 package in some parts. This module creates a .PY file from a registered COM type library, containing information about the available COM components of the COM server and can be helpful during the implementation of the client program.

Please refer to the documentation of the pywin32 package for further information about its features.

The documentation can be found in the Python installation directory under Lib\site-packages\win32com\HTML\docindex.html.

Installing the pywin32 package was successful. However, upon running a Python script using the win32com module, I receive the error
ImportError: DLL load failed while importing win32api: The specified module could not be found.

What can be done?

Answer 1:

This is a general installation issue. The following methods worked for us in the past:

  • Try reinstalling the pywin32 package.
  • Go to the Python installation directory, open the subfolder
    \Lib\site-packages\pywin32_system32 and copy & paste its two DLLs to the
    \Lib\site-packages\win32 folder.

I am trying to access a property or method of a COM component, but my client throws an attribute error:
AttributeError: ‘XXX’ object has no attribute ‘YYY’.

How can I fix this issue?

Answer 2:

There are different causes and solutions to this issue.

First Solution:

First, make sure that the correct CANoe type libraries are registered, if you have multiple CANoe versions installed on your system. In the CANoe help is documented which version introduced the feature. See column Availability:

If an older CANoe version is registered, this error will definitely occur.

You can find every COM component documented in the CANoe help via Technical References | COM Interface | Object Hierarchy.

Second Solution:

If the correct version is registered, you can use the method win32com.client.CastTo and explicitly cast the COM object to its latest interface version.

from win32com.client import *
from win32com.client.connect import *

mApp = DispatchEx('CANoe.Application')

mMeasurement = mApp.Measurement
mSystem = mApp.System
mNamespaces = mSystem.Namespaces
mNamespace = mNamespaces.Item("MyNamespace")
mVariables = mNamespace.Variables
mVariable = mVariables.Item("MyVariable")

#Casting mVariable to interface version IVariable11
mVariable = win32com.client.CastTo(mVariable, 'IVariable11')


print(mVariable.Value)
print(mVariable.Type)

The above sample shows how to use CastTo with the interface version IVariable11 of the COM component Variable. The available interface versions of the COM components can be found in the  file CANoe.h in the Exec32\COMdev folder or in the Python file of the CANoe type libraries, created in D:\TEMP\gen_py\ when running the module makepy.py from the win32com library.

Third Solution:

Another way of solving this issue is to delete the Python file created when makepy.py is executed on the registered CANoe Type Library.

The file is located in D:\TEMP\gen_py\ and should look something like this:

After deleting the file, casting the objects is not necessary anymore. However the CANoe.Application object needs to be instantiated with either “Dispatch” or “DispatchEx”.

Try not to use a method that basically runs makepy.py again like e.g. win32com.client.gencache.EnsureDispatch.

I am trying to use the GeneralSetup.Channels property like in the sample code, but I am unable to use it.

Example: The Python client shall set the number of available CAN channels to 4, just like the Visual Basic sample from the help entry:

Using it the same way in Python as in Visual Basic returns a syntax error:

from win32com.client import *
from win32com.client.connect import *

mApp = Dispatch('CANoe.Application')

mApp.Configuration.GeneralSetup.Channels(1)=4 #1 is cCAN

How can I set the number of available (CAN) channels in Python?

Answer 3:

Running makepy.py against the CANoe Type Library and inspecting the generated file provides further insights via the class definition IGeneralSetup3:

Using the SetChannels property from the generated code, we receive the expected result:

from win32com.client import *
from win32com.client.connect import *

mApp = Dispatch('CANoe.Application')


mApp.Configuration.GeneralSetup.SetChannels(1, 4) #1 is cCAN

How can I utilize the constants from the CANoe COM components? (E.g. cCAN, cLIN, cMOST, etc)?

Answer 4:

You can find all predefined constants in the Python class constants, which can also be found in the Python file generated with makepy.py.

Sample code:

from win32com.client import *
from win32com.client.connect import *

mApp = Dispatch('CANoe.Application')

mApp.Configuration.GeneralSetup.SetChannels(constants.cCAN, 4)

 
Article Options
2020-04-16
Views: 822