User Interface Function
1.Beeping and Keypress Detection
void OnKeyPress() {
if (keypressed()) {
write("A key was pressed!");
msgBeep();
}
}
keypressed() checks if any key is pressed.
msgBeep() plays a predefined Windows system sound.
2. Exiting and Minimizing CANoe
void ExitOrMinimize() {
sysMinimize(); // Minimize CANoe
write("CANoe minimized...");
delay(5000); // Wait 5 seconds
sysExit(); // Exit CANoe
}
sysMinimize() minimizes/restores CANalyzer or CANoe.
sysExit() closes CANoe after 5 seconds.
3. Writing and Clearing Text in a Write Window
void WriteExample() {
writeCreate(); // Create a Write window
write("This is a Write window example.");
delay(2000);
writeClear(); // Clear the Write window
write("Write window cleared.");
delay(2000);
writeDestroy(); // Destroy the Write window
}
writeCreate() opens a write window.
write() outputs text.
writeClear() clears the content.
writeDestroy() closes the window.
4. Writing Text Without and With New Lines
void WriteTextExample() {
writeEx("This is a single-line output. "); // No new line
writeLineEx("This is a new line output."); // With new line
}
writeEx() writes text without a new line.
writeLineEx() writes text with a new line.
5. Changing Background and Text Colors in Write Window
cpp
CopyEdit
void ColorExample() {
writeCreate();
writeTextBkgColor(0x0000FF); // Set background color to blue
writeTextColor(0xFFFFFF); // Set text color to white
write("This text has a blue background and white text.");
delay(3000);
writeDestroy();
}
writeTextBkgColor(color) changes the background color.
writeTextColor(color) changes the text color.
Time Functions
Getting the Current System Time (timeNow)
void OnStart() {
long currentTime = timeNow();
write("Current system time: %d ms", currentTime);
}
timeNow() gets the current system time in milliseconds.
2. Getting Local Time (getLocalTime)
void OnStart() {
dword timeVal;
getLocalTime(timeVal);
write("Local Windows time: %d", timeVal);
}
getLocalTime() retrieves the current local Windows time.
3. Getting Local Time as a String (getLocalTimeString)
void OnStart() {
char timeString[50];
getLocalTimeString(timeString);
write("Local time string: %s", timeString);
}
getLocalTimeString() returns the system time in a readable string
format.
4. Setting and Cancelling a Timer (setTimer, cancelTimer)
variables {
TIMER myTimer; // Declare a timer
}
void OnStart() {
setTimer(myTimer, 5000); // Start timer for 5 seconds (5000 ms)
write("Timer started for 5 seconds.");
}
void onTimer(myTimer) {
write("Timer expired!");
cancelTimer(myTimer); // Stop the timer
}
setTimer(myTimer, 5000) starts a timer for 5 seconds.
onTimer(myTimer) executes when the timer expires.
cancelTimer(myTimer) stops the timer.
5. Calculating Time Difference Between Two Messages (timeDiff)
variables {
long time1, time2;
}
void OnMessage() {
time1 = timeNow();
delay(3000); // Simulating a delay of 3 seconds
time2 = timeNow();
long diff = timeDiff(time1, time2);
write("Time difference: %d ms", diff);
}
timeDiff(time1, time2) calculates the difference between two
timestamps.
Message Handling Functions
1. Basic on message * Example
on message * {
write("Received Message - ID: 0x%X, DLC: %d", this.ID, this.DLC);
}
2. Filtering Specific CAN Message IDs
If you only want to process a specific CAN ID, you can add a condition:
on message * {
if (this.ID == 0x123) { // Filter for CAN ID 0x123
write("Message 0x123 received. Data: %X %X %X", this.byte(0),
this.byte(1), this.byte(2));
}
}
3. Handling Standard and Extended Identifiers
You can check whether a received message has a standard (11-bit) or
extended (29-bit) identifier.
on message * {
if (isStdId(this)) {
write("Standard ID received: 0x%X", this.ID);
} else if (isExtId(this)) {
write("Extended ID received: 0x%X", this.ID);
}
}
4. Logging Messages to a File
To save received messages in a log file, use:
variables {
dword logFile;
}
on start {
logFile = openFile("can_log.txt", O_WRONLY);
}
on message * {
write(logFile, "ID: 0x%X, Data: %X %X %X %X %X %X %X %X",
this.ID, this.byte(0), this.byte(1), this.byte(2),
this.byte(3), this.byte(4), this.byte(5), this.byte(6), this.byte(7));
}
on stop {
closeFile(logFile);
}
5. Counting Received Messages
You can count the number of messages received:
cpp
CopyEdit
variables {
int msgCount = 0;
}
on message * {
msgCount++;
write("Message Count: %d, Latest ID: 0x%X", msgCount, this.ID);
}
Convert a String to a Long Integer (atol)
cpp
CopyEdit
variables {
char strNum[] = "12345";
long num;
}
on start {
num = atol(strNum);
write("Converted Number: %d", num);
}
2. Convert a Number to a String (ltoa)
cpp
CopyEdit
variables {
long num = 9876;
char strNum[10];
}
on start {
ltoa(num, strNum, 10); // Convert to string in base 10
write("Converted String: %s", strNum);
}
3. Create a Formatted String (snprintf)
cpp
CopyEdit
variables {
char buffer[50];
long value = 2025;
}
on start {
snprintf(buffer, sizeof(buffer), "Year: %d", value);
write("Formatted String: %s", buffer);
}
4. Get the Length of a String (strlen)
cpp
CopyEdit
variables {
char myString[] = "CAPL Programming";
int length;
}
on start {
length = strlen(myString);
write("String Length: %d", length);
}
5. Concatenate Two Strings (strncat)
cpp
CopyEdit
variables {
char str1[30] = "Hello, ";
char str2[] = "World!";
}
on start {
strncat(str1, str2, sizeof(str1) - strlen(str1) - 1);
write("Concatenated String: %s", str1);
}
6. Compare Two Strings (strncmp)
cpp
CopyEdit
variables {
char str1[] = "CAPL";
char str2[] = "CAPL";
int result;
}
on start {
result = strncmp(str1, str2, 4);
if (result == 0) {
write("Strings are equal");
} else {
write("Strings are different");
}
}
7. Copy a String (strncpy)
cpp
CopyEdit
variables {
char source[] = "CANalyzer";
char destination[20];
}
on start {
strncpy(destination, source, sizeof(destination) - 1);
destination[sizeof(destination) - 1] = '\0'; // Null-terminate manually
write("Copied String: %s", destination);
}