Variables in CAPL
CAPL provides various data types for different use cases in automotive testing.
Basic Data Types
Integer Types
int i; // 16-bit signed integer (-32,768 to 32,767)
long l; // 32-bit signed integer (-2,147,483,648 to 2,147,483,647)
byte b; // 8-bit unsigned integer (0 to 255)
word w; // 16-bit unsigned integer (0 to 65,535)
dword dw; // 32-bit unsigned integer (0 to 4,294,967,295)
int64 i64; // 64-bit signed integer
Floating Point Types
float f; // 32-bit floating point
double d; // 64-bit floating point
Character Types
char c; // Single character
string s; // String of characters
CAN-Specific Types
message msg; // CAN message structure
signal sig; // CAN signal structure
timer tmr; // Timer
msTimer ms; // Millisecond timer
Variable Declaration
Variables are declared in the variables section:
variables {
int counter = 0;
long timestamp;
byte data[8];
string messageText = "Hello CAPL";
message CAN1::EngineData engineMsg;
timer cycleTimer;
}
Initialization
Variables can be initialized during declaration:
variables {
int speed = 0;
float temperature = 20.5;
byte flags = 0x00;
string config = "default";
}
Arrays
variables {
byte rxData[8]; // 1-dimensional array
int matrix[3][3]; // 2-dimensional array
char text[100]; // Character array for strings
}
Accessing Arrays
on message CAN1::* {
// Copy message data to array
memcpy(this.byte, rxData, 8);
// Access array elements
byte firstByte = rxData[0];
rxData[1] = 0xFF;
}
Constants
Use the const keyword for read-only values:
variables {
const int MAX_SPEED = 200;
const string VERSION = "1.0.0";
const byte MASK = 0x0F;
}
Best Practices
- Initialize variables: Always initialize variables when declaring them
- Use meaningful names: Choose descriptive variable names
- Follow naming conventions: Use camelCase or snake_case consistently
- Use appropriate types: Choose the smallest type that fits your data
- Avoid magic numbers: Use named constants instead of literal numbers
Examples
Counter Variable
variables {
int rxCounter = 0;
}
on message CAN1::* {
rxCounter++;
write("Received %d messages", rxCounter);
}
Data Processing
variables {
byte rawData[8];
int processedValue;
}
on message CAN1::SensorData {
// Store raw data
memcpy(this.byte, rawData, 8);
// Process data
processedValue = (rawData[0] << 8) | rawData[1];
write("Processed value: %d", processedValue);
}