HowTo: Create custom log entries for WAGO controller

HowTo Create Custom Log Entries.docx (88.9 KB)

I have had occasion where it would be helpful to send my own information from CODESYS to the log files that are created on a WAGO controller and can be accessed via the WBM. There are two parts to this HowTo: (1) Adding log entries during normal execution of a CODESYS project; and (2) Adding log entries when an Exception occurs that halts program operation. Part 2 builds on Part 1, so please read both.

1. Adding log entries during normal operation

First, add the library WagoSysLog to the Library Manager. This library contains two function blocks that will be used – FbLogger and FbLogChSysUdp.

FbLogger adds a descriptor to the log entries that will be created and also provides the .writeMsg method that will be used to create those entries.

FbLogChSysUdp is a fixed routing path for the ‘WAGO’ hardware. It tells the logger how to find the correct location for the log file so that the WBM Diagnostic page can see it.

After the first time the .writeMsg method is executed, wago_plc_log.log. This log contains the generated messages.

Example code:

PROGRAM In_PRG_log_trig
VAR
myExampleLogger : FbLogger(‘WagoExample’);
mySysLogChannel : FbLogChSysUdp(‘WAGO’);
xIsInit : BOOL;
xLogMessage : BOOL;
sMessage : STRING(255);

END_VAR

//— Register the Log-Channel once ---------------------------------

IF NOT xIsInit THEN // register the Log-Channel at Logger once
myExampleLogger.RegisterLogChannel( mySysLogChannel );
xIsInit := TRUE;
END_IF

//-------------------------------------------------------------------

//— Example for generating a Log-Message ------------------------------------------------------------

IF xLogMessage THEN
sMessage := CONCAT('a = ', TO_STRING(PLC_PRG.a));
myExampleLogger.writeMsg( ‘Manual’, eSysLogPriority.LOG_ERR, sMessage);
xLogMessage := FALSE;
END_IF

In the above example, “a” is a simple a:=a+1 counter. When xLogMessage is triggered, the following messages can be seen in wago_plc_log.log:

2. Adding log entries upon Exception

Open the Task Configuration and select the “System Events” tab. Click the “Add Event Handler” button. From this popup, select the Event “Exception” and name the Function that will be executed.

This function can then be created similar to the code above, capturing any variables status desired at the time the Exception occurs.

FUNCTION Exception_Log : DWORD
VAR_IN_OUT
EventPrm: CmpApp.EVTPARAM_CmpAppException;
END_VAR
VAR
myExampleLogger : FbLogger(‘WagoExample’);
mySysLogChannel : FbLogChSysUdp(‘WAGO’);
xIsInit : BOOL;
xLogMessage : BOOL;
sMessage : STRING(255);
END_VAR

//— Register the Log-Channel once ---------------------------------

IF NOT xIsInit THEN // register the Log-Channel at Logger once
myExampleLogger.RegisterLogChannel( mySysLogChannel );
xIsInit := TRUE;
END_IF

//-------------------------------------------------------------------

//— Example for generating a Log-Message ----------------------------------------------------------
sMessage := CONCAT('a = ', TO_STRING(PLC_PRG.a));
sMessage := CONCAT(sMessage, '; xCrash1 = ');
sMessage := CONCAT(sMessage, TO_STRING(PLC_PRG.xCrash1));
sMessage := CONCAT(sMessage, '; xCrash2 = ');
sMessage := CONCAT(sMessage, TO_STRING(PLC_PRG.xCrash2));
myExampleLogger.writeMsg( ‘Exception’, eSysLogPriority.LOG_ERR, sMessage);

In the above example, xCrash1 and xCrash2 are testing triggers that can generate an Exception. Here is the resulting message:

Appendix: Generating an Exception on demand for testing

(tested using CC100 FW28)

Both of the IF statements below can be used to generate a CODESYS Exception when either xCrash1 or xCrash 2 is set TRUE:

PROGRAM PLC_PRG
VAR
xCrash1: BOOL;
iVar1: INT;
wTest: WORD;
pPtr1: POINTER TO INT;
xCrash2: BOOL;
iTest2: INT;
END_VAR

IF xCrash1 THEN
wTest := pPtr1^;
END_IF

IF xCrash2 THEN
iTest2 := 100 / iVar1;
END_IF

3 Likes