/*Example 1-Event Message Transmission*/
//定义了ID为0x555长度为1的消息值为0xAA,按键b触发发送
variables
{
message 0x555 msg1 = {dlc=1};
}
on key 'b'
{
msg1.byte(0)=0xAA;
output(msg1);
}
/*Example 2-Periodic Message Transmission*/
//定义了ID为0x555长度为1的消息,按周期100ms,值++发送
variables
{
message 0x555 msg1 = {dlc=1};
mstimer timer1; //define timer1
}
on start
{
setTimer(timer1,100);//initialize timer1 to 100ms
}
on timer timer1
{
setTimer(timer1,100);//reset timer
msg1.byte(0)=msg1.byte(0)+1;//change the data
output(msg1);//output message
}
/*Example 3-Conditionally Periodic Message Transmission*/
//定义了ID为0x400长度为1的消息,按键a有效触发发送,按照周期200ms,值--发送
variables
{
message 0x400 msgA = {dlc=1};
mstimer timerA;
int conditionA = 0;//initialize conditionA =off
}
on key 'a'
{
conditionA = !conditionA;//toggle conditionA
if(conditionA == 1)//if conditionA is active
{
setTimer(timerA,200);//then start timer
}
}
on timer timerA
{
if(conditionA == 1)//if conditionA is still true
{
setTimer(timerA,200);//then continue timer
}
msgA.byte(0)=msgA.byte(0)-1;//change the data
output(msgA);//output the message
}
/*Example 4-Responding to a Received Message*/
on message ABSdata, EngineData
{
message WheelInfo wMsg;//define a new message
//print a message to the screen
write("Message %LX received on CAN %ld",this.ID,this.CAN);
//send out another message
output(wMsg);
}
/*Example 5-Reading the Data within a Received Message*/
on message EngineData
{
long product;
//read engspeed and engtemp signals
product = this.EngSpeed.phys*this.EngTemp.phys;
write("The product of engine speed and");
write("engine temperature is %l",product);
}
on message EngineTemp
{
byte value;
float temp;
value = this.byte(0);
//scale the number so we get 2 decimal places
temp = float/100;
write("The temperature is %lf degrees.",temp);
}
/*Example 6-Responding to a Message after a delay*/
variables
{
Timer delayTimer;//seconds-based timer
long delayTimerPeriod = 5;//Delay of 5 seconds-based
}
on message doorState
{
if(this.Closed == 1)
{
setTimer(delayTimer,delayTimerPeriod);
}
else
{
// do sth if doors are open
}
}
on timer delayTimer
{
message DomeLight dlMsg;
dlMsg.Status = 0;//Turn off dome light
output(dlMsg);
}
/*Example 7-Measuring the time between period message*/
//方法1
on message 0x101
{
long lastTime = 0;
write("Message 0x101 received at %ld",this.TIME);
write("Time interval = %lf milliseconds",(this.Time-lastTime)/100.0);
lastTime = this.TIME;
}
//方法2
variables
{
message 0x202 saveMsg;
}
on message 0x202
{
write("Message 0x202 received at %ld",this.TIME);
write("Time interval=%ld milliseconds",timeDiff(saveMsg,this));
saveMsg = this;//saveMsg declare globally
}
/*Example 8-Using CAPL to control logging*/
on message 0x101
{
//set the preTrigger time to 1 second 1000ms
setPreTrigger(1000);
//set the PostTrigger time to 5 second 5000ms
setPostTrigger(5000);
//set the name and path of the log file
setLogFileName("c:\\logs\\log101");
//Trigger logging
trigger();
}
/*Example 9-Responding to a change in an Environmental Variable */
//改变环境变量的响应
on envVar switchState
{
int switchValue;
switchValue = getValue(this);
switch(switchValue)
{
case 0:
write("switch is now off.");
break;
case 1:
write("switch is now on.");
break;
default:
write("switch returned an illeagal value.")
break;
}
}
on start
{
CallAllOnEnvVar();
}