/*********************************************/
/* Read/Write Method Variables               */
/*********************************************/


//Request Variables

VARIABLE Request_Reference
{
    LABEL           "Request Reference";
    CLASS           DEVICE;
    TYPE            UNSIGNED_INTEGER (1);
    HANDLING        READ & WRITE;
}

VARIABLE Request_ID
{
    LABEL           "Request ID";
    CLASS           DEVICE;
    TYPE            UNSIGNED_INTEGER (1);
    HANDLING        READ & WRITE;
}

VARIABLE Request_DOID
{
    LABEL           "Request DO ID";
    CLASS           DEVICE;
    TYPE            UNSIGNED_INTEGER (1);
    HANDLING        READ & WRITE;
}

VARIABLE Request_Num_Of_Parameters
{
    LABEL           "Number Of Parameters";
    CLASS           DEVICE;
    TYPE            UNSIGNED_INTEGER (1);
    HANDLING        READ & WRITE;
}

VARIABLE Request_Attribute
{
    LABEL           "Attribute";
    CLASS           DEVICE;
    TYPE            UNSIGNED_INTEGER (1);
    HANDLING        READ & WRITE;
}

VARIABLE Request_Num_Of_Elements
{
    LABEL           "Number of Elements";
    CLASS           DEVICE;
    TYPE            UNSIGNED_INTEGER (1);
    HANDLING        READ & WRITE;
}

VARIABLE Request_Parameter_Number
{
    LABEL           "Parameter Number";
    CLASS           DEVICE;
    TYPE            UNSIGNED_INTEGER (2);
    HANDLING        READ & WRITE;
}

VARIABLE Write_Parameter_Number
{
    LABEL           "Write Parameter Number";
    CLASS           DEVICE;
    TYPE            UNSIGNED_INTEGER (2);
    HANDLING        READ & WRITE;
}

VARIABLE Request_Subindex
{
    LABEL           "Subindex";
    CLASS           DEVICE;
    TYPE            UNSIGNED_INTEGER (2);
    HANDLING        READ & WRITE;
}


// Reply Variables

VARIABLE Reply_Reference
{
    LABEL       "Reply Reference";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER (1);
    HANDLING    READ & WRITE;
}

VARIABLE Reply_ID
{
    LABEL       "Reply ID";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER (1);
    HANDLING    READ & WRITE;
}

VARIABLE Reply_Axis
{
    LABEL       "Reply Axis";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER (1);
    HANDLING    READ & WRITE;
}

VARIABLE Reply_Num_Of_Parameters
{
    LABEL       "Reply Number of Parameters";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER (1);
    HANDLING    READ & WRITE;
}

VARIABLE Reply_Format
{
    LABEL       "Reply Format";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER (1);
    HANDLING    READ & WRITE;
}

VARIABLE Reply_Num_of_Words
{
    LABEL       "Reply Number of Words";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER (1);
    HANDLING    READ & WRITE;
}


// Profibus I&M Data variables
			
// Below variable relies on the fact that manufacturer ID of hex 0x160 is specific to
// Emerson Control Techniques.

VARIABLE I_M_MfrID
{
    LABEL		"Manufacturer";
	HELP		"Device manufacturer name";
	HANDLING    READ;
    CLASS       DEVICE;
    TYPE        ENUMERATED (2)
	{
		{0x0160, "Emerson Control Techniques", "Emerson Control Techniques"}
	}

	PRE_READ_ACTIONS
	{
		write_pre_read_I_and_M_data
	}
}

VARIABLE I_M_OrderID
{
    LABEL		"Model";
	HELP		"Device model number";
    CLASS       DEVICE;
    TYPE        ASCII(20);
    HANDLING    READ;
}

VARIABLE I_M_SerialNumber
{
    LABEL		"Serial Number";
	HELP		"Profibus Module Serial Number";
    CLASS       DEVICE;
    TYPE        ASCII(16);
    HANDLING    READ;
}

VARIABLE I_M_HWVer
{
    LABEL		"Hardware Revision";
	HELP		"Profibus Module Hardware Revision";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER(2);
    HANDLING    READ;
}

VARIABLE I_M_SWVer_A
{
	// TODO: Fix this.  This will not work as a FLOAT with 2 decimal places since it is implemented as two individual bytes.
    LABEL       "Major Software Revision";
	HELP		"Profibus Module Major Software Revision";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER(2)
	{
		SCALING_FACTOR 0.01;
	}
    HANDLING    READ;
}

VARIABLE I_M_SWVer_B
{
    LABEL       "Minor Software Revision";
	HELP		"Profibus Module Minor Software Revision";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER(1);
    HANDLING    READ;
}

VARIABLE I_M_RevCntr
{
    LABEL       "Request Repsonse - Rev Counter";
    CLASS       DEVICE;
    TYPE        UNSIGNED_INTEGER(2);
    HANDLING    READ;
}



/*********************************************/
/* Read/Write Methods                        */
/*********************************************/


METHOD write_map_info (int iNumElements, int iParamNum)
{
	LABEL "Send Mapping Information";
	DEFINITION
	{
		int		i;
		
		/*************************************/
		/* Header Information Write          */
		/*************************************/
		
		Request_Reference				= 0x01 ;
		Request_ID						= 0x01 ;
		Request_DOID					= 0x00 ;
		Request_Num_Of_Parameters		= 0x01 ;
		
		Request_Attribute				= 0x10 ;
		Request_Num_Of_Elements			= iNumElements ;
		Request_Parameter_Number		= iParamNum ;
		Request_Subindex				= 0x00 ;
		
		do
		{
			i++;
			WriteCommand(MapInfo);
		}
		while( (std_ResponseCode != 0x00) && (i <= 10) ) ;
		
	}
}

//The pre-read action method reading I_M variables
METHOD write_pre_read_I_and_M_data
{
	LABEL "Write pre_read I and M data";
	DEFINITION
	{
		int i;
		i = 0;
		do
		{
			i++;
			WriteCommand(pre_read_write_I_and_M_data);
		}
		while( (std_ResponseCode != 0x00) && (i <= 3) ) ;
	}
}


#ifdef PROFIBUS
COMMAND MapInfo
{
	
	SLOT		0 ;
	INDEX		47 ;
	OPERATION	WRITE ;
	TRANSACTION
	{
		REQUEST
		{
			Request_Reference,
			Request_ID,
			Request_DOID,
			Request_Num_Of_Parameters,
			
			Request_Attribute,
			Request_Num_Of_Elements,
			Request_Parameter_Number,
			Request_Subindex
		}

		REPLY
		{
		}
	}
	
}

COMMAND read_parm_I_and_M_data
{

	SLOT		0 ;
	INDEX		255 ;
	OPERATION	READ ;
	TRANSACTION
	{
		REQUEST
		{
		}

		REPLY
		{
			0x08,   // Start of 4 byte header
			0x00,
			0xfd,
			0xe8,
			0x00,   // Start of 10 byte manufacturer header
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			I_M_MfrID,
			I_M_OrderID,
			I_M_SerialNumber,
			I_M_HWVer,
			0x56,   // ASCII "V" that I want to strip off of the software rev
			I_M_SWVer_A,
			I_M_SWVer_B,
			I_M_RevCntr,
			0x00,	// Start of 2 byte profile ID (PROFIdrive)
			0x00,
			0x00,	// Start of 2 byte profile specifier
			0x00,
			0x01,   // Start of 4 byte I&M version and supported elements
			0x01,
			0x00,
			0x1e
		}
	}
	
}

COMMAND pre_read_write_I_and_M_data
{

	SLOT		0 ;
	INDEX		255 ;
	OPERATION	WRITE ;
	TRANSACTION
	{
		REQUEST
		{
			0x08,   // Start of 4 byte header
			0x00,
			0xfd,
			0xe8
		}

		REPLY
		{
		}
	}
}

#endif



