Pr og ramming wit h C APL
CANalyzer
CANoe
the art of engineering
Programming With CAPL
December 14, 2004
First printing
Vector CANtech, Inc.
Suite 550
39500 Orchard Hill Place
Novi, MI 48375
USA
http://www.vector-cantech.com
II
© 2004, 2005 Vector CANtech, Inc
Novi, Michigan 48375 USA
The authors and publishers have used their best efforts in preparing this book. These efforts include development,
research, and testing of the theories, principles, and programming sample code so as to determine their effectiveness.
The authors and/or publishers make no warranty, expressed or implied, with regard to the sample code or to any other
documentation contained in this book. The authors and/or publishers shall not be liable in any event for incidental or
consequential damages in connection with, or arising out of, the furnishing, performance, or use of the sample code or
any of the contents of this book.
All rights reserved.
No part of this book may be reproduced, in any form or by any means, without express permission in writing from
Vector CANtech, Inc.
Table of Contents
III
1
5.1.1
Preface..............................................................................................................................................................................IX
About This Book ............................................................................................................................................................IX
Organization ..................................................................................................................................................................IX
Acknowledgments .........................................................................................................................................................IX
Tell Us What You Think!.................................................................................................................................................X
Introduction to CAPL................................................................................................................................................... 1
1.1 Evaluation Capability........................................................................................................................................... 1
1.2 Simulation Capability........................................................................................................................................... 1
1.3 Prerequisites for Using CAPL.............................................................................................................................. 2
1.4
Learning Steps .................................................................................................................................................... 2
1.5 CAPL – Event-Driven Software........................................................................................................................... 3
1.6 CAPL Program Organization............................................................................................................................... 3
1.7 Using the CAPL Browser for Program Development .......................................................................................... 3
1.8 Program Development Uses the Database Tool – CANdb++............................................................................. 3
1.9 CAPL Programming Knowledge.......................................................................................................................... 4
1.10
CAN Communication Knowledge .................................................................................................................... 4
THE WARNING – Welcome to the Jungle....................................................................................................... 4
1.11
1.12
Responsibility................................................................................................................................................... 5
2 CANalyzer and CANoe............................................................................................................................................... 6
2.1 One Key Difference – Level of Node Control ...................................................................................................... 6
2.2 Graphic Panels – The Other Major Difference .................................................................................................... 7
3 A Brief Introduction to CANalyzer............................................................................................................................... 9
3.1 Value of the Downloadable Demo....................................................................................................................... 9
First-Time Considerations ................................................................................................................................... 9
3.2
3.3 How to Start and Stop CANalyzer.....................................................................................................................10
The Architecture of CANalyzer..........................................................................................................................10
3.4
3.5 Measurement Setup Window ............................................................................................................................12
Trace Window ............................................................................................................................................12
3.5.1
Statistics Window .......................................................................................................................................12
3.5.2
3.5.3
Bus Statistics Window................................................................................................................................13
3.5.4
Data Window..............................................................................................................................................13
3.5.5 Graphics Window .......................................................................................................................................13
3.5.6 Write Window .............................................................................................................................................13
3.6 Duplicating Analysis Blocks...............................................................................................................................13
3.7 Controlling Data Flow ........................................................................................................................................13
3.8
Function Blocks .................................................................................................................................................13
3.8.1
Filtering Blocks...........................................................................................................................................14
3.8.2 Message Blocks .........................................................................................................................................15
3.9 Using CAPL Program Blocks in CANalyzer ......................................................................................................17
CANalyzer Program Blocks for Transmission............................................................................................18
3.9.1
Program Blocks for Analysis ......................................................................................................................19
3.9.2
CANalyzer – CAPL Programming Environment ............................................................................................20
3.10
4 A Brief Introduction to CANoe ..................................................................................................................................21
4.1 Value of the Downloadable Demo.....................................................................................................................21
First-Time Considerations .................................................................................................................................21
4.2
4.3
Learning from the Demo....................................................................................................................................22
4.4
Tool Architecture of CANoe...............................................................................................................................23
4.5 Using CAPL Program Blocks in CANoe............................................................................................................24
CANoe Program Blocks for Simulation ......................................................................................................24
CANoe Program Blocks for Analysis..........................................................................................................25
4.6 CANoe – CAPL Programming Environment .....................................................................................................25
5 Using Databases with CAPL ....................................................................................................................................26
5.1 Why Use a Database with CAPL?.....................................................................................................................26
Additional Uses of the Database................................................................................................................26
5.2 Database Association with CAPL......................................................................................................................26
5.3 Database Objects Accessible by CAPL ............................................................................................................27
Attributes ....................................................................................................................................................28
5.3.1
5.3.2
Value Tables ..............................................................................................................................................28
Network Nodes...........................................................................................................................................28
5.3.3
5.3.4 Messages...................................................................................................................................................28
4.5.1
4.5.2
IV
7.4.1
8.13
8.14
8.15
8.9.1
8.9.2
5.3.5
5.3.6
Signals........................................................................................................................................................28
Environment Variables ...............................................................................................................................28
6 Using Panels with CAPL (CANoe only) ....................................................................................................................29
7 The CAPL Browser ...................................................................................................................................................30
7.1 Starting the CAPL Browser................................................................................................................................30
7.2 Browser Organization ........................................................................................................................................31
7.3 Using the Right Mouse Button...........................................................................................................................32
The Events Window...........................................................................................................................................32
7.4
Creating an Event Procedure.....................................................................................................................32
7.5
The Global Variables Window ...........................................................................................................................32
The Event Procedure Window...........................................................................................................................33
7.6
7.7 Colorized CAPL Syntax .....................................................................................................................................33
7.8 Compiling Code .................................................................................................................................................33
7.9
Fixing Compilation Errors ..................................................................................................................................34
7.10
Debugging Run-Time Errors ..........................................................................................................................34
7.11
Using Other Text Editors................................................................................................................................35
7.12
CAPL Source File Format ..............................................................................................................................35
8 CAPL Syntax and Semantics....................................................................................................................................37
8.1 Major CAPL Differences from C ........................................................................................................................37
8.2 CAPL Equivalents to C Functions .....................................................................................................................38
8.3 Notation .............................................................................................................................................................38
8.4 Comments .........................................................................................................................................................38
8.5 Naming Conventions .........................................................................................................................................39
8.6 CAPL is Case Sensitive.....................................................................................................................................39
8.7 CAPL Keywords.................................................................................................................................................39
8.8 Data Types ........................................................................................................................................................40
8.9 Declarations.......................................................................................................................................................41
Local Variables are Static...........................................................................................................................41
Initialization.................................................................................................................................................42
Type Casting..................................................................................................................................................42
Arrays.............................................................................................................................................................42
Constants .......................................................................................................................................................43
8.12.1
Integer Constants .......................................................................................................................................43
8.12.2 Floating Point Constants ............................................................................................................................44
8.12.3 Character Constants...................................................................................................................................44
8.12.4 String Constants.........................................................................................................................................45
8.12.5 Symbolic Constants....................................................................................................................................45
Operators .......................................................................................................................................................46
8.13.1 Arithmetic Operators...................................................................................................................................46
8.13.2 Assignment Operators................................................................................................................................47
8.13.3 Boolean Operators .....................................................................................................................................48
8.13.4 Relational Operators ..................................................................................................................................49
8.13.5 Bitwise Operators .......................................................................................................................................50
8.13.6 Miscellaneous Operators............................................................................................................................50
8.13.7 Unsupported Operators..............................................................................................................................51
Control Statements ........................................................................................................................................51
8.14.1 Branching Statements ................................................................................................................................51
8.14.2
If Statement ................................................................................................................................................52
8.14.3
If-Else Statement........................................................................................................................................52
8.14.4 Switch Statement........................................................................................................................................53
Loops .............................................................................................................................................................54
8.15.1 While Statement .........................................................................................................................................54
8.15.2 Do–While Statement ..................................................................................................................................55
8.15.3 For Statement.............................................................................................................................................55
8.15.4 Unconditional Branching ............................................................................................................................56
8.15.5 Return Statement .......................................................................................................................................57
8.16
The “this” Keyword.........................................................................................................................................57
9 CAPL Functions ........................................................................................................................................................59
9.1 User-defined Functions......................................................................................................................................59
Function Overloading is Allowed .......................................................................................................................59
9.2
8.10
8.11
8.12
Table of Contents
V
10
11
11.2
9.3
Function Naming Conventions ..........................................................................................................................60
9.4 CAPL Function Categories................................................................................................................................60
9.4.1 Mathematical Functions .............................................................................................................................60
9.4.2
User Interface Functions ............................................................................................................................61
Time Functions...........................................................................................................................................62
9.4.3
9.4.4 Message Handling Functions.....................................................................................................................63
9.4.5
Database Functions ...................................................................................................................................64
Byte Ordering Functions ............................................................................................................................64
9.4.6
9.4.7
Logging Functions......................................................................................................................................65
9.4.8
String Handling Functions ..........................................................................................................................66
9.4.9 Measurement Control Functions................................................................................................................66
9.4.10 Statistics Functions ....................................................................................................................................67
9.4.11 CAN Protocol Functions.............................................................................................................................68
9.4.12 Port Functions ............................................................................................................................................69
9.4.13 Replay Block Functions..............................................................................................................................69
9.4.14 Environment Variable and Panel Functions (CANoe only) ........................................................................70
9.4.15 Miscellaneous Functions............................................................................................................................71
9.5 CAPL Function Compatibilities..........................................................................................................................72
CAPL Events and Event Procedures....................................................................................................................73
10.1
Creating an Event Procedure ........................................................................................................................74
Event Procedure Requirements.....................................................................................................................74
10.2
Some Event Procedures Use the Keyword “this” ..........................................................................................75
10.3
10.4
The * symbol..................................................................................................................................................75
10.5
Event Priority..................................................................................................................................................76
Using Messages....................................................................................................................................................78
Messages in CAPL ........................................................................................................................................78
11.1
11.1.1 Declaring Messages in CAPL with a Database .........................................................................................78
11.1.2 Declaring Messages in CAPL Without a Database....................................................................................79
11.1.3 Message Selectors.....................................................................................................................................79
Accessing Data..............................................................................................................................................81
11.2.1 Using Signals (Only Available with a Database)........................................................................................82
11.2.2 Physical Values and the “phys” Attribute ...................................................................................................82
11.2.3 Round-Off Error in Symbolic Signal Access ..............................................................................................83
Message Transmission..................................................................................................................................83
Message Reception .......................................................................................................................................83
Error Frames..................................................................................................................................................86
11.5.1 Error Frame Event......................................................................................................................................86
Using Keyboard Events.........................................................................................................................................87
12.1
Keyboard Events and Procedures.................................................................................................................87
Using the Keyword “this” with the Wildcard Symbol “*” .................................................................................87
12.2
Using System Events............................................................................................................................................89
13.1
Types of System Events ................................................................................................................................89
13.2 What Happens When a System Event Occurs?............................................................................................89
Using Timers .........................................................................................................................................................91
14.1
Declaring a Timer...........................................................................................................................................91
Starting a Timer .............................................................................................................................................91
14.2
14.3
Expiration of a Timer......................................................................................................................................91
Resetting a Timer...........................................................................................................................................92
14.4
14.5
Periodic Clock................................................................................................................................................92
14.6
Stopping a Timer Before It Expires................................................................................................................92
14.7
Common Timer Mistake.................................................................................................................................92
Using CAN Protocol Controller Events .................................................................................................................94
15.1
CAN Controller States....................................................................................................................................94
CAN Controller Events...................................................................................................................................95
15.2
Using Environment Variables................................................................................................................................96
Environment Variable Types..........................................................................................................................97
16.1
Environment Variable Initialization.................................................................................................................97
16.2
Declaring an Environment Variable Event.....................................................................................................97
16.3
16.4
Event Execution.............................................................................................................................................98
16.5
The putValue() and getValue() Functions......................................................................................................98
11.3
11.4
11.5
12
13
14
15
16
VI
17
16.6
17.1
18
19
19.1
20
18.1
18.2
The “this” Keyword.........................................................................................................................................98
Using File Input/Output Functions.......................................................................................................................100
File Input/Output Functions..........................................................................................................................100
17.1.1 Set-Up for File Input/Output Functions.....................................................................................................100
17.1.2 Set-Up for Sequential File Access Functions...........................................................................................101
Using the Serial and Parellel Port .......................................................................................................................103
Installation of RS232VC DLL .......................................................................................................................103
RS232 CAPL Functions ...............................................................................................................................103
18.2.1 RS232SetCommState ..............................................................................................................................103
18.2.2 RS232ByteCallback..................................................................................................................................104
18.2.3 RS232WriteByte.......................................................................................................................................104
18.2.4 RS232WriteBlock .....................................................................................................................................104
18.2.5 RS232EscapeCommFunc........................................................................................................................105
18.2.6 RS232CloseHandle..................................................................................................................................105
Constructing CAPL Programs .............................................................................................................................106
CAPL Program Organization .......................................................................................................................106
19.1.1 Creating Network Nodes ..........................................................................................................................106
19.1.2 P Block Placement and Message Pass-Through.....................................................................................107
CAPL Program Examples ...................................................................................................................................108
The Write() Function ....................................................................................................................................108
20.1
Sending a Periodic Message .......................................................................................................................108
20.2
Sending a Conditionally Periodic Message..................................................................................................110
20.3
Reading Data in a Received Message.........................................................................................................111
20.4
Responding to a Message After a Delay .....................................................................................................112
20.5
Sending an Error Frame ..............................................................................................................................113
20.6
Using Panels ................................................................................................................................................113
20.7
Bus Off Condition.........................................................................................................................................114
20.8
Using a CAPL Program to Control Logging .................................................................................................115
20.9
20.10 Data Files and CAPL....................................................................................................................................116
Set 1: File I/O Functions .......................................................................................................................117
Set 2: ProFile Functions .......................................................................................................................119
Basic Steps in Creating Your First CANoe Application.......................................................................................121
CANoe Development - Five Step Process...................................................................................................121
21.1.1 Create a New Directory ............................................................................................................................122
21.1.2 Creating a New CANoe Configuration......................................................................................................122
21.1.3 Bus Parameters........................................................................................................................................123
21.1.4 Set Up Analysis Functions .......................................................................................................................124
21.1.5 Working with the Analysis Windows.........................................................................................................125
Using CANdb++ to Create a Database...............................................................................................................126
CANdb++ Database Development...............................................................................................................126
22.1
Starting the CANdb++ Program...................................................................................................................127
22.2
Defining Attributes........................................................................................................................................127
22.3
Defining Value Tables..................................................................................................................................128
22.4
Defining Nodes.............................................................................................................................................128
22.5
Defining Messages.......................................................................................................................................128
22.6
Defining Signals ...........................................................................................................................................129
22.7
Establishing Object Associations.................................................................................................................130
22.8
22.9
Defining Environment Variables...................................................................................................................130
22.10 Saving the Database....................................................................................................................................131
22.11 Associating a Database ...............................................................................................................................131
Using the Panel Editor to Create Panels.............................................................................................................132
23.1 What are Panels?.........................................................................................................................................132
Advantages of Using Panels........................................................................................................................133
23.2
23.3
Requirements...............................................................................................................................................133
23.3.1 Database Association...............................................................................................................................133
23.3.2 Environment Variables .............................................................................................................................133
Creating a New Panel ..................................................................................................................................134
23.4.1 Properties of the Panel.............................................................................................................................134
23.4.2 Using Elements ........................................................................................................................................135
Configuring Elements...................................................................................................................................135
20.10.1
20.10.2
23.4
23.5
21
21.1
22
23