Sample: SNMP multiple client trap

This Sample describes the sending of a trap by multiple values. Traps can be used to alert thresholds. On every hundred increment of the counter a trap will be send.

Download: PLC Project

Variablen Declaration

PROGRAM MAIN
VAR
    SendTrap:       FB_SendTrap;
    iState:         INT := 0;
    iCounter:       UINT:= 0;
    sObjectId:      STRING :='1.3.6.1.4.1.2';
    sMessage:       T_MaxString:='Information string';
    iGauge:         UDINT:= 4294967295;
    iTimeTicks:     UDINT:= 1000000;
    arrVarBind:     ARRAY[1..5] OF ST_SNMP_VariableBinding;
END_VAR

PLC Program

CASE iState OF

    0: (* send trap on every hundred cycle *)
        iCounter := iCounter + 1;
        IF ((iCounter MOD 100) = 0) THEN
            iState := 10;
        END_IF

    10:(* enable FB *)
        SendTrap.bEnable := TRUE;
        IF SendTrap.bEnabled THEN
            iState := 20;
        END_IF

    20: (* set SNMP trap parameter *)
        arrVarBind[1].iType := E_SNMP_INTEGER;
        arrVarBind[1].iLength := 4;
        arrVarBind[1].pArrValue := ADR(iCounter);
        arrVarBind[1].sOID := '1.3.1.3.255.1.1';

        (*Variable Binding 2*)
        arrVarBind[2].iType := E_SNMP_OBJECTID;
        arrVarBind[2].iLength := LEN(sObjectId);
        arrVarBind[2].pArrValue: = ADR(sObjectId);
        arrVarBind[2].sOID:= '1.3.6.1.3.255.2';
        
        (*Variable Binding 3*)
        arrVarBind[3].iType := E_SNMP_OCTETSTRING;
        arrVarBind[3].iLength := LEN(sMessage);
        arrVarBind[3].pArrValue := ADR(sMessage);
        arrVarBind[3].sOID := '1.3.6.1.3.255.3';

        (*Variable Binding 4*)
        arrVarBind[4].iType := E_SNMP_GAUGE32;
        arrVarBind[4].iLength := 4;
        arrVarBind[4].pArrValue := ADR(iGauge);
        arrVarBind[4].sOID := '1.3.6.1.3.255.4';
    
        (*Variable Binding 5*)
        arrVarBind[5].iType := E_SNMP_TIMETICKS;
        arrVarBind[5].iLength := 4;
        arrVarBind[5].pArrValue := ADR(iTimeTicks);
        arrVarBind[5].sOID := '1.3.6.1.3.255.5';

        SendTrap.sCommunity := 'public';
        SendTrap.iGenericTrapNumber := E_SNMP_WarmStart;
        SendTrap.bExecute := TRUE;
        iState := 30;

    30:     (* reset *)
        SendTrap.bExecute := FALSE;
        SendTrap.bEnable := FALSE;
        IF NOT SendTrap.bBusy THEN
            iState := 0;
        END_IF

        IF SendTrap.bError THEN
            SendTrap.bEnable := FALSE;
            iState := 99;
        END_IF

    99:     (* Error case *)
        ;
END_CASE

SendTrap(
        (* TODO: add device IP of TwinCAT device *)
            sLocalHostIp := '',
        (* TODO: add SNMP Manager IP *)
            sManagerIP := '',
            pArrVarBinding := ADR(arrVarBind),
            nVarBindings := 5
    );

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT version 2.8.0 or higher

PC or CX (x86)

TcSnmp.lib ( TcpIp.Lib; Standard.Lib; TcBase.Lib; TcSystem.Lib are included automatically )

TwinCAT v2.10.0 Build >= 1301

CX (ARM)