mainForm.cs
10.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
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 MachineDll;
using Dal;
using Comm;
namespace testSoftForContaminationExplorer
{
public partial class mainForm : Form, IReceiveData
{
#region 定义
private int timeData=0;//时间
private readonly Fuction fuction = new Fuction();
private TheMachine theMachine;
private string m_ReadHeatTemp;
private bool _IsRealStart=false;//是否开始测试
private bool _IsStartPrepare;
private double washUpperLimit=0.05;//过滤上限
private bool _IsPassJudgeWash;
private bool _IsFirstPassJudgeWash = true;
private DataTable table1;
private bool isStop = false;
public string portName = "";//设置端口值
private int timerWash = 0;//过滤清洗时间20s
#endregion
public mainForm()
{
InitializeComponent();
}
Setting s = null;
private void button1_Click(object sender, EventArgs e)
{
s = new Setting(this);
s.Show();
s.Activate();
}
private void mainForm_Load(object sender, EventArgs e)
{
this.groupBox1.ForeColor = Color.Gray;
this.label1.ForeColor = Color.Gray;
this.label2.ForeColor = Color.Gray;
this.textBox1.ForeColor = Color.Gray;
this.textBox2.ForeColor = Color.Gray;
this.lblY0.ForeColor = Color.Gray;
this.btnClean.ForeColor = Color.Gray;
this.filterStata.ForeColor = Color.Gray;
this.lblY1.ForeColor = Color.Gray;
this.btnFilter.ForeColor = Color.Gray;
this.lblY2.ForeColor = Color.Gray;
this.cleanState.ForeColor = Color.Gray;
CheckForIllegalCrossThreadCalls = false;
}
private bool isEnableButton8 = true;
private bool isEnableButton5 = true ;
/**
* 清洗
*/
private void button5_Click(object sender, EventArgs e)
{
this.btnClean.Enabled = false;
this.btnFilter.Enabled = false;
this.cleanState.Text = "正常";
this.cleanState.ForeColor = Color.Black;
this.lblY0.ForeColor = Color.Black;
this.lblY2.ForeColor = Color.Black;
this.lblY1.ForeColor = Color.Gray;
timeData = 0;
try
{
theMachine = GetMachineFromMe.GetMachine;
SerialPortSetting.PortName = portName;
if (theMachine.StartConnection())
{
theMachine.AddClients(this);
theMachine.GetControllor.OpenY0();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.OpenY2();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY1();
if (!theMachine.StartReadingIO())
{
this.Close();
}
}
else
{
this.lblY0.ForeColor = Color.Gray;
this.lblY2.ForeColor = Color.Gray;
this.cleanState.Text = "异常";
}
this.btnClean.Enabled = true;
this.btnFilter.Enabled = true;
}
catch (Exception ex)
{
this.lblY0.ForeColor = Color.Gray;
this.lblY2.ForeColor = Color.Gray;
this.cleanState.Text = "异常";
Console.WriteLine("清洗异常:"+ex.StackTrace);
}
}
/**
* 过滤
*/
private void button8_Click(object sender, EventArgs e)
{
this.btnClean.Enabled = false;
this.btnFilter.Enabled = false;
this.filterStata.Text = "正常";
this.filterStata.ForeColor = Color.Black;
this.lblY1.ForeColor = Color.Black;
this.lblY2.ForeColor = Color.Black;
this.lblY0.ForeColor = Color.Gray;
timeData = 0;
try
{
theMachine = GetMachineFromMe.GetMachine;
SerialPortSetting.PortName = portName;
if (theMachine.StartConnection())
{
theMachine.AddClients(this);
InitData();
}
else
{
this.lblY1.ForeColor = Color.Gray;
this.lblY2.ForeColor = Color.Gray;
this.filterStata.Text = "异常";
}
this.btnFilter.Enabled = true;
this.btnClean.Enabled = true;
}
catch (Exception ex)
{
this.lblY1.ForeColor = Color.Gray;
this.lblY2.ForeColor = Color.Gray;
Console.WriteLine("=====异常" + ex.StackTrace);
this.filterStata.Text = "异常";
}
}
private void InitData()
{
table1 = new DataTable();
DataColumnCollection columns = table1.Columns;
//先把Y1,Y2打开,D(也就是电导率)读数,其余关
theMachine.GetControllor.OpenY1();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.OpenY2();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY0();
if (!theMachine.StartReading())
{
this.Close();
}
}
private Byte[] _ReceiveValue;
private System.Timers.Timer timerStart;
public void NewReceiveData(int receivedCount, Byte[] receiveValue)
{
//try
//{
if (!isStop)
{
_ReceiveValue = receiveValue;
double tempData = ChangeData();
Console.WriteLine("=====time:" + timeData + ",data:" + tempData);
this.textBox1.Text = tempData.ToString();
this.textBox2.Text = timeData.ToString();
if (!_IsRealStart)//如果是过滤,则判断是否过过滤上限
{
JudgeWashUpperLimit(tempData);
}
}
}
private double ChangeData()
{
//转化前6为电导率数
double returnValue = 0;
string temp = "";
int index = 6;
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);
timeData++;
return returnValue;
}
private void JudgeWashUpperLimit(double tempData)
{
if (tempData >= washUpperLimit)//数据超过上限
{
if (timerWash >= 20)//时间超过20s
{
if (MessageBox.Show("过滤完成!") == DialogResult.Yes)
{
closeMachine();
}
}
else
{
timerWash++;
}
}
}
/**
* 测试
*/
private void button2_Click(object sender, EventArgs e)
{
if (portName == "")
{
MessageBox.Show("请先设置通信口");
}
else
{
this.btnClean.Enabled = true;
this.btnFilter.Enabled = true;
this.groupBox1.ForeColor = Color.Black;
this.label1.ForeColor = Color.Black;
this.label2.ForeColor = Color.Black;
this.textBox1.ForeColor = Color.Black;
this.textBox2.ForeColor = Color.Black;
this.btnClean.ForeColor = Color.Black;
this.btnFilter.ForeColor = Color.Black;
}
}
private void button3_Click(object sender, EventArgs e)
{
closeMachine();
this.groupBox1.ForeColor = Color.Gray;
this.label1.ForeColor = Color.Gray;
this.label2.ForeColor = Color.Gray;
this.textBox1.ForeColor = Color.Gray;
this.textBox2.ForeColor = Color.Gray;
this.lblY0.ForeColor = Color.Gray;
this.btnClean.ForeColor = Color.Gray;
this.filterStata.ForeColor = Color.Gray;
this.lblY1.ForeColor = Color.Gray;
this.btnFilter.ForeColor = Color.Gray;
this.lblY2.ForeColor = Color.Gray;
this.cleanState.ForeColor = Color.Gray;
}
private void closeMachine()
{
if (this.theMachine != null)
{
theMachine.StopReading();
theMachine.GetControllor.CloseY0();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY2();
System.Threading.Thread.Sleep(1500);
theMachine.GetControllor.CloseY1();
isStop = true;
}
}
/**
* 校准按钮
*/
private void button11_Click(object sender, EventArgs e)
{
if (portName != "")
{
//this.Close();
adjustingForm af = new adjustingForm(portName, "AdjustingTest");
af.Show();
//af.Activate();
}
else
{
MessageBox.Show("请先设置端口");
}
}
private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
{
closeMachine();
}
/**
* 循环测试
*/
private void btnLoopTest_Click(object sender, EventArgs e)
{
if (portName != "")
{
adjustingForm af = new adjustingForm(portName, "LoopTest");
af.Show();
}
else
{
MessageBox.Show("请先设置端口");
}
}
}
}