adjustingForm.cs
33.5 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dal;
using System.Xml;
using MachineDll;
using System.Threading;
using System.IO;
using Comm;
using Excel=Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using log4net;
namespace testSoftForContaminationExplorer
{
public partial class adjustingForm : Form, IReceiveData
{
//过滤水:filter
//清洗离子:clean
public double solution = -1;//溶液浓度值
public string portName;//端口号
public double testTime=-1;//测试时间
private double washTime=20;//静置时间为20s
private double lastData=0;//上一个值
public double filterWaveRange = 0;//过滤波动范围
private int filterCount=0;//计算波动连续小于范围的次数
public double startData = 0;//第一次清洗时的起始值
Dictionary<int, double> firstCollection = new Dictionary<int, double>();
Dictionary<int, double> secondCollection = new Dictionary<int, double>();
Dictionary<int, double> thirdCollection = new Dictionary<int, double>();
Queue<double> filterWaveData = new Queue<double>();//存放20个数据
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public adjustingForm(string portName,string testKind)
{
InitializeComponent();
this.portName = portName;
if ("LoopTest".Equals(testKind))
{
this.LoopTestPanel.Visible = true;
}
}
/**
* 点击开始校准
*/
private void button1_Click(object sender, EventArgs e)
{
if ("".Equals(txtErrorRang.Text.ToString().Trim()))
{
MessageBox.Show("请先输入误差范围");
}
else
{
if (solution == -1 || testTime == -1 || filterWaveRange==0 || startData==0)
{
MessageBox.Show("请先进行相关设置");
}
else
{
try
{
firstCollection = new Dictionary<int, double>();
secondCollection = new Dictionary<int, double>();
thirdCollection = new Dictionary<int, double>();
this.btnSartAjsuting.Enabled = false;
this.groupBox1.ForeColor = Color.Black;
startReadData(1);
//LOGGER.Info("startAdjustingForm");
}
catch (Exception ex)
{
LOGGER.Info("======" + ex.StackTrace);
}
}
}
}
/**
* 设置液体深度值
*/
private void button7_Click(object sender, EventArgs e)
{
SolutionConcentration sc = new SolutionConcentration(this);
sc.Show();
sc.Activate();
}
//放入样品按钮亮
public void button6bright()
{
this.label1.ForeColor = Color.Black;
this.lblPutProduct1.ForeColor = Color.Black;
}
//再次放入样品亮
public void button2bright()
{
this.label7.ForeColor = Color.Black;
this.lblPutProduct2.ForeColor = Color.Black;
}
//完成校准按钮亮
public void button3bright()
{
this.label3.ForeColor = Color.Black;
this.lblFinish.ForeColor = Color.Black;
}
double firstAndSecondDifferent = 0;
double areaSum2 = 0;
private bool ReadfirstAndSecondData()
{
double areaSum1 = 0;
//根据第二次清洗结果的开始在第一次噪声中找开始时间
int firstTime = 0;
int minTime = -1;//第一次时间
for (int i = 0; i < secondCollection.Count-1; i++)
{
if (firstCollection[i] >= secondCollection[i] && firstCollection[i + 1] <= secondCollection[i+1])//图上有交点
{
minTime = i;
firstTime = i;
break;
}
}
if (minTime == -1)
{
for (int i = 0; i < secondCollection.Count-1; i++)
{
if (firstCollection[i] >= secondCollection[0] && firstCollection[i + 1] <= secondCollection[0])//图上无交点
{
minTime = i;
break;
}
}
}
if (minTime == -1 || minTime >= 120)
{
this.lblFinish.Text="校验失败";
inputVerfailed();
closeMachine();
return false;
}
else
{
for (int i = firstTime; i < secondCollection.Count-1; i++)
{
areaSum2 += (secondCollection[i] + secondCollection[i+1]) / 2.0;
areaSum1 += (firstCollection[minTime] + firstCollection[minTime + 1]) / 2.0;
}
}
firstAndSecondDifferent = Math.Abs(areaSum2 - areaSum1);
this.txtDelta1.Text = firstAndSecondDifferent.ToString();
return true;
}
double areaSum3 = 0;
double firstAndThirdDifferent = 0;
private bool ReadfirstAndThirdData()
{
double areaSum1 = 0;
//根据第三次清洗结果在第一次结果中找开始时间
int firstTime = 0;
double minValue = 100;//最小差
int minTime = -1;//第一次时间
for (int i = 0; i < thirdCollection.Count-1; i++)
{
if (firstCollection[i] >= thirdCollection[i] && firstCollection[i + 1] <= thirdCollection[i+1])//在图上有交点
{
minTime = i;
firstTime = i;
break;
}
}
if (minTime == -1)
{
for (int i = 0; i < thirdCollection.Count-1; i++)
{
if (firstCollection[i] >= thirdCollection[0] && firstCollection[i + 1] <= thirdCollection[0])//在图上没有交点
{
minTime = i;
break;
}
}
}
if (minTime == -1 || minTime >= 120)
{
this.lblFinish.Text="校验失败";
inputVerfailed();
closeMachine();
return false;
}
else
{
for (int i = firstTime; i < thirdCollection.Count-1; i++)
{
areaSum3 += (thirdCollection[i] + thirdCollection[i + 1]) / 2.0;
areaSum1 += (firstCollection[minTime] + firstCollection[minTime + 1]) / 2.0;
}
}
firstAndThirdDifferent = Math.Abs(areaSum3 - areaSum1);
this.txtDelta2.Text = firstAndThirdDifferent.ToString();
return true;
}
//求得实际样品重量
private double thirdProductWeight()
{
if (firstAndSecondDifferent != 0)
{
double weight = solution * firstAndThirdDifferent / firstAndSecondDifferent;
return weight;
}
else
{
return 0;
}
}
//计算实际误差值
private double actError()
{
if (areaSum3 != 0)
{
double value = areaSum2 / areaSum3 * firstAndSecondDifferent;
if (Math.Abs(value - firstAndSecondDifferent) / firstAndSecondDifferent < double.Parse(this.txtErrorRang.Text.ToString()) / 100)
{
value = Math.Abs(value - firstAndSecondDifferent) / firstAndSecondDifferent;
this.lblFinish.Text = "校验成功";
}
else
{
this.lblFinish.Text = "校验失败";
inputVerfailed();
closeMachine();
}
return value*100;
}
else
{
return 30;
}
}
private void inputVerfailed()
{
MessageBox.Show("校验失败");
}
#region 定义
private double solvent;//溶剂
private int numberOfTime;//第几次测试
private delegate void BindtDataDelegate();
private readonly Fuction fuction = new Fuction();
private double washUpperLimit;
private TheMachine theMachine;
private bool _IsRealStart;
private bool _IsStartPrepare;
private DateTime StartTestTime;
private DateTime Date;
private bool _IsPassJudgeWash;
private bool _IsFirstPassJudgeWash = true;
private double timerWash;
private delegate void StartPrepareDelegate();
private System.Timers.Timer timerPrepare;
private Dictionary<int, double> dataCollection = new Dictionary<int, double>();//用于存放相关数据
private System.Timers.Timer timer1;
private System.Timers.Timer timer2;
#endregion
public void startReadData(int numberOfTime)
{
tenData = new Queue<double>();
filterCount = 0;
num = 0;
_IsFirstPassJudgeWash = true;
_IsRealStart = false;
_IfFirst = false;
this.numberOfTime = numberOfTime;
LOGGER.Info("the " + numberOfTime + " time");
Init();
}
private void Init()
{
defineTimer2();
}
private void defineTimer1()
{
timer1 = new System.Timers.Timer();
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 60000;
timer1.AutoReset = false;
timer1.Start();
}
private void defineTimer2()
{
timer2 = new System.Timers.Timer();
timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Elapsed);
timer2.Interval = 100;
timer2.AutoReset = false;
timer2.Start();
}
private void timer1_Elapsed(object sender, EventArgs e)
{
if (_ReceiveValue == null)
{
timer1.Stop();
MessageBox.Show("无数据读入");
this.Close();
}
}
private void timer2_Elapsed(object sender, EventArgs e)
{
timer2.Stop();
this.BeginInvoke(new BindtDataDelegate(BindtData));
defineTimer1();
}
private double cleanTime = 0;
private void BindtData()
{
//if (numberOfTime == 0)
//{
// cleanTime = testTime;
//}
//else
if (numberOfTime == 1)
{
//测试时间
cleanTime = testTime+120;
}
else
{
cleanTime = testTime+10;
}
theMachine = GetMachineFromMe.GetMachine;
SerialPortSetting.PortName = portName;
if (theMachine.StartConnection())//打开机器
{
theMachine.AddClients(this);
InitData();
}
else
{
MessageBox.Show("无数据读入");
this.Close();
}
}
private void InitData()
{
try
{
if (JudgeIsPass())
{
//打开过滤两个阀
theMachine.GetControllor.OpenY1();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.OpenY2();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY0();
TempTestTime = 0;
if (!theMachine.StartReadingIO())
{
this.Close();
}
}
else
{
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("无数据读入");
Close();
}
}
private bool JudgeIsPass()
{
//return true;
bool judgeIsPass = false;
Thread.Sleep(3000);
try
{
if (!theMachine.GetControllor.ReadX0())
{
//原提示加入溶剂
judgeIsPass = JudgeIsPass();
}
else
{
judgeIsPass = true;
}
}
catch
{
MessageBox.Show("无数据读入");
}
return judgeIsPass;
}
#region IReceiveData 成员
private Byte[] _ReceiveValue;
private System.Timers.Timer timerStart;
int num = 0;
/**
* 接收相关数据
*/
public void NewReceiveData(int receivedCount, Byte[] receiveValue)
{
try
{
_ReceiveValue = receiveValue;
double tempData = ChangeData();//从机器中读数
if (!_IsRealStart)//判断是否为过滤状态
{
//过滤完毕,准备初始值D0
if (_IsStartPrepare)
{
//第一次过滤至极限,就不需要将水再静置20s
PrepareData();
}
else
{
JudgeWashUpperLimit(tempData);//判断是否超过过滤上限
//if (numberOfTime == 1 && tempData < startData)//如果是第一次过滤,就把水过滤至极致
//{
// getFilterWaveData(tempData);
//}
//if (numberOfTime == 0)
//{
// firstCollection.Add(num++, tempData);
//}
LOGGER.Info("过滤:" + tempData + ",count:" + filterCount);
}
}
else
{
NewGetData();
//SaveFile();
}
}
catch (Exception ex)
{
LOGGER.Info("NewReceiveData出错:"+ex.StackTrace);
}
}
private bool _IfFirst = false;
public void getFilterWaveData(double tempData)
{
//将数据加入队列
if (filterWaveData.Count < 20)
{
filterWaveData.Enqueue(tempData);
}
else
{
filterWaveData.Enqueue(tempData);//加入最新的数据
double avg = filterWaveData.Average();//平均值
double variance = 0;//方差 +=(值-平均值)^2
foreach (double a in filterWaveData)
{
variance += (a - avg) * (a - avg);
}
variance = variance / filterWaveData.Count;//方差结果
double standardDeviation = Math.Sqrt(variance);//标准差=sqrt(方差)
//变异系数=标准差/平均值
double CV = standardDeviation / avg;
if (CV < filterWaveRange)
{
filterCount = 20;
}
//Console.WriteLine("CV:" + CV);
filterWaveData.Dequeue();//移除第一个数据
}
}
Queue<double> tenData;
private void AddDataGridView5()
{
if (_IfFirst)
{
timerStart = new System.Timers.Timer();
timerStart.Elapsed += new System.Timers.ElapsedEventHandler(timerStartToEnd);
timerStart.AutoReset = false;
timerStart.Interval = (int)cleanTime * 1000;
timerStart.Start();
StartTestTime = DateTime.Now;
Date = DateTime.Now;
TempTestData = Date;
TempTestTime = 0;
}
_IfFirst = false;
TempTestTime = TempTestTime + (DateTime.Now - TempTestData).TotalSeconds;
TempTestData = DateTime.Now;
double tempData = double.Parse(ChangeData().ToString("#0.000"));
if (tempData < 0)
{
tempData = 0;
}
tenData.Enqueue(tempData);
if (tenData.Count >= 10)
{
double avg = tenData.Average();
if (numberOfTime == 1)
{
firstCollection.Add(num++, avg);
}
else if (numberOfTime == 2)
{
secondCollection.Add(num++, avg);
}
else if (numberOfTime == 3)
{
thirdCollection.Add(num++, avg);
}
tenData.Dequeue();
}
LOGGER.Info("2、清洗:时间" + num + ",数据" + tempData);
}
private delegate void AddDataGridView5Delegate();
private void NewGetData()
{
BeginInvoke(new AddDataGridView5Delegate(AddDataGridView5));
}
private double TempTestTime = 0;
private DateTime TempWashTestData;
private DateTime TempTestData;
private string m_ParaReviseType;
private delegate void SetMyMdiParentDelegate(int treeID, string tempValue);
private double ChangeData()
{
//转化前6为电导率数
double returnValue = 0;
string temp = "";
int index = 6;
try
{
for (int i = 1; i < _ReceiveValue.Length; i++)//0~5
{
if (Convert.ToChar(_ReceiveValue[i]).ToString() == "+")
{
index = i;
break;
}
temp = temp + Convert.ToChar(_ReceiveValue[i]);
}
double.TryParse(temp, out returnValue);
;
for (int i = index + 1; i < _ReceiveValue.Length; i++)//6~11
{
if (Convert.ToChar(_ReceiveValue[i]).ToString() == "+")
{
break;
}
}
}
catch (Exception ex)
{
LOGGER.Info("ChangeData出错:"+ ex.StackTrace);
}
return returnValue;
}
#endregion
private double sumPrepare = 0;
private double D0 = 0;
private int count = 0;
private void PrepareData()
{
count = count + 1;
double tempData = ChangeData();
sumPrepare = sumPrepare + tempData;
D0 = sumPrepare / count;//得到初始值D0
if (numberOfTime == 1)
{
firstCollection.Add(num++, tempData);
}
else if (numberOfTime == 2)
{
secondCollection.Add(num++, tempData);
}
else if (numberOfTime == 3)
{
thirdCollection.Add(num++, tempData);
}
LOGGER.Info("1、清洗:时间" + num + ",数据" + tempData + ",now时间:" + DateTime.Now);
}
#region JudgeWashUpperLimit
private delegate void PrepareDataWashDelegate(double tempData);
private void JudgeWashUpperLimit(double tempData)
{
//if (numberOfTime == 1 || numberOfTime == 0 )
if(numberOfTime ==1)
{
if (tempData >= startData)//如果过滤数据大于极限数据,则还需要继续测试,比如0.02<0.05
{
if (_IsPassJudgeWash)
{
_IsPassJudgeWash = false;
theMachine.GetControllor.OpenY1();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.OpenY2();
}
_IsPassJudgeWash = false;
_IsFirstPassJudgeWash = true;
}
else
{
_IsPassJudgeWash = true;
}
}
else
{
if (tempData >= startData || tempData > firstCollection[100])//如果过滤数据大于极限数据,则还需要继续测试,比如0.02<0.05
{
if (_IsPassJudgeWash)
{
_IsPassJudgeWash = false;
theMachine.GetControllor.OpenY1();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.OpenY2();
}
_IsPassJudgeWash = false;
_IsFirstPassJudgeWash = true;
}
else
{
_IsPassJudgeWash = true;
}
}
BeginInvoke(new PrepareDataWashDelegate(PrepareDataWash), tempData);
}
public int number = 0;
private void PrepareDataWash(double temp)//准备
{
try
{
if (TempTestData.Year.ToString().Equals("1"))
{
TempTestData = DateTime.Now;
}
if (_IsPassJudgeWash && _IsFirstPassJudgeWash)
{
_IsFirstPassJudgeWash = false;
TempWashTestData = DateTime.Now;
timerWash = 0;
}
TempTestTime = TempTestTime + (DateTime.Now - TempTestData).TotalSeconds;//过滤时间,第几秒
timerWash = timerWash + (DateTime.Now - TempWashTestData).TotalSeconds;
TempWashTestData = DateTime.Now;
TempTestData = DateTime.Now;
double tempData;
if (temp == 0)
{
tempData = 100;
}
else
{
tempData = 1 / temp;
}
if (tempData > 100)
{
tempData = 100;
}
}
catch (Exception ex)
{
LOGGER.Info(ex.StackTrace);
}
try
{
if (_IsPassJudgeWash && timerWash >= washTime )
{
timerWashToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show("无数据读入");
LOGGER.Info("PrepareDataWash报错:" + ex.StackTrace);
//CloseAll();
Close();
}
}
#endregion
#region Prepare
private void timerWashToEnd()
{
theMachine.StopReading();
theMachine.GetControllor.CloseY2();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY1();
this.BeginInvoke(new StartPrepareDelegate(StartPrepare));
}
private void StartPrepare()
{
timerPrepare = new System.Timers.Timer();
timerPrepare.Elapsed += new System.Timers.ElapsedEventHandler(timerPrepareToEnd);
timerPrepare.Interval = 2000;//2秒 原600000
timerPrepare.AutoReset = false;
timerPrepare.Start();
//LOGGER.Info("startPrepare,OpenY0 and Y2");
//theMachine.GetControllor.OpenY0();
//System.Threading.Thread.Sleep(1500);
//theMachine.GetControllor.OpenY2();
//theMachine.StartReading();
//_IsStartPrepare = true;
}
private void timerPrepareToEnd(object sender, EventArgs e)
{
try
{
timerPrepare.Stop();
//theMachine.StopReading();
////关闭Y2,Y0,D停止读数D
//theMachine.GetControllor.CloseY2();
//System.Threading.Thread.Sleep(1500);
//theMachine.GetControllor.CloseY0();
if (numberOfTime == 2)
{
button6bright();
}
else if (numberOfTime == 3)
{
button2bright();
}
if (numberOfTime == 1 || (MessageBox.Show("请放入测试样品","提示",MessageBoxButtons.YesNo) == DialogResult.Yes))
{
_IsRealStart = true;
//准备结束,开始真正测试
_IsStartPrepare = false;
//打开Y0,Y2,D读数
//LOGGER.Info("timerPrepareToEnd,OpenY0 and Y2");
theMachine.GetControllor.OpenY0();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.OpenY2();
theMachine.StartReading();
}
else
{
BeginInvoke(new CloseFormDelegate(CloseForm));
}
_IfFirst = true;
}
catch (Exception ex)
{
LOGGER.Info("timerPrepareToEnd出错:"+ ex.StackTrace);
}
}
#endregion
public void timerStartToEnd(object source, System.Timers.ElapsedEventArgs e)
{
timerStart.Stop();
theMachine.StopReading();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY2();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY0();
theMachine.RemoveClients(this);
//LOGGER.Info("2、关闭Y0和Y2阀,时间:" + DateTime.Now);
try
{
//if (numberOfTime == 0)
//{
// timer1 = null;
// timer2 = null;
// timerStart = null;
// timerPrepare = null;
// SaveFile(firstCollection,secondCollection,null);
// startReadData(0);//不断进行过滤清洗
//}else
if (numberOfTime == 1)
{
timer1 = null;
timer2 = null;
timerStart = null;
timerPrepare = null;
startReadData(2);//第二次正常过滤清洗加样
}
else if (numberOfTime == 2)
{
timer1 = null;
timer2 = null;
timerStart = null;
timerPrepare = null;
//求两次面积之差
if (!ReadfirstAndSecondData())
{
return;
}
startReadData(3);//第三次验证:正常过滤清洗加样
}
else if (numberOfTime == 3)
{
if (!ReadfirstAndThirdData())
{
return;
}
this.txtWeight.Text = thirdProductWeight().ToString();
this.txtActualRange.Text = actError().ToString();
button3bright();
SaveFile(firstCollection, secondCollection, thirdCollection);
CloseRead();
}
}
catch (Exception ex)
{
LOGGER.Info("第"+numberOfTime+"次报错:" + ex.StackTrace);
}
}
public void SaveFile(Dictionary<int, double> firstCollection, Dictionary<int, double> secondCollection, Dictionary<int, double> thirdCollection)
{
if (thirdCollection.Count != 0)
{
SaveCSV("D://AdjustingData" + DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒") + ".csv", firstCollection, secondCollection, thirdCollection);
}
else
{
SaveCSV("D://LoopTestingData" + DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒") + ".csv", firstCollection, secondCollection);
}
}
/// <summary>
/// 将DataTable中数据写入到CSV文件中
/// </summary>
/// <param name="dt">提供保存数据的DataTable</param>
/// <param name="fileName">CSV的文件路径</param>
public void SaveCSV(string fullPath,params Dictionary<int,double>[] dataCollections)
{
FileInfo fi = new FileInfo(fullPath);
if (!fi.Directory.Exists)
{
fi.Directory.Create();
}
FileStream fs = new FileStream(fullPath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
//StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
if(dataCollections.Length==3){
//写出列名称
string data = "firstTime,value,secondTime,value,thirdTime,value";
sw.WriteLine(data);
//写出各行数据
foreach (var a in firstCollection)
{
data = a.Key.ToString() + "," + a.Value.ToString();
if (a.Key < secondCollection.Count)
{
data += "," + a.Key + "," + secondCollection[a.Key];
}
else
{
data += ",,";
}
if (a.Key < thirdCollection.Count)
{
data += "," + a.Key + "," + thirdCollection[a.Key];
}
else
{
data += ",,";
}
sw.WriteLine(data);
}
}
else if (dataCollections.Length == 2)
{
//写出列名称
string data = "filterTime,filterValue,cleanTime,CleanValue";
sw.WriteLine(data);
//写出各行数据
foreach (var a in firstCollection)
{
data = a.Key.ToString() + "," + a.Value.ToString();
if (a.Key < secondCollection.Count)
{
data += "," + a.Key + "," + secondCollection[a.Key];
}
else
{
data += ",,";
}
sw.WriteLine(data);
}
}
sw.Close();
fs.Close();
}
private object MyMdiParent;
private DateTime pauseTime;
private delegate void CloseFormDelegate();
private void CloseForm()
{
//((FrmMDI)MyMdiParent).GetLeftPauseTestInfo();
this.Close();
}
public void CloseRead()
{
theMachine.CloseConnection();
}
private void adjustingForm_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
/**
* 中止校准
*/
private void button1_Click_1(object sender, EventArgs e)
{
if(MessageBox.Show("确认中止校准?","提示",MessageBoxButtons.YesNo)==DialogResult.Yes){
closeMachine();
this.btnSartAjsuting.Enabled = true;
}
}
private void closeMachine()
{
if (this.theMachine != null)
{
try
{
theMachine.StopReading();
theMachine.GetControllor.CloseY0();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY2();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY1();
}
catch (Exception ex)
{
Console.WriteLine("ex:" + ex.StackTrace);
}
}
}
/**
* 将数据自动应用到
*/
private void btnApply_Click(object sender, EventArgs e)
{
}
private void adjustingForm_FormClosed(object sender, FormClosedEventArgs e)
{
}
//循环测试
private void button2_Click(object sender, EventArgs e)
{
startReadData(0);
}
private void adjustingForm_FormClosing(object sender, FormClosingEventArgs e)
{
closeMachine();
}
}
}