Using Hour and Minute values for start

CBlische
2010-01-25
2010-01-27
  • CBlische - 2010-01-25

    I have done this in the past(years ago) but i can't remember how to get a variable equal to the current hour and current minute. I want to be able have the operator input an hour value and a minute value that i can then do a simple compare and run controls based on this enable. I am also communicating over radio so the input values from the operator will be in Word or Integer.

     
  • unknown - 2010-01-26

    First of all, you have to use a function which returns the date and time from the realtime clock of your PLC. It can be RtcGetTime(TRUE). The other one function should be used for you task is DELETE. See code:

    PROGRAM PLC_PRG
    VAR
    Β  Β strTime, strDate: STRING (12);
    Β  Β iDay, iMonth, iYear, iHour, iMinute, iSecond: INT;
    END_VAR
    (*getting date and time*)
    strDate := DATE_TO_STRING(DT_TO_DATE(RtcGetTime(TRUE)));
    strTime := TOD_TO_STRING(DT_TO_TOD(RtcGetTime(TRUE)));
    (*date*)
    iYearΒ  := STRING_TO_INT(DELETE(DELETE(strDate, 2, 1), 10, 5));
    iMonth := STRING_TO_INT(DELETE(DELETE(strDate, 7, 1), 5, 3));
    iDayΒ  Β := STRING_TO_INT(DELETE(strDate, 10, 1));
    (*time*)
    iHour := STRING_TO_INT(DELETE(DELETE(strTime, 4, 1), 6, 3));
    iMinute := STRING_TO_INT(DELETE(DELETE(strTime, 7, 1), 5, 3));
    iSecond := STRING_TO_INT(DELETE(strTime, 10, 1));
    

    For my tasks I've created a FB:

    FUNCTION_BLOCK fbGetTime
    VAR_INPUT
    Β  Β todTime: TOD;
    END_VAR
    VAR_OUTPUT
    Β  Β iHour, iMinute, iSecond: INT;Β  Β 
    END_VAR
    VAR
    END_VAR
    iHour := STRING_TO_INT(DELETE(DELETE(TOD_TO_STRING(todTime), 4, 1), 6, 3));
    iMinute := STRING_TO_INT(DELETE(DELETE(TOD_TO_STRING(todTime), 7, 1), 5, 3));
    iSecond := STRING_TO_INT(DELETE(TOD_TO_STRING(todTime), 10, 1));
    

    You can insert it into your project and call in PLC_PRG:

    PROGRAM PLC_PRG
    VAR
    Β  Β fbTimeExtract: fbGetTime;
    END_VAR
    fbTimeExtract (todTime := DT_TO_TOD(RtcGetTime(TRUE)), iHour => , iMinute => , iSecond => );
    
     
    πŸ‘
    1
  • CBlische - 2010-01-26

    Thanks for the info. I will give it a try right away.

     
  • unknown - 2010-01-27

    Oops, I'm sorry. There was a mistake in the last one line of the code I pasted. Now, it has been fixed.

     

Log in to post a comment.