5/21/2014 11:15 AM | |
Posts: 51 Rating: (1) |
Hello everyone, is it possible to get UNIX timestamp in S7-1200 and how? Best regards |
5/21/2014 2:21 PM | |
Posts: 510 Rating: (120) |
Here is a link to an excellent reference on UNIX time: http://www.epochconverter.com/ That being said, the S7-1200 T_Diffinstruction uses the DTL format. The DTL formathas aa min and max of DTL#1970-01-01-00:00:00.0 and DTL#2200-12-31-23:59:59.99999999. The output is a Double Integer. Try the instruction and see if the output is UNIX Time, seconds since 1970-01-01 Use the RD_SYS_T instruction to get the current time. Good luck billwest |
This contribution was helpful to1 thankful Users |
3/2/2015 3:52 PM | |
Posts: 66 Rating: (5) |
As mferraz points out, the T_DIFF block only supports a 32-bit integer which allows about +- 24 days. Furthermore, S7-1200 doesn't support 64-bit integers. Therefore, the time stamp has to be 32-bit unless we have some library that allow 64-bit arithmetic. I created a naive implementation that provides a Unix timestamp as seconds. The basic approach is to call T_DIFF repeatedly, each time incrementing the unix timestamp by the range of T_DIFF and decrementing the date by that range. Eventually, T_DIFF will return a successful result. After that, I multiply the number of failures by the T_DIFF range and then add the T_DIFF result to it. My limited testing shows that this gives an accurate timestamp. However, this is pretty inefficient because it loops nearly 700 times. There are many ways to optimize the approach, but I wanted to share the "simple" version. Example is here: S7-1200 Unix Timestamp Example When copying this, notice I create 2 FCs, ConvertToSeconds and ConvertToUnixTime. Warning: Be sure to test this thoroughly before deploying in a real system! |
This contribution was helpful to1 thankful Users |
4/29/2015 9:30 PM | |
Joined: 7/24/2012 Last visit: 10/19/2022 Posts: 4 Rating: (0) |
You might try this SCL routine to convert DTL to Unix Time. The expressions in the CASE statement can be compressed but were expanded to show where the numbers originate. There also may be a more elegant method and I'm open to suggestions. Update May, 14 2015 Sorry, there was an error in the calculation of Unix Time in January and February of a leap year. The new attachment contains the logic to correct this problem. AttachmentConvertToUtime.zip (1397 Downloads) |
Last edited by: alynch at: 4/29/2015 9:33:43 PMLast edited by: alynch at: 4/30/2015 3:16:58 PMLast edited by: alynch at: 5/14/2015 11:27:56 PM |
|
11/6/2015 10:18 AM | |
Posts: 1 Rating: (0) |
Hi. I think it fails between 2000-01-01 and 2000-02-29:( |
Last edited by: Dust-bunny at: 11/6/2015 12:10:19 PM |
|
Follow us on