XrayControl.cs
16.3 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
using Asa;
using DeviceLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutoCountMachine
{
using crc = OnlineStore.CodeResourceControl;
public partial class XrayControl : UserControl
{
readonly System.Windows.Forms.Timer t1 = new System.Windows.Forms.Timer();
DeviceConfig _Config;
public DeviceConfig Config
{
get { return _Config; }
set
{
_Config = value;
ioControl1.Config = value;
configControl1.Config = value;
axisMoveControl1.LoadData(value);
LoadADIOList();
}
}
public XrayControl()
{
InitializeComponent();
t1.Stop();
t1.Interval = 500;
t1.Tick += T1_Tick;
GC.KeepAlive(t1);
countstate += XrayControl_countstate;
textBox_scancode.KeyUp += TextBox_scancode_KeyUp;
textBox_scancode.Leave += TextBox_scancode_Leave;
textBox_scancode.Click += TextBox_scancode_Click;
tabPage5.GotFocus += TabPage5_GotFocus;
tabPage5.MouseDown += TabPage5_MouseDown;
RoleManger.RoleChange += RoleManger_RoleChange;
}
private void RoleManger_RoleChange(object sender, Role e)
{
tabPage2.Controls[0].Enabled = false;
tabPage4.Controls[0].Enabled = false;
if (e != Role.Admin)
return;
tabPage2.Controls[0].Enabled = true;
tabPage4.Controls[0].Enabled = true;
}
private void TextBox_scancode_Click(object sender, EventArgs e)
{
textBox_scancode.SelectAll();
}
private void TextBox_scancode_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab || e.KeyCode == Keys.Enter)
{
textBox_scancode.Text = textBox_scancode.Text.Trim();
textBox_scancode.SelectAll();
}
}
private void TabPage5_MouseDown(object sender, MouseEventArgs e)
{
textBox_scancode.Focus();
}
private void TabPage5_GotFocus(object sender, EventArgs e)
{
textBox_scancode.Focus();
}
private void TextBox_scancode_Leave(object sender, EventArgs e)
{
textBox_scancode.Focus();
}
private void XrayControl_Load(object sender, EventArgs e)
{
}
private void XrayControl_countstate(object sender, string e)
{
LogUtil.info($"手动点料: {e}");
this.Invoke((EventHandler<string>)delegate
{
label_countstate.Text = crc.GetString("xmanual_current_step","当前: {0}",e);
},this,e);
}
private void T1_Tick(object sender, EventArgs e)
{
if (this.Visible)
{
ReadADIOList();
}
if (((Control)tabPage5).Visible) {
if (!ManualCountRunning && RobotManage.xrayMachine.IOValue(IO_XRay_Type.Start_BTN).Equals(IO_VALUE.HIGH))
{
btn_ManualCount_Click(sender, e);
}
}
}
private void LoadADIOList()
{
//ListViewItem lvi = new ListViewItem(new string[] { "", msg.datetime.ToString(), msg.msgtxt });
//listView_adio.Columns.Add("名称",200);
listView_adio.Columns.Add("Define", 150);
listView_adio.Columns.Add("Value");
listView_adio.Columns.Add("Base");
listView_adio.Columns.Add("Result");
t1.Start();
}
private void ReadADIOList()
{
this.SuspendLayout();
listView_adio.Items.Clear();
foreach (ConfigIO ioValue in Config.ADIOList.Values)
{
var pi = Config.GetType().GetProperty(ioValue.ProName + "_base");
var basevalue = (int)pi.GetValue(Config);
var v = IOManager.GetADIOValue("HC", 0, ushort.Parse(ioValue.ProValue));
var Tray_ADIO_Value = ((Config_XRay)Config).Tray_ADIO_Value;
var c = ((int)v- basevalue) / Tray_ADIO_Value;
ListViewItem lvi = new ListViewItem(new string[] { /*ioValue.Explain,*/ioValue.ProName, v.ToString(), basevalue.ToString(), c.ToString() });
listView_adio.Items.Add(lvi);
}
this.ResumeLayout(true);
}
private void btn_openXray_Click(object sender, EventArgs e)
{
if (RobotManage.xrayMachine.IOValue(IO_XRay_Type.Entry_Close).Equals(IO_VALUE.HIGH) && RobotManage.xrayMachine.IOValue(IO_XRay_Type.Exit_Close).Equals(IO_VALUE.HIGH))
{
RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.HIGH);
RobotManage.XRay.Start();
}
else {
MessageBox.Show(crc.GetString("xmanual_door_not_closed","X光机防护门未关闭,不可开启X光"));
}
}
private void btn_getXrayimage_Click(object sender, EventArgs e)
{
RobotManage.xrayMachine.GrabImage();
pictureBox1.Image = new Bitmap(RobotManage.xrayMachine.xrayImagePath);
}
private void btn_closeXray_Click(object sender, EventArgs e)
{
RobotManage.XRay.Stop();
RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.LOW);
}
private void btn_Reset_Click(object sender, EventArgs e)
{
if (RobotManage.xrayMachine.runStatus != RunStatus.HomeReset)
{
RobotManage.xrayMachine.BeginHomeReset();
}
}
event EventHandler<string> countstate;
private void btn_ManualCount_Click(object sender, EventArgs e)
{
if (RobotManage.isRunning) {
countstate?.Invoke(this, crc.GetString("xmanual_machine_is_running","机器正在运行中,不能使用手动点料"));
}
else if (CodeManager.HasRightCode(textBox_scancode.Text))
{
RobotManage.xrayMachine.MoveInfo.ReelParam = new ReelParam();
RobotManage.xrayMachine.MoveInfo.ReelParam.WareCode = textBox_scancode.Text;
countstate?.Invoke(this, crc.GetString("xmanual_being_countting",$"开始点料:{0} ", textBox_scancode.Text));
Thread td = new Thread(new ThreadStart(Count));
td.Start();
}
else {
countstate?.Invoke(this, crc.GetString("xmanual_barcode_uncorrect", "扫描的二维码不符合规则,请重新扫描"));
}
}
/// <summary>
/// 手动点料运行中
/// </summary>
bool ManualCountRunning = false;
string LastCountWareCode = "";
void Count()
{
if (ManualCountRunning)
return;
ManualCountRunning = true;
countstate?.Invoke(this, crc.GetString("xmanual_close_door","关闭防护门"));
RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Entry_Close, IO_XRay_Type.Entry_Open, IO_VALUE.LOW);
RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Exit_Close, IO_XRay_Type.Exit_Open, IO_VALUE.LOW);
int wait = 0;
while (true) {
if (RobotManage.xrayMachine.IOValue(IO_XRay_Type.Entry_Close).Equals(IO_VALUE.HIGH)
&& RobotManage.xrayMachine.IOValue(IO_XRay_Type.Exit_Close).Equals(IO_VALUE.HIGH)) {
break;
}
wait++;
Thread.Sleep(500);
countstate?.Invoke(this, crc.GetString("xmanual_wait_close_door", "等待关闭防护门"));
if (wait > 10) {
countstate?.Invoke(this, crc.GetString("xmanual_wait_close_door_timeout", "等待关闭防护门超时"));
ManualCountRunning = false;
return;
}
}
try
{
LastCountWareCode = RobotManage.xrayMachine.MoveInfo.ReelParam.WareCode;
countstate?.Invoke(this, crc.GetString("xmanual_open_xray", "打开X光"));
RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.HIGH);
Thread.Sleep(1000);
RobotManage.XRay.Start();
Thread.Sleep(1000);
countstate?.Invoke(this, crc.GetString("xmanual_get_image", "获取图像"));
if (RobotManage.xrayMachine.GrabImage())
{
Task.Run(() =>
{
countstate?.Invoke(this, crc.GetString("xmanual_being_countting", "开始点料"));
Process p = new Process();
p.StartInfo = new ProcessStartInfo("AlgoMatch\\AlgoMatch.exe");
p.StartInfo.Arguments =$"singlecount \"{RobotManage.xrayMachine.xrayImagePath}\" {crc.CurrLanguage}";
p.StartInfo.WorkingDirectory = Application.StartupPath;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += P_OutputDataReceived;
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
Environment.CurrentDirectory = Application.StartupPath;
XrayImage.ReloadTpl();
Pn_Algo_Match.LoadFile();
p.Dispose();
//countstate?.Invoke(this, "完成点料");
});
}
else
{
countstate?.Invoke(this, crc.GetString("xmanual_get_image_fail", "获取图像失败!"));
}
}
catch (Exception e){
countstate?.Invoke(this, crc.GetString("xmanual_error", "出错:{0}" ,e.ToString()));
}
finally
{
RobotManage.XRay.Stop();
Thread.Sleep(300);
RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.LOW);
countstate?.Invoke(this, crc.GetString("xmanual_close_xray", "关闭X光"));
Thread.Sleep(100);
}
Thread.Sleep(100);
RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Entry_Close, IO_XRay_Type.Entry_Open, IO_VALUE.HIGH);
RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Exit_Close, IO_XRay_Type.Exit_Open, IO_VALUE.HIGH);
countstate?.Invoke(this, crc.GetString("xmanual_door_opened", "防护门已打开"));
ManualCountRunning = false;
}
private void P_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data!=null && e.Data.StartsWith("CountResult"))
{
LogUtil.info($"收到手动点料确认结果:{e.Data}");
int count = int.Parse(e.Data.Substring(12));
var cl = ServerConn.inputCounterDataByXRayMachine(LastCountWareCode, count);
if (cl != null)
{
countstate?.Invoke(this, cl.message);
var reelLocation = ServerConn.DetermineReelStorageLocation(LastCountWareCode);
if (reelLocation == null)
{
countstate?.Invoke(this, crc.GetString("xmanual_print_label_fail","打印标签失败"));
}
else {
countstate?.Invoke(this, reelLocation.message);
}
}
else {
countstate?.Invoke(this, crc.GetString("xmanual_upload_fail", "上传点料结果失败"));
}
}
}
private void textBox_scancode_TextChanged(object sender, EventArgs e)
{
}
private void btn_Calibration_Click(object sender, EventArgs e)
{
if (RobotManage.isRunning)
{
Invoke(new Action(() => MessageBox.Show("Machine is running, Please stop.")));
return;
}
Thread td = new Thread(new ThreadStart(Calibration));
td.Start();
}
void Calibration() {
if (ManualCountRunning)
return;
ManualCountRunning = true;
countstate?.Invoke(this, crc.GetString("xmanual_close_door", "关闭防护门"));
RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Entry_Close, IO_XRay_Type.Entry_Open, IO_VALUE.LOW);
RobotManage.xrayMachine.CylinderMove(null, IO_XRay_Type.Exit_Close, IO_XRay_Type.Exit_Open, IO_VALUE.LOW);
int wait = 0;
while (true)
{
if (RobotManage.xrayMachine.IOValue(IO_XRay_Type.Entry_Close).Equals(IO_VALUE.HIGH)
&& RobotManage.xrayMachine.IOValue(IO_XRay_Type.Exit_Close).Equals(IO_VALUE.HIGH))
{
break;
}
wait++;
Thread.Sleep(500);
//countstate?.Invoke(this, crc.GetString("xmanual_wait_close_door", "等待关闭防护门"));
if (wait > 10)
{
//countstate?.Invoke(this, crc.GetString("xmanual_wait_close_door_timeout", "等待关闭防护门超时"));
ManualCountRunning = false;
return;
}
}
Invoke(new Action(() =>MessageBox.Show("Start Calibration. Wait 30s, Please")));
if (Directory.Exists("template"))
Directory.Delete("template", true);
var a = RobotManage.xrayImage.GenerateTemplate(1);
a.Wait();
if (a.Result)
{
LogUtil.info("暗场校准成功");
try
{
RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.HIGH);
Task.Delay(1000).Wait();
RobotManage.XRay.Start();
Task.Delay(5000).Wait();
var b = RobotManage.xrayImage.GenerateTemplate(2);
b.Wait();
if (b.Result)
{
LogUtil.info("亮场校准成功");
}
}
finally
{
RobotManage.XRay.Stop();
RobotManage.xrayMachine.IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.LOW);
}
}
RobotManage.xrayImage.Close();
Task.Delay(1000).Wait();
RobotManage.xrayImage.Open();
Invoke(new Action(() => MessageBox.Show("Calibration Finish.")));
ManualCountRunning = false;
}
private void btn_linesync_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
var Line_Entry_Axis = RobotManage.xrayMachine.Line_Entry_Axis;
var Line_In_Axis = RobotManage.xrayMachine.Line_In_Axis;
var Config = RobotManage.xrayMachine.Config;
Line_Entry_Axis.Open(true,out _);
Line_In_Axis.Open(true, out _);
Line_Entry_Axis.SpeedMove(Config.Line_Entry_Relative_speed);
Line_In_Axis.RelMove(null, Config.Line_In_Relative, Config.Line_In_Relative_speed);
RobotManage.xrayMachine.IOMove(IO_XRay_Type.ExitLine_Reel_Run, IO_VALUE.HIGH);
Task.Delay(200).Wait();
while (Line_In_Axis.IsBusy) {
Task.Delay(100).Wait();
}
Task.Delay(500).Wait();
Line_Entry_Axis.SuddenStop();
RobotManage.xrayMachine.IOMove(IO_XRay_Type.ExitLine_Reel_Run, IO_VALUE.LOW);
});
}
}
}