plctwincat

How to implement FB GetLocalAmsNetId?


this is my first question here on stackoverflow and im hoping someone might be able to help me out. I'm trying to get the local AmsNetId of my TwinCat PLC system. The Code is running on the TwinCat System locally. The function is working properly, no problems compiling. But the functionblock FB_GetLocalAmsNetId never seems to return the Ams Net Id. fbGetAmsNetId.bBusy is always busy. I dont know what i'm doing wrong.

Variables:

FUNCTION_BLOCK FB_GetAmsNetId
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
    fbGetAmsNetId : FB_GetLocalAmsNetId;
    bRequestStarted : BOOL := FALSE;
    sAmsNetId : T_AmsNetId;
END_VAR

Programcode:

IF(bRequestStarted = FALSE) THEN
    fbGetAmsNetId(bExecute := TRUE, tTimeOut := T#2S);
    bRequestStarted := TRUE;
ELSE
    IF(NOT fbGetAmsNetId.bBusy) THEN
        sAmsNetId := fbGetAmsNetId.AddrString;
        fbGetAmsNetId.bExecute := FALSE;
        bRequestStarted := FALSE;
    END_IF
END_IF

Solution

  • You need to cyclically call fbGetAmsNetId in your code, otherwise FB_GetLocalAmsNetId will not be able to finish it's internal operations beeing executed for only one plc cycle.

    For example:

    fbGetAmsNetId();
    IF(bRequestStarted = FALSE) THEN
        fbGetAmsNetId(bExecute := TRUE, tTimeOut := T#2S);
        bRequestStarted := TRUE;
    ELSE
        IF(NOT fbGetAmsNetId.bBusy) THEN
            sAmsNetId := fbGetAmsNetId.AddrString;
            fbGetAmsNetId.bExecute := FALSE;
            bRequestStarted := FALSE;
        END_IF
    END_IF