Jump to content

How to convert a variable to utc?

Recommended Posts

google traduction


I have been stuck on my project for several days and I specify that I am a beginner in programming.

My plan is that I have to sound a buzzer several times during the day is this setting made by HMI.
On my HMI, I create 2 variables.
Variable 1 is the hour and variable 2 is the minutes that I need to subtract.
The problem, this variable that I created on the HMI, time function should I convert it to seconds in the ladder program?
If it is necessary to convert it in second, for the obtained result I must use the block rtc to utc to compare it with SI31?
I tried to do a comparison but it doesn't work for me.

Thank you in advance for your help.

Link to comment
Share on other sites

  • MVP 2023

Further on from what Joe is saying, I think you need to go down 2 other steps at present in your learning curve.

1).  Have a look at the attached vlp which may need to be written out again for your plc...it may not run on your model immediately.  The vlp is actually just demonstrating what can be done with UTC, which is the method Flex and I were suggesting earlier.

2).  I often say on the forum that counters based on SB13 or 15 are a much better way of achieving some time functions needed.  People always seem to go "must use Time functions" when thinking of "Time".  A counter  based on time works in a totally different way and is ideal for your need.  I think that SB 13 would be enough accuracy for your need.  You either set the counter as an increment or decrement, the example below is increment.  Being an actual number rather than what Joe has explained about the complexities involved with time numbers, this idea gets you immediately into decimal land like he says.  The count can then be manipulated very easily with various compares, subsequent stores, or maths functions.  As Joe says, you then do simple maths to arrive at relevant numbers.    2 hours 30 minutes based on using SB13 as your trigger pulse is number  (60(seconds) x 60(minutes) x 2(hours)) + (60(seconds) x 30(minutes)) = 9000.   Don't forget that if you need to get into numbers larger than MIs can handle, you then need to use operands of larger capacity.


cheers, Aus



Link to comment
Share on other sites

  • MVP 2023

I think you're running into trouble because the project you're working on isn't exactly beginner level.  Time is more complicated than it looks.

UTC is going to throw you for a loop because it's the absolute number of seconds since some reference point.  You don't really care about this, you're more interested in how much time has passed today.  Then you'll do it again tomorrow.

I propose determining the number of minutes past midnight and referencing your time setpoints to that.  You'll enter your time setpoints for hours and minutes separately as regular MIs and then combine them into minutes behind the scenes in your code.  Then comparison becomes straightforward.

You'll see in the SI registers that there are additional ones available for RTC hour and minute (SI 35 and SI 36), so we'll use those for actual current time calculation.

The language barrier is a bit of a problem as I don't speak French, but I tried putting comments in the program using Google translate.  I've attached the beginning of the program with modifications to the Main Routine and the Start-Up Display to show what I'm talking about.

Also - you have nine subroutines in your program.  I don't know if you've gotten that far but you have to put Call Subroutine blocks in for them to work.  At your programming level I wouldn't use subroutines at this time as they only add to the confusion.  Put all your code into the Main routine and then break it up later.

Let us know if this clears things up a bit.

Joe T.


example JT.vlp

Link to comment
Share on other sites

Google traduction

Thanks Joe Taucher and Ausman for your help. Sorry for not having answered earlier, I am working hard on this project.

I found out why I couldn't subtract, it was due to the memory format to use.

When there were hours, minutes and seconds (ex: 13h30mn50s),
I did the calculation to convert in seconds then I save in ML (32 bits). And my variable which contained the minutes, second (ex: 20mn30s), I converted it into second and I save it in MI (16 bits). If I don't put these 2 values in ML, the value was wrong.

Lack of time,
i haven't try in BCD if that will work too. I just got some BCD encoding lessons, I will be working on it this weekend.

For the subroutines in my program, which I had put on this site, I had purposely put an example in the hand for simplicity.

My program is progressing well, and if you want it so that its help has someone else i will post it on this site.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...