您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 【灵冰肌】CAPL编程例子
CAPL编程例子/*Example1-EventMessageTransmission*/..................................................................1/*Example2-PeriodicMessageTransmission*/..............................................................1/*Example3-ConditionallyPeriodicMessageTransmission*/........................................2/*Example4-RespondingtoaReceivedMessage*/.......................................................2/*Example5-ReadingtheDatawithinaReceivedMessage*/........................................3/*Example6-Respondingtoamessageafteradelay*/..................................................3/*Example7-MeasuringtheTimeBetweenPeriodicMessages*/..................................4/*Example8-UsingCAPLtocontrollogging*/.................................................................4/*Example9-RespondingtoachangeinanEnvironmentalVariable*/..........................5/*Example1-EventMessageTransmission*///定义了ID为0x555长度为1的消息值为0xAA,按键b触发发送variables{message0x555msg1={dlc=1};}onkey‘b’{msg1.byte(0)=0xAA;output(msg1);}/*Example2-PeriodicMessageTransmission*///定义了ID为0x555长度为1的消息,按照周期100ms,值++发送variables{message0x555msg1={dlc=1};mstimertimer1;//definetimer1}onstart{setTimer(timer1,100);//initializetimerto100msec}ontimertimer1{setTimer(timer1,100);//resettimermsg1.byte(0)=msg1.byte(0)+1;//changethedataoutput(msg1);//outputmessage}CAPL编程例子/*Example3-ConditionallyPeriodicMessageTransmission*///定义了ID为0x400长度为1的消息,按住a有效触发发送,按照周期200ms,值--发送variables{message0x400msgA={dlc=1};mstimertimerA;intconditionA=0;//initializeconditionA=off}onkey‘a’{conditionA=!conditionA;//toggleconditionAif(conditionA==1)//ifconditionisactive{setTimer(timerA,200);//thenstarttimer}}ontimertimerA{if(conditionA==1)//ifconditionisstilltrue{setTimer(timerA,200);//thencontinuetimer}msgA.byte(0)=msgA.byte(0)-1;//changethedataoutput(msgA);//outputmessage}/*Example4-RespondingtoaReceivedMessage*/onmessageABSdata,EngineData{messageWheelInfowMsg;//Defineanewmessage//Printamessagetothescreenwrite(“Message%lXreceivedonCAN%ld”,this.ID,this.CAN);//Sendoutanothermessageoutput(cdMsg);}CAPL编程例子/*Example5-ReadingtheDatawithinaReceivedMessage*/onmessageEngineData{longproduct;//ReadEngSpeedandEngTempsignalsproduct=this.EngSpeed.phys*this.EngTemp.phys;write(“Theproductoftheenginespeedand”);write(“enginetemperatureis%l”,product);}onmessageEngineTemp{bytevalue;floattemp;value=this.byte(0);//Scalethenumbersoweget2decimalplacestemp=float/100;write(“Thetemperatureis%lfdegrees.”,temp);}/*Example6-Respondingtoamessageafteradelay*/Variables{TimerdelayTimer;//Seconds-basedtimerlongdelayTimerPeriod=5;//Delayof5seconds}onmessagedoorState{if(this.Closed==1)setTimer(delayTimer,delayTimerPeriod);else{//Dosthifdoorsareopen}}ontimerdelayTimer{messageDomeLightdlMsg;dlMsg.Status=0;//Turnoffdomelightoutput(dlMsg);}CAPL编程例子/*Example7-MeasuringtheTimeBetweenPeriodicMessages*///Method1:onmessage0x101{longlastTime=0;write(“Message0x101receviedat%ld”,this.TIME);write(“Timeinterval=%lfmilliseconds”,(this.TIME-lastTime)/100.0);lastTime=this.TIME;}//Method2:variables{message0x202saveMsg;}onmessage0x202{write(“Message0x202receivedat%this.TIME);write(“Timeinterval=%ldmilliseconds”,timeDiff(saveMsg,this));saveMsg=this;//saveMsgdeclaredglobally}/*Example8-UsingCAPLtocontrollogging*/onmessage0x101{//SetthePreTriggertimeto1second(1000ms)setPreTrigger(1000);//SetthePostTriggertimeto5seconds(5000ms)setPostTrigger(5000);//SetthenameandpathofthelogfilesetLogFileName(“c:\\logs\\log101”);//Triggerlogging.trigger();}CAPL编程例子/*Example9-RespondingtoachangeinanEnvironmentalVariable*///改变环境变量的响应onenvVarswitchState{intswitchValue;switchValue=getValue(this);switch(switchValue){case0:write(“Switchisnowoff.”);break;case1:write(“Switchisnowon.”);break;default:write(“Switchreturnedanillegalvalue.”);break;}}onstart{CallAllOnEnvVar();}
本文标题:【灵冰肌】CAPL编程例子
链接地址:https://www.777doc.com/doc-6131837 .html