Formating timer TON

MortenB

Member
Join Date
Apr 2016
Location
Fredrikstad
Posts
16
Hi

Is it possible to format the .et of a TON timer?

If I display the ton.et it typically displays T#8s126ms but I would really like to format this as hh:mm:ss. Or is there any other way to have a stop-watch in structured text? I use this to track and display the time a process has been in a sertain state.

//Morten
 
Welcomo to forum!


time is dint format (20s = 20000ms). It is only showed different way when you select time format for animation.

You can convert it with simple math (subtract,add, divide and multiple) to four different types (hour, minute, second and msecond)
 
Welcomo to forum!


time is dint format (20s = 20000ms). It is only showed different way when you select time format for animation.

You can convert it with simple math (subtract,add, divide and multiple) to four different types (hour, minute, second and msecond)

Thanks for the help on this. That was a good way of doing it. I created a Function Block that takes the [TIME] variable as an input and converts it to a string formatted as a clock. However it does not show leading zeros on hours, minutes and seconds lower than 10. I.e it now shows 0:9:45
I have played around a bit with formatting and in other languages I have used a format() function but that is not available in ST? My code is as follows:

Code:
FUNCTION_BLOCK FB_TIMEFORMAT
VAR_INPUT
	iTimeInMS : TIME;
END_VAR
VAR_OUTPUT
	sDisplayTid : STRING;
END_VAR
VAR
	dDisplayTidTime: DINT;
	dDisplayTidMinutt: DINT;
	dDisplayTidSekund: DINT;
END_VAR


Code:
dDisplayTidTime := TIME_TO_DINT(iTimeInMS / (60 * 60 * 1000));
dDisplayTidMinutt := TIME_TO_DINT(iTimeInMS / (60 * 1000)) MOD 60;
dDisplayTidSekund := TIME_TO_DINT(iTimeInMS / 1000) MOD 60;
sDisplayTid := CONCAT(CONCAT(CONCAT(DINT_TO_STRING(dDisplayTidTime),':'),CONCAT(DINT_TO_STRING(dDisplayTidMinutt),':')),DINT_TO_STRING(dDisplayTidSekund));

Any good tip on how to get a leading zero? By the way, I develop this for a Twincat3 based PLC.

//Morten
 
For the leading zero, try looking at the length of the string and then CONCAT'ing a 0 on the front if it is a single character. If not, don't do it.
 
:unsure:

is there build in tod_to_string function.

Change first int to bcd and then add two bcd variables to array variable

Then convert array variable to tod-format and finally tod to string ;)

(or maybe also with if you concat "0" if int value is below 10 :confused: )


p.s Twincat maybe don't have this. I readed different post and messed different PLC brands...


Are you showing result on HMI, HMIs usually have leading zero setting for variable. Maybe displaying 3 different ints and "drawing" ':' between different varible fields is easier than string format directly

time_string1.jpg time_string2.jpg
 
Last edited:
Thanks for the help on this. That was a good way of doing it. I created a Function Block that takes the [TIME] variable as an input and converts it to a string formatted as a clock. However it does not show leading zeros on hours, minutes and seconds lower than 10. I.e it now shows 0:9:45
I have played around a bit with formatting and in other languages I have used a format() function but that is not available in ST? My code is as follows:

Code:
FUNCTION_BLOCK FB_TIMEFORMAT
VAR_INPUT
	iTimeInMS : TIME;
END_VAR
VAR_OUTPUT
	sDisplayTid : STRING;
END_VAR
VAR
	dDisplayTidTime: DINT;
	dDisplayTidMinutt: DINT;
	dDisplayTidSekund: DINT;
END_VAR


Code:
dDisplayTidTime := TIME_TO_DINT(iTimeInMS / (60 * 60 * 1000));
dDisplayTidMinutt := TIME_TO_DINT(iTimeInMS / (60 * 1000)) MOD 60;
dDisplayTidSekund := TIME_TO_DINT(iTimeInMS / 1000) MOD 60;
sDisplayTid := CONCAT(CONCAT(CONCAT(DINT_TO_STRING(dDisplayTidTime),':'),CONCAT(DINT_TO_STRING(dDisplayTidMinutt),':')),DINT_TO_STRING(dDisplayTidSekund));

Any good tip on how to get a leading zero? By the way, I develop this for a Twincat3 based PLC.

//Morten

For the leading zero, try looking at the length of the string and then CONCAT'ing a 0 on the front if it is a single character. If not, don't do it.


Agreed with Keshik.

str_dDisplayTidMinutt :STRING;

IF dDisplayTidMinutt <10 then
str_dDisplayTidMinutt :=CONCAT( 0, DINT_TO_STRING(dDisplayTidMinutt) );
ELSE
str_dDisplayTidMinutt:= DINT_TO_STRING(dDisplayTidMinutt);
END_IF

//Do the same for hours and seconds.

// Concatenate the 3 ( hour, minute, second strings)
 
For the leading zero, try looking at the length of the string and then CONCAT'ing a 0 on the front if it is a single character. If not, don't do it.

Thanks for feedback. I did as you said and works like a charm. Probably not the most effective code but it works and my plc is not that busy :p

The code I used for converting to clock format:
Code:
dDisplayTidTime := TIME_TO_DINT(iTimeInMS / (60 * 60 * 1000));
dDisplayTidMinutt := TIME_TO_DINT(iTimeInMS / (60 * 1000)) MOD 60;
dDisplayTidSekund := TIME_TO_DINT(iTimeInMS / 1000) MOD 60;

sDisplayTidTime := DINT_TO_STRING(dDisplayTidTime);
sDisplayTidMinutt := DINT_TO_STRING(dDisplayTidMinutt);
sDisplaytidSekund := DINT_TO_STRING(dDisplayTidSekund);

IF LEN(sDisplayTidTime)<2 THEN
	sDisplayTidTime := CONCAT('0',sDisplayTidTime);
	sDisplayTidTime := CONCAT(sDisplayTidTime,':');
END_IF
IF LEN(sDisplayTidMinutt)<2 THEN
	sDisplayTidMinutt := CONCAT('0',sDisplayTidMinutt);
	sDisplayTidMinutt := CONCAT(sDisplayTidMinutt,':');
END_IF
IF LEN(sDisplayTidSekund)<2 THEN
	sDisplayTidSekund := CONCAT('0',sDisplayTidSekund);
END_IF

sDisplayTid := CONCAT(CONCAT(sDisplayTidTime,sDisplayTidMinutt),sDisplayTidSekund);
 
Glad to hear it's working out for you. That's some clean, understandable code. Nicely done.

One question - in looking at it quickly, are you getting all of the ":" marks in there? They are currently being CONCAT'd inside your "IF" statements, so they won't be getting added in 100% of the time.
 
use DT_TO_TIME or something like it. (too late for me to look it up.
or use the functions from oscat.de (english version)
 

Similar Topics

Hi, I am having trouble with Citect displaying a variable tag in the incorrect format. I have a tag (MC1_Input_temp) setup as a int with a...
Replies
1
Views
1,615
in Display PV plus 6 a text box, how to skip one row, means skip a row then print the next variable? How to make columns with equal spacing...
Replies
0
Views
2,477
We are collecting some data in Control Logix PLC. These data have associated date and time stamp obtained by GSV instruction and stored as DINT...
Replies
3
Views
3,801
I'm using 4.0. Is there a tool similar to the Format Painter in Excel and Word that can be used to quickly format a number of text objects based...
Replies
1
Views
1,935
I have some logic that I have written within a 5380 series controller that tracks the time an event is started, while the event is running an RTO...
Replies
2
Views
102
Back
Top Bottom