<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.0.20">
  <POU Name="FB_ChannelLogErrorEvent" Id="{9c58fa6f-f9fd-4ceb-b3ca-a3e51cef3dd3}">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_ChannelLogErrorEvent
VAR_INPUT
	stChanToPlc: 		NCTOPLC_NCICHANNEL_REF;
	nChan: 				UINT;
	bInit: 				BOOL;       (* rising edge initializes and starts FB *)
END_VAR
VAR_OUTPUT
	bBusy: 				BOOL;
	bErr: 			 	BOOL;
	nErrId:			 	UDINT;
END_VAR
VAR
	fbLogEvent:			ADSLOGEVENT;
	bInitialized: 		BOOL := FALSE;
	fbRTrig: 			R_TRIG;
	bInitRe: 			BOOL := FALSE;  (* Init Rising Edge *)

	nState: 				UINT;
	nErrorCode: 		UINT;
	bHasError: 			BOOL;
	sFormatString: 	STRING := '';
	sData:				STRING[255];
	fbAdsRead: 			ADSREAD;
	sPrgName:			STRING[101];
	sPrgLine:			STRING[101];
	nTextIndex: 		UDINT;
	nItpState: 			UDINT;
	stFileInfo:			ST_FileInfo;

	nError: 				INT;

	arrParaPointer: 	ARRAY [1..8] OF POINTER TO UDINT;

	nPara1:				DINT;
	sPara2:				STRING;
	sPara3:				STRING;
	nPara4:				DINT;
	sPara5:				STRING[101];
	nPara6:				DINT;
	sPara7:				STRING;
	sPara8:				STRING;
	fbAdsRdWrtEx: 		ADSRDWRTEX;
	fbAdsRdDevIinfo: 	ADSRDDEVINFO;
	nMajor: 				BYTE;
	nMinor: 				BYTE;
	nBuild: 				WORD;

	bEventSignaled: 	BOOL;
	nOffset2: 			DINT;
	nPos: 				UINT;
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[(* ==========================================================*)
(*     FB: FB_ChannelLogErrorEvent                           *)
(* ----------------------------------------------------------*)
(*   Desc:                                                   *)
(*   Auth: MiB                                               *)
(*   Date: 02.09.2005                                        *)
(*   Rev.: 1.0                                               *)
(* ==========================================================*)



fbRTrig(CLK:= bInit);
IF fbRTrig.Q THEN
	bInitRe := TRUE;
	bInitialized := FALSE;
	nState := 0;
END_IF


IF NOT bInitialized AND bInitRe THEN

		bInitialized := TRUE;
		bInitRe := FALSE;
		fbLogEvent(FbCleanup := TRUE);
		(* ----------------------------------------- *)
		(* initilize fixed elements of event 			*)
		(* ----------------------------------------- *)

		fbLogEvent.NETID := '';		(* Local Computer *)
		fbLogEvent.PORT:= 110;		(* Event Logger *)
		fbLogEvent.TMOUT:= t#10s;
		fbLogEvent.EventConfigData.Class := TCEVENTCLASS_ALARM;
		fbLogEvent.EventConfigData.Prio	 := TCEVENTPRIO_IMPLICIT;
		fbLogEvent.EventConfigData.ProgId	:= 'TcEventFormatter.TcXmlFormatter';
		fbLogEvent.EventConfigData.Flags	:= TCEVENTFLAG_SRCID OR TCEVENTFLAG_LOG;
		fbLogEvent.EventConfigData.SourceId := nSourceChannel + (nChan - 1);		(* Source ID for PTP/NCI errors *)
		fbLogEvent.EventConfigData.SourceString := '';
		fbLogEvent.EventConfigData.StreamType := TCEVENTSTREAM_NORMAL;
		fbLogEvent.EventConfigData.bQuitRequired := FALSE;
		fbLogEvent.EventConfigData.DataFormatStrAddress := ADR(sFormatString);
END_IF

bHasError := ItpHasError(stChanToPlc);
nItpState := ItpGetStateInterpreter(stChanToPlc);


CASE nState OF

0:
		IF bInitialized THEN
			bBusy	:= TRUE;
			bErr	:= FALSE;
			nErrId	:= 0;
			sPrgName := '';
			sPrgLine := '';
			nTextIndex := 0;
			sData := '';
			nState := 10;
			nPara1 := 0;
			sPara2 := '';
			sPara3 := '';
			nPara4 := 0;
			sPara5 := '';
			nPara6 := 0;
			sPara7 := '';
			sPara8 := '';
			bEventSignaled := FALSE;
		END_IF

10:
		IF bHasError THEN
			sFormatString := '%d%s%s%d%s%d%s%s';

			nErrorCode := UDINT_TO_UINT(ItpGetError(stChanToPlc));

			fbLogEvent.EventConfigData.Id := nEventChannel;						(* Channel error *)

			sPara2 := CONCAT('[S',UINT_TO_STRING(nSourceTextPtpNci));
			sPara2 := CONCAT(sPara2,'E');
			sPara2 := CONCAT(sPara2,UINT_TO_STRING(nErrorCode));
			sPara2 := CONCAT(sPara2,']');
			nError := UINT_TO_INT(nErrorCode);

			sPara3 := CONCAT('[S',UINT_TO_STRING(nSourceDescPtpNci));
			sPara3 := CONCAT(sPara3,'E');
			sPara3 := CONCAT(sPara3,UINT_TO_STRING(nErrorCode));
			sPara3 := CONCAT(sPara3,']');

			nPara1 := UINT_TO_DINT(nErrorCode);
			nPara4 := nChan;

			nState := 20;
		END_IF

(* get more information about the error *)
20:

		fbAdsRead(
			NETID:= '',
			PORT:= 501,
			IDXGRP:= 16#2100+nChan,
			IDXOFFS:= 16#12,			(*ReadFileInfo*)
			LEN:= SIZEOF(stFileInfo),
			DESTADDR:= ADR(stFileInfo),
			READ:= TRUE,
			TMOUT:= T#250ms,
			BUSY=> ,
			ERR=> ,
			ERRID=> );
		IF (NOT fbAdsRead.BUSY) THEN
			fbAdsRead(READ := FALSE);
			IF fbAdsRead.ERR THEN
				nErrId := fbAdsRead.ERRID;
				nState := 16#999;
			ELSE
				sPara5 := stFileInfo.sFilename;
				nPara6 := stFileInfo.nOffset;
				nState := 30;
			END_IF
		END_IF;

30: (* get text index *)
		fbAdsRead(
			NETID:= '',
			PORT:= 501,
			IDXGRP:= 16#2100 + nChan,
			IDXOFFS:= 16#10,			(* actual text index  *)
			LEN:= SIZEOF(nOffset2),
			DESTADDR:= ADR(nOffset2),
			READ:= TRUE,
			TMOUT:= T#250ms,
			BUSY=> ,
			ERR=> ,
			ERRID=> );
		IF (NOT fbAdsRead.BUSY) THEN
			fbAdsRead(READ := FALSE);
			IF (nPara6 <> -1 ) THEN
				nState := 40;			(* get line with error *)
			ELSE
				nState := 50;			(* TextIndex = -1 -> no line to get *)
			END_IF

		END_IF;

40:	(* get line with error *)
		fbAdsRdWrtEx(
			NETID:= '',
			PORT:= 501,
			IDXGRP:= 16#2100 + nChan,
			IDXOFFS:= 16#11,
			WRITELEN:= SIZEOF(nOffset2),
			READLEN:= SIZEOF(sPara7),
			SRCADDR:= ADR(nOffset2),
			DESTADDR:= ADR(sPara7),
			WRTRD:= TRUE,
			TMOUT:= T#250ms,
			BUSY=> ,
			ERR=> ,
			ERRID=> ,
			COUNT_R=> );
		IF NOT fbAdsRdWrtEx.BUSY THEN
			fbAdsRdWrtEx(WRTRD := FALSE);
			nPos := FIND( sPara7, '$R$N' );
			IF nPos <> 0 THEN
				sPara7 := LEFT(sPara7, nPos - 1);		(* delete $R$N in string*)
			END_IF
			nState := 50;
		END_IF

50: (* get version information *)
		fbAdsRdDevIinfo(
			NETID:= '',
			PORT:= 500,
			RDINFO:= TRUE,
			TMOUT:= t#250ms,
			BUSY=> ,
			ERR=> ,
			ERRID=> ,
			DEVNAME=> ,
			DEVVER=> );
		IF NOT fbAdsRdDevIinfo.BUSY THEN
			fbAdsRdDevIinfo(RDINFO := FALSE);
			IF NOT fbAdsRdDevIinfo.ERR THEN
				nMajor.0 := fbAdsRdDevIinfo.DEVVER.0;
				nMajor.1 := fbAdsRdDevIinfo.DEVVER.1;
				nMajor.2 := fbAdsRdDevIinfo.DEVVER.2;
				nMajor.3 := fbAdsRdDevIinfo.DEVVER.3;
				nMajor.4 := fbAdsRdDevIinfo.DEVVER.4;
				nMajor.5 := fbAdsRdDevIinfo.DEVVER.5;
				nMajor.6 := fbAdsRdDevIinfo.DEVVER.6;
				nMajor.7 := fbAdsRdDevIinfo.DEVVER.7;

				nMinor.0 := fbAdsRdDevIinfo.DEVVER.8;
				nMinor.1 := fbAdsRdDevIinfo.DEVVER.9;
				nMinor.2 := fbAdsRdDevIinfo.DEVVER.10;
				nMinor.3 := fbAdsRdDevIinfo.DEVVER.11;
				nMinor.4 := fbAdsRdDevIinfo.DEVVER.12;
				nMinor.5 := fbAdsRdDevIinfo.DEVVER.13;
				nMinor.6 := fbAdsRdDevIinfo.DEVVER.14;
				nMinor.7 := fbAdsRdDevIinfo.DEVVER.15;

				nBuild.0 := fbAdsRdDevIinfo.DEVVER.16;
				nBuild.1 := fbAdsRdDevIinfo.DEVVER.17;
				nBuild.2 := fbAdsRdDevIinfo.DEVVER.18;
				nBuild.3 := fbAdsRdDevIinfo.DEVVER.19;
				nBuild.4 := fbAdsRdDevIinfo.DEVVER.20;
				nBuild.5 := fbAdsRdDevIinfo.DEVVER.21;
				nBuild.6 := fbAdsRdDevIinfo.DEVVER.22;
				nBuild.7 := fbAdsRdDevIinfo.DEVVER.23;
				nBuild.8 := fbAdsRdDevIinfo.DEVVER.24;
				nBuild.9 := fbAdsRdDevIinfo.DEVVER.25;
				nBuild.10 := fbAdsRdDevIinfo.DEVVER.26;
				nBuild.11 := fbAdsRdDevIinfo.DEVVER.27;
				nBuild.12 := fbAdsRdDevIinfo.DEVVER.28;
				nBuild.13 := fbAdsRdDevIinfo.DEVVER.29;
				nBuild.14 := fbAdsRdDevIinfo.DEVVER.30;
				nBuild.15 := fbAdsRdDevIinfo.DEVVER.31;

				sPara8 := fbAdsRdDevIinfo.DEVNAME;
				sPara8 := CONCAT(sPara8,': v');
				sPara8 := CONCAT(sPara8, BYTE_TO_STRING(nMajor));
				sPara8 := CONCAT(sPara8,'.');
				sPara8 := CONCAT(sPara8, BYTE_TO_STRING(nMinor));
				sPara8 := CONCAT(sPara8,' (Build ');
				sPara8 := CONCAT(sPara8, WORD_TO_STRING(nBuild));
				sPara8 := CONCAT(sPara8,')');
			ELSE
				sPara8 := '';
			END_IF
			nState := 60;
		END_IF
60:
		arrParaPointer[1] := ADR(nPara1);
		arrParaPointer[2] := ADR(sPara2);
		arrParaPointer[3] := ADR(sPara3);
		arrParaPointer[4] := ADR(nPara4);
		arrParaPointer[5] := ADR(sPara5);
		arrParaPointer[6] := ADR(nPara6);
		arrParaPointer[7] := ADR(sPara7);
		arrParaPointer[8] := ADR(sPara8);
		fbLogEvent.EventDataAddress := ADR(arrParaPointer);
		fbLogEvent.EventDataLength := 0; (* SIZEOF(EventData);*)
		nState := 70;


70: (* Fire event *)
		IF NOT bEventSignaled THEN
			fbLogEvent(NETID:='', PORT:=110, Event:=bHasError , EventQuit:=FALSE,
					 FbCleanup:=FALSE, TMOUT:=t#1s);
		END_IF;
		IF fbLogEvent.EventState = TCEVENTSTATE_SIGNALED THEN
			bEventSignaled := TRUE;
			IF NOT bHasError THEN
				fbLogEvent(Event := FALSE);
			END_IF
		END_IF
		IF fbLogEvent.EventState = TCEVENTSTATE_INVALID OR fbLogEvent.EventState = TCEVENTSTATE_RESET  THEN
			bEventSignaled := FALSE;
			nState := 0;  (* init, wait for new event *)
		END_IF
		IF fbLogEvent.Err THEN
			fbLogEvent(Event := FALSE);
			nErrId := fbLogEvent.ErrId;
			nState := 999;
		END_IF

999: (*Error*)
		bBusy := FALSE;
		bErr := TRUE;
		bInitialized := FALSE;
		nState := 0;
END_CASE
]]></ST>
    </Implementation>
    <ObjectProperties />
  </POU>
</TcPlcObject>