Restrictions Abstract Table of Contents 1 2 CANoe/CANalyzer as a COM Server Version 1.2 Application Note AN-AND-1-117 Public Document This application note is a general introduction to the COM server functionality of CANoe and CANalyzer using Microsoft Visual Basic examples. 1.1... 2 1.2 COM (Component Object Model)... 2 1.3 COM CANoe/CANalyzer... 3 2 Microsoft Visual BASIC COM... 3 2.1... 3 2.2... 4 2.3... 5 2.4 (CANoe )... 7 2.5 CANoe/CANalyzer... 9 2.6 CAPL... 9 2.7 CAN... 13 3 Microsoft Visual Basic COM... 14 3.1...14 3.2... 16 4... 20 Copyright 2003 - Vector CANtech, Inc. Contact Information: http://www.vector-japan.co.jp or 81-3-5769-6971 1
CANoe/CANalyzer as a COM Server 1 1.1 CANoe CANalyzer CAN LINMOST FlexRay CANalyzer CANoe CANoe CANoe/CANalyzer COM COM Microsoft Visual Basic CANoe/CANalyzer COM 1.2 COM (Component Object Model) COM Microsoft Application Note AN-AND-1-117 2
CANoe/CANalyzer as a COM Server 1.3 COM CANoe/CANalyzer CANoe/CANalyzer 3.0 COM COM CANoe/CANalyzer / COM CANoe/CANalyzer CANoe/CANalyzer CANoe/CANalyzer COM Microsoft Windows Script VBScript Jscript Microsoft Windows 98Windows NTWindows 2000 Microsoft Excel Microsoft Word Microsoft Visual BasicMicrosoft Visual C++ Borland Delphi 2 Microsoft Visual BASIC COM 2.1 Microsoft Visual Basic Microsoft Windows Rapid Application Development (RAD) COM Microsoft Visual Basic CANoe/CANalyzer COM CANoe/CANalyzer COM COM 3 COM 1) CANController.Baudrate 2) Measurement.Start 3) Application.OnQuit COM COM COM CANoe CANalyzer Application Note AN-AND-1-117 3
CANoe/CANalyzer as a COM Server 1 CANalyzer Type Library Microsoft Visual Basic COM Microsoft Visual Basic CANalyzer ( CANoe) Microsoft Visual Basic 1 2.2 CANalyzer/CANoe Type Library Microsoft Visual Basic COM CANalyzer/CANoe COM Visual Basic COM Application Dim gcanapp As CANalyzer.Application COM Application Microsoft Visual Basic Set gcanapp = New Application CANalyzer CANalyzer COM CANoe/CANalyzer COM COM Application Open() gcanapp.open ("C:\Program Files\CANalyzer\Demo_CL\motbus.cfg") CAPL (CAN Application Programming Language CANoe/CANalyzer ) CAPL Application Note AN-AND-1-117 4
CANoe/CANalyzer as a COM Server COM CAPL Compile() CAPL Application gcanapp CAPL gcanapp.capl.compile Measurement / CAPL Measurement Application gcanapp Measurement Measurement Start() Stop() gcanapp.measurement.start COM Application Quit() CANalyzer CANalyzer Measurement Running CANalyzer If gcanapp.measurement.running Then stop the CANalyzer measurement gcanapp.measurement.stop quit the CANalyzer application gcanapp.quit 2.3 CANalyzer CANdb CANdb.DBC CANdb CANalyzer CAN CAN 8 CAN 1 CANdb Application Note AN-AND-1-117 5
CANoe/CANalyzer as a COM Server 2 CANdb++CAN 2 CANdb++ Database Editor CANdb Window CAN EngineData Window CANdb CAN EngineData ID 64 (hex) 4 4 EngSpeed 16 (2 ) EngineData CAN 0 1 CANdb EngSpeed CANdb CAN CANdb CAN CAN CANoe/CANalyzer CANdb COM CANdb Visual Basic Signal Signal Dim gengspeedsignal As CANalyzer.Signal Bus GetSignal() CAN gengspeedsignal CANalyzer/CANoe CANdb Bus Application gcanapp Bus Set gengspeedsignal = gcanapp.bus.getsignal(1, "EngineData", "EngSpeed") Application Note AN-AND-1-117 6
CANoe/CANalyzer as a COM Server GetSignal() 3 gengspeedsignal Signal Value lblengspeeddisplaycontrol.caption = gengspeedsignal.value COM 2.7 CAN COM CAPL Visual Basic CAPL CAPL CAN 2.4 (CANoe ) CANoe CAN CAN CAN CAN ( ) CANoe COM Application Note AN-AND-1-117 7
CANoe/CANalyzer as a COM Server 3 CANdb++ CANdb 3 CANdb++ Database Editor CANdb GUI (Graphical User Interface) CANoe GUI ( ) CANdb GUI CAPL (CAN Access Programming Language) 4 GUI 4 GUI 0 Env_Switch0 Env_Switch0 0 1 CAPL COM Microsoft Visual Basic GUI COM CAN CAN Visual Basic EnvironmentVariable Signal Dim gswitch0envvar As CANoe.EnvironmentVariable Application Note AN-AND-1-117 8
CANoe/CANalyzer as a COM Server Environment GetVariable() gswitch0envvar CANoe CANdb Environment Application gcanapp Environment Set gswitch0envvar = gcanapp.environment.getvariable("env_switch0") GetVariable() 1 CANdb gswitch0envvar EnvironmentVariable Value gswitch0envvar.value = 1 2.5 CANoe/CANalyzer COM CANalyzer/CANoe Measurement OnStart CANalyzer/CANoe COM CANalyzer/CANoe COM Visual Basic COM COM 2.2 Application Dim gcanapp As CANalyzer.Application Visual Basic WithEvents Dim WithEvents gcanapp As CANalyzer.Application COM Visual Basic Visual Basic <object variable name>_<event name>() Application OnQuit Private Sub gcanapp_onquit() create application specific OnQuit event handler here.. End Sub Visual Basic CANalyzer COM 2.6 CAPL CAPL CANalyzer/CANoe CAPL CAN CAN CAPL COM COM Microsoft Visual Basic CAPL COM CAPL CANalyzer/CANoe CAPL 5 CAPL CAPL Application Note AN-AND-1-117 9
CANoe/CANalyzer as a COM Server 5 CAPL COM CAPL CAPLFunction CAPL CAPL CAPL GetFunction() GetFunction() CAPL CAPL Measurement OnInit Measurement Visual Basic WithEvents Application Note AN-AND-1-117 10
CANoe/CANalyzer as a COM Server CAPL CAPL CAPLFunction Call() CANalyzer objects Dim WithEvents gcanapp As CANalyzer.Application Dim WithEvents gcanmeasurement As CANalyzer.Measurement Dim gwritetextfunction As CANalyzer.CAPLFunction Initialization of CANalyzer objects Set gcanapp = New Application Set gcanmeasurement = gcanapp.measurement Measurement OnInit event handler Private Sub gcanmeasurement_oninit() assign CAPL function Set gwritetextfunction = gcanapp.capl.getfunction("writetext") End Sub Call WriteText() CAPL function gwritetextfunction.call CAPL CAPL 10 CAPL Microsoft Visual Basic CAPL CAPL long byte (1 ) int(eger) (2 ) long (4 ) Microsoft Visual Basic Microsoft Visual Basic CAPL CANalyzer objects Dim WithEvents gcanapp As CANalyzer.Application Dim WithEvents gcanmeasurement As CANalyzer.Measurement Dim gmultiplyfunction As CANalyzer.CAPLFunction Dim gmultiplyresult As Integer Initialization of CANalyzer objects Set gcanapp = New Application Set gcanmeasurement = gcanapp.measurement Measurement OnInit event handler Private Sub gcanmeasurement_oninit() assign CAPL function Set gmultiplyfunction = gcanapp.capl.getfunction("multiply") End Sub multiply two values and get the result in the CAPL function s return value gmultiplyresult = gmultiplyfunction.call (4, 5) Application Note AN-AND-1-117 11
CANoe/CANalyzer as a COM Server Figure 6 CAPL Multiply() CAPL integer COM CANalyzer CAPL P Block CANalyzer CAPL CANalyzer 7 ( ) Application Note AN-AND-1-117 12
CANoe/CANalyzer as a COM Server 7 CANalyzer 2.7 CAN COM CAN CAPL output() CAPL output() CAN CAPL CAPL COM CAPL CAN CAPL ( 2.6 ) CANalyzer CAPL output() CANalyzer (7 ) CAPL COM CAPL CANalyzer CAPL CANalyzer CAPL CAN CAN Window CAN CAN CAPL CANalyzer COM CAPL output() CAPL COM CAPL output() CAN CAPL output() CAN Application Note AN-AND-1-117 13
CANoe/CANalyzer as a COM Server CANalyzer COM CAN CAPL COM CANalyzer CAN Microsoft Visual Basic CANalyzer CAN COM CAPL SendMsg() CAPL TxTimer_1ms 1 CAPL CAN COM CAPL CAPL (8 ) 8 CANalyzer CAN 3 Microsoft Visual Basic COM 3.1 9 Microsoft Visual Basic COM CANalyzer Motbus.cfg CANalyzer Application Note AN-AND-1-117 14
CANoe/CANalyzer as a COM Server 9 Application Note AN-AND-1-117 15
CANoe/CANalyzer as a COM Server 3.2 GLOBALS: objects: Dim WithEvents gcanapp As CANalyzer.Application Dim WithEvents gcanmeasurement As CANalyzer.Measurement signals: Dim gengspeedsignal As CANalyzer.Signal Dim gcarspeedsignal As CANalyzer.Signal Dim gengtempsignal As CANalyzer.Signal Dim gengstatussignal As CANalyzer.Signal CAPL functions Dim gsendgearfunction As CANalyzer.CAPLFunction CAN Channel 1 Dim gcanchannel1 As CANalyzer.CANController indicator flags Dim gconnected As Boolean Dim gmotbus As Boolean NAME: CanConnect DESCRIPTION: Initializes CANalyzer object variables and sets some indication flags used in the application Private Sub CanConnect() init new CANalyzer application Set gcanapp = New Application init measurement object Set gcanmeasurement = gcanapp.measurement init CAN channel variable of bustype CAN (=1) and CAN channel 1 Set gcanchannel1 = gcanapp.configuration.generalsetup.controllersetup(1, 1) indicate that the connection has been made gconnected = True check if MotBus.cfg configuration is loaded If (gcanapp.configuration.name = "motbus") Then gmotbus = True Else gmotbus = False End Sub *** end of CanConnect *** NAME: UpdateMotBusSignals DESCRIPTION: Read the signals values from the CAN messages and displays these values on the form Private Sub UpdateMotBusSignals() If (gmotbus = True) Then prgenginespeed.value = gengspeedsignal.value lblenginespeedval.caption = gengspeedsignal.value prgvehiclespeed.value = gcarspeedsignal.value lblvehiclespeedval.caption = gcarspeedsignal.value lblenginetemperatureval.caption = gengtempsignal.value If (gengstatussignal.value) Then lblenginestatusval.caption = "Idle" Else lblenginestatusval.caption = "Running" Application Note AN-AND-1-117 16
CANoe/CANalyzer as a COM Server End Sub *** end of UpdateMotBusSignals *** NAME: GetCANChannelParameters DESCRIPTION: Read the CAN channel parameters using the COM server and displays it using labels. Private Sub GetCANChannelParameters() lblbaudratevalue = gcanchannel1.baudrate lblbtr0value = gcanchannel1.btr0 lblbtr1value = gcanchannel1.btr1 lblsamplesvalue = gcanchannel1.samples End Sub *** end of GetCANChannelParameters *** NAME: ResetDisplayControls DESCRIPTION: Resets all the controls related to the MotBus signals Private Sub ResetDisplayControls() prgenginespeed.value = 0 lblenginespeedval.caption = "" prgvehiclespeed.value = 0 lblvehiclespeedval.caption = "" lblenginetemperatureval.caption = "" lblenginestatusval.caption = "" lblbaudratevalue = "" lblbtr0value = "" lblbtr1value = "" lblsamplesvalue = "" End Sub *** end of ResetDisplayControls *** NAME: btndbdialog_click DESCRIPTION: Opens CANalyzer database dialog Private Sub btndbdialog_click() gcanapp.ui.opendbdialog End Sub *** end of btndbdialog_click *** NAME: btnopenconfig_click DESCRIPTION: Opens the configuration as specified in the textbox and compiles the CAPL code. It also stops all running measurements Private Sub btnopenconfig_click() If (gconnected = False) Then make the connection CanConnect If (gcanapp.measurement.running) Then stop the CANalyzer measurement gcanapp.measurement.stop load an existing CANalyzer configuration gcanapp.open (txtconfiglocation.text) Application Note AN-AND-1-117 17
CANoe/CANalyzer as a COM Server compile CAPL code of the CANalyzer configuration if any available gcanapp.capl.compile check if MotBus.cfg configuration is loaded If (gcanapp.configuration.name = "motbus") Then gmotbus = True Else gmotbus = False enable database dialog button btndbdialog.enabled = True End Sub *** end of btnopenconfig_click *** NAME: btnstartmeasurement_click DESCRIPTION: Start the measurement if no measurements are running, otherwise it stops the measurement Private Sub btnstartmeasurement_click() If (btnstartmeasurement.caption = "Start Measurement") Then If (gconnected = False) Then make the connection CanConnect start a CANalyzer measurement gcanapp.measurement.start Else stop a CANalyzer measurement gcanapp.measurement.stop End Sub *** end of btnstartmeasurement_click*** NAME: gcanmeasurement_onstart (event handler) DESCRIPTION: Disables the open configuration button and changes the caption of the measurement button Private Sub gcanmeasurement_onstart() disable open configuration functionality btnopenconfig.enabled = False change the caption of the button btnstartmeasurement.caption = "Stop Measurement" display CAN channel parameters GetCANChannelParameters enable the timer tmrsignals.enabled = True End Sub *** end of gcanmeasurement_onstart *** NAME: gcanmeasurement_onstop (event handler) DESCRIPTION: Enables the open configuration button and changes the caption of the measurement button Private Sub gcanmeasurement_onstop() disable the timer tmrsignals.enabled = False enable open configuration functionality btnopenconfig.enabled = True change the caption of the button btnstartmeasurement.caption = "Start Measurement" update controls on form ResetDisplayControls Application Note AN-AND-1-117 18
CANoe/CANalyzer as a COM Server End Sub *** end of gcanmeasurement_onstop *** NAME: gcanapp_onquit (event handler) DESCRIPTION: Sets a flag to indicate that the connection with the COM server is no longer there Private Sub gcanapp_onquit() indicate that MotBus.cfg is not loaded gmotbus = False disnable the timer tmrsignals.enabled = False indicate that the connection is no longer there gconnected = False reset signals Set gengspeedsignal = Nothing Set gcarspeedsignal = Nothing Set gengtempsignal = Nothing Set gengstatussignal = Nothing reset CANalyzer object globals Set gcanapp = Nothing Set gcanmeasurement = Nothing Set gcanchannel1 = Nothing End Sub *** end of gcanapp_onquit *** NAME: gcanmeasurement_oninit (event handler) DESCRIPTION: Initializes the measurement signals of the MotBus.cfg configuration if this one is loaded Private Sub gcanmeasurement_oninit() If (gmotbus = True) Then assign signals Set gengspeedsignal = gcanapp.bus.getsignal(1, "EngineData", "EngSpeed") Set gcarspeedsignal = gcanapp.bus.getsignal(1, "ABSdata", "CarSpeed") Set gengtempsignal = gcanapp.bus.getsignal(1, "EngineData", "EngTemp") Set gengstatussignal = gcanapp.bus.getsignal(1, "EngineData", "IdleRunning") assign CAPL function Set gsendgearfunction = gcanapp.capl.getfunction("sendgear") End Sub *** end of gcanmeasurement_oninit *** NAME: tmrsignals_timer (event handler) DESCRIPTION: On timer event that occurs every 100ms if a measurement is running. If the MotBus.cfg con- figuration is active, the signals on the form are updated Private Sub tmrsignals_timer() If (gmotbus = True) Then UpdateMotBusSignals End Sub *** end of tmrsignals_timer *** Application Note AN-AND-1-117 19
CANoe/CANalyzer as a COM Server 4 Vector Informatik GmbH Ingersheimer Straße 24 70499 Stuttgart Germany Tel.: +49 711-80670-0 Fax: +49 711-80670-111 Email: info@vector-informatik.de Vector France SAS 168 Boulevard Camélinat 92240 Malakoff France Tel: +33 (0)1 42 31 40 00 Fax: +33 (0)1 42 31 40 09 Email: information@vector-france.fr Vector CANtech, Inc. 39500 Orchard Hill Pl., Ste 550 Novi, MI 48375 Tel: (248) 449-9290 Fax: (248) 449-9704 Email: info@vector-cantech.com Vector Japan Co. Ltd. Seafort Square Center Bld. 18F 2-3-12, Higashi-shinagawa, Shinagawa-ku Tokyo, 140-0002, Japan Tel:+81 03(5769)6970 Fax: +81 03(5769)6975 Email: info@vector-japan.co.jp VecScan AB Lindholmspiren 5 SE-417 56 Göteborg Sweden Tel: +46 (0) 31 764 76 00 Fax: +46 (0) 31 764 76 19 Email: info@vecscan.com Application Note AN-AND-1-117 20