How can I retrieve the display text of an event and use it as a string in twincat3?

121 views Asked by At

I am trying to create an FB for events handling, that can be fully translated. The only issue I am facis is with the sourceinfo. This is what I currently have:(The code is mainly irrelevant to the question but just in case suggestions might not work with what I currently have)

FUNCTION_BLOCK FB_Alarm
VAR_INPUT
    stTcEventEntry: TcEventEntry; //Event data (Class, EventId and Severity. eg. TC_EVENTS.DemoAlarmClass_1.DemoAlarm_1)
    stItem: TcEventEntry; //Item description. Events text must start with '{0}' if description should be added (TcEventClass with Item description eg. TC_EVENTS.ItemClass.G1M1)
    bWithConfirmation : BOOL := FALSE; //Alarm confirmation enable
    sSourceName : STRING; //Alarm source name eg. unit name
    sSubName: STRING; //Sub name eg. -M1
    udiId: UDINT:= 1; //Source id. Can be used to make alarm source unique if sSourceName and sSubName are identical
    sAdditionalText: STRING:= ''; //Additional text. !!!!!! Not in text file->No translation
    
    bAlarm: BOOL; // Alarm bit (True=Active)
END_VAR
VAR_OUTPUT
    bAlarmActive : BOOL; //Alarm active
    bConfirmed : BOOL; //Alarm acknowledged
END_VAR
VAR
    iState : INT := 0; //Seq. State
    iStatePrev : INT := -1; //Seq. state from last scan
    bEntry : BOOL; //New state entry one shot
    
    fbAlarm : FB_TcAlarm; // Alarm instance
    hr : HRESULT;
    hrLastError : HRESULT;
    
    fbSourceInfo : FB_TcSourceInfo; //Source info (instance info)
    ipSourceInfo : I_TcSourceInfo; //Pointer to source info
    fbRequestEventText: Tc3_EventLogger.FB_RequestEventText; //Get event text
    sEventText: STRING; //Event text (English)
    bReqEventTextError: BOOL; //Get event text failed
    bUseItemDescription: BOOL; //Use Item description for this event
END_VAR

//State machine
bEntry := iState<>iStatePrev;
iStatePrev := iState;

CASE iState OF
     0: //*** Init ***
        hrLastError := 0;
        
        // Update alarm source name
        IF len(sSourceName)>0 THEN
            fbSourceInfo.sName:= concat(sSourceName, sSubName);
            fbSourceInfo.nId:= udiId;
            ipSourceInfo := fbSourceInfo;
        ELSE
            ipSourceInfo := 0;
        END_IF;
        
        //Update alarm
        hr := fbAlarm.Release();
        hr := fbAlarm.Create(
                eventClass:= stTcEventEntry.uuidEventClass, 
                nEventId:= stTcEventEntry.nEventId, 
                eSeverity:= stTcEventEntry.eSeverity, 
                bWithConfirmation:= bWithConfirmation, 
                ipSourceInfo:= ipSourceInfo);       
        IF FAILED(hr) THEN
            hrLastError := hr;
        END_IF  
        
        //Conditions    
        iState := 1;
        
        
    1: //Check if Item description should be added
        IF fbAlarm.RequestEventText(nLangId:= 1033, sResult:= sEventText, nResultSize:= SIZEOF(sEventText), bError=>bReqEventTextError) THEN
            IF NOT bReqEventTextError THEN
                IF Tc2_Standard.MID(STR:= sEventText, LEN:= 3, POS:= 1)='{0}' THEN
                    //Event text prepared FOR Item description
                    bUseItemDescription:= TRUE;
                END_IF
            END_IF
            iState:= 10;
        END_IF

        
    10: //*** No alarm ***
        IF bEntry THEN
            IF fbAlarm.bRaised THEN
                //Don't clear alarm if not set
                hr := fbAlarm.Clear(nTimeStamp:=0, bResetConfirmation:=TRUE);
                IF FAILED(hr) THEN
                    hrLastError := hr;
                END_IF
            END_IF
            bAlarmActive := FALSE;
        END_IF
        
        //Conditions
        IF bAlarm THEN
            iState := 11;
        END_IF
        
    11: //*** Alarm ***
        IF bEntry THEN
            IF NOT bAlarmActive THEN
                //Insert additional informations and raise alarm
                fbAlarm.ipArguments.Clear();
                IF stItem.nEventId<>0 and bUseItemDescription THEN
                    //Item description class connected
                    fbAlarm.ipArguments.AddEventReferenceIdGuid(EventClass:= stItem.uuidEventClass, nEventId:= stItem.nEventId); 
                END_IF
                IF Tc2_Standard.LEN(sAdditionalText)>0 THEN
                    //Additional text specified
                    fbAlarm.ipArguments.AddString(sAdditionalText);
                END_IF
                
                hr := fbAlarm.Raise(nTimeStamp:=0);
                IF FAILED(hr) THEN
                    hrLastError := hr;
                END_IF
                bAlarmActive := TRUE;
            END_IF;
        END_IF
        
        //Conditions
        IF NOT bAlarm THEN
            IF NOT bWithConfirmation THEN
                //Confimation not required
                iState := 10;
            ELSE
                //Wait for confirmation
                iState := 12;
            END_IF;
        END_IF
        
        
        
    12: //*** Alarm not active and waiting for confirmation ***
        
        //Conditions
        IF bAlarm THEN
            //Alarm activated
            iState:= 11;
        END_IF
        IF fbAlarm.eConfirmationState=TcEventConfirmationState.Confirmed THEN
            //Alarm confirmed
            iState := 10;
        END_IF
ELSE
    iState := 0;
END_CASE

//Update confirmed
bConfirmed := fbAlarm.eConfirmationState=TcEventConfirmationState.Confirmed AND bAlarmActive; 

I would like to do a similar idea with that is being done with the sitem (the item type), which is retrived from an event class. Is it possible to get the display text of an event from its event class, and save it as a string? any suggestions?

0

There are 0 answers