<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.0.20">
  <POU Name="FB_AxisLogErrorEvent" Id="{ad0450ec-7024-4d9f-842e-a81908cc3568}">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_AxisLogErrorEvent
VAR_INPUT
	stAxisRef: 	Axis_Ref;
	bInit: 				BOOL;
END_VAR
VAR_OUTPUT
	bBusy:				BOOL;
	bErr: 				BOOL;
	nErrId:				UDINT;
END_VAR
VAR
	fbLogEvent: 		ADSLOGEVENT;
	bInitialized: 		BOOL := FALSE;
	nState: 				UINT;
	nErrorCode: 		DWORD;
	bHasError: 			BOOL;
	sFormatString: 	STRING := '';

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

	nPara1:				DINT;
	sPara2:				STRING;
	sPara3:				STRING;
	nPara4:				DINT;
	sPara5: 			STRING;

	fbRTrig: 			R_TRIG;
	bInitRe: 			BOOL;
	bEventSignaled: 	BOOL;
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[(* ==========================================================*)
(*     FB: FB_AxisLogErrorEvent                              *)
(* ----------------------------------------------------------*)
(*   Desc:                                                   *)
(*   Auth: MiB                                               *)
(*   Date: 02.09.2005                                        *)
(*   Rev.: 1.0                                               *)
(* ==========================================================*)


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

IF NOT bInitialized AND bInitRe THEN
	bInitialized := 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 := nSourceAxis + stAxisRef.NcToPlc.AxisId - 1;		(* Source ID for axis errors *)
		fbLogEvent.EventConfigData.SourceString := '';
		fbLogEvent.EventConfigData.StreamType := TCEVENTSTREAM_NORMAL;
		fbLogEvent.EventConfigData.bQuitRequired := FALSE;
		fbLogEvent.EventConfigData.DataFormatStrAddress := ADR(sFormatString);
END_IF

stAxisRef.ReadStatus();
bHasError := stAxisRef.Status.Error;
nErrorCode := stAxisRef.Status.ErrorId;

CASE nState OF
0:	(* init *)
		IF bInitialized THEN
			bBusy	:= TRUE;
			bErr	:= FALSE;
			nErrId	:= 0;
			nState := 10;
			bEventSignaled := FALSE;
		END_IF

10:
		IF bHasError THEN
			sFormatString := '%d%s%s%d%s';
			fbLogEvent.EventConfigData.Id := nErrorCode;
			fbLogEvent.EventConfigData.Id := 201;				(* Axis error *)

			nPara1 := DWORD_TO_DINT(nErrorCode);

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


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

			nPara4 := DWORD_TO_DINT(stAxisRef.NcToPlc.AxisId);
			sPara5 := LREAL_TO_STRING(stAxisRef.NcToPlc.ActPos);

			arrParaPointer[1] := ADR(nPara1);
			arrParaPointer[2] := ADR(sPara2);
			arrParaPointer[3] := ADR(sPara3);
			arrParaPointer[4] := ADR(nPara4);
			arrParaPointer[5] := ADR(sPara5);

			fbLogEvent.EventDataAddress := ADR(arrParaPointer);
			fbLogEvent.EventDataLength := 0;

			nState := 20;
		END_IF


20: (* 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 := 16#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;
		nState := 0;

END_CASE
]]></ST>
    </Implementation>
    <ObjectProperties />
  </POU>
</TcPlcObject>