FUNCTION_BLOCK RA_E300_OUTPUT_CONTROL
(*=====================================================================================================================================
	[UDFB Name]					:RA_E300_OUTPUT_CONTROL    
	[Category]					:Communication
	[Description]				:Control E300 Control Module and Digital Expansion Module output points using Explicit Messaging
	[Author]					:Rockwell Automation
	[Created using CCW version]	:8.0
	[FeedBack]					:RASampleCode@ra.rockwell.com
	[Revison History]			:2015-October - Original Release for Version 8
	[Notes]						:

 =====================================================================================================================================*)
MSGWriteOutput_EN						:= TRUE;
FBENO := FBEN;
(*-------- Initialize --------*)
FBEN_RTRIG(FBEN);
IF (__SYSVA_FIRST_SCAN OR FBEN_RTRIG.Q) THEN
	MSGProdName_EN						:= TRUE;

	ProdNameCtrlCfg.TriggerType			:= 5000;
	ProdNameTargetCfg.Path				:= '4,'+ IPAddress;
	ProdNameTargetCfg.CipConnMode		:= 0;    (*0-unconnected, 1-Class 3*)
	ProdNameTargetCfg.UcmmTimeout		:= 250;  (*Unconnected message time out, Set to 0 means 3000ms, range 250-10000*)
	ProdNameTargetCfg.ConnMsgTimeout	:= 800;  (*Class3 time out, Set to 0 means 3000ms, range 800-10000*)
	ProdNameTargetCfg.ConnClose			:= FALSE;(*Don't close the connection upon message completion*)
	ProdNameAppCfg.Service				:= 14;   (*Service code 0Eh for Get Single Attribute*)
	ProdNameAppCfg.Class				:= 1;    (*Class code 01h for Identity Object*)
	ProdNameAppCfg.Instance				:= 1;    (*Instance 1*)
	ProdNameAppCfg.Attribute			:= 7;    (*Attribute 7 for Product Name*)
	WriteOutputCtrlCfg.TriggerType		:= 1000;
	WriteOutputTargetCfg.Path			:= '4,'+ IPAddress;
	WriteOutputTargetCfg.CipConnMode	:= 1;    (*0-unconnected, 1-Class 3*)
	WriteOutputTargetCfg.UcmmTimeout	:= 0;    (*Unconnected message time out, Set to 0 means 3000ms, range 250-10000*)
	WriteOutputTargetCfg.ConnMsgTimeout	:= 0;    (*Class3 time out, Set to 0 means 3000ms, range 800-10000*)
	WriteOutputTargetCfg.ConnClose		:= FALSE;(*Don't close the connection upon message completion*)
	WriteOutputAppCfg.Service			:= 16;   (*Service code 10h for Set Attributes Single*)
	WriteOutputAppCfg.Class				:= 9;    (*Class code 09h for Discrete Output Point Object*)
	WriteOutputAppCfg.Instance			:= ANY_TO_UDINT(OutputNumber+1);
	WriteOutputAppCfg.Attribute			:= 3;    (*Set Value 0=OFF, 1=ON*)
	CIPErrorStatus						:= NoErrorStatus;
	ProductName[1]						:= '';
	SettingApplied						:= FALSE;
	
	CASE OutputNumber OF
		0:	OutputName:= 'OutputPt00';
		1:	OutputName:= 'OutputPt01';
		2:	OutputName:= 'OutputPt02';
		3:	OutputName:= 'OutDigMod1Pt00';
		4:	OutputName:= 'OutDigMod1Pt01';
		5:	OutputName:= 'OutDigMod2Pt00';
		6:	OutputName:= 'OutDigMod2Pt01';
		7:	OutputName:= 'OutDigMod3Pt00';
		8:	OutputName:= 'OutDigMod3Pt01';
		9:	OutputName:= 'OutDigMod4Pt00';
		10:	OutputName:= 'OutDigMod4Pt01';
	ELSE
		OutputName:= 'Invalid OutputNumber';
	END_CASE;
	
END_IF;

FBEN_FTRIG(FBEN);
IF FBEN_FTRIG.Q THEN
	CIPErrorStatus	:= NoErrorStatus;
	SettingApplied	:= FALSE;
END_IF;

(*-------- Main Routine --------*)
IF FBEN AND OutputNumber<=10 THEN
	WriteOutputReqData[1]	:= ANY_TO_USINT(OutputSetting);	(*0=OFF, 1=ON*)
	MSGProdName_Q_RTRIG(MSGProdName.Q);
	MSGWriteOutput_Q_RTRIG(MSGWriteOutput.Q);
	OutputSetting_RTRIG(OutputSetting);
	OutputSetting_FTRIG(OutputSetting);
		
	IF MSGProdName_Q_RTRIG.Q THEN
		CIPErrorStatus		:= MSGProdName.Status;
		ProdName_COP(TRUE,ProdNameResData,0,ProductName,0,1,FALSE);
		
		IF (LEFT(ProductName[1],4)='E300') THEN
			MSGProdName_EN		:= FALSE;
			MSGWriteOutput_EN	:= TRUE;
		ELSE 
			OutputName			:= 'Not An E300';
		END_IF;
		
	END_IF;
		
	IF MSGWriteOutput_Q_RTRIG.Q THEN
		SettingApplied		:= TRUE;
		(*MSGWriteOutput_EN	:= FALSE;*)
	END_IF;
	
	IF (OutputSetting_RTRIG.Q OR OutputSetting_FTRIG.Q) AND (LEFT(ProductName[1],4)='E300') THEN
		SettingApplied		:= FALSE;
		MSGWriteOutput_EN	:= TRUE;
	END_IF;
	
	MSGProdName_Err_RTRIG(MSGProdName.Status.Error);
	MSGWriteOutput_Err_RTRIG(MSGWriteOutput.Status.Error);
	
	IF MSGProdName_Err_RTRIG.Q THEN
		CIPErrorStatus	:= MSGProdName.Status;
		ProductName[1]	:= '';
	END_IF;
	
	IF MSGWriteOutput_Err_RTRIG.Q THEN
		MSGWriteOutput_EN	:= FALSE;
		CIPErrorStatus		:= MSGWriteOutput.Status;
		ProductName[1]		:= '';
		MSGProdName_EN		:= TRUE;
	END_IF;
		
ELSE
	MSGProdName_EN		:= FALSE;
	MSGWriteOutput_EN	:= FALSE;
	
END_IF;

MSGProdName(MSGProdName_EN, ProdNameCtrlCfg, ProdNameAppCfg, ProdNameTargetCfg,, 0, ProdNameResData);
MSGWriteOutput(MSGWriteOutput_EN, WriteOutputCtrlCfg, WriteOutputAppCfg, WriteOutputTargetCfg, WriteOutputReqData, 1,);

END_FUNCTION_BLOCK