S7 SCL Date and time question

svoicum

Member
Join Date
Sep 2006
Location
Cluj
Posts
43
Hello... could someone provide me the code to obtain the date and time from my PLC, using SCL and then obtain each component (year,month,day,hour,minute,second,etc.) as integers?
thank you
 
Here's the basis for the SCL implementation. As there are no BCD to integer conversions available in SCL, you will need to provide a conversion function yourself. I've shown an example assignment to make iTemp equal to the current year in BCD. Unless this is a programming exercise using SCL, I'd stick with krk's FC.

Code:
FUNCTION_BLOCK FB10
VAR_TEMP
  // Temporary Variables
	iTemp:INT;
ThetimeBCD:DATE_AND_TIME; 
Thetime AT ThetimeBCD:STRUCT
	Year:BYTE;
	Month:BYTE;
	Day:BYTE;
	Hour:BYTE;
	Minute:BYTE;
	Second:BYTE;
	MillisecondA:BYTE;
	MsBandWeekDay:BYTE;
	END_STRUCT;
sfc1Return:INT; 
END_VAR
  sfc1Return:=sfc1(CDT:=TheTimeBCD);
  iTemp:=WORD_TO_INT(BYTE_TO_WORD(TheTime.Year));
END_FUNCTION_BLOCK
 
i did it like that :
using SFC1 - READ_CLK(...)
in fact, i saw something in help's examples

//=================================
FUNCTION SetSummerTime: BOOL //FC510 // function for correcting summer /winter time
// Andr. !!! will work until 2089 year. Of course, plc will die before :)
AUTHOR: ANDR

VAR_TEMP
SummerNow : BOOL; // Признак времени зима-лето в момент вызова функции: летнее время (=TRUE), зимнее (=FALSE)

Err_ReadDate : INT; // error code return
zdn : INT; // help variable
year : INT; // year, format: ХХ (19XX - 20XX)
num_of_month : INT; // number of month, jan -1, feb -2,...
day_of_month : INT; // no comments :)
day_of_week : INT; // !!! sun -1, mon -2, tus -3,..., .

tmpDATE : DT; // time structure
current_DT AT tmpDATE: ARRAY[1..8] OF BYTE; // array for access to time structures
END_VAR


Err_ReadDate := READ_CLK( CDT := tmpDATE); // Getting of current date

year := WORD_TO_INT(SHR(IN:=current_DT[1],N:=4))*10 + WORD_TO_INT(current_DT[1] AND 16#0F); // calculation of year
num_of_month := WORD_TO_INT(SHR(IN:=current_DT[2],N:=4))*10 + WORD_TO_INT(current_DT[2] AND 16#0F); // calculation numb of month
day_of_month := WORD_TO_INT(SHR(IN:=current_DT[3],N:=4))*10 + WORD_TO_INT(current_DT[3] AND 16#0F); // calculation of day
day_of_week := WORD_TO_INT(current_DT[8] AND 16#0F); // calculation day of week
.
.
.
 
...

Depends on what you have,

We are a Carpet Factory and...

Our situation is like this:

A WinCC for measurements, mostly trending, this WinCC PC is connected on our officenetwork. So Windows XP it's date & time is synchronised on the office network. We have an ethernet connection to all the main PLC's of the production line's. Connected on lean's. So WinCC gives date & Time to the PLC's.
All panels are connected on profibus and communicate with the Main PLC. Date & Time is given from the PLC to the panels every day (interrupt).

So everything that is networked has the right date & time

But, for stand alone machines, it's indeed not bad to calculate the change summer/winter. If having a panel I would make it possible to adjust the time if it's too much different with your watch :p

andr said:
i did it like that :
using SFC1 - READ_CLK(...)
in fact, i saw something in help's examples

//=================================
FUNCTION SetSummerTime: BOOL //FC510 // function for correcting summer /winter time
// Andr. !!! will work until 2089 year. Of course, plc will die before :)
AUTHOR: ANDR

VAR_TEMP
SummerNow : BOOL; // Признак времени зима-лето в момент вызова функции: летнее время (=TRUE), зимнее (=FALSE)

Err_ReadDate : INT; // error code return
zdn : INT; // help variable
year : INT; // year, format: ХХ (19XX - 20XX)
num_of_month : INT; // number of month, jan -1, feb -2,...
day_of_month : INT; // no comments :)
day_of_week : INT; // !!! sun -1, mon -2, tus -3,..., .

tmpDATE : DT; // time structure
current_DT AT tmpDATE: ARRAY[1..8] OF BYTE; // array for access to time structures
END_VAR


Err_ReadDate := READ_CLK( CDT := tmpDATE); // Getting of current date

year := WORD_TO_INT(SHR(IN:=current_DT[1],N:=4))*10 + WORD_TO_INT(current_DT[1] AND 16#0F); // calculation of year
num_of_month := WORD_TO_INT(SHR(IN:=current_DT[2],N:=4))*10 + WORD_TO_INT(current_DT[2] AND 16#0F); // calculation numb of month
day_of_month := WORD_TO_INT(SHR(IN:=current_DT[3],N:=4))*10 + WORD_TO_INT(current_DT[3] AND 16#0F); // calculation of day
day_of_week := WORD_TO_INT(current_DT[8] AND 16#0F); // calculation day of week
.
.
.
 
...
that was just an example of getting
"to obtain the date and time from my PLC, using SCL and then obtain each component (year,month,day,hour,minute,second,etc.) as integers"

Usually we have no problems with time synchronization from plc to wincc neither from wincc to plc. :)
But sometimes we need the plc make something at the SAME LOCAL time. Master of time - WinCC server, on plc winter time only so we have to change time of call time-of-day interrupts OB(it seems to me using SFC28,30)...
 
Seems Simple

I am not sure why answers to simple questions have to be so complicated... Usually simple-ist is best-ist

Here is my code. This takes the current date and tme and puts it into individual integers. You can create a FC with this (hence the # before the variable) or just make it a rung in your program.

CALL SFC 1 //Get Date and Time
RET_VAL:=#RetVal
OUT0 :=#Out_Time_Date //Date and Time

//Extract Year
L LB 2 //Load Byte 2
BTI //Convert To Integer
T #int_Year //Store in Variable
//Extract Month
L LB 3
BTI //Convert To Integer
T #int_Month
//Extract Day
L LB 4
BTI //Convert To Integer
T #int_Day
//Extract Hour
L LB 5
BTI //Convert To Integer
T #int_Hour
//Extract Minute
L LB 6
BTI //Convert To Integer
T #int_Minute
//Extract Second
L LB 7
BTI //Convert To Integer
T #int_Second
 
Here is my code. This takes the current date and tme and puts it into individual integers. You can create a FC with this (hence the # before the variable) or just make it a rung in your program.

How do you know that someone else will allocate local data in the same order that you have ? When referencing the individual elements of date and time in the temp area you should use indirect addressing (to keep this simple ?).
 
Here's the basis for the SCL implementation. As there are no BCD to integer conversions available in SCL, you will need to provide a conversion function yourself. I've shown an example assignment to make iTemp equal to the current year in BCD. Unless this is a programming exercise using SCL, I'd stick with krk's FC.

Now I'm really confused didnt L D[AR2,P#0.0] know about BCD_TO_INT

Here is a function I use... (Some names in Swedish..)

Code:
FUNCTION DT_TO_INT : VOID

VAR_INPUT
    IN_TIME : DATE_AND_TIME;
    TidIn AT IN_TIME : ARRAY[0..7] OF BYTE;
    //Byte 0 Year:BYTE;
    //Byte 1 Month:BYTE;
    //Byte 2 Day:BYTE;
    //Byte 3 Hour:BYTE;
    //Byte 4 Minute:BYTE;
    //Byte 5 Second:BYTE;
    //Byte 6 mS:BYTE;
    //Byte 7 WeekDay:BYTE;
END_VAR

VAR_OUTPUT
    Artal:INT;
    Manad:INT;
    Dag:INT;
    Veckodag:INT;
    Timme:INT;
    Minut:INT;
    Sekund:INT;
    milliSekund:INT;
END_VAR


BEGIN
Artal:=BCD_TO_INT(TidIn[0]);
Manad:=BCD_TO_INT(TidIn[1]);
Dag:=BCD_TO_INT(TidIn[2]);
Veckodag:=BCD_TO_INT(TidIn[7] AND 2#00001111);
Timme:=BCD_TO_INT(TidIn[3]);
Minut:=BCD_TO_INT(TidIn[4]);
Sekund:=BCD_TO_INT(TidIn[5]);
milliSekund:=BCD_TO_INT(TidIn[6])*10 + (BCD_TO_INT(TidIn[7] AND 2#11110000) / 10);
END_FUNCTION
 
Thanks. Instead of talking about how I was wrong or what I could have done differently, how about you showing the poster the help he needs. You must be a teacher. Show us a real world example instead of replying stuff that doesnt help the cause....
 
Thanks for the reply. Next try replying to the poster with the problem and try to help them out.


....you resurrected a thread that was over 2 years old so there's a good chance the original poster would have solved his problem already....

:)
 

Similar Topics

HI i would like to know how to get a variable that will store the amount of times a program has been executed. The issue is I have 3 DBs for 1 FB...
Replies
2
Views
82
Hi, I have an intermediate-advance knowledge of programming with TIA Porta in Ladder, would you recommend me to start learning SCL for it to...
Replies
11
Views
564
Hello nice to meet you, im new in here, I'm currently trying to convert code written in STL for a S7-400 to SCL for an S7-1500, because when i run...
Replies
5
Views
320
Hi everyone, I am new to this amazing world of PLC, well... I mean, in practice, since I already knew electronics, programming languages, IT, and...
Replies
7
Views
656
Hi all, This is my first post; I am new to PLC Controls stuff and English is not my native language, so I apologize in advance if something is...
Replies
4
Views
514
Back
Top Bottom