Sign in to follow this  
Followers 0
pandersen

ControlLogix WallClockTime CurrentValue to seconds

11 posts in this topic

Hi all, I need to convert the 64-bit CurrentValue (microseconds) part of the WallClockTime into seconds (Unix/POSIX format), but I can't quite get my head around the operations I need to do. I can't do any regular math operations on it since CLX uses 32-bit DINTs for everything. Does anyone have any ideas on how to do this?

Share this post


Link to post
Share on other sites
Don't use the CurrentTime in the GSV. Use the DateTime. You will need to assign a DINT[7] for the destination. This give you the Year, Month Day, Hour, Minute, Second, and Microsecond, each in its own DINT

Share this post


Link to post
Share on other sites
I understand how to use DateTime, but it won't work for this application. What I need is the current time represented as the number of seconds from midnight on January 1, 1970 (the 'epoch'). See http://en.wikipedia.org/wiki/Unix_time for a more detailed description. The CurrentTime part of the WallClock is the number of microseconds from the epoch, so it seems the simplest way would be to get this into seconds, rather than convert the 7 DINT DateTime into unix time. Converting the DateTime to unix time is a pain because CurrentTime and Unix time are based on GMT time, while DateTime applies the timezone setting to convert CurrentTime to the local time. Then you have to take into account leap years where there are an extra 86400 seconds per year. I've tried doing this, but I could never get an accurate result. It's also really hard to find an algorithm because most other programming languages have a built in function to do it :) Also for anyone else who cares, Rockwell's epoch date in Logix5000 V15 and below is January 1, 1972. They finally fixed it in V16 to have the correct epoch date of Jan 1, 1970

Share this post


Link to post
Share on other sites
Have you tried dividing CurrentTime by 1000 and placing it in a REAL tag named something like UnixSeconds.

Share this post


Link to post
Share on other sites
I wish it were that easy :) The CurrentTime's actual value is split across 2 dints (to make a 64bit integer), which makes dividing impossible with any built in instructions. I tried doing some bit shifting, but I get nonsensical values because I really have to divide it by 1000 before I can do anything.

Share this post


Link to post
Share on other sites
I've been learning a little about UCT time and how Logix handles the new v16 clock features as I work on Sequence-Of-Events timestamping. It's all still a little swimmy in my head. I found a Knowledgebase article (AID# 38290) that includes some Add-On Instructions that perform comparison and Add/Subtract operations on 64-bit values, but they didn't include a Divide instruction. I presume that's what you need; the WallClock's CurrentValue is the epoch time in microseconds, so you need to divide by 1,000,000 to get seconds. Hmmm.

Share this post


Link to post
Share on other sites
That is exactly what I need. Also I don't know why I was thinking of dividing my 1000, you are correct with 1,000,000. I'll continue mucking around with it.

Share this post


Link to post
Share on other sites
This routine should do divide by 1,000,000 GSV(WallClockTime,,CurrentValue,TIME[0]); COP(TIME[0],TimeInMicroSeconds,1); result[1]:=0; result[0]:=0; initial_distribute.source:=TIME[1]; initial_distribute.sourcebit:=11; initial_distribute.length:=20; initial_distribute.DestBit:=0; initial_distribute.target:=0; btdt(initial_distribute); Temp1:=initial_distribute.dest; if Temp1>=1000000 then result[1].11:=1; Temp1:=Temp1-1000000; end_if; For i:=10 to 0 by -1 do Temp1:=Temp1+Temp1; Temp1.0:=TIME[1].[i]; if Temp1>=1000000 then result[1].[i]:=1; Temp1:=Temp1-1000000; end_if; End_for; For i:=31 to 0 by -1 do Temp1:=Temp1+Temp1; Temp1.0:=TIME[0].[i]; if Temp1>=1000000 then result[0].[i]:=1; Temp1:=Temp1-1000000; end_if; End_for; result[1].31:=TIME[1].31; COP(result[0],TimeInSeconds,1); Edited by Contr_Conn

Share this post


Link to post
Share on other sites
AOI instruction published in TN 52347 - must login with techconnect access to see it

Share this post


Link to post
Share on other sites
Very cool, thanks a lot! This is the first time I've seen a LINT in CLX - I didn't know it existed. What revision were they introduced? Anyone know when/if Rockwell will update all the instructions to work with them? Edited by pandersen

Share this post


Link to post
Share on other sites
LINT introduced in V16, it will have support in future versions, this is all info I have

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0