Contents
TIA Portal Jak wyświetlić timer na HMI
W tym tutorialu zajmiemy się wyświetlaniem timerów na HMI w odpowiednim formacie. Aby tego dokonać przekonwertujemy zmienną rodzaju LTime (która zawiera aktualny czas timera) na kilka zmiennych typu string (godziny, minuty, sekundy, itp.) aby potem połączyć je spowrotem w jedną zmienną string, która będzie wyświetlana na HMI.
1. Utwórz timer, którego wartość będzie wyświetlana na HMI.

Swój projekt zaczałem od utworzenia FB, w którym przywołałem funkcję Timer. W ten sposób mogłem utworzyć DB Timera jako multi-instance. Moje zmienne również zadeklarowałem wewnątrz FB jako Local Static.
Ponieważ chcę wyświetlać okresy dluzsze niż 24 dni, zmieniłem typ danych Timer’a z Time (T#-24d_20h_31m_23s_648ms do T#+24d_20h_31m_23s_647ms) na LTime (LT#-106751d_23h_47m_16s_854ms_775us_808ns do LT#+106751d_23h_47m_16s_854ms_775us_807ns).
2. Utwórz FC do konwersji LTime na String.
W tym FC wyciągamy z LTime dni, godziny, minuty, sekundy i milisekundy. Konwertujemy je na osobne zmienne String, a następnie łączymy ze sobą, dodajac separatory pomiedzy nimi.
Pominąłem nanosekundy i mikrosekundy, ponieważ nie bylibyśmy w stanie zauważyć ich zmiany w interfejsie HMI, ze względu na opóźnienia połączenia, czas reakcji ludzkiego oka, częstotliwość odświeżania ekranu itp.

Kopiuj-wklej kod poniżej:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#Days_ULint := ((LTIME_TO_ULINT(#Ltime))/86400000000000); //Divide Time (in nanoseconds) by the number of nanoseconds in 24h: result (quotient) = number of full days #Days_String := ULINT_TO_STRING(#Days_ULint); //Convert to String #Days_String := DELETE(IN := #Days_String, L := 1, P := 1); //Remove '+' sign #Hours_ULint :=(LTIME_TO_ULINT(#Ltime)) MOD 86400000000000; //Divide Time (in nanoseconds) by the number of nanoseconds in 24h: reminder = time without days (in nancoseconds) #Hours_String := ULINT_TO_STRING(#Hours_ULint / 3600000000000); //Divide time without days (in nanoseconds) by the number of nancoseconds in 1h: result (quotient) = number of full hours. Convert to String. #Hours_String := DELETE(IN := #Hours_String, L := 1, P := 1); //Remove '+' sign #Minutes_ULint := #Hours_ULint MOD 3600000000000; //Divide time without days (in nanoseconds) by the number of nanoseconds in 1h: reminder = time without days and hours (in nancoseconds) #Minutes_String := ULINT_TO_STRING(#Minutes_ULint / 60000000000); //Divide time without days and hours (in nanoseconds) by the number of nanoseconds in 1m: result (quotient) = number of full minutes. Convert to String. #Minutes_String := DELETE(IN := #Minutes_String, L := 1, P := 1); //Remove '+' sign #Seconds_ULint := #Minutes_ULint MOD 60000000000; //Divide time without days and hours (in nanoseconds) by the number of nanoseconds in 1m: reminder = time without days and hours and minutes (in nancoseconds) #Seconds_String := ULINT_TO_STRING(#Seconds_ULint / 1000000000); //Divide time without days and hours and minutes (in nanoseconds) by the number of nanoseconds in 1s: result (quotient) = number of full seconds. Convert to String #Seconds_String := DELETE(IN := #Seconds_String, L := 1, P := 1); //Remove '+' sign #miliSeconds_ULint := #Seconds_ULint MOD 1000000000; //Divide time without days and hours and minutes (in nanoseconds) by the number of nanoseconds in 1s: reminder = time without days and hours and minutes and seconds (in nanoseconds) #miliSeconds_String := ULINT_TO_STRING(#miliSeconds_ULint / 1000000); //Divide time without days and hours and minutes and seconds (in nanoseconds) by the number of nanoseconds in 1ms: result (quotient) = number of full miliseconds. Convert to String #miliSeconds_String := DELETE(IN := #miliSeconds_String, L := 1, P := 1);//Remove '+' sign #String_temp := CONCAT(IN1 := CONCAT(IN1 := CONCAT(IN1 := CONCAT(IN1 := CONCAT(IN1 := CONCAT(IN1 := (CONCAT(IN1 := #Days_String, IN2 := 'D:')), IN2 := #Hours_String), IN2 := 'H:'), IN2 := #Minutes_String), IN2 := 'M:'), IN2 := #Seconds_String), IN2 := 'S:'); //Maximum number of nested operation, thus: #Q_String := CONCAT(IN1 := CONCAT(IN1 := #String_temp, IN2 := #miliSeconds_String), IN2 := 'mS'); |
I nie zapomnij zdefiniować zmiennych lokalnych:

3. Wywołaj FC w kodzie.

Wywołanie funkcji konwersji, która umożliwi nam wyświetlanie timera na HMI.
4. Wstaw Timer na ekranie HMI.
Przeciągnij i upuść ‘I/O field’ na ekranie:

Skonfiguruj ‘I/O field’:
Podświetl ‘I/O field’ i przejdź do: Properties » General » Display formst i wybierz ‘String’ z rozwijanego menu. Zmień ilosc znakow (Field length) na co najmniej “22”, aby pomieścić caly String, który utworzyliśmy. Zmień tryb (Mode) na: ‘Output’ i co najważniejsze, wskaż prawidłową zmienna w PLC:
