
//*****************************************************************************
//* Variable Definitions
//*****************************************************************************


//*****************************************************************************
//**  response_code_msg                                       
//*****************************************************************************
VARIABLE response_code_msg
{
	LABEL "Response_Code_msg";
	CLASS LOCAL;
	HANDLING READ;
	VALIDITY  TRUE;
	TYPE UNSIGNED_INTEGER (1)
	{
		DEFAULT_VALUE 0;
	}
}

//*****************************************************************************
//** FLOAT VARIABLES
//*****************************************************************************
VARIABLE floatVal0
{
	LABEL "";
	CLASS LOCAL;
	HANDLING READ & WRITE;
	VALIDITY  TRUE; 
	TYPE FLOAT;
}


floatVal1 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal2 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal3 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal4 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal5 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal6 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal7 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal8 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal9 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal10 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal11 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal12 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal13 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal14 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }
floatVal15 LIKE VARIABLE floatVal0 { REDEFINE LABEL ""; }

//*****************************************************************************
//** BCT_request - Byte Count to test for
//*****************************************************************************
VARIABLE BCT_request
{
	LABEL "BCT_request";
	CLASS LOCAL;
	HANDLING READ & WRITE;
	TYPE UNSIGNED_INTEGER(1);
	DEFAULT_VALUE 0;
}

//*****************************************************************************
//** Device_BCT - Byte Count which is supported from device
//*****************************************************************************
VARIABLE Device_BCT
{
	LABEL "Device_BCT";
	CLASS LOCAL;
	HANDLING READ & WRITE;
	TYPE UNSIGNED_INTEGER(1);
	DEFAULT_VALUE 0;
}

//*************************************************************************
//** DWORD VARIABLES for max frame length test and echo curve transfer
//*************************************************************************
VARIABLE HelpDWORD_1
{
        LABEL "Blocktransfer DWORD(4)_1";
        CLASS LOCAL;
        HANDLING READ & WRITE;
        VALIDITY  TRUE; 
        TYPE UNSIGNED_INTEGER (4);
}


HelpDWORD_2 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_2"; }
HelpDWORD_3 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_3"; }
HelpDWORD_4 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_4"; }
HelpDWORD_5 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_5"; }
HelpDWORD_6 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_6"; }
HelpDWORD_7 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_7"; }
HelpDWORD_8 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_8"; }
HelpDWORD_9 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_9"; }
HelpDWORD_10 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_10"; }
HelpDWORD_11 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_11"; }
HelpDWORD_12 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_12"; }
HelpDWORD_13 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_13"; }
HelpDWORD_14 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_14"; }
HelpDWORD_15 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_15"; }
HelpDWORD_16 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_16"; }
HelpDWORD_17 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_17"; }
HelpDWORD_18 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_18"; }
HelpDWORD_19 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_19"; }
HelpDWORD_20 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_20"; }
HelpDWORD_21 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_21"; }
HelpDWORD_22 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_22"; }
HelpDWORD_23 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_23"; }
HelpDWORD_24 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_24"; }
HelpDWORD_25 LIKE VARIABLE HelpDWORD_1 { REDEFINE LABEL "Blocktransfer DWORD(4)_25"; }

//*****************************************************************************
//* Test Frame Length Command - COMMAND #143 Test HART Bus to detect maximal
//*											 supported byte count for a 
//* 										 transaction
//*****************************************************************************

COMMAND test_frame_length
{
	NUMBER 	143;
	OPERATION READ;
	TRANSACTION 0
	{ // 110 BCT_Request - 109 Response (106 / 105)
		REQUEST
		{
			0,0, 				// virtual_buffer_index (UINT_16) - constant
			BCT_request 		// UINT_8
		}
		REPLY
		{
            response_code,		// (1) UINT_8
			device_status,		// (2) UINT_8
			0,0,		 		// (4) virtual_buffer_index (UINT_16) - constant
			Device_BCT, 		// (5) UINT_8	
			HelpDWORD_1,		// (9) UINT_32
			HelpDWORD_2,		// (13) UINT_32
			HelpDWORD_3,		// (17) UINT_32
			HelpDWORD_4,		// (21) UINT_32
			HelpDWORD_5,		// (25) UINT_32
			HelpDWORD_6,		// (29) UINT_32
			HelpDWORD_7,		// (33) UINT_32
			HelpDWORD_8,		// (37) UINT_32
			HelpDWORD_9,		// (41) UINT_32
			HelpDWORD_10,		// (45) UINT_32
			HelpDWORD_11,		// (49) UINT_32
			HelpDWORD_12,		// (53) UINT_32
			HelpDWORD_13,		// (57) UINT_32
			HelpDWORD_14,		// (61) UINT_32
			HelpDWORD_15,		// (65) UINT_32
			HelpDWORD_16,		// (69) UINT_32
			HelpDWORD_17,		// (73) UINT_32
			HelpDWORD_18,		// (77) UINT_32
			HelpDWORD_19,		// (81) UINT_32
			HelpDWORD_20,		// (85) UINT_32
			HelpDWORD_21,		// (89) UINT_32
			HelpDWORD_22,		// (93) UINT_32
			HelpDWORD_23,		// (97) UINT_32
			HelpDWORD_24,		// (101) UINT_32
			HelpDWORD_25		// (105) UINT_32
		}
	}
	TRANSACTION 2
	{ // 53 BCT_Request - 52 Response (only for Workbench)
		REQUEST
		{
			0,0, 				// virtual_buffer_index (UINT_16) - constant
			BCT_request 		// UINT_8
		}
		REPLY
		{
			response_code,		// (1) UINT_8
			device_status,		// (2) UINT_8
			0,0, 				// (4) virtual_buffer_index (UINT_16) - constant
			Device_BCT, 		// (5) UINT_8	
			HelpDWORD_1,		// (9) UINT_32
			HelpDWORD_2,		// (13) UINT_32
			HelpDWORD_3,		// (17) UINT_32
			HelpDWORD_4,		// (21) UINT_32
			HelpDWORD_5,		// (25) UINT_32
			HelpDWORD_6,		// (29) UINT_32
			HelpDWORD_7,		// (33) UINT_32
			HelpDWORD_8,		// (37) UINT_32
			HelpDWORD_9,		// (41) UINT_32
			HelpDWORD_10,		// (45) UINT_32
			HelpDWORD_11,		// (49) UINT_32
			HelpDWORD_12		// (53) UINT_32
		}
	}
	TRANSACTION 9
	{ // BCT_Request 25 - 24 Response (HART5 Support)
		REQUEST
		{
			0,0, 				// virtual_buffer_index (UINT_16) - constant
			BCT_request 		// UINT_8
		}
		REPLY
		{
			response_code,		// (1) UINT_8
			device_status,		// (2) UINT_8
			0,0, 				// (4) virtual_buffer_index (UINT_16) - constant
			Device_BCT, 		// (5) UINT_8	
			HelpDWORD_1,		// (9) UINT_32
			HelpDWORD_2,		// (13) UINT_32
			HelpDWORD_3,		// (17) UINT_32
			HelpDWORD_4,		// (21) UINT_32
			HelpDWORD_5			// (25) UINT_32
		}
	}
	TRANSACTION 10
	{ // BCT_Request 0 (special use case to detect former use of command)
		REQUEST
		{
			0,0,0
		}
		REPLY
		{
			response_code,		// (1) UINT_8
			device_status,		// (2) UINT_8
			0,0, 				// (4) virtual_buffer_index (UINT_16) - constant
			Device_BCT	 		// (5) UINT_8	
		}
	}
	RESPONSE_CODES
	{
    0, SUCCESS, [no_command_specific_errors];
    5, MISC_ERROR, [too_few_data_bytes_recieved];
    6, MISC_ERROR, [xmtr_specific_command_error];
    8, DATA_ENTRY_WARNING, [set_to_nearest_possible_value];
	}
}


//*************************************************************************
//** abort method if retrieve of max frame length fails
//*************************************************************************
METHOD set_min_bct
{
	LABEL "Set minimum BCT message";
	DEFINITION
	{
		/*ACKNOWLEDGE("Ein Fehler ist beim Test auf die maximal unterstützte \
Framelänge aufgetreten. Der Host geht deshalb nur von \
HART 5 Support aus.\n\n Bitte setzen Sie das Flag HART 5 \
support enabled und starten sie die gewünschte Funktion \
noch einmal.");*/
		ACKNOWLEDGE(T_PrepareBusy);
	}
}
#define DETECT_MAX_FRAME_LENGTH		\
		ABORT_ON_ALL_COMM_STATUS(); \
		ABORT_ON_NO_DEVICE();	\
		IGNORE_ALL_RESPONSE_CODES(); \
		IGNORE_ALL_DEVICE_STATUS();   \
				\
		i = 0; \
		send_command_trans(143, 10); \
		nBCT = ivar_value(BCT_request);\
		nMaxBCT = ivar_value(Device_BCT);\
		if ((nMaxBCT == 0) || ((nMaxBCT == 107) && (nBCT == 0)))\
		{ \
			add_abort_method(set_min_bct); \
			\
			compareDW[0] = 0x3a7d6d3d;\
			compareDW[1] = 0x513e2d2d;\
			compareDW[2] = 0x717a2f6a;\
			compareDW[3] = 0x43755f46;\
			compareDW[4] = 0x41247658;\
			compareDW[5] = 0x6b3d3734;\
			compareDW[6] = 0x76737076;\
			compareDW[7] = 0x6137416f;\
			compareDW[8] = 0x435b515d;\
			compareDW[9] = 0x2b50534a;\
			compareDW[10] = 0x26766a33;\
			compareDW[11] = 0x626f235e;\
			compareDW[12] = 0x61aa7347;\
			compareDW[13] = 0x6e735477;\
			compareDW[14] = 0x264c4c34;\
			compareDW[15] = 0x3462787d;\
			compareDW[16] = 0x3a7d6d3d;\
			compareDW[17] = 0x513e2d2d;\
			compareDW[18] = 0x717a2f6a;\
			compareDW[19] = 0x43755f46;\
			compareDW[20] = 0x41247658;\
			compareDW[21] = 0x6b3d3734;\
			compareDW[22] = 0x76737076;\
			compareDW[23] = 0x6137416f;\
			compareDW[24] = 0x435b515d;\
			compareDW[25] = 0x2b50534a;\
			compareDW[26] = 0x26766a33;\
			\
			iassign(BCT_request, 110);  \
			send_command_trans(143, 0); \
			iassign(BCT_request, 107);  \
			send_command_trans(143, 0); \
			\
			for (i = 0; i < 25; i++) \
			{ \
				compareVal = ivar_value(test_fame_array[i]);\
				if (compareDW[i] != compareVal)\
				{ \
					break;\
				}\
            }\
			remove_all_abort_methods();\
		}

//*****************************************************************************
//* Prepare Command - COMMAND #146 Prepare of mass data transport
//*
//* Before a mass data transport can be started, the device has to initialize
//* the data. This command is set the specific values to initialize either echo
//* curve transport, trend data transport or event log transport
//*

ARRAY OF VARIABLE test_fame_array
{
        ELEMENTS
        {
		0, HelpDWORD_1;
		1, HelpDWORD_2;
		2, HelpDWORD_3;
		3, HelpDWORD_4;
		4, HelpDWORD_5;
		5, HelpDWORD_6;
		6, HelpDWORD_7;
		7, HelpDWORD_8;
		8, HelpDWORD_9;
		9, HelpDWORD_10;
		10, HelpDWORD_11;
		11, HelpDWORD_12;
		12, HelpDWORD_13;
		13, HelpDWORD_14;
		14, HelpDWORD_15;
		15, HelpDWORD_16;
		16, HelpDWORD_17;
		17, HelpDWORD_18;
		18, HelpDWORD_19;
		19, HelpDWORD_20;
		20, HelpDWORD_21;
		21, HelpDWORD_22;
		22, HelpDWORD_23;
		23, HelpDWORD_24;
		24, HelpDWORD_25;
	}
}

//*****************************************************************************
//* Variable Definitions
//*****************************************************************************
//*****************************************************************************
//** Port - Port selection
//*****************************************************************************

#define ReadTrend 132 // read trend
    
#define ReadHistoryLog 133 // event list
#define ReadLinearisation		134 // read lin
#define WriteLinearisation		135 // write lin
#define EndOfTransfer			255 // close transfer
    
VARIABLE port
{
        LABEL "port";
        CLASS LOCAL;
      HANDLING READ & WRITE;
        VALIDITY  TRUE;
        DEFAULT_VALUE 0;
        TYPE UNSIGNED_INTEGER(1);
}
			
//*****************************************************************************
//** device_channel - Channel selection                                       
//*****************************************************************************
VARIABLE device_channel
{
	LABEL "Sensor number";
	CLASS LOCAL;
	TYPE ENUMERATED (1)
	{
		{   1, "Sensor 1" },
		{   2, "Sensor 2" }
	}
	DEFAULT_VALUE 1;
	HANDLING READ & WRITE;
}

//*****************************************************************************
//** curve_id - selection of different HK curves
//*****************************************************************************

VARIABLE curve_id
{
	LABEL "Curve ID";
	CLASS LOCAL;
	TYPE ENUMERATED (1)
	{
		{   EndOfTransfer, 			 "Data transfer finished" }
	}
	DEFAULT_VALUE EndOfTransfer;
	HANDLING READ & WRITE;
}

//*****************************************************************************
//** x_min_value - starting x value
//*****************************************************************************
VARIABLE x_min_value
{
	LABEL "X minimum value";
	CLASS LOCAL;
	TYPE FLOAT;

	DEFAULT_VALUE 0.0;
	HANDLING READ & WRITE;
}

//*****************************************************************************
//** x_max_value - ending x value
//*****************************************************************************
VARIABLE x_max_value
{
	LABEL "X maximum value";
	CLASS LOCAL;
	TYPE FLOAT;
  
	DEFAULT_VALUE 0.0;
	HANDLING READ & WRITE;
}

//*****************************************************************************
//** x_dec_value - stepsize
//*****************************************************************************
VARIABLE x_dec_value
{
	LABEL "X decimation value";
	CLASS LOCAL;
	TYPE FLOAT;
	//DEFAULT_VALUE 0.0;
	HANDLING READ & WRITE;
}
//*****************************************************************************
//** compression support
//*****************************************************************************
#define NO_COMPRESSION 			0

//*************************************************************************
//** VARIABLE to get preparation state
//*************************************************************************
VARIABLE status_preparation
{
	LABEL "Preparation status";
	CLASS LOCAL;
	TYPE UNSIGNED_INTEGER (1);
	DEFAULT_VALUE 0;
	HANDLING READ;
}
			

//*****************************************************************************
//** x_eng_unit - Unit for X-Axis
//*****************************************************************************
VARIABLE x_eng_unit
{
	LABEL "X engineering unit";
	CLASS LOCAL;
	TYPE ENUMERATED (2)
	{
		{45, "UNITS_CODE(45)"}
	}
	DEFAULT_VALUE 45;
	HANDLING READ & WRITE;
}


//*****************************************************************************
//** y_eng_unit - Unit for Y-Axis
//*****************************************************************************
VARIABLE y_eng_unit
{
	LABEL "Y engineering unit";
	CLASS LOCAL;
	TYPE ENUMERATED (2)
	{
		{45, "UNITS_CODE(45)"}
	}
	DEFAULT_VALUE 45;
	HANDLING READ & WRITE;

}

    //*****************************************************************************
    //** supported data types
    //*****************************************************************************
    #define DATATYPE_FLOAT 		0
    #define DATATYPE_DOUBLE		1
    #define DATATYPE_UNSIGNED_4 2
    #define DATATYPE_UNSIGNED_2 3
    // *****************************************************************************
    // ** Prepare Data Transfer
    // *****************************************************************************
    COMMAND Prepare_Data_Transfer
    {
    NUMBER 	146;
      
	OPERATION COMMAND;

	TRANSACTION 1 // Logbook
	{
		REQUEST
		{
			port,				// UINT_8
			SPV_Version_1, 		// UINT_8 constant defined in DDL
			SPV_FilterOptions_1	// UINT_8
		}
		REPLY
		{
            response_code,		// UINT_8
            device_status,		// UINT_8
            port,				// UINT_8
			SPV_Version_1, 		// UINT_8 constant defined in DDL
                        SPV_FilterOptions_1,
			SPV_EventListStatus_1,	// UINT_8
                           SPV_NumEntriesEventList_1             // UINT_16
                  
		}
	}
  TRANSACTION 2 // Trend
	{
		REQUEST
		{
			port,				// UINT_8
			HB_Version_1, 		// UINT_8 constant defined in DDL
            		HB_ValueCount_1,     // UINT_16
			HB_FilterOption_1	// UINT_8
		}
		REPLY
		{
	            response_code,		// UINT_8
	            device_status,		// UINT_8
	            port,				// UINT_8
		    HB_Version_1, 		// UINT_8 constant defined in DDL
	            HB_ValueCount_1,    // UINT_16
	            HB_FilterOption_1,
		    HB_TrendListStatus_1 // UINT_8
                  
		}
	}


	RESPONSE_CODES
	{
    0, SUCCESS, [no_command_specific_errors];
    2, DATA_ENTRY_ERROR, [invalid_selection];
    5, MISC_ERROR, [too_few_data_bytes_recieved];
    6, MISC_ERROR, [xmtr_specific_command_error];
    7, MISC_ERROR, [in_write_protect_mode];
	}
}

//*****************************************************************************
//* GetStatus Command - COMMAND #147 Read status of mass data transport 
//*									 preparation
//*
//*****************************************************************************
//* Variable Definitions
//*****************************************************************************

//*****************************************************************************
//* transmission_length - count of data to transport in bytes
//*****************************************************************************
VARIABLE transmission_length
{
	LABEL "Transmission length";
	CLASS LOCAL;
	TYPE UNSIGNED_INTEGER (2);
	DEFAULT_VALUE 0;
	HANDLING READ & WRITE;
}

//*****************************************************************************
//* number_data_points - count of data to transport in logical units
//*****************************************************************************
VARIABLE number_data_points
{
	LABEL "Number of data points";
	CLASS LOCAL;
	TYPE UNSIGNED_INTEGER (2);
	DEFAULT_VALUE 0;
	HANDLING READ & WRITE;
}

//*****************************************************************************
//* compression_state - state of compression process in device
//*****************************************************************************
#define COMPRESSION_STATE_SUCCESS 		0
#define COMPRESSION_STATE_INEFFICIENT 	1
#define COMPRESSION_STATE_DST2SMALL 	2 
#define COMPRESSION_STATE_WRINTORD		3
#define COMPRESSION_STATE_FAIL			4
VARIABLE compression_state
{
	LABEL "Compression state";
	CLASS LOCAL;
	TYPE ENUMERATED (1)
	{
		{   COMPRESSION_STATE_SUCCESS, "success" },
		{   COMPRESSION_STATE_INEFFICIENT, "inefficient (nur geräteintern)" },
		{   COMPRESSION_STATE_DST2SMALL, "Dst2Small" },
		{   COMPRESSION_STATE_WRINTORD, "WrIntoRd" },
		{   COMPRESSION_STATE_FAIL, "Fail (schwerer interner Fehler)" }
	}
	HANDLING READ & WRITE;
}



//*****************************************************************************
//* corr_x_min - Device corrected starting point
//*****************************************************************************
VARIABLE corr_x_min
{
	LABEL "Corrected Xmin";
	HELP "Device corrected starting point in engineering unit";
	CLASS LOCAL;
	TYPE FLOAT;
	DEFAULT_VALUE 0.0;
	HANDLING READ & WRITE;
}

//*****************************************************************************
//* corr_x_max - Device corrected ending point
//*****************************************************************************
VARIABLE corr_x_max
{
	LABEL "Corrected Xmax";
	HELP "Device corrected ending point in engineering unit";
	CLASS LOCAL;
	TYPE FLOAT;
	DEFAULT_VALUE 0.0;

	HANDLING READ & WRITE;
}

    
      // *****************************************************************************
      // * Get Prepare Status
      // *****************************************************************************
      COMMAND Get_Prepare_Status
      {
      NUMBER 	 147;
    
	OPERATION COMMAND;

	TRANSACTION 1 // logbook
	{
		REQUEST
		{
			port 				// UINT_8
		}
		REPLY
		{
            response_code,		// UINT_8
            device_status,		// UINT_8
			      port, 				// UINT_8
            SPV_EventListStatus_1,		// UINT_16
            SPV_NumEntriesEventList_1 // UINT_16
    
		}
	}


	TRANSACTION 2 // trend
	{
		REQUEST
		{
			port 				// UINT_8
		}
		REPLY
		{
	            response_code,		// UINT_8
	            device_status,		// UINT_8
		    port, 				// UINT_8
	            HB_TrendListStatus_1		// UINT_8
		}
	}

	RESPONSE_CODES
	{
    0, SUCCESS, [no_command_specific_errors];
    2, DATA_ENTRY_ERROR, [invalid_selection];
    5, MISC_ERROR, [too_few_data_bytes_recieved];
    6, MISC_ERROR, [xmtr_specific_command_error];
    16, MISC_ERROR, [access_restricted];
	}
}

//*****************************************************************************
//* Block Data Transfer - COMMAND #111 Transfer Service Control
//*
//* Block data transfer as defined by HCF-SPEC_190 not supported by now from
//* HCF Tokenizer. Therefore we use manufacturer specific command 211 instead 
//* of command 111.
//*
//*****************************************************************************
//* Variable Definitions
//*****************************************************************************
//** function_code - set to function which should be performed                                 
//*****************************************************************************

#define PORT_TRANSFER 	0
#define PORT_OPEN       1
#define PORT_RESET 		  2
#define PORT_CLOSE		  3
#define PORT_CLOSED	    4

VARIABLE function_code
{
        LABEL "function_code";
        CLASS LOCAL;
         HANDLING READ & WRITE;
        VALIDITY  TRUE;
        TYPE UNSIGNED_INTEGER(1);
}

//*****************************************************************************
//** master_byte_count                                       
//*****************************************************************************
VARIABLE master_byte_count
{
	LABEL "Master Byte Count";
	CLASS LOCAL;
	HANDLING READ;
	VALIDITY  TRUE;
	DEFAULT_VALUE 0;
	TYPE UNSIGNED_INTEGER(2);
}

//*****************************************************************************
//** device_byte_count                                       
//*****************************************************************************
VARIABLE device_byte_count
{
    CLASS LOCAL;
	LABEL "Device Byte Count";
	HANDLING READ & WRITE;
	VALIDITY  TRUE;
	DEFAULT_VALUE 0;
	TYPE UNSIGNED_INTEGER (2);
}

COMMAND Transfer_Service_Control
{
        NUMBER 	211;
  OPERATION READ;
	TRANSACTION 0
	{
		REQUEST
		{
			port,				// UINT_8
			function_code,		// UINT_8
			BCT_request,		// UINT_8
			0,0 				// UINT_16 starting_master_byte_count (constant)
		}
		REPLY
		{
			response_code,		// UINT_8
			device_status,		// UINT_8
			port,				// UINT_8
			function_code,		// UINT_8
			Device_BCT,			// UINT_8
			master_byte_count,	// UINT_16 (constant 0,0 here)
			device_byte_count	// UINT_16
		}
	}
	RESPONSE_CODES
	{
    0, SUCCESS, [no_command_specific_errors];
    2, DATA_ENTRY_ERROR, [invalid_selection];
    5, MISC_ERROR, [too_few_data_bytes_recieved];
    6, MISC_ERROR, [xmtr_specific_command_error];
    8, DATA_ENTRY_WARNING , "Segment Length Changed";
    9, MODE_ERROR , "Port not Found";
    10, MODE_ERROR , "Port in Use";
    11, MODE_ERROR , "Maximum Ports In Use";
    12, MODE_ERROR , "Segment Length Too Small";
    16, MODE_ERROR , [access_restricted];
	}
}


//*****************************************************************************
//** FLOAT VARIABLES for TRANSPORT of FLOAT VALUES with BLOCK DATA TRANSFER		
//*****************************************************************************
VARIABLE HelpFloat_1
{
        LABEL "Blocktransfer Float_1";
        HELP [blank];
        CLASS LOCAL;
        HANDLING READ & WRITE;
        VALIDITY  TRUE;
        DEFAULT_VALUE 0.0;
        TYPE FLOAT;
}

HelpFloat_2 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_2"; }
HelpFloat_3 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_3"; }
HelpFloat_4 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_4"; }
HelpFloat_5 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_5"; }
HelpFloat_6 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_6"; }
HelpFloat_7 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_7"; }
HelpFloat_8 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_8"; }
HelpFloat_9 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_9"; }
HelpFloat_10 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_10"; }
HelpFloat_11 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_11"; }
HelpFloat_12 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_12"; }
HelpFloat_13 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_13"; }
HelpFloat_14 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_14"; }
HelpFloat_15 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_15"; }
HelpFloat_16 LIKE VARIABLE HelpFloat_1 { REDEFINE LABEL "Blocktransfer Float_16"; }


//*****************************************************************************
//** ARRAY for transport of float values with BLOCK DATA TRANSFER			
//*****************************************************************************
ARRAY OF VARIABLE block_data_float_array
{
	ELEMENTS
	{
		0, HelpFloat_1;
		1, HelpFloat_2;
		2, HelpFloat_3;
		3, HelpFloat_4;
		4, HelpFloat_5;
		5, HelpFloat_6;
		6, HelpFloat_7;
		7, HelpFloat_8;
		8, HelpFloat_9;
		9, HelpFloat_10;
		10, HelpFloat_11;
		11, HelpFloat_12;
		12, HelpFloat_13;
		13, HelpFloat_14;
		14, HelpFloat_15;
		15, HelpFloat_16;
/*#ifdef TREND_INCLUDED
		16, HelpFloat_17;
		17, HelpFloat_18;
		18, HelpFloat_19;
		19, HelpFloat_20;
		20, HelpFloat_21;
		21, HelpFloat_22;
		22, HelpFloat_23;
		23, HelpFloat_24;
		24, HelpFloat_25;
#endif*/
	}
}


//*****************************************************************************
//** number_bytes_message
//*****************************************************************************
VARIABLE number_bytes_message
{
        LABEL "number_bytes_message";
        CLASS LOCAL;
        HANDLING READ;
        VALIDITY  TRUE;
        DEFAULT_VALUE 0;
        TYPE UNSIGNED_INTEGER (1);
}



//*****************************************************************************
//* Block Data Transfer - COMMAND #112 Block Transfer
//*
//* Block data transfer as defined by HCF-SPEC_190 not supported by now from
//* HCF Tokenizer. Therefore we use manufacturer specific command 212 instead 
//* of command 112.
//*
//*****************************************************************************
COMMAND block_transfer
{
	NUMBER 212;
	OPERATION READ;
	TRANSACTION 0
	{
		REQUEST 
		{
		}
		REPLY
		{
			response_code,          // 1 Byte (1)
			device_status           // 1 Byte (2)
		}
	}

      TRANSACTION 51 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_1,		    // 14Byte (23)
      HelpDWORD_1		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_1,		    // 1 Byte (28)

      }
      }

      TRANSACTION 52 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_2,		    // 14Byte (23)
      HelpDWORD_2		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_2,		    // 1 Byte (28)

      }
      }

      TRANSACTION 53 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_3,		    // 14Byte (23)
      HelpDWORD_3		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_3,		    // 1 Byte (28)

      }
      }

      TRANSACTION 54 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_4,		    // 14Byte (23)
      HelpDWORD_4		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_4,		    // 1 Byte (28)

      }
      }

      TRANSACTION 55 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_5,		    // 14Byte (23)
      HelpDWORD_5		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_5,		    // 1 Byte (28)

      }
      }

      TRANSACTION 56 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_6,		    // 14Byte (23)
      HelpDWORD_6		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_6,		    // 1 Byte (28)

      }
      }

      TRANSACTION 57 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_7,		    // 14Byte (23)
      HelpDWORD_7		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_7,		    // 1 Byte (28)

      }
      }

      TRANSACTION 58 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_8,		    // 14Byte (23)
      HelpDWORD_8		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_8,		    // 1 Byte (28)

      }
      }
      TRANSACTION 59 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_9,		    // 14Byte (23)
      HelpDWORD_9		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_9,		    // 1 Byte (28)

      }
      }
      TRANSACTION 60 	// 25 Byte Historom (actually we need 26 Bytes but we need
      // to support HART5 protocol, so last byte of each historom
      // entry is not used in EDD)
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      device_byte_count,
      //cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_10,		    // 14Byte (23)
      HelpDWORD_10		    	// 4 Byte (27)
      // to ensure HART5 compatibility
      //			HelpByte_10,		    // 1 Byte (28)

      }
      }
      TRANSACTION 61 // 47 Byte Historom
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_1,		    // 14Byte (23)
      HelpDWORD_1,		    // 4 Byte (27)
      HelpByte_1, 		    // 1 Byte (28)
      Eventlog_Message_2,		    // 14Byte (42)
      HelpDWORD_2,		    // 4 Byte (46)
      HelpByte_2 		    // 1 Byte (47)

      }
      }

      TRANSACTION 62 // 47 Byte Historom
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_3,		    // 14Byte (23)
      HelpDWORD_3,		    // 4 Byte (27)
      HelpByte_3, 		    // 1 Byte (28)
      Eventlog_Message_4,		    // 14Byte (42)
      HelpDWORD_4,		    // 4 Byte (46)
      HelpByte_4 		    // 1 Byte (47)
      }
      }


      TRANSACTION 63 // 47 Byte Historom
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_5,		    // 14Byte (23)
      HelpDWORD_5,		    // 4 Byte (27)
      HelpByte_5, 		    // 1 Byte (28)
      Eventlog_Message_6,		    // 14Byte (42)
      HelpDWORD_6,		    // 4 Byte (46)
      HelpByte_6 		    // 1 Byte (47)
      }
      }

      TRANSACTION 64 // 47 Byte Historom
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_7,		    // 14Byte (23)
      HelpDWORD_7,		    // 4 Byte (27)
      HelpByte_7, 		    // 1 Byte (28)
      Eventlog_Message_8,		    // 14Byte (42)
      HelpDWORD_8,		    // 4 Byte (46)
      HelpByte_8 		    // 1 Byte (47)
      }
      }
      TRANSACTION 65 // 47 Byte Historom
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      cur_device_byte_count,  // 2 Byte (9)
      Eventlog_Message_9,		    // 14Byte (23)
      HelpDWORD_9,		    // 4 Byte (27)
      HelpByte_9, 		    // 1 Byte (28)
      Eventlog_Message_10,		    // 14Byte (42)
      HelpDWORD_10,		    // 4 Byte (46)
      HelpByte_10 		    // 1 Byte (47)
      }
      }
    
      TRANSACTION 70 // Trend
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      cur_device_byte_count,  // 2 Byte (9)
      block_data_float_array[0],
      block_data_float_array[1],
      block_data_float_array[2],
      block_data_float_array[3],
      block_data_float_array[4],
      block_data_float_array[5],
      block_data_float_array[6],
      block_data_float_array[7],
      block_data_float_array[8],
      block_data_float_array[9],
      block_data_float_array[10],
      block_data_float_array[11],
      block_data_float_array[12],
      block_data_float_array[13],
      block_data_float_array[14]
      }
      }
      TRANSACTION 71 // Trend
      {
      REQUEST
      {
      port,
      function_code,
      number_bytes_message,
      master_byte_count,
      device_byte_count
      }
      REPLY
      {
      response_code,          // 1 Byte (1)
      device_status,          // 1 Byte (2)
      port,                   // 1 Byte (3)
      function_code,          // 1 Byte (4)
      number_bytes_message,   // 1 Byte (5)
      master_byte_count,      // 2 Byte (7)
      cur_device_byte_count,  // 2 Byte (9)
      block_data_float_array[0],
      block_data_float_array[1],
      block_data_float_array[2],
      block_data_float_array[3]
      }
      }
    
      RESPONSE_CODES
      {
      0, SUCCESS, [no_command_specific_errors];
      2, DATA_ENTRY_ERROR, [invalid_selection];
      5, MISC_ERROR, [too_few_data_bytes_recieved];
      6, MISC_ERROR, [xmtr_specific_command_error];
      9, DATA_ENTRY_ERROR,    "Port not Open";
      16, MISC_ERROR, [access_restricted];
      }
      }

      //*****************************************************************************
      //** METHOD close_port - closes an opened port
      //*****************************************************************************
      METHOD close_port
      {
      LABEL "Close port";
      CLASS COMPUTATION;
      DEFINITION
      {
      iassign(function_code, PORT_CLOSE);
      send_command_trans(211,0);
    iassign(CountOfHRData, 0);
    save_values();
	}
}

//*****************************************************************************
//** METHOD finish_transfer - finish mass data transport initiated with cmd 146
//*****************************************************************************
METHOD finish_transfer_log
{
	LABEL "End of transfer";
	CLASS COMPUTATION;
	DEFINITION
	{
		int closeTransaction;
 
		switch (ivar_value(port))
		{
			case ReadTrend:
				closeTransaction = 2;
				break;
			case ReadHistoryLog:
				closeTransaction = 1;
				break;
		}
		iassign(curve_id, EndOfTransfer);
		send_command_trans(146, closeTransaction);
                                iassign(CountOfHRData, 0);
               save_values();
	}
}

//*************************************************************************
//** VARIABLE progress to show progress of method execution
//*************************************************************************
VARIABLE progress
{
        LABEL "Progress";
        CLASS LOCAL;
        HANDLING READ & WRITE;
        VALIDITY  TRUE;
        DEFAULT_VALUE 0;
        TYPE UNSIGNED_INTEGER (1);
}