thiagoralves
Hi all,

I just updated OpenPLC v3 code to support special functions. These are values related to internal PLC status and data. Currently only two special functions are implemented: current time (RTC) and number of scan cycles executed. These functions are located in memory at address %ML1024 onwards. To be more specific:

%ML1024   ->   Current time in seconds since Epoch

%ML1025   ->   Number of scan cycles executed since the runtime started

%ML1026   ->   Slave Devices communication error counter

Attached is a simple program that works with the current time special function. The program turns the lamp on if the current time is greater than 15:06pm on Nov, 1st, 2018.

Just keep in mind that this program will fail if you don't update your OpenPLC v3 installation with the newer code. On Windows, to update just run "Rebase OpenPLC" from start menu. On other systems you will have to manually update following the installation instructions again.
Reply 5 0
Raj Iyer
I am unable to open this program despite rebasing OpenPLC as instructed above.

I get the message.. "Chosen folder doesn't contain a program, It's not a valid project!"

Please help me out of this one.
Raj
Reply 0 0
thiagoralves
It was made for a previous version of the editor. You can still open it if you create a new blank program, and then replace the plc.xml file with the .xml file from the example. Make sure that the file is called plc.xml or else it won’t work
Reply 1 0
Raj Iyer
Thanks Thiagor.
Raj
Reply 0 0
Laila Ana
Hi all,

I just updated OpenPLC v3 code to support special functions. These are values related to internal PLC status and data. Currently only two special functions are implemented: current time (RTC) and number of scan cycles executed. These functions are located in memory at address %ML1024 onwards. To be more specific:

%ML1024   ->   Current time in seconds since Epoch

%ML1025   ->   Number of scan cycles executed since the runtime started

%ML1026   ->   Slave Devices communication error counter

Attached is a simple program that works with the current time special function. The program turns the lamp on if the current time is greater than 15:06pm on Nov, 1st, 2018.

Just keep in mind that this program will fail if you don't update your OpenPLC v3 installation with the newer code. On Windows, to update just run "Rebase OpenPLC" from start menu. On other systems you will have to manually update following the installation instructions again.



Hi, what which function/block should I use if let say, I'm using a light sensor, then my LED will light up if light is detected. But I want this LED only to light up at a specific time interval for example from 7 in the morning to 10 in the morning. p/s: I'm new to ladder programming
Reply 0 0
thiagoralves
ULINT_TO_DT. You use this block to convert %ML1024 to a DateTime (DT) variable that has the current time and date. Once you have that just use logic to compare the current time with the constraints you want.
Reply 0 0
wji
Hi @thiagoralves, what can I do if I need the millisecond? (eg: 000000 of `DT#1970-01-01-00:00:00.000000`)

It tells me that it have not VAL function, I trying to do like this:
```
MillisecondsElapsed := VAL(LEFT(IN_DateTime, LEN(IN_DateTime) - 8));
```
Reply 0 0
marco0
Hi all,

I just updated OpenPLC v3 code to support special functions. These are values related to internal PLC status and data. Currently only two special functions are implemented: current time (RTC) and number of scan cycles executed. These functions are located in memory at address %ML1024 onwards. To be more specific:

%ML1024   ->   Current time in seconds since Epoch

%ML1025   ->   Number of scan cycles executed since the runtime started

%ML1026   ->   Slave Devices communication error counter

Attached is a simple program that works with the current time special function. The program turns the lamp on if the current time is greater than 15:06pm on Nov, 1st, 2018.

Just keep in mind that this program will fail if you don't update your OpenPLC v3 installation with the newer code. On Windows, to update just run "Rebase OpenPLC" from start menu. On other systems you will have to manually update following the installation instructions again.
Is there any manner to get a smaller time granularity, such as milliseconds, as the scan cycles are fixed to be 20ms long?
Reply 0 0
thiagoralves
You can use timers for ms accuracy. %ML1024 reads system time, and that has precision set to seconds.
Reply 0 0
marco0
Thanks Thiago, What instruction would you suggest me to use in order to calculate the actual scan cycle length?  I know that the scan cycle by default is 20ms, even if the actual execution time is faster. In order to calculate the actual time of "computation" that the Ladder Logic takes, I thought about calculating the epoch at the start of the ladder logic and at the end and subtracting it to get the the duration. It is unclear to me how to use timers to achieve this goal. 
Reply 0 0
thiagoralves
You can't do this. You will have to patch the runtime code to achieve what you want. Execution time can be smaller or even bigger than your configured cycle time. If the execution time is smaller, the CPU will sleep (kind of) for the remaining time until it is time to execute the next cycle. If the execution time is bigger, then you have a problem. That is called a "missed deadline". Mosty likely you will need to either increase your cycle time, or reduce the complexity of your program to avoid missed deadlines
Reply 0 0
marco0
I had a look at the runtime, it seems that you already write that information on a variable called cycle_time, if I print it I should get the execution time. Did I got that straight or is the content of this variable still "default scan cycle"?
Reply 0 0
Patrick890
Does %ML1024 output UTC time (GMT)?
 
I ask because the output is 3 hours different than 'date +%s' from command line. Raspberry pi is synced to time server.
 
How do I make sure OpenPLC is using correct time zone?
 
Thanks for a great project.
Reply 0 0
thiagoralves
This is the code snippet that does the time retrieval:
 
    time_t rawtime;
    struct tm *current_time;
    
    time(&rawtime);
    
    // Store the UTC clock in [%ML1027]
    if (special_functions[3] != NULL)
    {
        *special_functions[3] = rawtime;
    }

    current_time = localtime(&rawtime);
    
    // Adjust for local time
    time_t localtime = rawtime - timezone;
    if (current_time->tm_isdst > 0)
    {
        localtime += 3600; // Add one hour for daylight saving time
    }
        
    // Store local time in [%ML1024]
    if (special_functions[0] != NULL)
    {
        *special_functions[0] = localtime;
    }
It should calculate the timezone difference according to the timezone variable as retrieved from time.h. Maybe your system has the timezone set up incorrectly.
Reply 0 0
mostafa_aghahaji
hi.i want store uptime to a varaible.how i can do that?
Reply 0 0
thiagoralves
You can use a TON timer with 1 minute PT (or longer if you want). Every time it expires you count +1 on your minute variable to track time. To get the minute fraction you just need to read the timer ET which will give you the exact time interval since the last minute.
Reply 0 0
Reply