TwinCAT - measuring time

Bennie_S

Member
Join Date
Jul 2012
Location
Halsteren
Posts
5
Hi all! im a two week old TwinCAT newby and im having some problems to measure the time that a BOOL is TRUE, for exmple a machine thats turned on.
I first tried getting the systemtime and tried some math on that but it didnt work.
so i tried the following, but for some reason the string remains empty, or at least '???'.

If there is a more simple or easy way to sole this, im all ears!

thanks guys/girls! (code below)

(*CODE*)
VAR
fbFormat : FB_FormatString;
bError : BOOL;
nErrID : UDINT;
sOut : T_MaxString;
iSecond : INT;
iMinute : INT;
iHour : INT;
Teller_NSA_OFF : TON;
Teller_NSA_ON : TON;
bTeller_NSA : BOOL := TRUE;
stNSA_bedrijfstijd : STRING;
END_VAR

(*The format for the string i use*)
fbFormat( sFormat := '%.2d:%.2d:%.2d', arg1 := F_INT( iHour ),
arg2 := F_INT( iMinute ),
arg3 := F_INT( iSecond ),
sOut => stNSA_bedrijfstijd, bError => bError, nErrID => nErrID );

(*Here i use two Timer on-delay's to make a BOOL high every second*)
Teller_NSA_ON (IN:= bTeller_NSA, PT:= T#500ms);
Teller_NSA_OFF(IN:= NOT bTeller_NSA, PT:= T#500ms);

IF (Teller_NSA_ON.Q = TRUE) THEN
bTeller_NSA := FALSE;
END_IF
IF (Teller_NSA_OFF.Q = TRUE) THEN
bTeller_NSA := TRUE;
END_IF

(*Seconds become minutes and minutes become hours, this is displayed in VIS_NSA_bedrijfstijd.stTextisplay*)
IF (IN_NSA_bedrijf = TRUE AND bTeller_NSA = TRUE) THEN
iSecond := iSecond + 1;
VIS_NSA_bedrijfstijd.stTextDisplay := stNSA_bedrijfstijd;
IF ( iSecond = 60 ) THEN
iMinute := iMinute + 1;
ELSIF ( iMinute = 60) THEN
iHour := iHour + 1;
iMinute := 0;
END_IF
END_IF
 
I am dutch so no problem when you call me.

The answer is very simple.
use a elapsetimer out of www.oscat.de they have twincat.lib
look for ontime

the time of a timer (you can also use that for a test.
use timer1:TON;
elapsedtime:=timer1.ET
this time is in DUINT
to convert to string you can use TIME_TO_STRING see the online help for this.
another way is to add 1 to a var every second by using a task every second (see resources)
this with two timers is not very precise.
and your format is the systemtime, you can also use that one
by calculating the difference of the stored time and the systemtime.
it rolls over every 45 days or so.
 
Thanks!

Thanks a lot! i managed to get it working with your advice!
For anyone who migh have the same question, code is below


(*VAR*)
PROGRAM P_Bedrijfstijd_NSA
VAR
fbFormat : FB_FormatString;
bError : BOOL;
nErrID : UDINT;
sOut : T_MaxString;
iSecond : INT;
iMinute : INT;
iHour : INT;
stNSA_bedrijfstijd : STRING;
END_VAR

(*CODE*)
(*Seconds become minutes and minutes become hours, this is displayed in VIS_NSA_bedrijfstijd.stTextisplay*)
IF (IN_NSA_bedrijf = TRUE ) THEN
iSecond := iSecond + 1;
IF ( iSecond = 60 ) THEN
iMinute := iMinute + 1;
iSecond := 0;
ELSIF ( iMinute = 60) THEN
iHour := iHour + 1;
iMinute := 0;
END_IF
(*The format of the string used to display time*)
fbFormat( sFormat := '%.2d:%.2d:%.2d',
arg1 := F_INT( iHour ),
arg2 := F_INT( iMinute ),
arg3 := F_INT( iSecond ),
sOut => stNSA_bedrijfstijd, bError => bError, nErrID => nErrID );
(*VIS_NSA_bedrijfstijd.stTextDisplay is a programmable text object in a visualisation screen*)
VIS_NSA_bedrijfstijd.stTextDisplay := stNSA_bedrijfstijd;
END_IF
 

Similar Topics

Hi, I'm looking to measure the time between an input and an output in twinCAT using structured text. How would I go about doing so? I've seen the...
Replies
1
Views
1,690
I am using twincat 3 to send some strings over TCP/IP. Where the server is a sensor and my PLC is the client. I noticed that the sensor didnt...
Replies
2
Views
89
I'm trying to control a device via MODBUS RTU and the ModbusRtuMasterV2_PcCOM in Twincat 3. I've configured a device with the right com port and...
Replies
7
Views
225
Hi! I am trying to run the 'SimpleSample' (https://infosys.beckhoff.com/content/1033/TF5100_TC3_NC_I/Resources/3438746891/.zip ) to understand the...
Replies
2
Views
109
I am developing a library in twincat and one of the function uses IID_ITcVnAccess_TcVnPoint2_DINT and the definition of this type is defined in...
Replies
0
Views
74
Back
Top Bottom