瀚欽科技網(wǎng)站建設(shè)谷歌搜索引擎免費
定時器在C語言中的使用我想學(xué)習(xí)過C編程的都不會陌生,它能夠提供延時,完成等待一定的時間;它也可以實現(xiàn)多線程的操作,并行實行某些軟件功能。那在CAPL中,定時器又能做哪些工作呢?又是怎么使用的呢?今天我們就一起來看下。

timer&msTimer
在CAPL中主要的定時器有毫秒定時器和秒定時器,他們分別是Timer和msTimer。對于計時器類型的變量,最長時間為2147483647 s(=596523.23h),對于msTimer類型的變量最長時間是2147483647ms(=2147483647s=596,52h);我們常定義在variables中,如下:
variables
{timer timer1;msTimer mstimer2;message 0x100 msg1;message 0x101 msg2;
}on timer timer1
{output(msg1);write("執(zhí)行timer1內(nèi)部內(nèi)容,發(fā)送0x100報文");
}on timer mstimer2
{output(msg2);write("執(zhí)行mstimer2內(nèi)部內(nèi)容,發(fā)送0x101報文");
}
setTimer

功能:CAPL中的定時器精度取決于所使用的的硬件,一般精度為一毫秒;
msTimer的最大值為:2147483647 ms=2147483648 s=596,52h。
from 2:計時器的最大值為2147483647 s=596523.23h
from 3:計時器的UI大致為2147483647秒+214783647ns ~ 2147483649秒
t:上面在variables中定時的毫秒計時器或秒定時器
duration:設(shè)置定時器時間為duration,時間為毫秒或者秒
durationSec:起始時間
durationNanoSec:終止時間
variables {msTimer t1;Timer t23;
}on key F1 {setTimer(t1, 200); // set timer t1 to 200 ms
}on key F2 {setTimer (t23, 2); // set timer t23 to 2 sec
}on key F3 {setTimer (t23, 0, 1250*1000 ); // set timer t23 to 1.250 milliseconds
}on timer t1 {write("F1 was pressed 200ms ago");
}on timer t23 {write("F2 was pressed 2 sec ago or F3 1250000 nsec ago");
}

setTimerCyclic

功能:設(shè)置一個循環(huán)計時器,循環(huán)調(diào)用定時器
t:毫秒定時器或秒定時器(timer&msTimer),在variables中定義的定時器
firstDuration:啟動定時器起始時間
period:計時器在到期時重新啟動的時間,周期時間以毫秒為單位
periodInNs:計時器在到期時重新啟動的時間,,周期時間以納秒為單位
variables {msTimer t1;Timer t23;message 0x100 msg1;
}
on timer timer1
{output(msg1);write("執(zhí)行timer1內(nèi)部內(nèi)容,發(fā)送0x100報文");
}
void send_msg1()
{//以周期為20ms的周期發(fā)送0x100setTimerCyclic(timer1, 20)
}
void send_msg2()
{//等待100ms后以周期為20ms的周期發(fā)送0x100setTimerCyclic(timer1, 100,20)
}
cancelTimer

功能:停止正在激活的定時器
t:毫秒定時器或秒定時器(timer&msTimer),在variables中定義的定時器
variables {msTimer t1;Timer t23;message 0x100 msg1;
}
on timer timer1
{output(msg1);write("執(zhí)行timer1內(nèi)部內(nèi)容,發(fā)送0x100報文");
}
void send_msg1()
{//以周期為20ms的周期發(fā)送0x100setTimerCyclic(timer1, 20)
}
void send_msg2()
{//等待100ms后以周期為20ms的周期發(fā)送0x100setTimerCyclic(timer1, 100,20)
}
void stop_msg()
{//停止周期發(fā)送報文0x100cancelTimer(timer1);
}

timetoelapse

功能:計時器超時并調(diào)用事件過程之前所需的時間
返回一個值,該值指示在調(diào)用計時器事件過程之前還要經(jīng)過多少時間。對于Form 1,時間值以秒為單位返回;對于Form 2,時間值以毫秒為單位返回。如果計時器未激活,則返回-1;計時器事件過程本身也是如此。
t:毫秒定時器或秒定時器(timer&msTimer),在variables中定義的定時器
timer t;
setTimer(t, 5);
write("Time to elapse: %d", timeToElapse(t)); // writes 5
isTimerActive

功能:返回值指示特定計時器是否處于活動狀態(tài),在調(diào)用setTimer函數(shù)和調(diào)用ontimer事件過程之間就是這種情況。
t:毫秒定時器或秒定時器(timer&msTimer),在variables中定義的定時器;
返回值:如果計時器激活狀態(tài)則為1;否則為0。0也在計時器事件過程中返回。
timer t;
write("Active? %d", isTimerActive(t)); // writes 0
setTimer(t, 5);
write("Active? %d", isTimerActive(t)); // writes 1
以上是CAPL相關(guān)定時器的所有常用函數(shù),歡迎大家評論區(qū)交流!!!