资源预览内容
第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
亲,该文档总共5页全部预览完了,如果喜欢就下载吧!
资源描述
CAPL 编程例子 /*Example 1 - Event Message Transmission*/ . 1 /*Example 2 - Periodic Message Transmission*/ . 1 /*Example 3 - Conditionally Periodic Message Transmission*/ . 2 /*Example 4 - Responding to a Received Message*/ . 2 /*Example 5 - Reading the Data within a Received Message*/ . 3 /*Example 6 - Responding to a message after a delay*/ . 3 /*Example 7 - Measuring the Time Between Periodic Messages*/ . 4 /*Example 8 - Using CAPL to control logging*/ . 4 /*Example 9 - Responding to a change in an Environmental Variable*/ . 5 /*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 timer to 100msec on timer timer1 setTimer(timer1,100); / reset timer msg1.byte(0)=msg1.byte(0)+1; / change the data output(msg1); / output message CAPL 编程例子 /*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 condition is active setTimer(timerA,200); / then start timer on timer timerA if(conditionA = 1) / if condition is still true setTimer(timerA,200); / then continue timer msgA.byte(0)=msgA.byte(0)-1; / change the data output(msgA); / output 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(cdMsg); CAPL 编程例子 /*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 the 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 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); CAPL 编程例子 /*Example 7 - Measuring the Time Between Periodic Messages*/ /Method 1: on message 0x101 long lastTime = 0; write(“Message 0x101 recevied at %ld”, this.TIME); write(“Time interval = %lf milliseconds”,(this.TIME - lastTime) / 100.0); lastTime = this.TIME; /Method 2: variables message 0x202 saveMsg; on message 0x202 write(“Message 0x202 received at % this.TIME); write(“Time interval = %ld milliseconds”,timeDiff(saveMsg, this); saveMsg = this; / saveMsg declared globally /*Example 8 - Using CAPL to control logging*/ on message 0x101 / Set the PreTrigger time to 1 second (1000 ms) setPreTrigger(1000); / Set the PostTrigger time to 5 seconds (5000 ms) setPostTrigger(5000); / Set the name and path of the log file setLogFileName(“c:logslog101”); / Trigger logging. trigger(); CAPL 编程例子 /*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 illegal value.”); break; on start CallAllOnEnvVar();
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号