Codesys convert TIME to INT

Jonnie_R

Member
Join Date
Feb 2012
Location
Brecon
Posts
216
Hi,

It's been a while since I've been doing PLC work as I've been concentrating on a large security project.

However I have a TIME variable in a project I am working on that I want to check to see if it is greater than zero before executing the code. The easiest way I can think of is to convert to INT, but this doesn't seem possible.

Is there an easy way to do this?



Jon
 
It may depend on what your hardware supports but you should be able to convert a TIME variable to a UDINT (unsigned double integer) or DWORD since it is a 32 bit value.

I believe it converts it to the number of milliseconds represented by the TIME var.
 
I'll try one of those then, cheers norm.

Garry,

I tried IF {VAR} = 0 but it didn't like it, I'm writing a custom function block in ST which I neglected to mention in my original post.


Jon
 
as a negative time does not exist and the format is 4 bytes conversion to int is no good, with longer times it could even go negative.
to compare make variabele called
zerotime:TIME:=t#0ms;
now you can easy compare.
a nice lib is oscat.de go to downloadmanager version 3.3
get txt,pdf,lib
 
Nice solution shooter, I have the Oscat libraries installed already, but I've downloaded the PDF.


Thanks,


Jon
 
TIME is a DINT that is in milliseconds, not a UDINT. There is no rule against negative TIME values and you can easily get them if you are subtracting two times to get a time difference. Even Date_Time is a DINT of ms since Jan 1, 1970, so dates before 1970 are negative DINT values.
 
sorry cap you are correct as long as the top bit is not used. but TIME is unsigned, however yes you are correct when saying you can subtract them but you get a very high value so it is unsigned. only when using a signed variabele you get the correct value.
try subtracting 2 TIME values and you will see.

and when using DATE format same problem,
i am getting dates in 2106 (i will be dead)
 
I just checked and signed or unsigned seems to be platform specific. PacDriveM it is unsigned, and B&R it is signed. The IEC standard also doesn't explicitly say if it should be signed or unsigned (or even that it should be 32 bit).
 
If it is signed the maxvalue is different probably.

@jonnie
to keep out of problems when overflowing use
if ((now()- previous) >= setting )
 
Convert DT to Individual Numbers

There is a Function called "SplitDateTime" (Search it in the Library Manager) and it's available in the system "Util" Library! I swear to god, it's that easy!
It took me a week of searching, off and on whenever I had time, to find it.

It gives you Year, Month, Day, Hour, Min, Sec, Milisec, and DayOfWeek as UINTs, Simple as can be!

SEE ATTACHED .jpeg FILES

it takes an input of type ULINT which you can get by using the function block "GetTime" from the same library, This "GetTime" has a type ULINT output, which you can take it and feed it directly as input to the "SplitDateTime" function and out comes exactly what you need!
🔨

GetDateTimeFB.JPG SplitDateTimeExample.JPG SplitDateTimeLib.JPG
 

Similar Topics

Hello, I am new to Codesys 3.5 and I am trying to figure out how to convert a word into 2 bytes to send to another plc via ethernet/ip. Could...
Replies
5
Views
5,059
Hello experts :) I'm new to Codesys (3.5) and I am trying to make a program which takes a number 1-50 (read from some tags UID) and outputs each...
Replies
15
Views
7,470
Hello, I am using a Hitachi Micro EHV+ for a small project, and I wanted to have a Web visu, done with Codesys V3.5 SP13 Patch 2. I test the...
Replies
6
Views
269
Hello, I have a requirement to manage the text alignment dynamically. So, for example: 1. English Texts should be displayed from Left in...
Replies
0
Views
87
Hello, I am new to Codesys, and am trying to learn about it for a project we're developing. I've got a couple questions, but first a little...
Replies
1
Views
133
Back
Top Bottom