V 10
1
LANG:1 8 (NoName)
PANEL,258 160 661 571 N "_3DFace" 0
EEE E E 1 -1 -1 0  40 30
"" 0 1
E"//HWcfg & more
//PLEASE NOTE: getHWcfg functions are local and different for each button!

HWcfg(dyn_int & Crates, dyn_int & Buses, dyn_int & Nodes, 
						dyn_string & Drivers, dyn_int & Ports, dyn_int & BaudRates)
{
	file hdw;
	string path, strn;
	int err, i=0;
	
	path = getPath(PANELS_REL_PATH, \"hardware.cfg\");
	hdw = fopen(path,\"r\");
	err = ferror(hdw); // export error
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to open file:\", path);
		return;
		}

	fgets(strn, 300, hdw);

	while( fgets(strn, 300, hdw ) ) {
		if (strlen (strn) < 12) break;
		sscanf( strn, \"%d %d %d %s %d %d\", 
			Crates[++i], Buses[i], Nodes[i], Drivers[i], Ports[i], BaudRates[i]);
	}

	fclose(hdw);

}

/**********************************************************************************\\
|* Apply function, called before each operation!
\\**********************************************************************************/
Apply()
{
	file cfg;
	string path, driver, to_file, from_file, content, old_content;
	int err, crate, bus, node, port, baud, i, k;
	bool ok;
	dyn_string strn;
	shape table=getShape(\"Table1\");

	setValue (table, \"sort\", \"Crate\");
	
	for (i=0; i< table.lineCount(); i++)	{
		getValue (table, \"cellValueRC\", i, \"Crate\", content);
		if (content == old_content)	{
		DebugN(\"Error: you set 2 times Crate\", content);
		return;
		}
		old_content = content;
	}

	
	path = getPath(PANELS_REL_PATH, \"hardware.cfg\");
	fileToString(path, from_file);
	
	
	//fputs(\"crate\\tbus\\tnode\\tdriver\\tport\\tbaudrate\\n\", cfg);
	
	to_file = \"crate\\tbus\\tnode\\tdriver\\tport\\tbaudrate\\n\";
	
	for (i=0; i< table.lineCount(); i++)	{
		getValue (getShape(\"Table1\"), \"getLineN\", i, strn);
			for (k=1; k <= dynlen(strn); k++)	{
					to_file = to_file + strn[k] + \"\\t\";
	//			fputs(strn[k] + \"\\t\", cfg);
			}
			to_file = to_file + \"\\n\";
	//		fputs(\"\\n\", cfg);
	}
	
	if (to_file != from_file)	{
		// copy the old file in a new *_old.gfg
		ok = copyFile(path, strrtrim(path, \"hardware.cfg\") + \"hardware_old.cfg\");
		if(!ok)	{
			DebugN(\"Error in moving old hardware.cfg. No hardware_old.cfg file created!\");
			}
		cfg = fopen(path,\"w\");
		err = ferror(cfg); // export error
		if (err)
			{ 
			DebugN(\"Error no. \",err,\" occurred: not able to open file:\", path);
			return;
			}
		fputs(to_file, cfg);
		fclose(cfg);
		DebugN(\"Changes written in hardware.cfg file\");
		}
	else
		DebugN(\"No changes need to be saved\");
}" 0
 2
"CBRef""1"
"EClose"E
""
DISPLAY_LAYER, 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
LAYER, 0 
1
LANG:1 6 Layer1
2 34
"Text4"
""
1 178 258 E E E 1 E 1 E N "_WindowText" E N "_Transparent" E E
 E E
29 0 0 0 0 0
EE E
0
1
LANG:1 0 
1
"dashclr"N "_Transparent"
E E 0 1 1 0 1 E U  0 E 180 260 212 276
0 2 0 "s" 0 0 0 192 0 0  180 260
 1
1
LANG:1 107 -microsoft windows-Arial-normal-r-normal-*-*-120-100-100-*-*-iso8859-1|-13,0,0,0,400,0,0,0,0,3,2,1,34,Arial
0 ""
1
LANG:1 5 Text4
30 38
"Frame1"
""
1 460 350 E E E 1 E 1 E N "_3DText" E N "_Transparent" E E
 E E
32 0 0 0 0 0
EE E
1
1
LANG:1 0 
0
E E 0 1 1 0 1 E U  0 E 460 24 640 351
1
LANG:1 107 -microsoft windows-Arial-bold-i-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,255,0,0,0,3,2,1,34,Arial
0 ""
1
LANG:1 14 Table Settings
25 7
"Table1"
""
1 20 20 E E E 1 E 1 E N "_WindowText" E N "_Window" E E
 "main()
{
	dyn_int n;
	int answer, i;
	shape table=getShape(\"Table1\");
	dyn_string txt;
	
	txt = makeDynString(\"PUSH_BUTTON, add line, 1, 1\",
										\"PUSH_BUTTON, delete selected lines, 2, 1\");

	popupMenu(txt, answer);

	if(answer == 1) {
		setValue (	table, \"appendLine\", 
			\"Crate\", \"\", \"Bus\", \"\", \"Node\", \"\", \"Driver\", \"\", \"Port\", \"\", \"BaudRate\", \"\");
	}
	if(answer == 2) {
		getValue (table, \"getSelectedLines\", n);
		for(i=1; i<=dynlen(n); i++) {
			setValue (table, \"deleteLineN\", n[i]);
		}
	}
}" 0
 E
8 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 118 -microsoft windows-Courier New-normal-r-normal-*-*-120-100-100-*-*-iso8859-1|-13,0,0,0,400,0,0,0,0,0,0,0,0,Courier New
0 ""
18 18 443 363
"main()
{
file cfg;
string path, strn, driver;
int err, crate, bus, node, port, baud;

shape table=getShape(\"Table1\");
setValue (table, \"tableMode\", TABLE_SELECT_MULTIPLE);
setValue (table, \"selectByClick\", TABLE_SELECT_LINE);

path = getPath(PANELS_REL_PATH, \"hardware.cfg\");
cfg = fopen(path,\"r\");
err = ferror(cfg); // export error
if (err)
	{ 
	DebugN(\"Error no. \",err,\" occurred: not able to open file:\", path);
	return;
	}

fgets(strn, 300, cfg);

while( fgets(strn, 300, cfg ) ) {
	sscanf( strn, \"%d %d %d %s %d %d\", crate, bus, node, driver, port, baud);
//	DebugN(crate, bus, node, driver, port, baud);
	setValue (getShape(\"Table1\"), \"appendLine\", 
	\"Crate\", crate, \"Bus\", bus, \"Node\", node, \"Driver\", driver, \"Port\", port, \"BaudRate\", baud);
//	DebugN(table.lineCount());
}
fclose(cfg);
}" 0
 E 1 0 1 6 1 "Crate" 0 1 -1 "s" 1
LANG:1 5 Crate
E1
LANG:1 5 Crate
 60 "Bus" 0 1 -1 "s" 1
LANG:1 3 Bus
E1
LANG:1 0 
 50 "Node" 0 1 -1 "s" 1
LANG:1 4 Node
E1
LANG:1 0 
 50 "Driver" 0 1 -1 "s" 1
LANG:1 6 Driver
E1
LANG:1 0 
 128 "Port" 0 1 -1 "s" 1
LANG:1 4 Port
E1
LANG:1 0 
 50 "BaudRate" 0 1 -1 "s" 1
LANG:1 8 BaudRate
E1
LANG:1 0 
 80 26 26 "" 1 1
LANG:1 3 Set
8 30 1
LANG:1 118 -microsoft windows-Courier New-normal-r-normal-*-*-120-100-100-*-*-iso8859-1|-13,0,0,0,400,0,0,0,0,0,0,0,0,Courier New
0 ""
0 1 0 3 0
1 0
13 10
"apply"
""
1 470 50 E E E 1 E 1 E N "_3DText" E N "_3DFace" E E
 E E
11 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 119 -microsoft windows-Courier New-normal-r-normal-*-*-110-100-100-*-*-iso8859-1|-12,0,0,0,400,0,0,0,0,3,2,1,49,Courier New
0 ""
468 48 632 102
T 
1
LANG:1 13 APPLY Changes
"main()
{
	Apply();
}" 0
 E E E
13 11
"DISCARD"
""
1 1238 150 E E E 1 E 1 E N "_3DText" E N "_3DFace" E E
 E E
12 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 119 -microsoft windows-Courier New-normal-r-normal-*-*-110-100-100-*-*-iso8859-1|-12,0,0,0,400,0,0,0,0,3,2,1,49,Courier New
0 ""
468 108 632 162
T 
1
LANG:1 15 Discard Changes
"main()
{
file cfg;
string path, strn, driver;
int err, crate, bus, node, port, baud;

shape table=getShape(\"Table1\");

setValue (table, \"deleteLinesN\", 0, table.lineCount());

path = getPath(PANELS_REL_PATH, \"hardware.cfg\");
cfg = fopen(path,\"r\");
err = ferror(cfg); // export error
if (err)
	{ 
	DebugN(\"Error no. \",err,\" occurred: not able to open file:\", path);
	return;
	}

fgets(strn, 300, cfg);

while( fgets(strn, 300, cfg ) ) {
	sscanf( strn, \"%d %d %d %s %d %d\", crate, bus, node, driver, port, baud);
//	DebugN(crate, bus, node, driver, port, baud);
	setValue (getShape(\"Table1\"), \"appendLine\", 
	\"Crate\", crate, \"Bus\", bus, \"Node\", node, \"Driver\", driver, \"Port\", port, \"BaudRate\", baud);
//	DebugN(table.lineCount());
}
fclose(cfg);

}" 0
 E E E
13 12
"add_lines"
""
1 470 230 E E E 1 E 1 E N "_3DText" E N "_3DFace" E E
 E E
12 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 119 -microsoft windows-Courier New-normal-r-normal-*-*-110-100-100-*-*-iso8859-1|-12,0,0,0,400,0,0,0,0,3,2,1,49,Courier New
0 ""
468 228 632 282
T 
1
LANG:1 11 Append Line
"main()
{
string path, strn, driver;
int err, crate, bus, node, port, baud;

shape table=getShape(\"Table1\");

setValue (	table, \"appendLine\", 
\"Crate\", \"\", \"Bus\", \"\", \"Node\", \"\", \"Driver\", \"\", \"Port\", \"\", \"BaudRate\", \"\");
//setValue (getShape(\"Table1\"), \"updateLine\", 6, \"Bus\", \"ecco\");
//setValue (getShape(\"Table1\"), \"cellValueRC\", 0, \"Bus\", \"vai\");
//setValue (getShape(\"Table1\"), \"cellValueRC\", 1, \"Bus\", \"vai\");
//setValue (getShape(\"Table1\"), \"cellValueRC\", 2, \"Bus\", \"vai\");

}" 0
 E E E
13 17
"Button9"
""
1 449 737 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
16 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 105 -microsoft windows-Arial-bold-r-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,0,0,0,0,3,2,1,34,Arial
0 ""
238 378 422 432
T 
1
LANG:1 16 Create Addresses
"//Create Address
/******************************************************************************\\
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
|*=====================================-======================================*|
|*			               SCT POWER SUPPLIES MODULES     		             				*|
|*=====================================-======================================*|
|*   AUTHORs:        G.Codispoti - codispoti@cs.infn.it			    		  				*|
|*								   Giuseppe.Codispoti@cern.ch			 											    *|
|*																		  							  							  	    *|
|*   DESIGN ISSUEs:  This Script add DPe address loading them from					  *|
|*   				 the text file \"OPCsettings.cfg\" 											 						*|
|*																				  							  									*|
|*   MODIF_HISTORY:  17.03.2003  - first release								  					  *|
|*																								  							  		      *|
\\******************************************************************************/

main()
{
	file cfg;
	dyn_string OPCItem_chan, type_chan, OPCItem_card, type_card, OPCItem_iv, type_iv,
											 OPCItem_PDOchan, type_PDOchan, OPCItem_PDOcard, type_PDOcard;
	dyn_int Crates, Bus;
	string path, strn, dpeName, Crate;
	int err, i, j, index, count, count2, LVCard, HVCard, max;
	
	setValue(\"\",\"foreCol\",\"Red\");
	Apply();
	DebugN( getCurrentTime(), \": Start setting _address configs\");  
	index = 0;
	
	// Check if the opc configuration is already stored in the PVSS datapoint
	if (!dpExists(\"_CANopen\"))
		{ 
	    dpCreate(\"_CANopen\", \"_OPCServer\"); 
	    dpSet(\"_CANopen.StateTimer:_original.._value\", 10);
	    dpSet(\"_CANopen.ReconnectTimer:_original.._value\", 10);
	    DebugN(\"OPC DP created!\");
	//    setOPCGroup(string groupName, int UpdateRate, string DataSource,
	//   								 bool enableCallBack, bool active);
	    setOPCGroup(\"Comm\", 1000, \"Device\", FALSE, TRUE ); 
			setOPCGroup(\"Moni\",  100, \"Cache\",  TRUE,  TRUE ); 
			setOPCGroup(\"sdo\",     0,	\"Device\", FALSE, FALSE);
		}

	// getting hardware configuration
	getHWcfg( Crates, Bus );
	
	/* Opening configuration database */
	path = getPath(PANELS_REL_PATH, \"OPCsettings.cfg\");
	cfg = fopen(path,\"r\");
	err = ferror(cfg); // export error
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to open file:\", path);
		return;
		}
	fgets(strn, 300, cfg);
	fgets(strn, 300, cfg); // jump first two lines
	
	// Reading File
	readSDO (cfg, OPCItem_chan, type_chan); // channnels
	readSDO (cfg, OPCItem_card, type_card); // cards
	readSDO (cfg, OPCItem_iv,   type_iv); // IV curve
	// Jump Cc SDO section
	jumpSection ( cfg );
	// Jump PDO section
	jumpSection ( cfg );
	// Get PDO Items 
	readPDO (cfg, OPCItem_PDOchan, type_PDOchan ); // channnels
	readPDO (cfg, OPCItem_PDOcard, type_PDOcard); // cards
	// Jump CC PDO section
	jumpSection ( cfg );
	// Jump INIT section
	jumpSection ( cfg );
		
	// setting some boundaries for cards
		for (index = 1; index <= dynlen(OPCItem_card); index++)	{
			if ( strpos (OPCItem_card[index], \"LV\") != -1) count++;
			}	
		for (index = 1; index <= dynlen(OPCItem_PDOcard); index++)	{
			if ( strpos (OPCItem_PDOcard[index], \"LV\") != -1) count2++;
			}
	
	// schifo: rifare! **********************************************************************************
	if (dynlen(OPCItem_PDOchan) > dynlen(OPCItem_chan) && dynlen(OPCItem_PDOchan) > dynlen(OPCItem_iv))
		max = dynlen(OPCItem_PDOchan);
	else if (dynlen(OPCItem_chan) > dynlen(OPCItem_PDOchan) && dynlen(OPCItem_chan) > dynlen(OPCItem_iv))
		max = dynlen(OPCItem_chan);
	else if  (dynlen(OPCItem_iv) > dynlen(OPCItem_PDOchan) && dynlen(OPCItem_iv) > dynlen(OPCItem_chan))
		max = dynlen(OPCItem_iv);
	//
	
	for(j=1;j<=dynlen(Crates);j++)	{
		sprintf(Crate, \"Crate%02d\", Crates[j]);
		if ( dynlen( dpNames (Crate, \"MPS_Crates\" ) )	== 0 )	{
				DebugN(Crate, \"do not exists!\");
		}
		else	{
			DebugN(\"Setting\", Crate);
			// Set Channel Items
			DebugN(\"Setting Channel Addresses...\");
			for (i=0; i<48; i++)	{
				for (index = 1; index <= max; index++)	{
					// Set PDO Channel Items
					if (index <= dynlen(OPCItem_PDOchan))	{
						PDOAddress(OPCItem_PDOchan[index], type_PDOchan[index], Bus[j], Crates[j], i, \"Channel\");
					}
					// Set SDO Channel Items
					if (index <= dynlen(OPCItem_chan))		{
						SDOAddress(OPCItem_chan[index], type_chan[index], Bus[j], Crates[j], i, \"Channel\");
					}
					// Set SDO for IVCurve 
					if (index <= dynlen(OPCItem_iv))	{
						SDOIVAddress(OPCItem_iv[index], type_iv[index], Bus[j], Crates[j], i, \"Channel\");
					}
				}
			}
	// END \"schifo: rifare!\" ******************************************************************************
			// Set Card Items
			DebugN(\"Setting Card Addresses...\");
			LVCard = HVCard = 0;
			index = 0;
			for (i=0; i<6; i++)
				{
				for (index = 1; index <= count; index++)	{
					SDOAddress(OPCItem_card[index], type_card[index], Bus[j], Crates[j], LVCard, \"LV_card\");
				}
				for (index = 1; index <= count2; index++)	{
					PDOAddress(OPCItem_PDOcard[index], type_PDOcard[index], Bus[j], Crates[j], LVCard, \"LV_card\");
				}
				LVCard++;
				for (index = count+1; index <= dynlen(OPCItem_card); index++)	{
					SDOAddress(OPCItem_card[index], type_card[index], Bus[j], Crates[j], i, \"HV_card\");
				}
				for (index = count2+1; index <= dynlen(OPCItem_PDOcard); index++)	{
					PDOAddress(OPCItem_PDOcard[index], type_PDOcard[index], Bus[j], Crates[j], HVCard, \"HV_card\");
				}
				HVCard++;
				for (index = 1; index <= count; index++)	{
					SDOAddress(OPCItem_card[index], type_card[index], Bus[j], Crates[j], LVCard, \"LV_card\");
				}
				for (index = 1; index <= count2; index++)	{
					PDOAddress(OPCItem_PDOcard[index], type_PDOcard[index], Bus[j], Crates[j], LVCard, \"LV_card\");
				}
				LVCard++;
				}
			// Set Cc Items
			DebugN(\"Setting Crate Controller Addresses\");
			setCCaddress( Crates[j], Bus[j]);
		}
	}
	
	// Set Buses addresses
	DebugN(\"Setting Buses Addresses\");
	for(j=1;j<=dynlen(Bus);j++)	{
		setBusAddress( Bus[j]);
	}
	
	fclose(cfg);
	
	//Set more Items
	OPCAddress( getSystemName() + \"TEalert.state\", \"Moni\", \"TE_State\", \"VT_FLOAT8\" );
	OPCAddress( getSystemName() + \"TEalert.alarm\", \"Moni\", \"TE_Alarm\", \"VT_FLOAT8\" );
	OPCAddress( getSystemName() + \"Fatl.Indx\", \"Comm\", \"Fatl_Indx\", \"VT_FLOAT8\" );
	OPCAddress( getSystemName() + \"Fatl.Numb\", \"Moni\", \"Fatl_Numb\", \"VT_FLOAT8\" );
	

	DebugN( getCurrentTime(), \": setting _address configs end\"); 
	setValue(\"\",\"foreCol\",\"\");
}


/****************************************************************************\\
|* Settings for Crate Controller addresses																	*|
\\****************************************************************************/
setCCaddress( int Crate, int Bus)
{
	int i;
	dyn_string OPCItem, suffix, dir, dpeType;
	string OPCItemName, dpeName, mode;

//Crate00_IVchStat_RecvChan   = Crate00 301 VT_UI1 ALL 6

	OPCItem = suffix = dir = dpeType = makeDynString(\"\");
	i=0;
	OPCItem[++i] = \"Ackn\"									;	suffix[i] = \"CCcf_Ack\"	;	dir[i] = 	\"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"AcknOK\"								;	suffix[i] = \"CCcf_AckE\"	;	dir[i] = 	\"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"EMER_pdo\"							;	suffix[i] = \"CCchStat\"	;	dir[i] = 	\"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"_Set\"									;	suffix[i] = \"CCch_Set\"	;	dir[i] = 	\"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"Read\"									;	suffix[i] = \"CCchRead\"	;	dir[i] = 	\"Comm\"; dpeType[i] = \"default\";
	
	for(i=1;i<=dynlen(OPCItem);i++)	{
		sprintf (OPCItemName, \"Bus%02d.Crate%02d.Crate%02d_\" + OPCItem[i], Bus, Crate, Crate );
		switch(dir[i]){
			case \"Comm\" :	mode=\".Send\"; 	break;
			case \"Moni\" :	mode=\".Recv\"; 	break;
			case \"sdo\"	:	mode=\".Recv\"; 	break;
			}
		sprintf( dpeName, getSystemName() + \"Crate%02d.Controller.\" + suffix[i] + mode, Crate );
		OPCAddress( dpeName, dir[i], OPCItemName, dpeType[i] );
	}

	OPCItem = suffix = dir = dpeType = makeDynString(\"\");
	i=0;
	OPCItem[++i] = \"SendingDBInProgress\";	suffix[i] = \"CCcfsetDB\"	;	dir[i] = 	\"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"SendingDBInProgress\";	suffix[i] = \"CCcfsetDB\"	;	dir[i] = 	\"sdo\";  dpeType[i] = \"VT_FLOAT8\";
	
	for(i=1;i<=dynlen(OPCItem);i++)	{
		sprintf (OPCItemName, \"Bus%02d.Crate%02d.\" + OPCItem[i] + \"%02d\", Bus, Crate, Crate);
		switch(dir[i]){
			case \"Comm\" :	mode=\".Send\"; 	break;
			case \"Moni\" :	mode=\".Recv\"; 	break;
			case \"sdo\"	:	mode=\".Recv\"; 	break;
			}
		sprintf( dpeName, getSystemName() + \"Crate%02d.Controller.\" + suffix[i] + mode, Crate );
		OPCAddress( dpeName, dir[i], OPCItemName, dpeType[i] );
	}

	
	OPCItem = suffix = dir = dpeType = makeDynString(\"\");
	i=0;
	OPCItem[++i] = \"lifeguard\"									;	suffix[i] = \"CClifeguard\"	;	dir[i] = \"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"bootUpMessage\"							;	suffix[i] = \"CCieBoot\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"emergencyErrorCode\"				;	suffix[i] = \"CCieEcod\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"Error\"											;	suffix[i] = \"CC_error\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"specificErrorCodeByte1\"	;	suffix[i] = \"CCieEcod1\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"specificErrorCodeByte2\"	;	suffix[i] = \"CCieEcod2\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"specificErrorCodeByte3\"	;	suffix[i] = \"CCieEcod3\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"specificErrorCodeByte4\"	;	suffix[i] = \"CCieEcod4\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"specificErrorCodeByte5\"	;	suffix[i] = \"CCieEcod5\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"State\"											;	suffix[i] = \"CCieStat\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"run_state\"									;	suffix[i] = \"CCcf_RST\"		;	dir[i] = \"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"run_state\"									;	suffix[i] = \"CCcf_RST\"		;	dir[i] = \"sdo\";  dpeType[i] = \"VT_FLOAT8\";
	OPCItem[++i] = \"save\"												;	suffix[i] = \"CCcf_Set\"		;	dir[i] = \"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"save\"												;	suffix[i] = \"CCcf_Set\"		;	dir[i] = \"sdo\";  dpeType[i] = \"VT_FLOAT8\";
	OPCItem[++i] = \"StartIV_Send\"								;	suffix[i] = \"StartIVCurve\";	dir[i] = \"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"StartIV_Recv\"								;	suffix[i] = \"StartIVCurve\";	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"NMT\"												;	suffix[i] = \"CCie_NMT\"		;	dir[i] = \"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"NMT\"												;	suffix[i] = \"CCie_NMT\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	
	for(i=1;i<=dynlen(OPCItem);i++)	{
		sprintf (OPCItemName, \"Bus%02d.Crate%02d.\" + OPCItem[i], Bus, Crate);
		switch(dir[i]){
			case \"Comm\" :	mode=\".Send\"; 	break;
			case \"Moni\" :	mode=\".Recv\"; 	break;
			case \"sdo\"	:	mode=\".Recv\"; 	break;
			}
		sprintf( dpeName, getSystemName() + \"Crate%02d.Controller.\" + suffix[i] + mode, Crate );
		OPCAddress( dpeName, dir[i], OPCItemName, dpeType[i] );
	}
}
/****************************************************************************\\
|* Settings for Buses addresses																							*|
\\****************************************************************************/
setBusAddress( int Bus)
{
	int i;
	dyn_string OPCItem, suffix, dir, dpeType;
	string OPCItemName, dpeName, mode;

	i=0;
	OPCItem[++i] = \"portError\"						;	suffix[i] = \"BusPortError\"		;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"portErrorDescription\"	;	suffix[i] = \"BusPortErrorCode\";	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"NMT\"									;	suffix[i] = \"Bus_NMT\"					;	dir[i] = \"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"NMT\"									;	suffix[i] = \"Bus_NMT\"					;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"NodeGuardInterval\"		;	suffix[i] = \"Bus_NGT\"					;	dir[i] = \"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"NodeGuardInterval\"		;	suffix[i] = \"Bus_NGT\"					;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"SYNC\"									;	suffix[i] = \"BusSync\"					;	dir[i] = \"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"SYNC\"									;	suffix[i] = \"BusSync\"					;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"SyncInterval\"					;	suffix[i] = \"BusSyncInt\"			;	dir[i] = \"Comm\"; dpeType[i] = \"default\";
	OPCItem[++i] = \"SyncInterval\"					;	suffix[i] = \"BusSyncInt\"			;	dir[i] = \"Moni\"; dpeType[i] = \"default\";
	
	for(i=1;i<=dynlen(OPCItem);i++)	{
		sprintf (OPCItemName, \"Bus%02d.\" + OPCItem[i], Bus);
		switch(dir[i]){
			case \"Comm\" :	mode=\".Send\"; 	break;
			case \"Moni\" :	mode=\".Recv\"; 	break;
			case \"sdo\"	:	mode=\".Recv\"; 	break;
			}
		sprintf( dpeName, getSystemName() + \"Bus%02d.\" + suffix[i] + mode, Bus);
		OPCAddress( dpeName, dir[i], OPCItemName, dpeType[i] );
	}
}

/****************************************************************************\\
|* This function sets the address values for the SDO OPCItems.				*|
\\****************************************************************************/
void SDOAddress(string OPCItem, string dpeType, int Bus, int Crate, 
																										int num, string type)
{
bool ok;
string prefix, OPCItemName, dpeName;

ok = TRUE;
prefix = OPCItem;
switch(type)
	{
	case \"Channel\" : strreplace(prefix, \"_SendChan\", \".Send\"); break;
	case \"LV_card\" : strreplace(prefix, \"_SendCard\", \".Send\"); break;
	case \"HV_card\" : strreplace(prefix, \"_SendCard\", \".Send\"); strreplace(prefix, \"_SendCard\", \".Send\"); break;
	default : ok = FALSE; break;
	}
if (!ok)
	{
	DebugN(\"Error! Invalid unit:\", prefix);
	return;
	}
	
if( strpos (prefix, \"IV\") != -1) prefix = \"IVCurve.\" + prefix;

sprintf (OPCItemName, \"Bus%02d.Crate%02d.Crate%02d_\" + OPCItem + \"%d\", Bus, Crate, Crate, num);
sprintf( dpeName, getSystemName() + \"Crate%02d.\" + type + \"%02d.\" + prefix, Crate, num );

// Set Address
	OPCAddress( dpeName, \"Comm\", OPCItemName, dpeType );
}

/****************************************************************************\\
|* This function sets the address values only for the IVCurve SDO.			*|
\\****************************************************************************/
void SDOIVAddress(string OPCItem, string dpeType, int Bus, int Crate,	
																											int num, string type)
{
string prefix, OPCItemName, dpeName;
bool ok = TRUE;

switch( OPCItem )
	{
	case \"IVchStrt_SendChan\" : prefix = \"IVCurve.HV_Start.Send\"; break;
	case \"IVchStop_SendChan\" : prefix = \"IVCurve.HV_Stop.Send\"; break;
	case \"IVchStep_SendChan\" : prefix = \"IVCurve.HV_Step.Send\"; break;
	case \"IVchVend_SendChan\" : prefix = \"IVCurve.HV_End.Send\"; break;
	case \"IVchImax_SendChan\" : prefix = \"IVCurve.Curr_Max.Send\"; break;
	case \"IVchIwar_SendChan\" : prefix = \"IVCurve.Curr_Warn.Send\"; break;
	case \"IVchWait_SendChan\" : prefix = \"IVCurve.TimeWait.Send\"; break;
	default : ok = FALSE; break;
	}
if (!ok)
	{
	DebugN(\"Error! There are not IV Datapoint ending with\", prefix);
	return;
	}

sprintf (OPCItemName, \"Bus%02d.Crate%02d.Crate%02d_\" + OPCItem + \"%d\", Bus, Crate, Crate, num);
sprintf( dpeName, getSystemName() + \"Crate%02d.\" + type + \"%02d.\" + prefix, Crate, num );	

// Set Address
OPCAddress( dpeName, \"Comm\", OPCItemName, dpeType );
}

/****************************************************************************\\
|* This function sets the address for PDO's									*|
\\****************************************************************************/
void PDOAddress(string OPCItem, string dpeType, int Bus, int Crate,
																										 int num, string type)
{
bool ok;
string prefix, OPCItemName, dpeName;

ok = TRUE;
prefix = OPCItem;

switch(type)
	{
	case \"Channel\" : strreplace(prefix, \"_RecvChan\", \".Recv\"); break;
	case \"LV_card\" : strreplace(prefix, \"_RecvCard\", \".Recv\"); break;
	case \"HV_card\" : strreplace(prefix, \"_RecvCard\", \".Recv\"); break;
	
	case \"Controller\" : break;
	default : ok = FALSE; break;
	}
if (!ok)
	{
	DebugN(\"Error! Invalid unit:\", prefix);
	return;
	}

if (strpos(OPCItem, \"MOch_Tm\") != -1)	{
	sprintf (OPCItemName, \"Bus%02d_Crate%02d_\" + OPCItem + \"%d\", Bus, Crate, num);
	dpeType = \"default\";
	}
else if (strpos(OPCItem, \"HVchCurr\") != -1)	{
	sprintf (OPCItemName, \"Bus%02d_Crate%02d_\" + OPCItem + \"%d\", Bus, Crate, num);
	dpeType = \"default\";
}
else 
	sprintf (OPCItemName, \"Bus%02d.Crate%02d.Crate%02d_\" + OPCItem + \"%d\", Bus, Crate, Crate, num);
sprintf( dpeName, getSystemName() + \"Crate%02d.\" + type + \"%02d.\" + prefix, Crate, num );
if ( strpos(OPCItem , \"IVchStat\") != -1) {
	sprintf( dpeName, getSystemName() + \"Crate%02d.\" + type + \"%02d.\" + \"IVCurve.Status.Recv\", Crate, num );
}

// Set Address
OPCAddress( dpeName, \"Moni\", OPCItemName, dpeType );
}

/****************************************************************************\\
|* This function sets the address for datapoint dpeName											*|
\\****************************************************************************/
OPCAddress(string dpeName, string OPCGroup, string OPCItemName, string dpeType )
{
string reference;
int direction, intDpeType, rc, type;
dyn_errClass err;
bool ok;

ok = TRUE;
// check if the datapoint exists
if (!dpExists (dpeName))
	{
	DebugN(\"Error! Datapoint\", dpeName, \"doesn't exist!\");
	return;
	}

// check if the address already exists
rc = dpGet (dpeName+\":_address.._type\", type);
err = getLastError ();	
if (rc | dynlen(err))
	{
	DebugN(\"Failed to check whether address already exists for dpe: \"+dpeName+\" : \"+rc);
	return;
	}
// if it exists, deactivate the alarm handling (so we can change it)
if (type != DPCONFIG_NONE)
	{
	dpSetWait(dpeName + \":_address.._type\", DPCONFIG_NONE);  // delete previous address
//	dpSet(dpeName + \":_address.._type\", DPCONFIG_NONE);  // delete previous address
	}

//Values for intDpeType:
switch(dpeType)
	{
	case \"VT_UI1\":    intDpeType = 0;    break;  //uint8	???
	case \"VT_UI2\":    intDpeType = 488;  break;  //uint16
	case \"VT_UI4\":    intDpeType = 489;  break;  //uint32
	case \"VT_I1\":     intDpeType = 0;    break;  //int8	???
	case \"VT_I2\":     intDpeType = 481;	 break;  //int16
	case \"VT_I4\":     intDpeType = 482;	 break;  //int32
	case \"VT_FLOAT4\": intDpeType = 484;  break;  //float32
	case \"VT_FLOAT8\": intDpeType = 485;  break;  //float64
	case \"VT_BOOL\":   intDpeType = 486;  break;  //Bool
	case \"VT_STRING\": intDpeType = 487;  break;  //string
	case \"default\":   intDpeType = 0;    break;  //default
	case \"undef\":	   	intDpeType = 480;  break;  //undefined
	case \"uchar\":	  	intDpeType = 483;  break;  //uchar
	case \"time\": 		  intDpeType = 490;  break;  //time
	case \"bitstring\": intDpeType = 491;  break;  //bitstring
	default: ok = FALSE;
	}

if (!ok)
	{
	DebugN(\"Error! The element type \", dpeType, \"is invalid for OPC server\");
	return;
	}

	if(OPCGroup == \"Comm\") 
		direction = DPATTR_ADDR_MODE_OUTPUT_SINGLE;	//output for single queries
	else if(OPCGroup == \"Moni\") 
		direction = DPATTR_ADDR_MODE_INPUT_SPONT;	//input for spontaneous data
	else if(OPCGroup == \"sdo\") 
		direction = DPATTR_ADDR_MODE_INPUT_SPONT;	//input for spontaneous data
	else
		{
		DebugN(\"Error! Invalid group:\", OPCGroup);
		return;
		}

	reference = \"CANopen$\" + OPCGroup + \"$\" + OPCItemName; 
/*   dpSet(dpeName + \":_distrib.._type\", DPCONFIG_DISTRIBUTION_INFO, 
             dpeName + \":_distrib.._driver\", 1);               //driver number 1

   dpSet(dpeName + \":_address.._type\", DPCONFIG_PERIPH_ADDR_MAIN, // general peripheral address
             dpeName + \":_address.._reference\", reference,
             dpeName + \":_address.._datatype\" , intDpeType,
             dpeName + \":_address.._mode\"     , direction,
             dpeName + \":_address.._drv_ident\", \"OPCCLIENT\");*/
   dpSetWait(dpeName + \":_distrib.._type\", DPCONFIG_DISTRIBUTION_INFO, 
             dpeName + \":_distrib.._driver\", 1);               //driver number 1

   dpSetWait(dpeName + \":_address.._type\", DPCONFIG_PERIPH_ADDR_MAIN, // general peripheral address
             dpeName + \":_address.._reference\", reference,
             dpeName + \":_address.._datatype\" , intDpeType,
             dpeName + \":_address.._mode\"     , direction,
             dpeName + \":_address.._drv_ident\", \"OPCCLIENT\");
/*DebugN(dpeName);
DebugN(reference);
DebugN(\"---------\");*/
} 
/*******************************************************************************/
setOPCGroup(string groupName, int UpdateRate, string DataSource,
                            	bool enableCallBack, bool active)
{
bool DataSourceDevice;
	
     //rename groupName just the internal name form
   groupName = \"_\" + groupName;
     //create groupName datapoint if it does not already exist 
  if (!dpExists(groupName))
	{ //create groupName datapoint of type _OPCGroup (PVSS internal type)
	dpCreate(groupName, \"_OPCGroup\"); 
	}
     //set some of the groupName datapoint elements
  dpSet(groupName+\".UpdateRateReq:_original.._value\", UpdateRate);
  if(DataSource == \"Device\")
	{
	DataSourceDevice = TRUE;
	}
  else
	{
	DataSourceDevice =FALSE;
	}

  dpSet(groupName+\".DataSourceDevice:_original.._value\", DataSourceDevice);
  dpSet(groupName+\".Active:_original.._value\", active);
  dpSet(groupName+\".EnableCallback:_original.._value\", enableCallBack);
}
/*******************************************************************************/
/*******************************************************************************/
readSDO (file cfg, dyn_string & OPCItem, dyn_string & type)
{
string strn;
int index=0;
dyn_int num;
dyn_string byte;
num = makeDynInt(0);
byte = OPCItem = type = makeDynString(\"\");

while(1)
	{
	fgets( strn, 300, cfg);
	if( strpos (strn, \"[\") != -1) break;
	sscanf( strn, \"%s %x %s %s\", OPCItem[++index], num[index], byte[index], type[index] );
	}
}
/*******************************************************************************/
jumpSection ( file cfg )
{
	string strn;
	
	while(1)	{
		fgets( strn, 300, cfg);
		if( strpos (strn, \"[\") != -1) break;
		strreplace( strn, \"\\t\", \" \");
	}
}
/*******************************************************************************/
readPDO(file cfg, dyn_string & OPCItem, dyn_string & type)
{
	string strn;
	int index = 0;
	dyn_int num;
	dyn_string byte, card;
	num = makeDynInt(0);
	byte = OPCItem = type = card = makeDynString(\"\");
	
	while(1)	{
		fgets( strn, 300, cfg);
		if( strpos (strn, \"[\") != -1) break;
		sscanf( strn, \"%s %d %s %s %s\", OPCItem[++index], num[index], type[index], byte[index], card[index] );
	}
}
/*******************************************************************************/
getHWcfg(dyn_int & Crates, dyn_int & Bus )
{
	dyn_string Drivers;
	dyn_int Nodes, Ports, BaudRates;
	// getting hardware configuration: function in \"General\"!
	HWcfg( Crates, Bus, Nodes, Drivers, Ports, BaudRates);
}" 0
 E E E
13 18
"Button5"
""
1 187 505 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
17 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 105 -microsoft windows-Arial-bold-r-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,0,0,0,0,3,2,1,34,Arial
0 ""
28 378 212 432
T 
1
LANG:1 20 Create OPC Conf file
"//Create OPC File
/******************************************************************************\\
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
|*=====================================-======================================*|
|*			               SCT POWER SUPPLIES MODULES     		            			  *|
|*=====================================-======================================*|
|*   AUTHORs:        G.Codispoti - codispoti@cs.infn.it			    						  *|
|*								   Giuseppe.Codispoti@cern.ch			    										  *|
|*																		  																	    *|
|*   DESIGN ISSUEs:  This Script create the file \"OPCCanServer.cfg\".		  		*|
|*																		      																	*|
|*   MODIF_HISTORY:  17.03.2003  - first release												      *|
|*																		  																	    *|
\\******************************************************************************/
main()
{
file cfg, opc;
dyn_string Drivers, OPCItem_chan, byte_chan, type_chan, OPCItem_card, byte_card, type_card,
			OPCItem_iv, byte_iv, type_iv, OPCItem_cc, byte_cc, type_cc, card, dummy;
dyn_int Crates, Buses, Nodes, Ports, BaudRates, Bus, num_chan, num_card, num_iv, num_cc;
string path, strn, strn0, strn1;
int err, i, j, index;

	setValue(\"\",\"foreCol\",\"Red\");
	Apply();
	DebugN( getCurrentTime(), \": Start creating OPCCanServer.cfg file\");


	/* Opening configuration database */
	path = getPath(PANELS_REL_PATH, \"OPCsettings.cfg\");
	cfg = fopen(path,\"r\");
	err = ferror(cfg); // export error
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to open file:\", path);
		return;
		}
	fgets(strn, 300, cfg);
	fgets(strn, 300, cfg); // jump first two lines

	// open new file for OPC configuration 
	openSafeOPC(opc, strrtrim(path, \"OPCsettings.cfg\"));

	// getting hardware configuration: function in \"General\"!
	HWcfg( Crates, Buses, Nodes, Drivers, Ports, BaudRates);
	DebugN(Crates, Buses, Nodes, Drivers, Ports, BaudRates);

	// writing hardware configuration
	j=0;
	index=9999;
	for(i=1; i<=dynlen(Crates); i++) {	
		if (Buses[i] != index) {
			sprintf(strn0, strn0+\"Bus%02d = %s %d %d\\n\",
													 Buses[i], Drivers[i], Ports[i], BaudRates[i]);
			index=Buses[i];
		}
		sprintf(strn1, strn1+\"Crate%02d = Bus%02d %d NG\\n\", Crates[i], Buses[i], Crates[i]+1 );
	}
	fputs(\"[CANBUS]\\n\"+strn0+\"\\n\\n[DEVICE]\\n\"+strn1, opc);

	// Setting SDO Entries
	fputs(\"\\n\\n[SDOItem]\\n\", opc);
	
	DebugN(\"Setting SDO's...\");
	readSDO (cfg, OPCItem_chan, num_chan, byte_chan, type_chan); // channnels
	readSDO (cfg, OPCItem_card, num_card, byte_card, type_card); // cards
	readSDO (cfg, OPCItem_iv,   num_iv,   byte_iv,   type_iv); // IV curve
	readSDO (cfg, OPCItem_cc,  num_cc,  byte_cc,  type_cc); // others
	
	for(i=1;i<=dynlen(Crates);i++)	{
		writeSDOchan (opc, OPCItem_chan, num_chan, byte_chan, type_chan, Crates[i]);
		writeSDOcard (opc, OPCItem_card, num_card, byte_card, type_card, Crates[i]);
		writeSDOiv   (opc, OPCItem_iv,   num_iv,   byte_iv,   type_iv,   Crates[i]);
		fputs(\"\\n\", opc);
		writeSDOcc  (opc, OPCItem_cc,  num_cc,  byte_cc,  type_cc,  Crates[i]);
		fputs(\"\\n\", opc);
	}
	
	// Setting PDO Entries
	fputs(\"\\n\\n[PDO]\\n\", opc);
	setPDO (cfg, opc, Buses, Nodes);
	
	OPCItem_chan = byte_chan = type_chan = OPCItem_card = byte_card = type_card = makeDynString(\"\");
	OPCItem_iv = byte_iv = type_iv = OPCItem_cc = byte_cc = type_cc = card = makeDynString(\"\");
	num_chan = num_card = num_iv = num_cc = makeDynInt(0);
	
	// Get PDO Channel Items 
	fputs(\"\\n\\n[PDOItem]\\n\", opc);
	DebugN(\"Setting PDO's...\");
	readPDO (cfg, OPCItem_chan, num_chan, byte_chan, type_chan, card ); // channnels
	readPDO (cfg, OPCItem_card, num_card, byte_card, type_card, dummy ); // cards
	readPDO (cfg, OPCItem_cc,  num_cc,  byte_cc,  type_cc , dummy ); // crate controller
	for(i=1;i<=dynlen(Crates);i++)	{
		writePDOchan(opc, OPCItem_chan, num_chan, byte_chan, type_chan, card, Crates[i], Nodes[i] );
		writePDOcard(opc, OPCItem_card, num_card, byte_card, type_card, Crates[i], Nodes[i] );
		fputs(\"\\n\", opc);
		writePDOcc  (opc, OPCItem_cc,  num_cc,  byte_cc,  type_cc,  Crates[i], Nodes[i]);
		fputs(\"\\n\", opc);
	}
	
	
	// Get INIT Items
	DebugN(\"Setting INIT Items...\");
	fputs(\"\\n\\n[INIT]\\n\", opc);
	OPCItem_cc = num_cc = makeDynString(\"\");
	index = 0;
	while(1)	{
		fgets( strn, 300, cfg);
		if( strpos (strn, \"[\") != -1) break;
		sscanf( strn, \"%s %d\", OPCItem_cc[++index], num_cc[index]);
	}
	
	// Put INIT Items
	Bus = Buses; 	// obtaining a new Bus vector
	dynUnique(Bus);
	for (i = 1; i <= dynlen(Bus); i++)	{
		for (index = 1; index <= dynlen(OPCItem_cc); index++)	{
			fprintf( opc, \"Bus%02d.\" + OPCItem_cc[index] + \"\\t\\t  = %d \\n\", Bus[i], num_cc[index] );
		}
	}
	
	//Get more Items
	fputs(\"\\n\\n[ITEM]\\n\", opc);
	DebugN(\"Loading Additional Items...\"); 
	while(1)	{
		fgets( strn, 300, cfg);
		DebugN(strn);
		if( strpos (strn, \"[END]\") != -1) break;
			if(( strpos (strn, \"MOch_Tm\") != -1 )|| (strpos (strn, \"HVchCurr\") != -1 )){
				for(j=1;j<=dynlen(Crates);j++)	{
					sprintf(strn0, \"Crate%02d\", Crates[j]);
					for (i=0; i<48; i++){
						sprintf(strn1, \"Crate%02d\", i);
						strreplace (strn, strn1, strn0);
					}
					sprintf(strn0, \"Bus%02d\", Buses[j]);
					for (i=0; i<48; i++)	{
						sprintf(strn1, \"Bus%02d\", Buses[j]);
						strreplace (strn, strn1, strn0);
					}
					fputs(strn, opc);
				}
			}
			else	fputs(strn, opc);
	}
	
	fclose(cfg);
	fclose(opc);
	
	DebugN(getCurrentTime(), \": OPCCanServer.cfg file created\");
	setValue(\"\",\"foreCol\",\"\");
}

/**********************************END******************************************/
/*******************************************************************************/
/*******************************************************************************/

readSDO (file cfg, dyn_string & OPCItem, dyn_int & num, dyn_string & byte, dyn_string & type)
{
string strn;
int index=0;
num = makeDynInt(0);
byte = OPCItem = type = makeDynString(\"\");

while(1)
	{
	fgets( strn, 300, cfg);
	if( strpos (strn, \"[\") != -1) break;
	sscanf( strn, \"%s %x %s %s\", OPCItem[++index], num[index], byte[index], type[index] );
	}
}

/*******************************************************************************/
writeSDOchan(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, dyn_string type, int Crate)
{
int i, index;
// Write SDO Channel Items
for (i=0; i<48; i++)
	{
	for (index = 1; index <= dynlen(num); index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
										Crate, i, Crate, num[index] + i );
		}
	}
}

/*******************************************************************************/
writeSDOcard(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, dyn_string type, int Crate)
{
int i, LVCard, HVCard, count, index;
LVCard = HVCard = count = 0;

for (index = 1; index <= dynlen(num); index++)
	{
	if ( strpos (OPCItem[index], \"LV\") != -1) count++;
	}

for (i=0; i<6; i++)
	{
	for (index = 1; index <= count; index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
										Crate, LVCard, Crate, num[index] + LVCard );
		}
	LVCard++;
	for (index = count+1; index <= dynlen(num); index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   = Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
									Crate, HVCard, Crate, num[index] + HVCard );
			}
	HVCard++;
	for (index = 1; index <= count; index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   = Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
									Crate, LVCard, Crate, num[index] + LVCard );
		}
	LVCard++;
	}
}

/*******************************************************************************/
writeSDOiv(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, dyn_string type, int Crate)
{
int i, index;

for (i=0; i<48; i++)
	{
	for (index = 1; index <= dynlen(num); index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
											Crate, i, Crate, num[index] + i );
		}
	}
}	

/*******************************************************************************/
writeSDOcc(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, dyn_string type, int Crate)
{
int index;

	for (index = 1; index <= dynlen(num); index++)	{
		if( strpos (OPCItem[index], \"SendingDBInProgress\") != -1) {
			fprintf( opc,
				OPCItem[index] + \"%02d\\t   =  Crate%02d %x \"
			 	+ byte[index] + \" IO \" + type[index] + \"\\n\",
										Crate, Crate, num[index]);
		}
		else {
			fprintf( opc, 
				OPCItem[index] + \"\\t\\t\\t   = Crate%02d %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
										Crate, num[index] );
		}
	}
}

/*******************************************************************************/
setPDO ( file cfg, file opc, dyn_int Buses, dyn_int Nodes)
{
	string strn;
	dyn_int PDO_ID;
	dyn_string PDOnum, PDOdir, PDOnum2, PDOnum3;
	int index=0, i;
	PDO_ID = makeDynInt(\"\");
	PDOnum = PDOdir = PDOnum2 = PDOnum3 = makeDynString(\"\");
//	DebugN(\"Setting PDO's values...\");
	
	while(1)	{
		fgets( strn, 300, cfg);
		if( strpos (strn, \"[\") != -1) break;
		strreplace( strn, \"\\t\", \" \");
		sscanf( strn, \"%d %s %s %s %s\", PDO_ID[++index], PDOnum[index],
					PDOdir[index], PDOnum2[index], PDOnum3[index] );
		if (PDOnum2[index] == \"\") PDOnum3[index] = \"\" ;
	}
//DebugN(PDO_ID, PDOnum, PDOdir, PDOnum2, PDOnum3);
	for (i = 1; i <= dynlen(Nodes); i++)	{
		for (index = 1; index <= dynlen(PDO_ID); index++)	{
			if ( PDOdir[index] == \"IN\" )
				fprintf(opc, \"%d   =  Bus%02d  \" + PDOnum[index] + \" \" + PDOdir[index]
						 				+ \" \" + PDOnum2[index]	+ \" \" + PDOnum3[index] + \"\\n\",
						 				PDO_ID[index] + Nodes[i], Buses[i] );
			else if ( PDOdir[index] == \"OUT\" )
				fprintf(opc, \"%d   =  Bus%02d \" + PDOnum[index] + \" \" + PDOdir[index] + \"\\n\",
						 				PDO_ID[index] + Nodes[i], Buses[i] );
			else	{
				DebugN(\"Error in Reading PDO direction:\", PDOdir[index]);
				return;
			}
		}
	}
}


/*******************************************************************************/
readPDO(file cfg, dyn_string & OPCItem, dyn_int & num, 
			dyn_string & byte, dyn_string & type, dyn_string & card )
{
	string strn;
	int index = 0;
	num = makeDynInt(0);
	byte = OPCItem = type = card = makeDynString(\"\");
	
	while(1)	{
		fgets( strn, 300, cfg);
		if( strpos (strn, \"[\") != -1) break;
//		strreplace( strn, \"\\t\", \" \");
//		DebugN(strn);
		sscanf( strn, \"%s %d %s %s %s\", OPCItem[++index], num[index], type[index], byte[index], card[index] );
//		DebugN(OPCItem[index], byte[index], type[index], num[index], card[index] );
	}
//DebugN(dynlen(OPCItem), dynlen(num), dynlen(type), dynlen(byte), dynlen(card) );
//DebugN(\"OPCItem\", OPCItem, \"num\", num, \"type\", type, \"byte\", byte, \"card\", card );
}
/*******************************************************************************/
writePDOchan(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, 
							dyn_string type, dyn_string card, int Crate, int node )
{
int	index;	
//DebugN(dynlen(OPCItem), dynlen(num), dynlen(type), dynlen(byte), dynlen(card) );
//DebugN(OPCItem, num, type, byte, card );
	for (index = 1; index <= dynlen(num); index++)	{
		fprintf( opc, 
			\"Crate%02d_\" + OPCItem[index] + \"   =  Crate%02d %d \" + 
			type[index] + \" \" + card[index] + \" \" + byte[index] + \"\\n\",  Crate, Crate, num[index]+node );
	}
}

/*******************************************************************************/

writePDOcard(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, 
											dyn_string type, int Crate, int node)
{
int	i, index, count, count2, LVCard, HVCard;
count = count2 = LVCard = HVCard = 0 ;

//DebugN(dynlen(OPCItem), dynlen(num), dynlen(type), dynlen(byte));

	for (index = 1; index <= dynlen(num); index++)	{
		if ( strpos (OPCItem[index], \"LV\") != -1) count++;
	}
	

	for( i=0; i<6; i++)	{
		for (index = 1; index <= count; index++)	{
			fprintf( opc, 
					\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d %d \"
						+ type[index] + \" %d \" + byte[index] + \"\\n\",
									Crate, LVCard, Crate, num[index]+node, count2 );
		}
		count2++; LVCard++;
		for (index = count+1; index <= dynlen(num); index++)	{
			fprintf( opc, 
					\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d %d \"
						+ type[index] + \" %d \" + byte[index] + \"\\n\",
									Crate, HVCard, Crate, num[index]+node, count2 );
		}
		count2++; HVCard++;
		for (index = 1; index <= count; index++)	{
			fprintf( opc, 
					\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d %d \"
						+ type[index] + \" %d \" + byte[index] + \"\\n\",
									Crate, LVCard, Crate, num[index]+node, count2 );
		}
		count2++; LVCard++;
	}
}


/*******************************************************************************/
writePDOcc(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, 
														dyn_string type, int Crate, int node)
{
int	index, count = 0;
//DebugN(dynlen(OPCItem), dynlen(num), dynlen(type), dynlen(byte));

	for (index = 1; index <= dynlen(num); index++)	{
		fprintf( opc, 
			\"Crate%02d_\" + OPCItem[index] + \"\\t\\t   =  Crate%02d  %d \"	 + type[index]
								 + \" \" + byte[index] + \"\\n\", Crate, Crate, num[index] + node );
	}
}

//////////////////////////////////////////

openSafeOPC(file & opc, string path)
{
	time tm;
	string date, plush;
	int n, err;
	bool ok = TRUE;

/*	DebugN(\"Creating file C:/ETM/OPCCANopenserver/OPCCanServer.cfg\");
	tm = getCurrentTime();
	date = formatTime(\"%d%m%y\", tm);
	//look at other files created in the same day
	n = dynlen (getFileNames(\"C://ETM//OPCCANopenserver//\", \"OPCCanServer_\" 
															+ date + \"*.cfg\"));
	//if there are previos files with same date, add a (n)
	if (n!=0) sprintf(plus, date + \"(%d)\", n);
	else plus = date;
	DebugN(n, plus);
	// copy the old file in a new *_date or *_date(n) file
	ok = copyFile(\"C://ETM//OPCCANopenserver//OPCCanServer.cfg\",
			 \"C://ETM//OPCCANopenserver//OPCCanServer_\" + plus + \".cfg\");
	if(!ok)
		{
		DebugN(\"Error in moving old OPCCanServer.cfg. Creating file in the panels directory\");
		//if the rename faults, the file will be created in the panel directory
		path = strrtrim(path, \"OPCsettings.cfg\") + \"OPCCanServer.cfg\";
		}
	else path = \"C://ETM//OPCCANopenserver//OPCCanServer.cfg\";

	path = strrtrim(path, \"OPCsettings.cfg\") + \"OPCCanServer.cfg\"; // TEMPORARY
	opc = fopen(path,\"w\");
	err = ferror(opc); // export error
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to create file:\", path);
		fclose(cfg);
		return;
		}*/
		
	path = path + \"OPCCanServer.cfg\"; // TEMPORARY
	opc = fopen(path,\"w\");
	err = ferror(opc); // export error
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to create file:\", path);
		fclose(cfg);
		return;
		}
}" 0
 E E E
13 25
"Button1"
""
1 1222 210 E E E 1 E 1 E N "_3DText" E N "_3DFace" E E
 E E
22 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 119 -microsoft windows-Courier New-normal-r-normal-*-*-110-100-100-*-*-iso8859-1|-12,0,0,0,400,0,0,0,0,3,2,1,49,Courier New
0 ""
468 168 632 222
T 
1
LANG:1 16 Load Last Config
"main()
{
file cfg;
string path, strn, driver;
int err, crate, bus, node, port, baud;

shape table=getShape(\"Table1\");

setValue (table, \"deleteLinesN\", 0, table.lineCount());

path = getPath(PANELS_REL_PATH, \"hardware_old.cfg\");
cfg = fopen(path,\"r\");
err = ferror(cfg); // export error
if (err)
	{ 
	DebugN(\"Error no. \",err,\" occurred: not able to open file:\", path);
	return;
	}

fgets(strn, 300, cfg);

while( fgets(strn, 300, cfg ) ) {
	sscanf( strn, \"%d %d %d %s %d %d\", crate, bus, node, driver, port, baud);
//	DebugN(crate, bus, node, driver, port, baud);
	setValue (getShape(\"Table1\"), \"appendLine\", 
	\"Crate\", crate, \"Bus\", bus, \"Node\", node, \"Driver\", driver, \"Port\", port, \"BaudRate\", baud);
//	DebugN(table.lineCount());
}
fclose(cfg);

}" 0
 E E E
13 28
"Button7"
""
1 137 598 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
24 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 105 -microsoft windows-Arial-bold-r-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,0,0,0,0,3,2,1,34,Arial
0 ""
28 438 212 492
T 
1
LANG:1 14 Set Conversion
"//Set Conversions
/******************************************************************************\\
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
|*=====================================-======================================*|
|*			               SCT POWER SUPPLIES MODULES     		              *|
|*=====================================-======================================*|
|*   AUTHORs:        G.Codispoti - codispoti@cs.infn.it			    		  *|
|*								   Giuseppe.Codispoti@cern.ch			      *|
|*																		      *|
|*   DESIGN ISSUEs:  This Script add DPe conversions using	 			      *|
|*					 the function \"Conversion\".								  *|
|*																		      *|
|*   MODIF_HISTORY:  17.03.2003  - first release						      *|
|*																		      *|
\\******************************************************************************/
main()
{
	string 	prefix, Crate, dpeName;
	int i, k, UnitMax;
	dyn_int Crates;

	setValue(\"\",\"foreCol\",\"Red\");
	Apply();
	DebugN(getCurrentTime(), \": Start setting conversion configs\");

	// getting hardware configuration	
	getHWcfg( Crates);
			
	for ( k = 1; k <= dynlen(Crates); k++ ) {
		sprintf( Crate, \"Crate%02d\", Crates[k]);
		DebugN(\" Setting Conversion for Crate \"+Crate);
	
		/* Channels Conversion *******************************************/
			for(i=0; i<48; i++)
			{
			sprintf( prefix, getSystemName() + Crate + \".Channel%02d.\", i );
		
			dpeName		= \"LVch_Vcc.Send\";
			Conversion(prefix + dpeName, \"OUT\", 0.0, 1000.0 / 20.0);
	
			dpeName 	= \"LVch_Vdd.Send\";
			Conversion(prefix + dpeName, \"OUT\", 0.0, 1000.0 / 20.0);
	
			dpeName 	= \"LVchVCSV.Send\";
			Conversion(prefix + dpeName, \"OUT\", 0.0, 1000.0 / 40.0);
			
			dpeName 	= \"LVchPINV.Send\";
			Conversion(prefix + dpeName, \"OUT\", 0.0, 1000.0 / 50.0);
			
			dpeName		= \"LVchDTrp.Send\";
			Conversion(prefix + dpeName, \"OUT\", 0.0, 1.0 / 2.0);
			
			dpeName 	= \"LVchCTrp.Send\";
			Conversion(prefix + dpeName, \"OUT\", 0.0, 1.0 / 2.0);
	
			dpeName 	= \"LVchTLim.Send\";
			Conversion(prefix + dpeName, \"OUT\", 128.0, 1.0);
	
			dpeName 	= \"LVchTTrp.Send\";
			Conversion(prefix + dpeName, \"OUT\", 128.0, 1.0);
	
			dpeName 	= \"HVchITrp.Send\";
			Conversion(prefix + dpeName, \"OUT\", 0.0, 100.0);
	
			dpeName 	= \"HVchRamp.Send\";
			Conversion(prefix + dpeName, \"OUT\", 0.0, 1.0);
				
			dpeName 	= \"LVch_Vcc.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 20.0 / 1000.0);
		
			dpeName 	= \"LVps_Vcc.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 50.0 / 1000.0);
	
			dpeName 	= \"LVretVcc.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 20.0 / 1000.0);
	
			dpeName 	= \"LVch_Icc.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 10.0);
	
			dpeName		= \"LVch_Vdd.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 20.0 / 1000.0);
	
			dpeName		= \"LVps_Vdd.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 50.0 / 1000.0);
	
			dpeName 	= \"LVretVdd.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 20.0 / 1000.0);
	
			dpeName 	= \"LVch_Idd.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 10.0);
	
			dpeName 	= \"LVchVCSV.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 40.0 / 1000.0);
	
			dpeName 	= \"LVchVCSI.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 1.0 / 25.0);
	
			dpeName 	=  \"LVchPINV.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 50.0 / 1000.0);
	
			dpeName 	= \"LVchPINI.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 1.0 / 100.0);
	
			dpeName 	= \"MOch_Tm0.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 1.0);
	
			dpeName 	= \"MOch_Tm1.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 1.0);
	
			dpeName 	= \"HVchVolt.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 1.0 / 8.0);
	
			dpeName 	= \"HVchCurr.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 1.0);
			}
	
		/* LVs cards Conversion *******************************************/
		  for(i=0; i<8; i++)
		  	{
			sprintf( prefix, getSystemName() + Crate + \".LV_card%02d.\", i);
	
			dpeName		= \"LVcaTemp.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 1.0);
	
			dpeName		= \"LVcaPowr.Recv\";
			Conversion(prefix + dpeName, \"IN\", 0.0, 1.0 / 2.5);
	
			dpeName		= \"LVcaTrip.Send\";
			Conversion(prefix + dpeName, \"OUT\", 0.0, 2.5);
			}
		DebugN(\" Conversion Set for Crate \", Crate);
		}

	DebugN(getCurrentTime(), \": setting conversion configs end\");
	setValue(\"\",\"foreCol\",\"\");
}


/****************************************************************************\\
|* This function sets conversions for the datapoint element dpeName   		*|
\\****************************************************************************/
 Conversion(string dpeName, string Direction, float Alfa, float Beta)
{
  dyn_errClass err;

  switch(Direction)
	{
	case \"IN\":                            // Monitored value conversion
		dpSetWait(
			dpeName+\":_msg_conv.._type\", 33, //Coversion from raw to physical
			dpeName+\":_msg_conv.1._type\", 2, //Polynomial 4th degree conversion
			dpeName+\":_msg_conv.1._poly_grade\", 1,  //Take only: y = a + b*x
			dpeName+\":_msg_conv.1._poly_a\", Alfa,
			dpeName+\":_msg_conv.1._poly_b\", Beta); break;
	case \"OUT\":                           // Controled value conversion
		dpSetWait(
			dpeName+\":_cmd_conv.._type\", 36, //Coversion from raw to physical
			dpeName+\":_cmd_conv.1._type\", 2, //Polynomial 4th degree conversion
			dpeName+\":_cmd_conv.1._poly_grade\", 1,  //Take only: y = a + b*x
			dpeName+\":_cmd_conv.1._poly_a\", Alfa,
			dpeName+\":_cmd_conv.1._poly_b\", Beta); break;
	default: DebugN(\"Error! Not able to set the direction\", Direction, \"for\", dpeName); return;	
	}//end of switch(...)
	err = getLastError();
	if(dynlen(err) > 0) 
		{
		throwError(err);
		}
}

getHWcfg( dyn_int & Crates )
{
	dyn_string Drivers;
	dyn_int Bus, Nodes, Ports, BaudRates;
	// getting hardware configuration: function in \"General\"!
	HWcfg( Crates, Bus, Nodes, Drivers, Ports, BaudRates);
}" 0
 E E E
13 30
"Button2"
""
1 450 410 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
26 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 105 -microsoft windows-Arial-bold-r-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,0,0,0,0,3,2,1,34,Arial
0 ""
448 408 632 462
T 
1
LANG:1 16 Add DPs and DPTs
"// Add DPTs & DPs if needed
/******************************************************************************\\
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
|*=====================================-======================================*|
|*			               SCT POWER SUPPLIES MODULES     		           				  *|
|*=====================================-======================================*|
|*   AUTHORs:        G.Codispoti - codispoti@cs.infn.it							    		  *|
|*								   Giuseppe.Codispoti@cern.ch										     				*|
|*																																	    	    *|
|*   DESIGN ISSUEs:  This Script creates the full set of DPTs and DPs needed  *|
|*									 just adding what is missing. System features are loaded	*|
|*   				 				 From the \"hardware.cfg\" file.														*|
|*																																		        *|
|*   MODIF_HISTORY:  17.03.2003  - first release						  							  *|
|*																		  																      *|
\\******************************************************************************/

main()
{
  dyn_dyn_int dpType, dpType_old;
  dyn_dyn_string dpName, dpName_old;
  dyn_string dpAllTypes;
  string chan, LV, HV, Crate, num;
  dyn_int Crates, Bus, crateBus;
  int i, j, index, count;
  bool change = FALSE;

  setValue(\"\",\"foreCol\",\"Red\");  
	Apply();
	DebugN(getCurrentTime(), \": Start Creating DPs & DPTs\");
	
	getValue(getShape(\"change\"), \"state\", 0, change);
	DebugN(change);
  
/******************************************************************************/ 
//define the datapoint structure and types: ps_data_recv
  dpName[1] = makeDynString(\"ps_data_recv\");	dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"Recv\");			dpType[2] = makeDynInt(0, DPEL_FLOAT);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
	//define the datapoint structure and types: ps_data_send
  dpName[1] = makeDynString(\"ps_data_send\");	dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"Send\");			dpType[2] = makeDynInt(0, DPEL_FLOAT);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 

//define the datapoint structure and types: ps_data_dupl
 dpName[1] = makeDynString(\"ps_data_dupl\");	dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"Send\");		dpType[2] = makeDynInt(0, DPEL_FLOAT);
  dpName[3] = makeDynString(\"\", \"Recv\");		dpType[3] = makeDynInt(0, DPEL_FLOAT);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
//define the datapoint structure and types: ps_stat_recv
	dpName[1] = makeDynString(\"ps_stat_recv\");	dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"Recv\");			dpType[2] = makeDynInt(0, DPEL_INT);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
//define the datapoint structure and types: ps_stat_send
  dpName[1] = makeDynString(\"ps_stat_send\");	dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"Send\");			dpType[2] = makeDynInt(0, DPEL_INT);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
//define the datapoint structure and types: ps_stat_dupl
	dpName[1] = makeDynString(\"ps_stat_dupl\");	dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"Send\");			dpType[2] = makeDynInt(0, DPEL_INT);
  dpName[3] = makeDynString(\"\", \"Recv\");			dpType[3] = makeDynInt(0, DPEL_INT);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
//define the datapoint structure and types: ps_crate
  dpName[1]  = makeDynString(\"ps_crate\");													dpType[1]  = makeDynInt(DPEL_STRUCT);
  dpName[2]  = makeDynString(\"\", \"CC_Reset\",     \"ps_stat_send\");	dpType[2]  = makeDynInt(0, DPEL_TYPEREF);
  dpName[3]  = makeDynString(\"\", \"CCch_Set\",     \"ps_stat_send\");	dpType[3]  = makeDynInt(0, DPEL_TYPEREF);
  dpName[4]  = makeDynString(\"\", \"CCchRead\",     \"ps_stat_send\");	dpType[4]  = makeDynInt(0, DPEL_TYPEREF);
  dpName[5]  = makeDynString(\"\", \"StartIVCurve\", \"ps_stat_dupl\");	dpType[5]  = makeDynInt(0, DPEL_TYPEREF);
	dpName[6]  = makeDynString(\"\", \"CCcf_Set\",     \"ps_stat_dupl\");	dpType[6]  = makeDynInt(0, DPEL_TYPEREF);
	dpName[7]  = makeDynString(\"\", \"CCcf_RST\",     \"ps_stat_dupl\");	dpType[7]  = makeDynInt(0, DPEL_TYPEREF);
	dpName[8]  = makeDynString(\"\", \"CCie_NMT\",   	\"ps_stat_dupl\");	dpType[8]  = makeDynInt(0, DPEL_TYPEREF);
	dpName[9]  = makeDynString(\"\", \"CC_error\",   	\"ps_stat_recv\");	dpType[9]  = makeDynInt(0, DPEL_TYPEREF);
	dpName[10] = makeDynString(\"\", \"CCieStat\",  	\"ps_stat_recv\");	dpType[10] = makeDynInt(0, DPEL_TYPEREF);
	dpName[11] = makeDynString(\"\", \"CCieBoot\",  	\"ps_stat_recv\");	dpType[11] = makeDynInt(0, DPEL_TYPEREF);
	dpName[12] = makeDynString(\"\", \"CCieEcod\",  	\"ps_stat_recv\");	dpType[12] = makeDynInt(0, DPEL_TYPEREF);
	dpName[13] = makeDynString(\"\", \"CCieEcod1\", 	\"ps_stat_recv\");	dpType[13] = makeDynInt(0, DPEL_TYPEREF);
	dpName[14] = makeDynString(\"\", \"CCieEcod2\", 	\"ps_stat_recv\");	dpType[14] = makeDynInt(0, DPEL_TYPEREF);
	dpName[15] = makeDynString(\"\", \"CCieEcod3\", 	\"ps_stat_recv\");	dpType[15] = makeDynInt(0, DPEL_TYPEREF);
	dpName[16] = makeDynString(\"\", \"CCieEcod4\", 	\"ps_stat_recv\");	dpType[16] = makeDynInt(0, DPEL_TYPEREF);
	dpName[17] = makeDynString(\"\", \"CCieEcod5\", 	\"ps_stat_recv\");	dpType[17] = makeDynInt(0, DPEL_TYPEREF);
	dpName[18] = makeDynString(\"\", \"CCcf_Ack\",  	\"ps_stat_recv\");	dpType[18] = makeDynInt(0, DPEL_TYPEREF);
	dpName[19] = makeDynString(\"\", \"AlertToDaq\");										dpType[19] = makeDynInt(0, DPEL_STRING);
	dpName[20] = makeDynString(\"\", \"CClifeguard\", \"ps_stat_send\");	dpType[20] = makeDynInt(0, DPEL_TYPEREF);
	dpName[21] = makeDynString(\"\", \"CCchStat\", 		\"ps_stat_recv\");	dpType[21] = makeDynInt(0, DPEL_TYPEREF);
	dpName[22] = makeDynString(\"\", \"CCcfsetDB\", 	\"ps_stat_dupl\");	dpType[22] = makeDynInt(0, DPEL_TYPEREF);
	dpName[23] = makeDynString(\"\", \"CCcf_AckE\", 	\"ps_stat_recv\");	dpType[23] = makeDynInt(0, DPEL_TYPEREF);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
//define the datapoint structure and types: ps_alert
  dpName[1] = makeDynString(\"ps_alert\");			dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"Message\");		dpType[2] = makeDynInt(0, DPEL_STRING);
  dpName[3] = makeDynString(\"\", \"Threshold\");	dpType[3] = makeDynInt(0, DPEL_INT);
  dpName[4] = makeDynString(\"\", \"Counter\");		dpType[4] = makeDynInt(0, DPEL_INT);
	
 	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
//define the datapoint structure and types: hv_card
  dpName[1] = makeDynString(\"hv_card\");												dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"HVcaStat\", \"ps_stat_recv\");	dpType[2] = makeDynInt(0, DPEL_TYPEREF);
  dpName[3] = makeDynString(\"\", \"HVcaMask\", \"ps_data_dupl\");	dpType[3] = makeDynInt(0, DPEL_TYPEREF);
  dpName[4] = makeDynString(\"\", \"Stat\");											dpType[4] = makeDynInt(0, DPEL_UINT);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
//define the datapoint structure and types: lv_card
  dpName[1] = makeDynString(\"lv_card\");												dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"LVcaStat\", \"ps_stat_recv\");	dpType[2] = makeDynInt(0, DPEL_TYPEREF);
  dpName[3] = makeDynString(\"\", \"LVcaTemp\", \"ps_data_recv\");	dpType[3] = makeDynInt(0, DPEL_TYPEREF);
  dpName[4] = makeDynString(\"\", \"LVcaPowr\", \"ps_data_recv\");	dpType[4] = makeDynInt(0, DPEL_TYPEREF);
  dpName[5] = makeDynString(\"\", \"LVcaMask\", \"ps_data_dupl\");	dpType[5] = makeDynInt(0, DPEL_TYPEREF);
  dpName[6] = makeDynString(\"\", \"LVcaTrip\", \"ps_data_send\");	dpType[6] = makeDynInt(0, DPEL_TYPEREF);
  dpName[7] = makeDynString(\"\", \"Stat\");											dpType[7] = makeDynInt(0, DPEL_UINT);
  dpName[8] = makeDynString(\"\", \"WarnMesg\", \"ps_alert\");			dpType[8] = makeDynInt(0, DPEL_TYPEREF);
  dpName[9] = makeDynString(\"\", \"AlrmMesg\", \"ps_alert\");			dpType[9] = makeDynInt(0, DPEL_TYPEREF);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
i=1;
//define the datapoint structure and types: ps_chan
  dpName[i] = makeDynString(\"ps_chan\");														dpType[i++] = makeDynInt(DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"LVchStat\", \"ps_stat_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVch_Vcc\", \"ps_data_dupl\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVps_Vcc\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVretVcc\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVch_Icc\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVch_Vdd\", \"ps_data_dupl\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVps_Vdd\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVretVdd\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVch_Idd\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchVCSV\", \"ps_data_dupl\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchVCSI\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchPINV\", \"ps_data_dupl\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchPINI\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"MOch_Tm0\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"MOch_Tm1\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchDTrp\", \"ps_data_send\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchCTrp\", \"ps_data_send\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchTLim\", \"ps_data_send\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchTTrp\", \"ps_data_send\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchCLKS\", \"ps_stat_send\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchCLKR\", \"ps_stat_send\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"LVchVOut\", \"ps_stat_send\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"HVchStat\", \"ps_stat_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"HVchVolt\", \"ps_data_dupl\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"HVchCurr\", \"ps_data_recv\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"HVchITrp\", \"ps_data_send\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"HVchRamp\", \"ps_data_send\");	dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"Stat\");											dpType[i++] = makeDynInt(0, DPEL_UINT);
  dpName[i] = makeDynString(\"\", \"WarnMesg\", \"ps_alert\");			dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"AlrmMesg\", \"ps_alert\");			dpType[i++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[i] = makeDynString(\"\", \"IVCurve\");										dpType[i++] = makeDynInt(0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"HV_Start\");			dpType[i++] = makeDynInt(0, 0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"\", \"Send\");			dpType[i++] = makeDynInt(0, 0, 0, DPEL_FLOAT);
  dpName[i] = makeDynString(\"\", \"\", 				\"HV_Step\");				dpType[i++] = makeDynInt(0, 0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"\", \"Send\");			dpType[i++] = makeDynInt(0, 0, 0, DPEL_FLOAT);
  dpName[i] = makeDynString(\"\", \"\", 				\"HV_Stop\");				dpType[i++] = makeDynInt(0, 0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"\", \"Send\");			dpType[i++] = makeDynInt(0, 0, 0, DPEL_FLOAT);
  dpName[i] = makeDynString(\"\", \"\", 				\"HV_End\");				dpType[i++] = makeDynInt(0, 0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"\", \"Send\");			dpType[i++] = makeDynInt(0, 0, 0, DPEL_FLOAT);
  dpName[i] = makeDynString(\"\", \"\", 				\"Curr_Max\");			dpType[i++] = makeDynInt(0, 0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"\", \"Send\");			dpType[i++] = makeDynInt(0, 0, 0, DPEL_FLOAT);
  dpName[i] = makeDynString(\"\", \"\", 				\"Curr_Warn\");			dpType[i++] = makeDynInt(0, 0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"\", \"Send\");			dpType[i++] = makeDynInt(0, 0, 0, DPEL_FLOAT);
  dpName[i] = makeDynString(\"\", \"\", 				\"StartIVCurve\");	dpType[i++] = makeDynInt(0, 0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"\", \"Send\");			dpType[i++] = makeDynInt(0, 0, 0, DPEL_INT);
  dpName[i] = makeDynString(\"\", \"\", 				\"Status\");				dpType[i++] = makeDynInt(0, 0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"\", \"Recv\");			dpType[i++] = makeDynInt(0, 0, 0, DPEL_INT);
  dpName[i] = makeDynString(\"\", \"\", 				\"StartTime\");			dpType[i++] = makeDynInt(0, 0, DPEL_TIME);
  dpName[i] = makeDynString(\"\", \"\", 				\"TimeWait\");			dpType[i++] = makeDynInt(0, 0, DPEL_STRUCT);
  dpName[i] = makeDynString(\"\", \"\", 				\"\", \"Send\");			dpType[i++] = makeDynInt(0, 0, 0, DPEL_INT);

	dpTypeEv( dpName, dpType, change );
	//clear the arrays  
  dynClear(dpName);
	dynClear(dpType);
 		
/******************************************************************************************/ 
	// getting hardware configuration
	getHWcfg(Crates, Bus );
//define the datapoint structure and types: MPS_Buses
  dpName[1] = makeDynString(\"MPS_Buses\");													dpType[1] = makeDynInt(DPEL_STRUCT);
  dpName[2] = makeDynString(\"\", \"Bus_NMT\", 					\"ps_stat_dupl\");	dpType[2] = makeDynInt(0, DPEL_TYPEREF);
  dpName[3] = makeDynString(\"\", \"Bus_NGT\", 					\"ps_stat_dupl\");	dpType[3] = makeDynInt(0, DPEL_TYPEREF);
  dpName[4] = makeDynString(\"\", \"BusSync\", 					\"ps_stat_dupl\");	dpType[4] = makeDynInt(0, DPEL_TYPEREF);  
  dpName[5] = makeDynString(\"\", \"BusSyncInt\", 			\"ps_stat_dupl\");	dpType[5] = makeDynInt(0, DPEL_TYPEREF);
  dpName[6] = makeDynString(\"\", \"BusPortError\", 		\"ps_stat_recv\");	dpType[6] = makeDynInt(0, DPEL_TYPEREF);
  dpName[7] = makeDynString(\"\", \"BusPortErrorCode\", \"ps_stat_recv\");	dpType[7] = makeDynInt(0, DPEL_TYPEREF);

	dpTypeEv( dpName, dpType, change );
  dynClear(dpName);
	dynClear(dpType);
 	//create a datapoint of the above defined type
	for(i=1;i<=dynlen(Bus);i++)	{
		sprintf(num, \"Bus%02d\", Bus[i]);
		if ( dynlen( dpNames (num, \"MPS_Buses\" ) )	== 0 )	{
			dpCreate(num, \"MPS_Buses\");
			DebugN(\"DataPoint \", num, \" created\");
		}
	}
/******************************************************************************************/
//define the datapoint structure and types: MPS_Crates
  index=1;
  count=0;
  dpName[index] = makeDynString(\"MPS_Crates\");							dpType[index++] = makeDynInt(DPEL_STRUCT);
  for (i=0; i<6; i++) 
  	{
	for (j=0; j<=7; j++)
		{
		sprintf(chan, \"Channel%02d\", count);
		dpName[index] = makeDynString(\"\", chan, \"ps_chan\");					dpType[index++] = makeDynInt(0, DPEL_TYPEREF);
//  		DebugN(chan);
		count++;
		}
	sprintf(LV, \"LV_card%02d\", 2*i);
	dpName[index] = makeDynString(\"\", LV, \"lv_card\");							dpType[index++] = makeDynInt(0, DPEL_TYPEREF);
//    DebugN(LV);
	sprintf(HV, \"HV_card%02d\", i);
	dpName[index] = makeDynString(\"\", HV, \"hv_card\");							dpType[index++] = makeDynInt(0, DPEL_TYPEREF);
//	DebugN(HV);
	sprintf(LV, \"LV_card%02d\", 2*i+1);
	dpName[index] = makeDynString(\"\", LV, \"lv_card\");							dpType[index++] = makeDynInt(0, DPEL_TYPEREF);	
//   DebugN(LV);
	}
  dpName[index] = makeDynString(\"\", \"Controller\", \"ps_crate\");	dpType[index++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[index] = makeDynString(\"\", \"Stat\");										dpType[index++] = makeDynInt(0, DPEL_UINT);
  dpName[index] = makeDynString(\"\", \"WarnMesg\", 	\"ps_alert\");	dpType[index++] = makeDynInt(0, DPEL_TYPEREF);
  dpName[index] = makeDynString(\"\", \"AlrmMesg\", 	\"ps_alert\");	dpType[index++] = makeDynInt(0, DPEL_TYPEREF);
  
	dpTypeEv( dpName, dpType, change );
  dynClear(dpName);
	dynClear(dpType);
	
  //create a datapoint of the above defined type
	for(i=1;i<=dynlen(Crates);i++)	{
		sprintf(num, \"Crate%02d\", Crates[i]);
	if ( dynlen( dpNames (num, \"MPS_Crates\" ) )	== 0 )	{
			dpCreate(num, \"MPS_Crates\");
			DebugN(\"DataPoint \", num, \" created\");
		}
	}

	DebugN(getCurrentTime(), \": DPs & DPTs created\");
	setValue(\"\",\"foreCol\",\"\");
}
/****************************************END*******************************\\  
\\**************************************************************************/


dpTypeEv( dyn_dyn_string dpName, dyn_dyn_int dpType, bool change)
{
  dyn_dyn_int dpType_old;
  dyn_dyn_string dpName_old;
  int i;
 
  //remove the type if exist
  if(!dpTypeExists(dpName[1][1]))	{
	  //create the above defined type
	  dpTypeCreate(dpName, dpType);
		DebugN(dpName[1][1], \"created\");
	}
  else	{
		dpTypeGet(dpName[1][1], dpName_old, dpType_old);
		for(i=1; i<=dynlen(dpName_old); i++)	{
			if (dynlen(dpName_old[i])==0) dynRemove ( dpName_old, i);
			if (dynlen(dpType_old[i])==0) dynRemove ( dpType_old, i);
		}
		if ( (dpName != dpName_old) || (dpType != dpType_old) )	{
			if(change)	{
				DebugN(\"Changing DPT\", dpName[1][1]);
				dpTypeChange(dpName, dpType );
			}
			else DebugN(\"Found a different\", dpName[1][1], \"keeping old settings.\");
			for(i=1; i<=dynlen(dpName); i++)	{
				if (dpName_old[i] != dpName[i]) DebugN(i, dpName_old[i], dpName[i]);
				if (dpType_old[i] != dpType[i]) DebugN(i, dpName_old[i], dpType_old[i], dpType[i]);
			}
		}
	}
}

//this subroutine check the existence of a given type
bool dpTypeExists(string dpType)
{
  int index;
  dyn_string dpAllTypes;
  
  //get all types
  dpAllTypes = dpTypes();
  
  //check the existence of the given type 
  for(index=1; index<=dynlen(dpAllTypes); index++)
  	{
  		if(dpAllTypes[index] == dpType) return 1;
  	}
  
  return 0;
}

//this subroutine deletes a given type. to do so, it deletes all the datapoint of this type
//then delete the type
deleteDpType(string dpType)
{
  int index;
  dyn_string dpAllNames;
  
  //get all datapoint of the given type
  dpAllNames = dpNames(\"*\", dpType);
  
  //delete all the datapoint of the given type
  for(index=1; index<=dynlen(dpAllNames); index++)
  	{
  		dpDelete(dpAllNames[index]);
  	}
  
  //delete the type
  dpTypeDelete(dpType);
}



getHWcfg(dyn_int & Crates, dyn_int & Bus )
{
	dyn_string Drivers;
	dyn_int Nodes, Ports, BaudRates;
	// getting hardware configuration: function in \"General\"!
	HWcfg( Crates, Bus, Nodes, Drivers, Ports, BaudRates);
	dynUnique(Bus);
}" 0
 E E E
13 31
"Button3"
""
1 367 1028 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
26 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 105 -microsoft windows-Arial-bold-r-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,0,0,0,0,3,2,1,34,Arial
0 ""
238 438 422 492
T 
1
LANG:1 13 Create Alerts
"//Create alerts configs
/******************************************************************************\\
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
|*=====================================-======================================*|
|*			               SCT POWER SUPPLIES MODULES 			    		              *|
|*=====================================-======================================*|
|*   AUTHORs:        G.Codispoti - codispoti@cs.infn.it
|*								   Giuseppe.Codispoti@cern.ch
|*
|*   DESIGN ISSUEs:  This Script add DPe alerts configs
|*
|*   MODIF_HISTORY:  17.03.2003  - first release
|*
\\******************************************************************************/
main()
{
dyn_string Chan, LVcard;
int i, j, k;
string prefix;
string systemName=getSystemName();
dyn_int Crates;

	setValue(\"\",\"foreCol\",\"Red\");
	Apply();
	DebugN(getCurrentTime(), \": Start creating _alert configs\");
	// getting hardware configuration
	getHWcfg( Crates );	

//LVcard = dpNames(\"Crate*.LV_LVcard*.*.Recv\", \"MPS_Crates\");
//Chan = dpNames(\"Crate*.Channel*.*.Recv\", \"MPS_Crates\");
//for(i=1; i<=dynlen(Chan); i++)	{
//	if(i <= dynlen(Chan)) setAlerts( Chan[i], 0, 0, 0, 0);
//	if(i <= dynlen(LVcard)) setAlerts(LVcard[i], 0, 0, 0, 0);
//	}

	Chan = makeDynString( \"HVchCurr\", \"HVchVolt\", \"LVch_Icc\", \"LVch_Idd\",
												\"LVch_Vcc\", \"LVch_Vdd\", \"LVchPINI\", \"LVchPINV\",
												\"LVchVCSI\", \"LVchVCSV\", \"LVps_Vcc\", \"LVps_Vdd\",
												\"LVretVcc\", \"LVretVdd\", \"MOch_Tm0\", \"MOch_Tm1\" );
	
	LVcard = makeDynString( \"LVcaPowr\", \"LVcaTemp\" );

	for(i=1; i<=dynlen(Crates); i++)	{

		for(j=0; j<=47; j++)	{
			sprintf(prefix, systemName+\"Crate%02d.Channel%02d.\", Crates[i], j);
			for(k=0; k<=dynlen(Chan); k++)	{
				setAlerts(prefix+Chan[k]+\".Recv\", 0, 0, 0, 0);
			}
		}

		for(j=0; j<=11; j++)	{
			sprintf(prefix, systemName+\"Crate%02d.LV_card%02d.\", Crates[i], j);
			for(k=0; k<=dynlen(LVcard); k++)	{
				setAlerts(prefix+LVcard[k]+\".Recv\", 0, 0, 0, 0);
			}
		}
}

	DebugN(getCurrentTime(), \": _alert configs created!\");
  setValue(\"\",\"foreCol\",\"\");
}


setAlerts(string dpeName, float Fatal_low, float Warn_low,
                                float Warn_high, float Fatal_high)
{
	string prefix = dpeName+\":_alert_hdl.\";
	dyn_errClass err;
	int rc, type;
	bool ok;
	dyn_string rangeTexts   = makeDynString (\"Data is fatal Low\", \"Data is warn. Low\", \"Data is Normal\",
															\"Data is warn. High\", \"Data is fatal High\");
	dyn_string rangeClasses = makeDynString (\"alert.\", \"warning.\", \"\", \"warning.\", \"alert.\");

if ( !( (Fatal_low < Warn_low) && (Warn_low < Warn_high) && (Warn_high < Fatal_high) ) )
	{
	DebugN(\"Error! Invalid limits for\", dpeName, \" : \", Fatal_low, Warn_low, Warn_high, Fatal_high);
	return; 
	}
//	DebugN(dpeName, \" : \", Fatal_low, Warn_low, Warn_high, Fatal_high);
	
	// check if the alarm already exists
	rc = dpGet (prefix+\"._type\", type);
	err = getLastError ();	
	if (rc | dynlen(err)) {
		DebugN(\"Failed to check whether alarm already exists for dpe: \"+dpeName+\" : \"+rc);
		throwError (err);
		return;
	}

	// if it exists, deactivate the alarm handling (so we can change it)
	if (type != DPCONFIG_NONE) {
		dpDeactivateAlert (dpeName, ok);		
		if ( ! ok) {
			DebugN(\"Failed to deactivate alarm on dpe: \"+dpeName);
			return;
		}
	}
	
	// configure the alarm
	rc = dpSetWait (prefix+\"._type\", DPCONFIG_ALERT_NONBINARYSIGNAL, // equal to 13!

					prefix+\"1._type\", DPDETAIL_RANGETYPE_MINMAX, // equal to 4!
					prefix+\"1._u_limit\", Fatal_low,
					prefix+\"1._l_incl\", TRUE,
					prefix+\"1._u_incl\", FALSE,
					prefix+\"1._text\",  rangeTexts [1],
					prefix+\"1._class\", rangeClasses [1],

					prefix+\"2._type\", DPDETAIL_RANGETYPE_MINMAX,
					prefix+\"2._l_limit\", Fatal_low,
					prefix+\"2._u_limit\", Warn_low,
					prefix+\"2._l_incl\", TRUE,
					prefix+\"2._u_incl\", FALSE,
					prefix+\"2._text\", rangeTexts [2],
					prefix+\"2._class\", rangeClasses [2],

					prefix+\"3._type\", DPDETAIL_RANGETYPE_MINMAX,
					prefix+\"3._l_limit\", Warn_low,
					prefix+\"3._u_limit\", Warn_high,
					prefix+\"3._l_incl\", TRUE,
					prefix+\"3._u_incl\", FALSE,
					prefix+\"3._text\", rangeTexts [3],
					prefix+\"3._class\", rangeClasses [3],

					prefix+\"4._type\", DPDETAIL_RANGETYPE_MINMAX,
					prefix+\"4._l_limit\", Warn_high,
					prefix+\"4._u_limit\", Fatal_high,
					prefix+\"4._l_incl\", TRUE,
					prefix+\"4._u_incl\", FALSE,
					prefix+\"4._text\", rangeTexts [4],
					prefix+\"4._class\", rangeClasses [4],

					prefix+\"5._type\", DPDETAIL_RANGETYPE_MINMAX,
					prefix+\"5._l_limit\", Fatal_high,
					prefix+\"5._l_incl\", TRUE,
					prefix+\"5._u_incl\", FALSE,
					prefix+\"5._text\", rangeTexts [5],
					prefix+\"5._class\", rangeClasses [5],
					prefix+\"._orig_hdl\", TRUE);
			
	err = getLastError ();	
	if (rc | dynlen(err)) {
		DebugN(\"Failed to set alarm on dpe: \"+dpeName+\" : \"+rc);
		throwError (err);
		return;
	}

	//activate the alarm handling
	dpActivateAlert (dpeName, ok);		
	if ( ! ok) {
		DebugN(\"Failed to activate alarm on dpe: \"+dpeName);
		return;
	//Acknoledge Alerts		
	dpSetWait(dpeName+\":_alert_hdl.._ack\", DPATTR_ACKTYPE_MULTIPLE);
	}
}

getHWcfg( dyn_int & Crates )
{
	dyn_string Drivers;
	dyn_int Bus, Nodes, Ports, BaudRates;
	// getting hardware configuration: function in \"General\"!
	HWcfg( Crates, Bus, Nodes, Drivers, Ports, BaudRates);
}" 0
 E E E
13 32
"Button4"
""
1 787 1488 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
27 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 105 -microsoft windows-Arial-bold-r-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,0,0,0,0,3,2,1,34,Arial
0 ""
448 468 632 522
T 
1
LANG:1 14 Create Archive
"//Create archive configs
/******************************************************************************\\
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
|*=====================================-======================================*|
|*			               SCT POWER SUPPLIES MODULES 			    		              *|
|*=====================================-======================================*|
|*   AUTHORs:        G.Codispoti - codispoti@cs.infn.it
|*								   Giuseppe.Codispoti@cern.ch
|*
|*   DESIGN ISSUEs:  This Script add DPe archive configs
|*
|*   MODIF_HISTORY:  17.03.2003  - first release
|*
\\******************************************************************************/
main()
{
dyn_string Chan, LVcard, HVcard, CC;
int i, j, k;
string prefix;
string systemName=getSystemName();
dyn_int Crates;

	setValue(\"\",\"foreCol\",\"Red\");
	Apply();
	DebugN(getCurrentTime(), \": Start creating _archive configs\");
	// getting hardware configuration
	getHWcfg( Crates );


HVcard = dpNames(\"Crate*.HV_card*.*.Recv\", \"MPS_Crates\");
LVcard = dpNames(\"Crate*.LV_card*.*.Recv\", \"MPS_Crates\");
Chan = dpNames(\"Crate*.Channel*.*.Recv\", \"MPS_Crates\");
for(i=1; i<=dynlen(Chan); i++)	{
	if(i <= dynlen(Chan)) setArchiveConfig( Chan[i] );
	if(i <= dynlen(LVcard)) setArchiveConfig(LVcard[i] );
	if(i <= dynlen(HVcard)) setArchiveConfig(HVcard[i] );
	}

/*	Chan = makeDynString( \"HVchCurr\", \"HVchVolt\", \"LVch_Icc\", \"LVch_Idd\",
												\"LVch_Vcc\", \"LVch_Vdd\", \"LVchPINI\", \"LVchPINV\",
												\"LVchVCSI\", \"LVchVCSV\", \"LVps_Vcc\", \"LVps_Vdd\",
												\"LVretVcc\", \"LVretVdd\", \"MOch_Tm0\", \"MOch_Tm1\",
												\"HVchStat\", \"LVchStat\");

	LVcard = makeDynString( \"LVcaPowr\", \"LVcaTemp\" );

	CC = makeDynString( \"CCieEcod\", \"CCieEcod1\", \"CCieEcod2\", \"CCieEcod3\",
											\"CCieEcod4\", \"CCieEcod5\");


	for(i=1; i<=dynlen(Crates); i++)	{

		for(j=0; j<=47; j++)	{
			sprintf(prefix, systemName+\"Crate%02d.Channel%02d.\", Crates[i], j);
			for(k=0; k<=dynlen(Chan); k++)	{
				setArchiveConfig(prefix+Chan[k]+\".Recv\");
			}
		}

		for(j=0; j<=11; j++)	{
			sprintf(prefix, systemName+\"Crate%02d.LV_card%02d.\", Crates[i], j);
			for(k=0; k<=dynlen(LVcard); k++)	{
				setArchiveConfig(prefix+LVcard[k]+\".Recv\");
			}
		}

		sprintf(prefix, systemName+\"Crate%02d.Controller.\", Crates[i]);
		for(k=0; k<=dynlen(CC); k++)	{
			setArchiveConfig(prefix+CC[k]+\".Recv\");
		}
	}
*/
	DebugN(getCurrentTime(), \": _archive configs Created!\");
  setValue(\"\",\"foreCol\",\"\");
}

setArchiveConfig(string dpeName)
{   
    //set archive parameters
 	dpSetWait(dpeName+\":_archive.._type\", 45,
 			  		dpeName+\":_archive.._archive\", TRUE,
            dpeName+\":_archive.1._type\", 15,
 			  		dpeName+\":_archive.1._class\", getSystemName()+\"_ValueArchive_1\");
}

getHWcfg( dyn_int & Crates )
{
	dyn_string Drivers;
	dyn_int Bus, Nodes, Ports, BaudRates;
	// getting hardware configuration: function in \"General\"!
	HWcfg( Crates, Bus, Nodes, Drivers, Ports, BaudRates);
}" 0
 E E E
20 29
"change"
""
1 460 394 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
25 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 117 -microsoft windows-Courier New-bold-r-normal-*-*-120-100-100-*-*-iso8859-1|-12,0,0,0,700,0,0,0,0,3,2,1,49,Courier New
0 ""
458 392 622 412
1
T 
1
LANG:1 19 Change if Different
 0
1
LANG:1 0 
 E  E  0 0 0 0 0
1
E E 
EE13 35
"Button8"
""
1 1222 470 E E E 1 E 1 E N "_3DText" E N "_3DFace" E E
 E E
29 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 119 -microsoft windows-Courier New-normal-r-normal-*-*-110-100-100-*-*-iso8859-1|-12,0,0,0,400,0,0,0,0,3,2,1,49,Courier New
0 ""
468 288 632 342
T 
1
LANG:1 18 Del Selected Lines
"main()
{
	dyn_int n;
	int i;
	shape table=getShape(\"Table1\");
	
	getValue (table, \"getSelectedLines\", n);
	 
	for(i=1; i<=dynlen(n); i++) {
		setValue (table, \"deleteLineN\", n[i]);
	}
}" 0
 E E E
13 36
"LoadDB"
""
1 30 500 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
30 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 105 -microsoft windows-Arial-bold-r-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,0,0,0,0,3,2,1,34,Arial
0 ""
28 498 212 552
T 
1
LANG:1 7 Load DB
"main()
{
  setValue(\"\",\"foreCol\",\"Red\");
  DebugN(getCurrentTime(), \": Start loading mapping\");
  resetAliases();
	loadFile();
	DebugN(getCurrentTime(), \": mapping loaded\");
  setValue(\"\",\"foreCol\",\"\");
}

resetAliases()
{
	dyn_string dps, aliases;
	int index;
	
	dpGetAllAliases ( dps, aliases );

  for(index=1; index<=dynlen(dps); index++)	{
  	dpSetAlias(dps[index], \"\");
	}
}

loadFile()
{
	file cfg;
	string raw, path, dpName, alias, systemName=getSystemName();
	int serial, group, err;
	
	path=getPath (PANELS_REL_PATH, \"Cnf_Map.txt\");
	cfg = fopen(path, \"r\");
	err = ferror(cfg);
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to open file:\",path);
		return;
		}
	
	fgets(raw, 300, cfg);
	
	while(fgets(raw, 300, cfg))
		{
		strreplace( raw, \"\\t\", \" \");
		sscanf(raw, \"%s %d %d %s\", dpName, serial, group, alias);
		dpSetAlias(systemName+dpName, alias);
		}
	fclose(cfg);
}" 0
 E E E
13 37
"Button6"
""
1 240 500 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
31 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 105 -microsoft windows-Arial-bold-r-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,0,0,0,0,3,2,1,34,Arial
0 ""
238 498 422 552
T 
1
LANG:1 14 Set Cooling DB
"
main()
{
  setValue(\"\",\"foreCol\",\"Red\");
  DebugN(getCurrentTime(), \": Start loading mapping for cooling\");
	loadFile();
  DebugN(getCurrentTime(),\": mapping for cooling loades\");
  setValue(\"\",\"foreCol\",\"\");
}

loadFile()
{
	file cfg;
	string raw, path;
	int err;
	
	path=getPath (PANELS_REL_PATH, \"CoolMap.txt\");
	cfg = fopen(path, \"r\");
	err = ferror(cfg);
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to open file:\",path);
		return;
		}
	
	fgets(raw, 300, cfg);
	
	while(fgets(raw, 300, cfg))
		{
		strreplace( raw, \"\\\"\", \"\");
		strreplace( raw, \"\\t\", \" \");
		setLoop(raw);
		}
	
	fclose(cfg);
}


setLoop(string raw)
{
	int layer, nrack, inf, sup, k, i, pos;
	string section, rack, unit, alias;
	dyn_string dps, aliases;
	
	sscanf(raw, \"%s %d %s %d\", section, layer, unit, nrack);
 	sprintf(rack, \"Rack%d.\", nrack);
//	DebugN(section, layer, unit, nrack);

	if (section == \"Barrel\") {
	 	sscanf(unit, \"%d-%d\", inf, sup);
	 	if ( inf<0 || sup>inf+3 || layer<3 || layer>6) {
	 		DebugN(\"Error in reading file: \"+raw);
	 		return;
	 	}
	 	for (k=inf; k<= sup; k++)	{
			sprintf(alias, \"Barrel%02d.Row%02d\", layer, k);
//			DebugN(alias);
			dpGetAllAliases(dps, aliases, \"*\"+alias+\"*\", \"SCTmops1:Crate*.Channel*\");
			for (i=1; i<=dynlen(dps); i++) {
				setAlias(dps[i], aliases[i], rack);
			}
		}
	}
	
	else if (strpos( strtolower(section), \"endcap\") != -1){
	 	sscanf (unit, \"%d\", k);
	 	if ( k<0 || k>3 || layer<1 || layer>9) {
	 		DebugN(\"Error in reading file: \"+raw);
	 	}
		sprintf(alias, section+\"%d.Quadrant%d\", layer, k);
//		DebugN(alias);
		dpGetAllAliases(dps, aliases, \"*\"+alias+\"*\", \"SCTmops1:Crate*.Channel*\");
		for (i=1; i<=dynlen(dps); i++) {
			setAlias(dps[i], aliases[i], rack);
		}
	}
	
	else DebugN(\"Error! In reading:\", section, \"?\");
}

setAlias(string dpName, string alias, string rack)
{
	int pos;

	pos = strpos( alias, \"Rack\");
	DebugN(dpName, alias);
	if ( pos == -1) alias = rack + alias;
	else alias =  rack + substr( alias, pos+6);
//	dpSetAlias(dpName, \"\"); // Delete alias if exists
	dpSetAlias(dpName, alias);
}" 0
 E E E
13 39
"Button10"
""
1 187 505 E E E 1 E 1 E N "_3DText" E N {102,204,255} E E
 E E
33 0 0 0 0 0
EE E
0
1
LANG:1 0 
0
1
LANG:1 105 -microsoft windows-Arial-bold-r-normal-*-*-140-100-100-*-*-iso8859-1|-16,0,0,0,700,0,0,0,0,3,2,1,34,Arial
0 ""
28 378 212 432
T 
1
LANG:1 20 Create OPC Conf file
"//Create OPC File
/******************************************************************************\\
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
|*=====================================-======================================*|
|*			               SCT POWER SUPPLIES MODULES     		            			  *|
|*=====================================-======================================*|
|*   AUTHORs:        G.Codispoti - codispoti@cs.infn.it			    						  *|
|*								   Giuseppe.Codispoti@cern.ch			    										  *|
|*																		  																	    *|
|*   DESIGN ISSUEs:  This Script create the file \"OPCCanServer.cfg\".		  		*|
|*																		      																	*|
|*   MODIF_HISTORY:  17.03.2003  - first release												      *|
|*																		  																	    *|
\\******************************************************************************/
main()
{
file cfg, opc;
dyn_string OPCItem_chan, byte_chan, type_chan, OPCItem_card, byte_card, type_card,
			OPCItem_iv, byte_iv, type_iv, OPCItem_cc, byte_cc, type_cc, card, dummy;
dyn_int Crates, Buses, Nodes, Bus, num_chan, num_card, num_iv, num_cc;
string path, strn, strn0, strn1;
int err, i, j, index;

setValue(\"\",\"foreCol\",\"Red\");
DebugN(getCurrentTime());  

/* Opening configuration database */
path = getPath(PANELS_REL_PATH, \"OPCsettings.cfg\");
cfg = fopen(path,\"r\");
err = ferror(cfg); // export error
if (err)
	{ 
	DebugN(\"Error no. \",err,\" occurred: not able to open file:\", path);
	return;
	}
fgets(strn, 300, cfg);
fgets(strn, 300, cfg); // jump first two lines

// open new file for OPC configuration 
	openSafeOPC(opc, strrtrim(path, \"OPCsettings.cfg\"));

// witing hardware configuration
	writeHWcfg( opc, Crates, Buses, Nodes, Bus);
	DebugN(Crates, Buses, Nodes, Bus);

// Setting SDO Entries
fputs(\"\\n\\n[SDOItem]\\n\", opc);

DebugN(\"Setting SDO's...\");
readSDO (cfg, OPCItem_chan, num_chan, byte_chan, type_chan); // channnels
readSDO (cfg, OPCItem_card, num_card, byte_card, type_card); // cards
readSDO (cfg, OPCItem_iv,   num_iv,   byte_iv,   type_iv); // IV curve
readSDO (cfg, OPCItem_cc,  num_cc,  byte_cc,  type_cc); // others

for(i=1;i<=dynlen(Crates);i++)	{
	writeSDOchan (opc, OPCItem_chan, num_chan, byte_chan, type_chan, Crates[i]);
	writeSDOcard (opc, OPCItem_card, num_card, byte_card, type_card, Crates[i]);
	writeSDOiv   (opc, OPCItem_iv,   num_iv,   byte_iv,   type_iv,   Crates[i]);
	fputs(\"\\n\", opc);
	writeSDOcc  (opc, OPCItem_cc,  num_cc,  byte_cc,  type_cc,  Crates[i]);
	fputs(\"\\n\", opc);
}

// Setting PDO Entries
fputs(\"\\n\\n[PDO]\\n\", opc);
setPDO (cfg, opc, Buses, Nodes);

OPCItem_chan = byte_chan = type_chan = OPCItem_card = byte_card = type_card = makeDynString(\"\");
OPCItem_iv = byte_iv = type_iv = OPCItem_cc = byte_cc = type_cc = card = makeDynString(\"\");
num_chan = num_card = num_iv = num_cc = makeDynInt(0);

// Get PDO Channel Items 
fputs(\"\\n\\n[PDOItem]\\n\", opc);
DebugN(\"Setting PDO's...\");
readPDO (cfg, OPCItem_chan, num_chan, byte_chan, type_chan, card ); // channnels
readPDO (cfg, OPCItem_card, num_card, byte_card, type_card, dummy ); // cards
readPDO (cfg, OPCItem_cc,  num_cc,  byte_cc,  type_cc , dummy ); // crate controller
for(i=1;i<=dynlen(Crates);i++)	{
	writePDOchan(opc, OPCItem_chan, num_chan, byte_chan, type_chan, card, Crates[i], Nodes[i] );
	writePDOcard(opc, OPCItem_card, num_card, byte_card, type_card, Crates[i], Nodes[i] );
	fputs(\"\\n\", opc);
	writePDOcc  (opc, OPCItem_cc,  num_cc,  byte_cc,  type_cc,  Crates[i], Nodes[i]);
	fputs(\"\\n\", opc);
}


// Get INIT Items
DebugN(\"Setting INIT Items...\");
fputs(\"\\n\\n[INIT]\\n\", opc);
OPCItem_cc = num_cc = makeDynString(\"\");
index = 0;
while(1)	{
	fgets( strn, 300, cfg);
	if( strpos (strn, \"[\") != -1) break;
	sscanf( strn, \"%s %d\", OPCItem_cc[++index], num_cc[index]);
}

// Put INIT Items
for (i = 1; i <= dynlen(Bus); i++)	{
	for (index = 1; index <= dynlen(OPCItem_cc); index++)	{
		fprintf( opc, \"Bus%02d.\" + OPCItem_cc[index] + \"\\t\\t  = %d \\n\", Bus[i], num_cc[index] );
	}
}

//Get more Items
fputs(\"\\n\\n[ITEM]\\n\", opc);
DebugN(\"Loading Additional Items...\"); 
while(1)	{
	fgets( strn, 300, cfg);
	DebugN(strn);
	if( strpos (strn, \"[END]\") != -1) break;
		if(( strpos (strn, \"MOch_Tm\") != -1 )|| (strpos (strn, \"HVchCurr\") != -1 )){
			for(j=1;j<=dynlen(Crates);j++)	{
				sprintf(strn0, \"Crate%02d\", Crates[j]);
				for (i=0; i<48; i++){
					sprintf(strn1, \"Crate%02d\", i);
					strreplace (strn, strn1, strn0);
				}
				sprintf(strn0, \"Bus%02d\", Buses[j]);
				for (i=0; i<48; i++)	{
					sprintf(strn1, \"Bus%02d\", Buses[j]);
					strreplace (strn, strn1, strn0);
				}
				fputs(strn, opc);
			}
		}
		else	fputs(strn, opc);
}

fclose(cfg);
fclose(opc);

DebugN(getCurrentTime());  
setValue(\"\",\"foreCol\",\"\");
}

/**********************************END******************************************/
/*******************************************************************************/
/*******************************************************************************/

readSDO (file cfg, dyn_string & OPCItem, dyn_int & num, dyn_string & byte, dyn_string & type)
{
string strn;
int index=0;
num = makeDynInt(0);
byte = OPCItem = type = makeDynString(\"\");

while(1)
	{
	fgets( strn, 300, cfg);
	if( strpos (strn, \"[\") != -1) break;
	sscanf( strn, \"%s %x %s %s\", OPCItem[++index], num[index], byte[index], type[index] );
	}
}

/*******************************************************************************/
writeSDOchan(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, dyn_string type, int Crate)
{
int i, index;
// Write SDO Channel Items
for (i=0; i<48; i++)
	{
	for (index = 1; index <= dynlen(num); index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
										Crate, i, Crate, num[index] + i );
		}
	}
}

/*******************************************************************************/
writeSDOcard(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, dyn_string type, int Crate)
{
int i, LVCard, HVCard, count, index;
LVCard = HVCard = count = 0;

for (index = 1; index <= dynlen(num); index++)
	{
	if ( strpos (OPCItem[index], \"LV\") != -1) count++;
	}

for (i=0; i<6; i++)
	{
	for (index = 1; index <= count; index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
										Crate, LVCard, Crate, num[index] + LVCard );
		}
	LVCard++;
	for (index = count+1; index <= dynlen(num); index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   = Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
									Crate, HVCard, Crate, num[index] + HVCard );
			}
	HVCard++;
	for (index = 1; index <= count; index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   = Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
									Crate, LVCard, Crate, num[index] + LVCard );
		}
	LVCard++;
	}
}

/*******************************************************************************/
writeSDOiv(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, dyn_string type, int Crate)
{
int i, index;

for (i=0; i<48; i++)
	{
	for (index = 1; index <= dynlen(num); index++)
		{
		fprintf( opc, 
				\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d  %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
											Crate, i, Crate, num[index] + i );
		}
	}
}	

/*******************************************************************************/
writeSDOcc(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, dyn_string type, int Crate)
{
int index;

	for (index = 1; index <= dynlen(num); index++)	{
		if( strpos (OPCItem[index], \"SendingDBInProgress\") != -1) {
			fprintf( opc,
				OPCItem[index] + \"%02d\\t   =  Crate%02d %x \"
			 	+ byte[index] + \" IO \" + type[index] + \"\\n\",
										Crate, Crate, num[index]);
		}
		else {
			fprintf( opc, 
				OPCItem[index] + \"\\t\\t\\t   = Crate%02d %x \"
				 + byte[index] + \" IO \" + type[index] + \"\\n\",
										Crate, num[index] );
		}
	}
}

/*******************************************************************************/
setPDO ( file cfg, file opc, dyn_int Buses, dyn_int Nodes)
{
	string strn;
	dyn_int PDO_ID;
	dyn_string PDOnum, PDOdir, PDOnum2, PDOnum3;
	int index=0, i;
	PDO_ID = makeDynInt(\"\");
	PDOnum = PDOdir = PDOnum2 = PDOnum3 = makeDynString(\"\");
//	DebugN(\"Setting PDO's values...\");
	
	while(1)	{
		fgets( strn, 300, cfg);
		if( strpos (strn, \"[\") != -1) break;
		strreplace( strn, \"\\t\", \" \");
		sscanf( strn, \"%d %s %s %s %s\", PDO_ID[++index], PDOnum[index],
					PDOdir[index], PDOnum2[index], PDOnum3[index] );
		if (PDOnum2[index] == \"\") PDOnum3[index] = \"\" ;
	}
//DebugN(PDO_ID, PDOnum, PDOdir, PDOnum2, PDOnum3);
	for (i = 1; i <= dynlen(Nodes); i++)	{
		for (index = 1; index <= dynlen(PDO_ID); index++)	{
			if ( PDOdir[index] == \"IN\" )
				fprintf(opc, \"%d   =  Bus%02d  \" + PDOnum[index] + \" \" + PDOdir[index]
						 				+ \" \" + PDOnum2[index]	+ \" \" + PDOnum3[index] + \"\\n\",
						 				PDO_ID[index] + Nodes[i], Buses[i] );
			else if ( PDOdir[index] == \"OUT\" )
				fprintf(opc, \"%d   =  Bus%02d \" + PDOnum[index] + \" \" + PDOdir[index] + \"\\n\",
						 				PDO_ID[index] + Nodes[i], Buses[i] );
			else	{
				DebugN(\"Error in Reading PDO direction:\", PDOdir[index]);
				return;
			}
		}
	}
}


/*******************************************************************************/
readPDO(file cfg, dyn_string & OPCItem, dyn_int & num, 
			dyn_string & byte, dyn_string & type, dyn_string & card )
{
	string strn;
	int index = 0;
	num = makeDynInt(0);
	byte = OPCItem = type = card = makeDynString(\"\");
	
	while(1)	{
		fgets( strn, 300, cfg);
		if( strpos (strn, \"[\") != -1) break;
//		strreplace( strn, \"\\t\", \" \");
//		DebugN(strn);
		sscanf( strn, \"%s %d %s %s %s\", OPCItem[++index], num[index], type[index], byte[index], card[index] );
//		DebugN(OPCItem[index], byte[index], type[index], num[index], card[index] );
	}
//DebugN(dynlen(OPCItem), dynlen(num), dynlen(type), dynlen(byte), dynlen(card) );
//DebugN(\"OPCItem\", OPCItem, \"num\", num, \"type\", type, \"byte\", byte, \"card\", card );
}
/*******************************************************************************/
writePDOchan(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, 
							dyn_string type, dyn_string card, int Crate, int node )
{
int	index;	
//DebugN(dynlen(OPCItem), dynlen(num), dynlen(type), dynlen(byte), dynlen(card) );
//DebugN(OPCItem, num, type, byte, card );
	for (index = 1; index <= dynlen(num); index++)	{
		fprintf( opc, 
			\"Crate%02d_\" + OPCItem[index] + \"   =  Crate%02d %d \" + 
			type[index] + \" \" + card[index] + \" \" + byte[index] + \"\\n\",  Crate, Crate, num[index]+node );
	}
}

/*******************************************************************************/

writePDOcard(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, 
											dyn_string type, int Crate, int node)
{
int	i, index, count, count2, LVCard, HVCard;
count = count2 = LVCard = HVCard = 0 ;

//DebugN(dynlen(OPCItem), dynlen(num), dynlen(type), dynlen(byte));

	for (index = 1; index <= dynlen(num); index++)	{
		if ( strpos (OPCItem[index], \"LV\") != -1) count++;
	}
	

	for( i=0; i<6; i++)	{
		for (index = 1; index <= count; index++)	{
			fprintf( opc, 
					\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d %d \"
						+ type[index] + \" %d \" + byte[index] + \"\\n\",
									Crate, LVCard, Crate, num[index]+node, count2 );
		}
		count2++; LVCard++;
		for (index = count+1; index <= dynlen(num); index++)	{
			fprintf( opc, 
					\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d %d \"
						+ type[index] + \" %d \" + byte[index] + \"\\n\",
									Crate, HVCard, Crate, num[index]+node, count2 );
		}
		count2++; HVCard++;
		for (index = 1; index <= count; index++)	{
			fprintf( opc, 
					\"Crate%02d_\" + OPCItem[index] + \"%d   =  Crate%02d %d \"
						+ type[index] + \" %d \" + byte[index] + \"\\n\",
									Crate, LVCard, Crate, num[index]+node, count2 );
		}
		count2++; LVCard++;
	}
}


/*******************************************************************************/
writePDOcc(file opc, dyn_string OPCItem, dyn_int num, dyn_string byte, 
														dyn_string type, int Crate, int node)
{
int	index, count = 0;
//DebugN(dynlen(OPCItem), dynlen(num), dynlen(type), dynlen(byte));

	for (index = 1; index <= dynlen(num); index++)	{
		fprintf( opc, 
			\"Crate%02d_\" + OPCItem[index] + \"\\t\\t   =  Crate%02d  %d \"	 + type[index]
								 + \" \" + byte[index] + \"\\n\", Crate, Crate, num[index] + node );
	}
}

//////////////////////////////////////////

openSafeOPC(file & opc, string path)
{
	time tm;
	string date, plush;
	int n, err;
	bool ok = TRUE;

/*	DebugN(\"Creating file C:/ETM/OPCCANopenserver/OPCCanServer.cfg\");
	tm = getCurrentTime();
	date = formatTime(\"%d%m%y\", tm);
	//look at other files created in the same day
	n = dynlen (getFileNames(\"C://ETM//OPCCANopenserver//\", \"OPCCanServer_\" 
															+ date + \"*.cfg\"));
	//if there are previos files with same date, add a (n)
	if (n!=0) sprintf(plus, date + \"(%d)\", n);
	else plus = date;
	DebugN(n, plus);
	// copy the old file in a new *_date or *_date(n) file
	ok = copyFile(\"C://ETM//OPCCANopenserver//OPCCanServer.cfg\",
			 \"C://ETM//OPCCANopenserver//OPCCanServer_\" + plus + \".cfg\");
	if(!ok)
		{
		DebugN(\"Error in moving old OPCCanServer.cfg. Creating file in the panels directory\");
		//if the rename faults, the file will be created in the panel directory
		path = strrtrim(path, \"OPCsettings.cfg\") + \"OPCCanServer.cfg\";
		}
	else path = \"C://ETM//OPCCANopenserver//OPCCanServer.cfg\";

	path = strrtrim(path, \"OPCsettings.cfg\") + \"OPCCanServer.cfg\"; // TEMPORARY
	opc = fopen(path,\"w\");
	err = ferror(opc); // export error
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to create file:\", path);
		fclose(cfg);
		return;
		}*/
		
	path = path + \"OPCCanServer.cfg\"; // TEMPORARY
	opc = fopen(path,\"w\");
	err = ferror(opc); // export error
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to create file:\", path);
		fclose(cfg);
		return;
		}
}
///////////////////////////////

writeHWcfg(file opc, dyn_int & Crates, dyn_int & Buses, dyn_int & Nodes, dyn_int & Bus )
{
	file hdw;
	string path, strn, driver, device, canbus;
	int err, crate, bus, node, port, baud;
	int i=0, j=0, prev_bus=9999;
	
	path = getPath(PANELS_REL_PATH, \"hardware.cfg\");
	hdw = fopen(path,\"r\");
	err = ferror(hdw); // export error
	if (err)
		{ 
		DebugN(\"Error no. \",err,\" occurred: not able to open file:\", path);
		return;
		}
	
	fgets(strn, 300, hdw);
//	fputs(\"[CANBUS]\\n\", opc);
	while( fgets(strn, 300, hdw ) ) {
	if (strlen (strn) < 12) break;
		sscanf( strn, \"%d %d %d %s %d %d\", crate, bus, node, driver, port, baud);
		if (bus != prev_bus) {
			sprintf(canbus, canbus+\"Bus%02d = %s %d %d\\n\", bus, driver, port, baud );
		Bus[++j] = bus;
		}
		Buses[++i] = bus;
		Crates[i] = crate;
		Nodes[i] = node;
		prev_bus = bus;
		sprintf(device, device+\"Crate%02d = Bus%02d %d NG\\n\", crate, bus, crate+1 );
	}
	
	fputs(\"[CANBUS]\\n\"+canbus+\"\\n\\n[DEVICE]\\n\"+device, opc);
	fclose(hdw);
}
" 0
 E E E
0
LAYER, 1 
1
LANG:1 6 Layer2
0
LAYER, 2 
1
LANG:1 6 Layer3
0
LAYER, 3 
1
LANG:1 6 Layer4
0
LAYER, 4 
1
LANG:1 6 Layer5
0
LAYER, 5 
1
LANG:1 6 Layer6
0
LAYER, 6 
1
LANG:1 6 Layer7
0
LAYER, 7 
1
LANG:1 6 Layer8
0
0