aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Stubs Collection/stubs/Scripts/Assemblies/MidTank/MIDTANK_JIG.cs
blob: 6b864dfee92034c8de974c3c095f1b44be2cd415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
using System;
using System.Text;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Tango.PMR.Stubs;
using Tango.Stubs;
using System.Timers;


// private static System.Timers.Timer aTimer;

//--------------------------------------------------------------------------------
const Int32 I2C_ID =  2;
const  int UNIT_UNDER_TEST	=3*8;
const  Int32 F3_VALVE_OUT	= 0x600008e2;		
//--------------------------------------------------------------------------------
string path1="P:\\Scripts\\Assemblies\\MidTank\\MidTankLog\\";
string path2="C:\\MidTankLog\\";

string	File_Name1	;
string Status;
//---------------------------
int [] Convert_Midtank_No = new Int32 [8] {1,3,5,7,2,4,6,8};
uint [,] ADC_Add_Chnl = new uint [8,2]	{
												{0x44,0x20},				
												{0x44,0x08},				
												{0x46,0x80},
												{0x46,0x20},
												{0x44,0x10},
												{0x44,0x04},
												{0x46,0x40},
												{0x46,0x10},
										};


string [,] Units_status = new string[UNIT_UNDER_TEST,2]	{ 
												{"Midtank1_Valve_Air","Not Test"},				//0
												{"Midtank1_Valve_Ink","Not Test"},				//1
												{"Midtank1_Pressure_Sensor","Not Test"},		//2
												{"Midtank2_Valve_Air","Not Test"},				//3
												{"Midtank2_Valve_Ink","Not Test"},				//4
												{"Midtank2_Pressure_Sensor","Not Test"},		//5
												{"Midtank3_Valve_Air","Not Test"},				//6
												{"Midtank3_Valve_Ink","Not Test"},				//7
												{"Midtank3_Pressure_Sensor","Not Test"},		//8
												{"Midtank4_Valve_Air","Not Test"},				//9
												{"Midtank4_Valve_Ink","Not Test"},				//10
												{"Midtank4_Pressure_Sensor","Not Test"},		//11
												{"Midtank5_Valve_Air","Not Test"},				//12
												{"Midtank5_Valve_Ink","Not Test"},				//13
												{"Midtank5_Pressure_Sensor","Not Test"},		//14
												{"Midtank6_Valve_Air","Not Test"},				//15
												{"Midtank6_Valve_Ink","Not Test"},				//16
												{"Midtank6_Pressure_Sensor","Not Test"},		//17
												{"Midtank7_Valve_Air","Not Test"},				//18
												{"Midtank7_Valve_Ink","Not Test"},				//19
												{"Midtank7_Pressure_Sensor","Not Test"},		//20
												{"Midtank8_Valve_Air","Not Test"},				//21
												{"Midtank8_Valve_Ink","Not Test"},				//22
												{"Midtank8_Pressure_Sensor","Not Test"},		//23
											};
//-----------------------------------------------------
string MIDTANK_Sn;
//--------------------------------------------------------------------------------
 // private static void SetTimer()
 //  {
 //      // Create a timer with a one second interval.
 //       aTimer = new System.Timers.Timer(1000);
//        // Hook up the Elapsed event for the timer. 
//        aTimer.Elapsed += OnTimedEvent;
//        aTimer.AutoReset = true;
//        aTimer.Enabled = true;
//    }

//    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
 //   {
//		bool x=0;
//		if (x==true)
//			Label5.Text="Pros";
//		else
////				Label5.Text="    ";
//		x = !x	;
//	
//    }









public void OnExecute(StubManager stubManager)
{
string  path="";
		Form form1 = new Form();
		
// Set the caption bar text of the form.
		form1.TopMost = true;
		form1.Text = "MIDTANK Test";
  		form1.FormBorderStyle = FormBorderStyle.FixedDialog;
 		form1.MaximizeBox = false;
		form1.MinimizeBox = false;
 		form1.StartPosition = FormStartPosition.CenterScreen;
	    form1.AutoSize = true;
	    form1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
// Create buttons to Run. 
 		Button button1 = new Button () 
	{
		Width = 100,
   	  	Height = 100,
  	};
		button1.Text = "Run";
		button1.Location = new Point (40, 100);
	    form1.Controls.Add(button1);
//-----MIDTANK Serial Number -----------

	Label Label1 = new Label () 
		{
		Width = 150,
   	  	Height = 20,
   	  	Text ="  MIDTANK S/N"
  		};
	Label1.Location = new Point (30, 10);
 	form1.Controls.Add(Label1);
	TextBox TextBox1 = new TextBox () 
		{
		Width = 100,
   	  	Height = 10,
   	  	Text =""
  		};
	TextBox1.Location = new Point (40, 40);
	form1.Controls.Add(TextBox1);
 //----------LOCATION----------
	 
 Label Label3 = new Label () 
	{
		Width = 150,
   	  	Height = 20,
   	  	Text ="LOCATION"
  	};
Label3.Location = new Point (350, 10);
 form1.Controls.Add(Label3);
	 


TextBox TextBox3 = new TextBox () 
	{
		Width = 100,
   	  	Height = 10,
   	  	Text =""
  	};
TextBox3.Location = new Point (360, 40);
 form1.Controls.Add(TextBox3);
 
 
 	 
	 
	Label Label5 = new Label () 
	{
		Width = 120,
   	  	Height =100,
   	  	Text = ""
  	}; 
	Label5.Location = new Point (300, 100);
	Label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

	Label5.Text="";
	Label5.Font = new Font("Arial", 24,FontStyle.Bold);
	form1.Controls.Add(Label5); 	

//----------------------------

bool BlinkOn = false;

 
 
//--------------------------------------------------------------------------------
	      
	    button1.Click += (_,__) => 
	    
	{ 
//	    SetTimer();

//		Label5.Text="Pros";




//--------------------------------------------------------------------------------
	      



                Label5.Text = "Pros";
  //              if (_click)
  //                  _show_text.Text = "Fail";
  //              else
 //                   _show_text.Text = "Pass";

 //               _click = !_click;
   //             TextBox1.Text = "";
 


//--------------------------------------------------------------------------------
	      

	    	
	if (!Directory.Exists(path1))
		{
			path=path2;	
			if (!Directory.Exists(path2))
			Directory.CreateDirectory(path2);	
		}			
	else
		path=path1;	

	File_Name1=path + TextBox1.Text +"_MIDTANK_" + DateTime.Now.ToString("MM_dd_yyyy_HH_mm_ss")+".log" ;


	stubManager.Write(File_Name1 +"\n");
	stubManager.WriteToFile(File_Name1,DateTime.Now +" " );
	stubManager.Write(DateTime.Now +"\n" );

	stubManager.AppendToFile(File_Name1,"MIDTANK Test Report" );
	stubManager.Write("MIDTANK Test Report\n" );

	stubManager.AppendToFile(File_Name1,"MIDTANK S/N: \t" + TextBox1.Text  );
	stubManager.Write("MIDTANK S/N:" + TextBox1.Text +"\n" );
                  

	stubManager.AppendToFile(File_Name1,"Location:\t" + TextBox3.Text );
	stubManager.Write("Location:" + TextBox3.Text +"\n" );



//------------------------------- start test -----------
	Test_MIDTANK();

	Status=write_to_file();
	stubManager.Write(Status +"\n");	
  			
	Label5.Text=Status;
	
									//	_show_text.Text			=Status;
	
	TextBox1.Text="";
	};	   
	   
	   form1.ShowDialog();
	
	return ;
   }
 
 //------------------------------------  
int Test_MIDTANK()
{
	double Pressur_Volt;
	double temp;
	for (Int32 i=1;i<9;i++)
	{
		open_valve(i,0,1)		;	//open Air valve
		open_valve(i,1,1)		;	//open Ink valve
		Thread.Sleep(2000);
		Pressur_Volt=read_pressuere_sense(i);
		if ((Pressur_Volt>3 )||(Pressur_Volt<2))
			{
				Units_status[(i-1)*3+2,1]="Fail";
				stubManager.Write("pressuere_sense of midtank" + i +"\tFail\n");	
			}
		else 	
			{
				close_valve(i,1,0)		;	//close Ink valve
				DialogResult result = MessageBox.Show("ADD 20cc air thru Air valve of Midtank"+ i, "Warning");
				close_valve(i,0,0)		;	//close Air valve
				Thread.Sleep(2000);

				temp=read_pressuere_sense(i);
				temp=temp-Pressur_Volt;
				if ((temp>0.5) && ( temp <2))
					Units_status[(i-1)*3+2,1]="Pass";
				else
					Units_status[(i-1)*3+2,1]="Fail";
			}
		
		close_valve(i,0,0)		;	//close Air valve
		close_valve(i,1,0)		;	//close Ink valve
	}
	return 1;	
}

double read_pressuere_sense(Int32 Midtank_Nm)
{
double 	Pressur_Volt;
	adc_configuration((ADC_Add_Chnl[Midtank_Nm-1,0]),(ADC_Add_Chnl[Midtank_Nm-1,1])	);		//configure address and channel of midtank 
	adc_set_for_read_ch(ADC_Add_Chnl[Midtank_Nm-1,0]);
	Pressur_Volt =adc_read_ch(ADC_Add_Chnl[Midtank_Nm-1,0]);
	
	stubManager.Write("pressure sense of midtank " + Midtank_Nm + "is " + Pressur_Volt+ "Volt\n");	
	return Pressur_Volt;
}


//--------------------------------
int adc_configuration(uint I2C_Slave_Add,uint channel ) 
	{
	StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
	stubI2CWriteBytesRequest.I2CId = I2C_ID;
	stubI2CWriteBytesRequest.SlaveAddress = I2C_Slave_Add;
	
	UInt32 uInt32 = new UInt32();
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x00);//Byte 0 to write
	stubI2CWriteBytesRequest.BytesTWrite.Add(channel);//Byte 1 to Write, must be 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02 or 0x01 for different channels
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x80);//Byte 2 to Write
	
	var response = stubManager.Run<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
	
	return 1;
	}
//--------------------------------
int adc_set_for_read_ch(uint I2C_Slave_Add) 
	{
	StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
	stubI2CWriteBytesRequest.I2CId = I2C_ID;
	stubI2CWriteBytesRequest.SlaveAddress = I2C_Slave_Add;
	
	UInt32 uInt32 = new UInt32();
	stubI2CWriteBytesRequest.BytesTWrite.Add(0x01);//Byte 0 to write

	var response = stubManager.Run<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
	
	return 1;
	}
//--------------------------------	

double adc_read_ch(uint I2C_Slave_Add) 
	{
	StubI2CReadBytesRequest stubI2CReadBytesRequest = new StubI2CReadBytesRequest();
	stubI2CReadBytesRequest.I2CId = I2C_ID;
	stubI2CReadBytesRequest.SlaveAddress = I2C_Slave_Add;
	stubI2CReadBytesRequest.NumberOfBytesToRead = 2; // Number of bytes to read

	var response1 = stubManager.Run<StubI2CReadBytesResponse>(stubI2CReadBytesRequest);
	uint temph=0;
	uint templ=0;
	uint temp;
	double calc;
	double calc1;
			temph=response1.ReadBytes[0];
			templ=response1.ReadBytes[1];
			temp = (temph << 8) | templ;
			temp=temp & 0x0fff;
				
			calc = (double)temp *2.5 / 4096.0;
			calc1 = (1.96- calc) * 4.64 + 1.96;
	return calc1;
	}
//--------------------------------
int open_valve (int valve_nm,int valve_type,int check_valve)		//valve_type=0 is Air ,1 is Ink ; check_valve=1 send feedbak
{
	int temp1=Convert_Midtank_No[valve_nm-1]	;

	string temp;
	SetBit (F3_VALVE_OUT,((valve_type+1)*8)- temp1, 1);
		if (check_valve!=0)
		{
			temp="Air";
			if (valve_type==1)
			temp="Ink";
				
			DialogResult result = MessageBox.Show("Did " + temp + "Valve of Midtank " + valve_nm +" open?", "Warning",MessageBoxButtons.YesNo);
			if(result == DialogResult.No)
				{
					Units_status[(valve_nm-1)*3+valve_type,1]="Fail";
					stubManager.Write(temp +"Valve\tFail\n");	
					return 0;
				}
			else 
				{
					Units_status[(valve_nm-1)*3+valve_type,1]="Pass";
					stubManager.Write(temp +"Valve\tOpen\n");	
					return 1;
				}
			
		}
		return 1;

}
//--------------------------------	
int close_valve (int valve_nm,int valve_type,int check_valve)
{
	int temp1=Convert_Midtank_No[valve_nm-1];	

	string temp;
	SetBit (F3_VALVE_OUT,((valve_type+1)*8)- temp1, 0);
	if (check_valve!=0)
		{
			temp="Air";
			if (valve_type==1)
			temp="Ink";
				
			DialogResult result = MessageBox.Show("Did " + temp +" Valve of Midtank " + valve_nm +" close?", "Warning",MessageBoxButtons.YesNo);
			if(result == DialogResult.No)
				{
					Units_status[(valve_nm-1)*3+valve_type,1]="Fail";
					stubManager.Write(temp +"Valve\tFail\n");	
					return 0;
				}
			else 
				{
					Units_status[(valve_nm-1)*3+valve_type,1]="Pass";
					stubManager.Write(temp +"Valve\tclose\n");	
					return 1;
				}			
		}
	return 1;




}
//--------------------------------
Int32 SetBit(Int32 Adr, Int32 BitNo, Int32 Bit)
{
	Int32 BitMask;

    var response = stubManager.Run<StubFpgaReadRegResponse>("StubFpgaReadRegRequest" , F3_VALVE_OUT);
	Int32 RV = (Int32) response.Value & 0xffff;
	
	
	if (Bit == 0x1) 
	{
		BitMask = 0x1 << BitNo;
		RV = RV | BitMask;
		stubManager.Run<StubFpgaWriteRegResponse>("StubFpgaWriteRegRequest" ,F3_VALVE_OUT , RV);
	}
	else if (Bit == 0x0)
	{
		BitMask = ~(0x1 << BitNo);
		RV = RV & BitMask;
		stubManager.Run<StubFpgaWriteRegResponse>("StubFpgaWriteRegRequest" ,F3_VALVE_OUT , RV);
	}
	return 1;
}
 //------------------------------------  
string write_to_file() 
{	
	    stubManager.Write("write_to_file\n");
	
string Stemp="Pass";
		for (int i=0;i<UNIT_UNDER_TEST;i++)
		{
			stubManager.AppendToFile(File_Name1,Units_status[i,0]+"\t" + Units_status[i,1] );
			stubManager.Write(Units_status[i,0]+"\t" + Units_status[i,1]+"\n" );

			if ((Units_status[i,1]=="Fail")||(Units_status[i,1]=="Not Test"))
			Stemp="Fail";
		}
		if (Stemp=="Fail")
			{
			stubManager.AppendToFile(File_Name1,"-------------MidTank  Fail ------------ ");
			stubManager.Write("-------------MidTank Fail ------------ ");
			}
		else
			{
			stubManager.AppendToFile(File_Name1,"-------------MidTank Pass ------------ )");
			stubManager.Write("-------------MidTank Pass ------------ ");
			}
			
	return Stemp;
	
}