Get Alarm status in Codesys

kris-samoy
2024-06-03
8 hours ago
  • kris-samoy - 2024-06-03

    I am rather new to Codesys and I'm trying to figure out how to get the status of an alarm.
    This to interlock the start of a device until the alarm has been acknowledged...

    I am not sure if IAlarm.GetState (METH) can be used for this purpose. If yes then could someone please briefly explain how to use this?

    https://content.helpme-codesys.com/en/libs/AlarmManager/4.1.0.0/Interfaces/pou-IAlarm/GetState.html

    Thx & grtz,
    Kris

     

    Last edit: kris-samoy 2024-06-03
  • kristianh - 6 days ago

    Hi,
    I'm trying to accomplish the same, have you had any success?

    Kristian

     
  • TimvH

    TimvH - 8 hours ago

    You are right, that you can use the GetState method, but it is not that simple. You first have to get a list of (filtered) alarms from the AlarmManager. You can find an example in Forge how to do this. See https://forge.codesys.com/prj/codesys-example/alarm-manager/home/Home/

    Then you can go through this list to get the state of all the (filtered) alarms.

    See below the code which you could use. This is all based on the example from forge.

    Create a program:

    // This example shows how to access alarms via structured text.
    PROGRAM PLC_PRG
    VAR
        xInit : BOOL := TRUE;
        udiResult : UDINT;
        fbAlarmFilterCriteriaAll : FB_AlarmFilterCriteriaAll;
        fbAlarmManagerClient : FB_AlarmManagerClient;
        itfAlarmManagerClient : IAlarmManagerClient := fbAlarmManagerClient;
    
        xAlarm1 : BOOL;
        xAlarm2 : BOOL;
        xWarning : BOOL;
    
        iNrOfAlarmsInAlarmList : INT;
        iNrOfActiveAlarmsInAlarmList : INT;
        paitfAlarm: POINTER TO ARRAY [0..0] OF AlarmManager.IAlarm;
        iAlarmIndex : INT;
        eAlarmState: AlarmManager.AlarmState;
    END_VAR
    
    IF xInit THEN
        xInit := FALSE;
        fbAlarmManagerClient.itfAlarmFilterCriteria := fbAlarmFilterCriteriaAll;
        // register alarm client to get updated about alarm status / changes
        udiResult := AlarmManager.g_AlarmHandler.RegisterClient(itfAlarmManagerClient, 0, 0);
    END_IF
    
    // Polling the number of alarms
    udiResult := AlarmManager.g_AlarmHandler.GetActiveAlarms(itfAlarmManagerClient, parritfActiveAlarms => paitfAlarm, iCountActiveAlarms => iNrOfAlarmsInAlarmList);
    iAlarmIndex := 0;
    iNrOfActiveAlarmsInAlarmList := 0;
    WHILE iAlarmIndex < iNrOfAlarmsInAlarmList DO
        eAlarmState := paitfAlarm^[iAlarmIndex].GetState();
        IF eAlarmState = AlarmManager.AlarmState.Active OR eAlarmState = AlarmManager.AlarmState.ActiveAcknowledged THEN
            iNrOfActiveAlarmsInAlarmList := iNrOfActiveAlarmsInAlarmList + 1;
        END_IF
        iAlarmIndex := iAlarmIndex + 1;
    END_WHILE
    

    See below some details about the function blocks:
    One function block should implement the IAlarmFilterCriteria interface. This can be empty except a few methods.

    FUNCTION_BLOCK FB_AlarmFilterCriteriaAll IMPLEMENTS AlarmManager.IAlarmFilterCriteria
    
    Method implementation (others related to interface are empty)
    
    METHOD AreAllAlarmClassesSelected : BOOL
    AreAllAlarmClassesSelected := TRUE;
    
    METHOD AreAllAlarmGroupsSelected : BOOL
    AreAllAlarmGroupsSelected := TRUE;
    
    METHOD GetPriorityFrom : USINT
    GetPriorityFrom := 0;
    
    METHOD GetPriorityTo : USINT
    GetPriorityTo := 255;
    

    The other function block should implement IAlarmManagerClient and get a reference to the FB which implements the IAlarmFilterCriteria

    FUNCTION_BLOCK FB_AlarmManagerClient IMPLEMENTS AlarmManager.IAlarmManagerClient
    VAR_INPUT
        itfAlarmFilterCriteria: AlarmManager.IAlarmFilterCriteria;
    END_VAR
    
    Method implementation (others related to the interface are empty)
    
    METHOD GetFilterCriteria : AlarmManager.IAlarmFilterCriteria
    // see VAR_INPUT for filter
    GetFilterCriteria := itfAlarmFilterCriteria;
    

    Off course you have to add the AlarmManager to your application and add some alarms to it.

     

Log in to post a comment.