MainMachine_Counting.cs
22.1 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
using Asa;
using CodeLibrary;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary
{
partial class MainMachine
{
public string xrayImagePath;
Task GetResultTask = null;
Dictionary<string, int> CountList = new Dictionary<string, int>();
void CountingMoveProcess()
{
if (CheckWait(CountMoveInfo))
return;
switch (CountMoveInfo.MoveStep)
{
case MoveStep.Wait:
//if (IOValue(IO_Type.Counting_Reel_Check).Equals(IO_VALUE.HIGH) && IOValue(IO_Type.Counting_EnterDoor_Close).Equals(IO_VALUE.HIGH) && IOValue(IO_Type.Counting_ExitDoor_Close).Equals(IO_VALUE.HIGH))
//{
// CountMoveInfo.NextMoveStep(MoveStep.Count_01);
//}
break;
case MoveStep.Count_01:
if (IOValue(IO_Type.Counting_Reel_Check).Equals(IO_VALUE.HIGH))
{
CountMoveInfo.NextMoveStep(MoveStep.Count_02);
CylinderMove(CountMoveInfo, IO_Type.Counting_EnterDoor_Close, IO_Type.Counting_EnterDoor_Open, IO_VALUE.LOW);
CylinderMove(CountMoveInfo, IO_Type.Counting_ExitDoor_Close, IO_Type.Counting_ExitDoor_Open, IO_VALUE.LOW);
CountMoveInfo.log($"关闭防护门");
if (CountMoveInfo.MoveParam.PlateH == 0)
{
CountMoveInfo.MoveParam.PlateH = Setting_Init.Temp_Last_Reel_Height;
CountMoveInfo.MoveParam.PlateW = Setting_Init.Temp_Last_Reel_Width;
CountMoveInfo.log($"读取到存储的料盘尺寸:{CountMoveInfo.MoveParam.PlateW}x{CountMoveInfo.MoveParam.PlateH}");
}
else if (CountMoveInfo.MoveParam.PlateH > 0)
{
Setting_Init.Temp_Last_Reel_Height = CountMoveInfo.MoveParam.PlateH;
Setting_Init.Temp_Last_Reel_Width = CountMoveInfo.MoveParam.PlateW;
}
else
{
Setting_Init.Temp_Last_Reel_Height = 8;
}
}
else {
Msg.add("点料机放料位没有检测到料盘",MsgLevel.alarm);
RobotManage.UserPause("点料机放料位没有检测到料盘");
}
break;
case MoveStep.Count_02:
CountMoveInfo.NextMoveStep(MoveStep.Count_03);
IOMove(IO_Type.Xray_Lock, IO_VALUE.HIGH);
ScanCode();
CountMoveInfo.log($"开始扫码");
break;
case MoveStep.Count_03:
CountMoveInfo.NextMoveStep(MoveStep.Count_04);
int retrytime = 0;
retry:
if (RobotManage.XRay.Start())
{
CountMoveInfo.log($"打开X光");
CountMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(Setting_Init.XRay_Light_Delay));
}
else {
if (RobotManage.XRay.Start())
{
CountMoveInfo.log($"打开X光第2次");
CountMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(Setting_Init.XRay_Light_Delay));
return;
}
CountMoveInfo.log($"X光打开失败");
retryopen:
retrytime++;
RobotManage.XRay.Close();
Thread.Sleep(1000);
if (!RobotManage.XRay.Open(Setting_Init.XRay_Port, string.Format("{0:yyyy-MM-dd}", DateTime.Now)))
{
if (retrytime > 3)
{
Msg.add("X光打开失败", MsgLevel.warning);
RobotManage.UserPause("X光打开失败");
}
goto retryopen;
}
Thread.Sleep(1000);
if (!RobotManage.XRay.SetVC(Setting_Init.XRay_Voltage.ToString(), Setting_Init.XRay_Current.ToString()))
{
CountMoveInfo.log(string.Format("X光电压电流设置失败:{0},{1}", Setting_Init.XRay_Voltage, Setting_Init.XRay_Current));
goto retryopen;
}
Thread.Sleep(1000);
goto retry;
}
break;
case MoveStep.Count_04:
if (ScanTask.IsCompleted)
{
var (x,s) = ScanTask.Result;
if (x.Count == 0)
{
CountMoveInfo.NextMoveStep(MoveStep.Count_ReadyOut);
CountMoveInfo.log($"未识别到有效二维码,送到NG口");
CountMoveInfo.MoveParam.IsNg = true;
CountMoveInfo.MoveParam.NgMsg = crc.GetString(L.not_detect_reel_code, "未识别到有效二维码");
return;
}
else
{
CountMoveInfo.NextMoveStep(MoveStep.Count_05);
CountMoveInfo.MoveParam.IsNg = false;
CountMoveInfo.MoveParam.codeInfos = x;
CountMoveInfo.MoveParam.bitmapfilename = s;
var cc = x.Select((a) => a.CodeType+":"+a.CodeStr);
LastCode = String.Join("##", cc);
CountMoveInfo.log("已完成扫码 Code:" + LastCode);
//Common.codeProcess(CountMoveInfo.MoveParam, out string mmm);
CountMoveInfo.MoveParam.WareCode = CountMoveInfo.MoveParam.codeInfos.Where(c=>c.CodeStr.StartsWith("S20")).FirstOrDefault().CodeStr;
CountMoveInfo.MoveParam.ReeID = CountMoveInfo.MoveParam.WareCode;
CountMoveInfo.MoveParam.PN = CountMoveInfo.MoveParam.codeInfos.Where(c=>c.CodeStr.StartsWith("AT")).FirstOrDefault()?.CodeStr;
if (string.IsNullOrWhiteSpace(CountMoveInfo.MoveParam.PN)) {
var hc =CountMoveInfo.MoveParam.codeInfos.Where(c => Regex.IsMatch(c.CodeStr, @"(?:@@|-)(AT\w*)(?:@@|-)")).FirstOrDefault()?.CodeStr;
var pn = Regex.Match(hc, "@@(AT\\w*)@@").Groups[1].Value;
CountMoveInfo.MoveParam.PN = pn;
}
//var xx = CountMoveInfo.MoveParam.codeInfos.Find(c=> { return c.CodeStr.StartsWith("S20") && c.CodeType == "barcode"; });
//if (xx != null)
//{
// CountMoveInfo.MoveParam.codeInfos.Remove(xx);
// CountMoveInfo.log("移除了一个点料标签上的reelid");
//}
CountMoveInfo.log("codeProcess:" + CountMoveInfo.MoveParam.ReeID+";"+CountMoveInfo.MoveParam.PN);
}
}
else if (CountMoveInfo.IsTimeOut(15))
{
scantrytimes++;
if (scantrytimes < 3)
{
CountMoveInfo.log($"扫码超时,第{scantrytimes}次重试,拍照扫码");
ScanTask = null;
ScanCode();
}
else
{
CountMoveInfo.NextMoveStep(MoveStep.Count_ReadyOut);
CountMoveInfo.MoveParam.IsNg = false;
CountMoveInfo.MoveParam.NgMsg = "扫码超时";
CountMoveInfo.log($"等待扫码超时,NG口送出");
}
}
break;
case MoveStep.Count_05:
CountMoveInfo.NextMoveStep(MoveStep.Count_06);
GrabImageTask = GrabImageEX();
CountMoveInfo.log($"X光开始成像");
break;
case MoveStep.Count_06:
if (GrabImageTask.IsCompleted)
{
CountMoveInfo.NextMoveStep(MoveStep.Count_ReadyOut);
if (GrabImageTask.Result)
{
CountMoveInfo.log($"获取图像,开始点料 IsRayOpen:{RobotManage.XRay.IsRayOpen}");
GetResultTask = Task.Run(() => {
string reelid = CountMoveInfo.MoveParam.ReeID;
int count = GetCountResult();
lock (CountList)
{
CountList[reelid] = count;
}
CountMoveInfo.log($"点料完成:{reelid},{count}");
if (count == 0) {
CountMoveInfo.MoveParam.IsNg = true;
CountMoveInfo.MoveParam.NgMsg = "点料结果为0";
CountMoveInfo.MoveParam.logresult(xrayImagePath);
}
});
}
else
{
CountMoveInfo.MoveParam.IsNg = true;
CountMoveInfo.MoveParam.NgMsg = "获取图像失败";
CountMoveInfo.MoveParam.logresult();
}
CountMoveInfo.log($"关闭X光");
RobotManage.XRay.Stop();
IOMove(IO_Type.Xray_Lock, IO_VALUE.LOW);
CountMoveInfo.log("准备送出");
}
else if (CountMoveInfo.IsTimeOut(10)) {
Msg.add("获取料盘X光图像超时", MsgLevel.alarm);
RobotManage.UserPause("获取料盘X光图像超时");
}
break;
case MoveStep.Count_ReadyOut:
if (IOValue(IO_Type.Counting_Reel_Check).Equals(IO_VALUE.LOW))
{
CountMoveInfo.log("等待料盘被取走");
} else if (CountMoveInfo.IsTimeOut(60)) {
Msg.add("点料区等待料盘被取走超时",MsgLevel.alarm);
CountMoveInfo.log("等待料盘被取走超时");
}
break;
case MoveStep.Count_Outted:
CountMoveInfo.NextMoveStep(MoveStep.Count_20);
CountMoveInfo.log("料盘已取走");
break;
case MoveStep.Count_20:
if (IOValue(IO_Type.Counting_ExitDoor_Close).Equals(IO_VALUE.HIGH))
{
CountMoveInfo.NextMoveStep(MoveStep.Count_21);
CountMoveInfo.log("出口门已关闭");
}
else if (CountMoveInfo.IsTimeOut(60)) {
CountMoveInfo.log("等待出口门关闭");
}
break;
case MoveStep.Count_21:
CountMoveInfo.log("点料结束");
CountMoveInfo.EndMove();
break;
default:
CountMoveInfo.log($"未找到对应步骤:{CountMoveInfo.MoveStep}");
break;
}
}
string CountState()
{
string state = "";
if (CountMoveInfo.MoveStep == MoveStep.Wait)
{
return "空闲中";
}
else if (CountMoveInfo.MoveStep > MoveStep.Wait)
{
state = "取料位置" + ":" + InCurrentStringNum;
}
else if (CountMoveInfo.MoveStep > MoveStep.Wait)
{
state = "取料位置"+":"+InCurrentStringNum;
}
return state;
}
public bool IsScanRunning() {
if (ScanTask == null)
return false;
return !ScanTask.IsCompleted;
}
int scantrytimes = 0;
/// <summary>
/// 扫码线程
/// </summary>
Task<(List<CodeInfo>, string)> ScanTask;
public void ScanCode() {
CountMoveInfo.log("开始扫码");
try
{
ScanTask = Task.Run(new Func<(List<CodeInfo>, string)>(() =>
{
IOMove(IO_Type.Counting_Camera_Led, IO_VALUE.HIGH);
var (LastCodeList, filename) = CodeManager.CameraScan(CodeManager.hikNameList);
return (LastCodeList, filename);
}));
}
catch (Exception ex)
{
LogUtil.error("FI_13_ScanCode扫码出错:", ex);
}
}
const string xraydir = @"\Image\Xray";
Task<bool> GrabImageTask = null;
public Task<bool> GrabImageEX() {
return Task.Run(new Func<bool>(GrabImage));
}
[HandleProcessCorruptedStateExceptions]
public bool GrabImage()
{
Bitmap bmp = null;
try
{
CountMoveInfo.log("start get image from plate");
int xplate_getimage_retry_times = 5;
int xplate_open_retry_times = 3;
for (int ii = 1; ii <= xplate_getimage_retry_times; ii++)
{
bool rtn = RobotManage.xrayImage.GetImage(1);
if (!rtn)
{
if (ii == xplate_getimage_retry_times)
return false;
for (int jj = 1; jj <= xplate_open_retry_times; jj++)
{
bool closeR = RobotManage.xrayImage.Close();
if (!closeR)
CountMoveInfo.log($"xray plate Close ii={ii}, times={jj}, result={closeR}");
Thread.Sleep(500 * jj + (jj - 1) * 1000);
rtn = RobotManage.xrayImage.Open();
Thread.Sleep(500);
if (!rtn)
{
CountMoveInfo.log($"xray plate open failed ii={ii}, times={jj}");
if (jj == xplate_open_retry_times)
return false;
continue;
}
else
break;
}
Thread.Sleep(1000 * ii + (ii - 1) * 2000);
}
else
break;
}
CountMoveInfo.log("get image from plate scusses");
//RobotManage.xrayImage.WindowWidth = 30000;
//RobotManage.xrayImage.WindowLevel = 15000;
bmp = RobotManage.xrayImage.Get48bImage();
Directory.CreateDirectory(xraydir);
var n = DateTime.Now;
string backDir = Path.Combine(xraydir, "back", n.Year.ToString() + "-" + n.Month.ToString(), n.Day.ToString(), n.Hour.ToString());
Directory.CreateDirectory(backDir);
xrayImagePath = Path.Combine(backDir, CountMoveInfo.MoveParam.GetImgName());
bmp.Save(xrayImagePath, System.Drawing.Imaging.ImageFormat.Png);
// File.Copy(xrayImagePath, backDir);
CountMoveInfo.log($"save xray pic success. current file:{xrayImagePath}");
return true;
}
catch (Exception ex)
{
CountMoveInfo.log(ex.ToString());
return false;
}
finally
{
if (bmp != null)
bmp.Dispose();
//StepImage?.Invoke(bmp);
}
}
[HandleProcessCorruptedStateExceptions]
public int GetCountResult(bool dontlog = false)
{
//返回的数量是string类型,count = "4000,3000,3500,2000"
int count = 0;
//resfile = "";
//tpDstImg = new API.EyemImage();
try
{
int[] countStr = new int[4];
int result = 0;
int ShrinkOffset = ConfigHelper.Config.Get<int>("ShrinkOffset", 100);
var pps = CountMoveInfo.MoveParam.PN;
if (string.IsNullOrEmpty(pps))
pps = "";
var type = Pn_Algo_Match.MatchPN(pps, out _, out double? finetuning);
CountMoveInfo.log($"匹配到算法.{pps}={type}, finetuning={finetuning}");
CountMoveInfo.MoveParam.Algo = type;
if (finetuning != null)
{
XrayImage.setFineTuning = finetuning.Value;
}
else
{
XrayImage.setFineTuning = ConfigHelper.Config.Get("setFineTuningDefault", 0.7);
}
if (type.StartsWith("IP_Template_PARTS"))
{
if (!File.Exists(type.Substring(17 + 1)))
{
CountMoveInfo.log($"模版文件丢失.{type.Substring(17 + 1)}");
type = "auto";
}
}
Bitmap dst = null;
if (type == "auto")
{
result = XrayImage.GetLocalCount(xrayImagePath, ShrinkOffset, out countStr, out dst);
CountMoveInfo.log("GetCountResult " + type + " 调用 GetLocalCount 【" + xrayImagePath + "】,返回【" + result + "】,结果【" + string.Join(",", countStr) + "】");
}
else if (type.StartsWith("IP_Template_PARTS"))
{
string template = "";
if (type.Length > 18)
{
template = type.Substring(17 + 1);
}
result = XrayImage.GetLocalCountTemplate(xrayImagePath, ShrinkOffset, template, out countStr, out dst);
CountMoveInfo.log("GetCountResult " + template + " 调用 GetLocalCountTemplate 【" + xrayImagePath + "】,返回【" + result + "】,结果【" + string.Join(",", countStr) + "】");
}
else
{
result = XrayImage.GetLocalCountIrregular(xrayImagePath, ShrinkOffset, type.ToString(), out countStr, out dst);
CountMoveInfo.log("GetCountResult " + type + " 调用 GetLocalCountIrregular 【" + xrayImagePath + "】,返回【" + result + "】,结果【" + string.Join(",", countStr) + "】");
}
if (dst != null)
{
dst.Save("ResOut\\" + Path.GetFileNameWithoutExtension(xrayImagePath) + "-Mark.png");
dst.Dispose();
}
if (result != 0)
{
//CountMoveInfo.MoveParam.IsNg = true;
//CountMoveInfo.MoveParam.NgMsg = "点料失败";
//CountMoveInfo.MoveParam.logresult(xrayImagePath);
return count;
}
if (countStr != null)
{
//string[] array = countStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
count = (from a in countStr where a > 0 select a).FirstOrDefault();
}
else
{
count = 0;
}
if (result == 0 && count > 0)
{
CountMoveInfo.MoveParam.QTY = count;
string resfilename = Path.GetFileNameWithoutExtension(xrayImagePath) + "-Mark.png";
string root = Path.Combine(Application.StartupPath, "ResOut");
var n = DateTime.Now;
Directory.CreateDirectory("\\Image\\Xray\\ResOut");
string destdir = Path.Combine("\\Image\\Xray\\ResOut", n.Year.ToString() + "-" + n.Month.ToString(), n.Day.ToString(), n.Hour.ToString());
Directory.CreateDirectory(destdir);
string resfile = Path.Combine(destdir, resfilename);
if (File.Exists(resfile))
File.Delete(resfile);
Directory.Move(Path.Combine(root, resfilename), resfile);
CountMoveInfo.MoveParam.logresult(xrayImagePath, resfile);
}
else
{
//CountMoveInfo.MoveParam.IsNg = true;
//CountMoveInfo.MoveParam.NgMsg = "点料结果为0";
//CountMoveInfo.MoveParam.logresult(xrayImagePath);
}
}
catch (Exception ex)
{
CountMoveInfo.log("GetCountResult 出错: " + ex.ToString());
}
return count;
}
}
}