革博士程序V1仓库
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1994 Zeilen
106 KiB

  1. using IKapC.NET;
  2. using LeatherApp.Device;
  3. using LeatherApp.Device.CamerUtil;
  4. using LeatherApp.Interface;
  5. using LeatherApp.Utils;
  6. using Models;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using OpenCvSharp;
  10. using OpenCvSharp.Extensions;
  11. using S7.Net;
  12. using SqlSugar;
  13. using Sunny.UI;
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.ComponentModel;
  18. using System.Data;
  19. using System.Diagnostics;
  20. using System.Drawing;
  21. using System.Drawing.Imaging;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Reflection;
  25. using System.Text;
  26. using System.Threading;
  27. using System.Threading.Tasks;
  28. using System.Windows.Forms;
  29. namespace LeatherApp.Page
  30. {
  31. public partial class FHome : UIPage
  32. {
  33. private WarningEnum warningLevel;//警告等级
  34. private CurrentStateEnum currentState;//当前状态
  35. private DevContainer devContainer = new DevContainer();
  36. private Service.ProductService svcProduct = new Service.ProductService();
  37. private Service.RecordsService svcRecord = new Service.RecordsService();
  38. private bool defectPauseForUser = false;
  39. //private Models.Product currProductModel = null;//当前产品
  40. //private Models.Records record = null;
  41. private Stopwatch pStopWatch=new Stopwatch();//计算速度用,暂停时停止计数
  42. private object lockScanPhoto = new object();
  43. private object lockCurrKey = new object();
  44. private int currKey=0;
  45. private Hashtable htTask = new Hashtable();//默认单线程写入不用lock, 多线程安全同步读取用Synchronized
  46. //无产品编码时加载
  47. FProductInfo frmProduct;
  48. public FHome(FProductInfo frm)
  49. {
  50. InitializeComponent();
  51. frmProduct = frm;
  52. if(Config.Camer_Name == CamerDevNameEnum.埃科)
  53. IKapCLib.ItkManInitialize();
  54. #region dataGridView设置
  55. uiDataGridView1.AllowUserToAddRows = uiDataGridView1.AllowUserToDeleteRows = false;//用户添加删除行
  56. uiDataGridView1.AllowUserToResizeRows = false;//用户调整行大小
  57. uiDataGridView1.AllowUserToResizeColumns = false;//用户调整列大小
  58. uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//只可选中整行,不是单元格
  59. //显示行号与列宽度自动调整
  60. uiDataGridView1.RowHeadersVisible = true;
  61. uiDataGridView1.RowHeadersWidth = 60;
  62. uiDataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  63. uiDataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
  64. uiDataGridView1.RowPostPaint += (sender, e) =>//序号列头
  65. {
  66. Utils.Util.showRowNum_onDataGrid_RowPostPaint(this.uiDataGridView1, sender, e);
  67. //Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, uiDataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
  68. //TextRenderer.DrawText(e.Graphics, (e.RowIndex).ToString(), uiDataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, uiDataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  69. };
  70. for (int i = 0; i < uiDataGridView1.Columns.Count; i++)//禁止点击列头排序
  71. uiDataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  72. ////行列交叉处标题
  73. //if (dataGridView1.RowHeadersVisible) dataGridView1.TopLeftHeaderCell.Value = "SPH/CYL";
  74. //事件
  75. //this.uiDataGridView1.DataBindingComplete += this.uiDataGridView1_DataBindingComplete;//bing data时发生,可修改单元格内容
  76. //this.uiDataGridView1.SelectIndexChange += uiDataGridView1_SelectIndexChange;//选择行时发行
  77. this.uiDataGridView1.CellClick += uiDataGridView1_CellClick; ;
  78. #endregion
  79. this.ucColorListDefect.ColorChanged = (xcode, xcolor) =>
  80. {
  81. try
  82. {
  83. var item=Config.getDefectItem(xcode);
  84. if(item != null)
  85. {
  86. item["color"] = xcolor;
  87. Config.SaveDefectItemList(Config.defectItemList);
  88. }
  89. }
  90. catch (Exception ex)
  91. {
  92. this.AddTextEvent(DateTime.Now,"事件", "缺陷颜色修改后保存失败!");
  93. }
  94. };
  95. }
  96. private void uiDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  97. {
  98. if (e.RowIndex < 0 || currKey == 0)
  99. return;
  100. Records record = Hashtable.Synchronized(htTask)[currKey] as Records;
  101. //var defectInfo = record.DefectInfoList[record.DefectInfoList.Count - e.RowIndex-1];//按顺序索引引用
  102. var defectInfo = record.DefectInfoList.FirstOrDefault(m => m.uid == (long)this.uiDataGridView1.CurrentRow.Cells["colUid"].Value);
  103. if(defectInfo == null)
  104. {
  105. UIMessageTip.ShowError("此记录已不存在!", 1000);
  106. return;
  107. }
  108. this.picDefectImage.loadImage(defectInfo.image);
  109. //选中
  110. this.Invoke(new System.Action(() =>
  111. {
  112. if (lineChartDefect.Option.Series != null && lineChartDefect.Option.Series.ContainsKey("SELECT"))
  113. {
  114. lineChartDefect.Option.Series["SELECT"].Clear();
  115. lineChartDefect.Option.Series["SELECT"].Add( defectInfo.CentreX, defectInfo.CentreY / 100);
  116. lineChartDefect.Refresh();
  117. }
  118. }));
  119. }
  120. private void lineChartDefect_Click(object sender, EventArgs e)
  121. {
  122. if (lineChartDefect.Option.Series != null && lineChartDefect.Option.Series.ContainsKey("SELECT") && lineChartDefect.Option.Series["SELECT"].DataCount > 0)
  123. {
  124. lineChartDefect.Option.Series["SELECT"].Clear();
  125. lineChartDefect.Refresh();
  126. }
  127. }
  128. #region 内部方法
  129. private void resetUIValue(bool resetCurrKey=true)
  130. {
  131. firstTest = true;
  132. if(resetCurrKey)
  133. currKey = 0;
  134. pStopWatch.Reset();
  135. this.Invoke(new System.Action(() =>
  136. {
  137. lblLen.Text = "0米";
  138. lblSpeed.Text = "速度:0米/分";
  139. txtBarCodeName.Text = txtBatchId.Text = txtReelId.Text = "";
  140. numErpLen.Text = "0";
  141. this.lineChartDefect.SetOption(new UILineOption());
  142. this.lineChartFaceWidth.SetOption(new UILineOption());
  143. this.uiDataGridView1.DataSource = null;
  144. this.uiDataGridView1.Rows.Clear();
  145. this.lstboxLog.Items.Clear();
  146. this.picDefectImage.clear();
  147. this.picScanner1.Image = this.picScanner2.Image = null;
  148. picScanner1.Refresh();
  149. picScanner2.Refresh();
  150. }));
  151. }
  152. /// <summary>
  153. /// 全局中断
  154. /// </summary>
  155. private void globalBreakEvent(int portIndex, byte data)
  156. {
  157. AddTextEvent(DateTime.Now,"I/0状态", $"{portIndex}:{Convert.ToString(data, 2)}");
  158. if (compareIOInput(CMDName.启动按钮) && this.btnStart.Enabled)
  159. {
  160. this.devContainer.devIOCard.writeBitState(0, 0, true);
  161. this.startCommand();
  162. Task.Run(async () =>
  163. {
  164. await Task.Delay(500);
  165. this.devContainer.devIOCard.writeBitState(0, 0, false);
  166. });
  167. }
  168. else if (compareIOInput(CMDName.暂停按钮) && this.btnPause.Enabled)
  169. {
  170. this.devContainer.devIOCard.writeBitState(0, 1, true);
  171. this.pauseCommand();//true 输出暂停不会触发输入暂停
  172. Task.Run(async () =>
  173. {
  174. await Task.Delay(500);
  175. this.devContainer.devIOCard.writeBitState(0, 1, false);
  176. });
  177. }
  178. //else if (compareIOInput(CMDName.复位按钮) && this.tsbtnReset.Enabled)
  179. // resetCommand();
  180. //else if (!this.disableBuzzer && compareIOInput(CMDName.门磁告警输入))
  181. // warning(WarningEnum.Low, true);
  182. //else if (!this.disableBuzzer && compareIOInput(CMDName.喷墨告警输入))
  183. // warning(WarningEnum.Low, true);
  184. }
  185. private bool compareIOInput(CMDName key)
  186. {
  187. JObject joJson = Config.CMDProcess[key];
  188. IODirectionEnum direction = (IODirectionEnum)joJson.Value<int>("Direction");
  189. if (direction == IODirectionEnum.输入 || direction == IODirectionEnum.输入输出)
  190. {
  191. return Util.compareIOInput(
  192. joJson.Value<JArray>("IN_OP_SHOW").ToObject<List<string>>().ToArray(),
  193. devContainer.devIOCard.DIData);
  194. }
  195. return false;
  196. }
  197. /// <summary>
  198. /// 报警,只响应low,high
  199. /// </summary>
  200. private void warning(WarningEnum level, bool buzzer = true)
  201. {
  202. if (level == WarningEnum.Normal)
  203. return;
  204. //lock (myLock)
  205. warningLevel = level;
  206. //if ((int)level >= (int)WarningEnum.Low)//暂停
  207. {
  208. //currentState = CurrentStateEnum.暂停;
  209. if (!Config.StopPLC)
  210. this.devContainer.devPlc.pauseDev();
  211. else if (!Config.StopIO && devContainer.devIOCard.IsInit)
  212. {
  213. this.devContainer.devIOCard.writeBitState(0, 1, true);
  214. Task.Run(async () =>
  215. {
  216. await Task.Delay(500);
  217. this.devContainer.devIOCard.writeBitState(0, 1, false);
  218. });
  219. }
  220. pauseCommand(buzzer);
  221. }
  222. //else if (level == WarningEnum.High)//急停
  223. //{
  224. // currentState = CurrentStateEnum.急停;
  225. // devContainer.devAxis.stopNow();
  226. // stopNowCommand();
  227. //}
  228. //启用报警消除按钮
  229. //this.Invoke(new System.Action(() =>
  230. //{
  231. // tsbtnWarning.Enabled = true;
  232. //}));
  233. }
  234. /// <summary>
  235. /// 重新生成缺陷分布(cm2M在内部转换)
  236. /// </summary>
  237. /// <param name="lstDefectInfo">Records.DefectInfoList</param>
  238. /// <param name="XSizeRange">幅宽</param>
  239. /// <param name="YSizeRange">卷长度</param>
  240. private void reDrawDefectPoints(List<DefectInfo> lstDefectInfo, double[] XSizeRange=null, double[] YSizeRange=null,bool addSelRect=true)
  241. {
  242. UILineOption option;
  243. //AddTextEvent(DateTime.Now,$"绘图", $"缺陷分布, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(lstDefectInfo[lstDefectInfo.Count - 1])}");
  244. var lstData = lstDefectInfo.OrderBy(m => m.Code).ThenBy(m => m.Code).ToList();
  245. if (XSizeRange == null || YSizeRange == null)
  246. option = this.lineChartDefect.Option;
  247. else
  248. {
  249. if (YSizeRange[0] == YSizeRange[1])
  250. {
  251. YSizeRange[0] -= YSizeRange[0] / 10f;
  252. YSizeRange[1] += YSizeRange[1] / 10f;
  253. }
  254. YSizeRange[0] /= 100;
  255. YSizeRange[1] /= 100;
  256. option = new UILineOption();
  257. option.XAxis.Name = "面宽(cm)";
  258. option.YAxis.Name = "长度(米)";
  259. //option.Grid.Top = 20;//边距
  260. option.Grid.Right = 20;//边距
  261. //X轴数据类型
  262. option.XAxisType = UIAxisType.Value;
  263. //设置X/Y轴显示范围
  264. option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
  265. option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
  266. //坐标轴显示小数位数
  267. option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1;
  268. //X/Y轴画参考线
  269. //option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
  270. //option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));
  271. //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
  272. //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));
  273. option.ToolTip.Visible = true;
  274. //option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}";
  275. option.Title = new UITitle();
  276. option.Title.Text = "";
  277. option.Title.SubText = "";
  278. }
  279. option.Series.Clear();
  280. string preCode = "";
  281. UILineSeries series=null;
  282. foreach (var item in lstData)
  283. {
  284. if (preCode != item.Code)//加一组新类型及样式
  285. {
  286. preCode = item.Code;
  287. var one = Config.getDefectItem(item.Code);
  288. if (one == null)
  289. {
  290. AddTextEvent(DateTime.Now,$"绘图", $"getDefectItem({item.Code}) is null!");
  291. continue;
  292. }
  293. JObject objItem= one as JObject;
  294. Color color = ColorTranslator.FromHtml(objItem.Value<string>("color"));
  295. series = option.AddSeries(new UILineSeries(objItem.Value<string>("name"), color));//加一组
  296. series.Symbol = UILinePointSymbol.Star;
  297. series.SymbolSize = 4;
  298. series.SymbolLineWidth = 2;
  299. series.ShowLine = false;
  300. series.SymbolColor = color;
  301. //数据点显示小数位数(针对当前UILineSeries)
  302. series.XAxisDecimalPlaces = 1;
  303. series.YAxisDecimalPlaces = 2;
  304. //series.Smooth = false;
  305. }
  306. series.Add( item.CentreX, item.CentreY / 100); //cm -> m
  307. }
  308. ////加一组框选
  309. if (addSelRect && !option.Series.ContainsKey("SELECT"))
  310. {
  311. series=option.AddSeries(new UILineSeries("SELECT", Color.Red));
  312. series.Symbol = UILinePointSymbol.Circle;
  313. series.SymbolSize = 6;
  314. series.SymbolLineWidth = 3;
  315. series.ShowLine = false;
  316. series.SymbolColor = Color.Red;
  317. //数据点显示小数位数(针对当前UILineSeries)
  318. series.XAxisDecimalPlaces = 1;
  319. series.YAxisDecimalPlaces = 2;
  320. //series.Add(1, 1);
  321. }
  322. //====
  323. //option.GreaterWarningArea = new UILineWarningArea(3.5);
  324. //option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
  325. this.BeginInvoke(new System.Action(() =>
  326. {
  327. this.lineChartDefect.SetOption(option);
  328. //series.UpdateYData();//按序号更新Y轴值(可设置值超出范围用于闪烁)
  329. }));
  330. }
  331. /// <summary>
  332. /// 重新门幅宽度
  333. /// </summary>
  334. /// <param name="points"></param>
  335. /// <param name="XSizeRange">卷长度</param>
  336. /// <param name="YSizeRange">幅宽</param>
  337. private void reDrawFaceWidth(List<float[]> points, double[] XSizeRange, double[] YSizeRange)
  338. {
  339. //AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(points[points.Count-1])}");
  340. if(YSizeRange[0]== YSizeRange[1])
  341. {
  342. YSizeRange[0] -= YSizeRange[0] / 10f;
  343. YSizeRange[1] += YSizeRange[1] / 10f;
  344. }
  345. XSizeRange[0] /= 100;
  346. XSizeRange[1] /= 100;
  347. //防止超限
  348. XSizeRange[1] += 0.01;
  349. YSizeRange[1] += 0.1;
  350. UILineOption option = new UILineOption();
  351. option.XAxis.Name = "长度(米)";
  352. option.YAxis.Name = "面宽(cm)";
  353. option.Grid.Top = 20;
  354. option.Grid.Right = 20;
  355. //X轴数据类型
  356. option.XAxisType = UIAxisType.Value;
  357. //设置X/Y轴显示范围
  358. option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
  359. option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
  360. //坐标轴显示小数位数
  361. option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1;
  362. //X/Y轴画参考线
  363. //option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
  364. //option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));
  365. //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
  366. //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));
  367. option.ToolTip.Visible = true;
  368. //option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}";
  369. option.Title = new UITitle();
  370. option.Title.Text = "";
  371. option.Title.SubText = "";
  372. Color color = Color.Blue;
  373. UILineSeries series = null;
  374. series = option.AddSeries(new UILineSeries("面宽", color));
  375. series.Symbol = UILinePointSymbol.Circle;
  376. series.ShowLine = true;
  377. series.SymbolSize = 1;//4
  378. series.SymbolLineWidth = 1;//2
  379. series.SymbolColor = color;
  380. //数据点显示小数位数(针对当前UILineSeries)
  381. series.XAxisDecimalPlaces = 2;
  382. series.YAxisDecimalPlaces = 1;
  383. float x;
  384. foreach (var item in points)
  385. {
  386. x = item[0] / 100; //cm -> m
  387. series.Add(x, item[1]);
  388. if (x < XSizeRange[0]) AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 1!!!! {x}<{XSizeRange[0]}",WarningEnum.High);
  389. if (x > XSizeRange[1]) AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 2!!!! {x}>{XSizeRange[1]}", WarningEnum.High);
  390. if (item[1] < YSizeRange[0]) AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 3!!!! {item[1]}<{YSizeRange[0]}", WarningEnum.High);
  391. if (item[1] > YSizeRange[1]) AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 4!!!! {item[1]}>{YSizeRange[1]}", WarningEnum.High);
  392. }
  393. //====
  394. //option.GreaterWarningArea = new UILineWarningArea(3.5);
  395. //option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
  396. this.BeginInvoke(new System.Action(() =>
  397. {
  398. this.lineChartFaceWidth.SetOption(option);
  399. }));
  400. }
  401. private delegate void AddTextDelegate(DateTime time,string tag, string msg, WarningEnum level);
  402. private void AddTextEvent(DateTime now, string tag, string msg, WarningEnum level = WarningEnum.Normal)
  403. {
  404. try
  405. {
  406. if (InvokeRequired)
  407. {
  408. Invoke(new AddTextDelegate(AddTextEvent), new object[]
  409. {
  410. now,
  411. tag,
  412. msg,
  413. level
  414. });
  415. }
  416. else
  417. {
  418. if (tag != null && tag != "")
  419. tag = $" - [{tag}]";
  420. //var now = DateTime.Now;
  421. msg = now.ToString("HH:mm:ss fff") + tag + " - " + msg;
  422. //cont = MyHelper.subString(cont, 300);
  423. //写日志,warn和error日志直接写
  424. writeLog(now, level, msg);
  425. //
  426. //if (type > 0)
  427. // cont = $"<color=\"{(type == 1 ? "Yellow" : "Red")}\">{cont}</color>";
  428. //msg = (level == WarningEnum.Normal ? "B" : level == WarningEnum.Low ? "Y" : "R") + msg;
  429. msg = (level == WarningEnum.Normal ? "" : level == WarningEnum.Low ? "Y" : "R") + msg;
  430. //this.Invoke(new System.Action(() =>
  431. //{
  432. if (this.lstboxLog.Items.Count > 1000)
  433. this.lstboxLog.Items.Clear();
  434. lstboxLog.Items.Insert(0, msg);
  435. //}));
  436. //日志滚动
  437. //lstLog.SelectedIndex = lstLog.Items.Count - 1;
  438. }
  439. }
  440. catch (Exception ex)
  441. {
  442. //MessageBox.Show("AddTextEvent ex=(" + ex.Message + ")", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  443. }
  444. }
  445. private void writeLog(DateTime now, WarningEnum level, string text)
  446. {
  447. string directory = Config.LogPath + "\\" + DateTime.Now.ToString("yyyyMM") + "\\";
  448. //if (type == 0) directory = Application.StartupPath + "\\Log\\Info\\";
  449. //else if (type == 1) directory = Application.StartupPath + "\\Log\\Warn\\";
  450. //else directory = Application.StartupPath + "\\Log\\Error\\";
  451. if (!System.IO.Directory.Exists(directory))
  452. System.IO.Directory.CreateDirectory(directory);
  453. File.AppendAllText(directory + now.ToString("yyyyMMdd") + ".log", text + "\r\n");
  454. }
  455. #endregion
  456. private void FHome_Load(object sender, EventArgs e)
  457. {
  458. ucColorListDefect.initData(Config.defectItemList);
  459. this.lineChartDefect.SetOption(new UILineOption());
  460. this.lineChartFaceWidth.SetOption(new UILineOption());
  461. }
  462. private void FHome_Shown(object sender, EventArgs e)
  463. {
  464. //picScanner1.BackColor = Color.Red;
  465. //picScanner2.BackColor = Color.Green;
  466. picScanner1.Width = picScanner2.Width = pnlScannerImg.ClientSize.Width / 2 - 5;
  467. picScanner2.Left = picScanner1.Width + 5;
  468. }
  469. //开机
  470. private void btnOpen_Click(object sender, EventArgs e)
  471. {
  472. //this.txtNote.Text = (currProductModel == null ? "NO" : currProductModel.Name);
  473. Config.LoadAllConfig();
  474. //设置程序最小/大线程池
  475. // Get the current settings.
  476. int minWorker, minIOC;
  477. ThreadPool.GetMinThreads(out minWorker, out minIOC);
  478. ThreadPool.SetMinThreads(25, minIOC);
  479. //ThreadPool.SetMaxThreads(256, 256);
  480. this.btnOpen.Enabled = false;
  481. this.resetUIValue();
  482. AddTextEvent(DateTime.Now,"设备启动", $"正在启动硬件设备(版本:v{ Assembly.GetExecutingAssembly().GetName().Version.ToString()})。。。");
  483. warningLevel = WarningEnum.Normal;
  484. this.btnStart.Enabled = this.btnEnd.Enabled = this.btnPause.Enabled = false;
  485. //后台线程回调事件
  486. devContainer = new DevContainer();
  487. devContainer.StateChange = (state, msg) =>
  488. {
  489. if (state)
  490. {
  491. currentState = CurrentStateEnum.初始;
  492. AddTextEvent(DateTime.Now,"设备启动", $"启动成功,请点开始验布按钮!");
  493. //全局中断
  494. devContainer.devIOCard.INEvent = globalBreakEvent;
  495. // I/O reset后,输出默认状态
  496. if (devContainer.devIOCard.IsInit)
  497. {
  498. AddTextEvent(DateTime.Now,"复位", $"I/O复位...");
  499. if (!devContainer.devIOCard.reset())
  500. {
  501. AddTextEvent(DateTime.Now,"复位", "I/O板卡复位失败!", WarningEnum.High);
  502. return;
  503. }
  504. if (!devContainer.io_output(CMDName.IO默认输出))
  505. {
  506. //AddTextEvent(DateTime.Now,"复位", "I/O板卡复位默认值失败!", WarningEnum.High);
  507. //return;
  508. }
  509. int portIndex = 0;
  510. foreach(var data in devContainer.devIOCard.DIData)
  511. AddTextEvent(DateTime.Now,"I/0状态", $"DIData: {portIndex++}-{Convert.ToString(data, 2)}");
  512. }
  513. devContainer.OutDebugEvent = (tag, debugInfo) =>
  514. {
  515. AddTextEvent(DateTime.Now,tag, debugInfo);
  516. };
  517. //
  518. this.Invoke(new System.Action(() =>
  519. {
  520. this.btnOpen.Enabled = false;
  521. this.btnClose.Enabled = true;
  522. this.btnStart.Enabled = true;
  523. this.tcbarLightValue.Enabled = true;
  524. tcbarLightValue.Value=devContainer.devLight.getDigitalValue(1);
  525. }));
  526. devContainer.libPhoto.QueueCountEvent = (count) =>
  527. {
  528. this.BeginInvoke(new System.Action(() =>
  529. {
  530. this.lblWaitImageCount.Text = count.ToString();
  531. }));
  532. };
  533. devContainer.libDefect.QueueCountEvent = (type, count) =>
  534. {
  535. this.BeginInvoke(new System.Action(() =>
  536. {
  537. switch (type)
  538. {
  539. case 0: this.lblDefectQueue0.Text = count.ToString(); break;
  540. case 1: this.lblDefectQueue1.Text = count.ToString(); break;
  541. case 2: this.lblDefectQueue2.Text = count.ToString(); break;
  542. }
  543. }));
  544. };
  545. //在【暂停】和【运行中】时都可以扫码,扫码后即开始即结束上一卷(如存在),自动开始下一卷,等待相机照片回调
  546. devContainer.devCodeScanner.ScanerEvent = (barCode) =>
  547. {
  548. int errCode = 0;
  549. try
  550. {
  551. //AddTextEvent(DateTime.Now,"扫码", $"{code}:{currentState.ToString()}");
  552. if (!devContainer.state || string.IsNullOrWhiteSpace(barCode)
  553. || (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停))
  554. return;
  555. string barCodeName="",len = "0", batchId = "", reelId = "";
  556. if (!string.IsNullOrWhiteSpace(Config.ErpDBConStr) && !string.IsNullOrWhiteSpace(Config.ErpSql) && !string.IsNullOrWhiteSpace(barCode))
  557. {
  558. AddTextEvent(DateTime.Now,"扫码", $"产品条码({barCode})到ERP查询对应数据...", WarningEnum.Normal);
  559. var rowData = this.loadErpData(barCode);
  560. if (rowData == null)
  561. {
  562. AddTextEvent(DateTime.Now,"扫码", $"产品条码({barCode})无对应ERP数据,不做响应!", WarningEnum.Low);
  563. return;
  564. }
  565. barCodeName = rowData[0].ToString();
  566. if (rowData.ItemArray.Length > 1) len = rowData[1].ToString();
  567. if (rowData.ItemArray.Length > 2) batchId = rowData[2].ToString();
  568. if (rowData.ItemArray.Length > 3) reelId = rowData[3].ToString();
  569. }
  570. else
  571. barCodeName = barCode;//没有ERP对应关系时直接使用条码做为品名
  572. errCode = 1;
  573. //SHNY-PX-6-***
  574. string[] codes = barCodeName.Split(new char[] { '-' });
  575. if (codes.Length < 4)
  576. {
  577. AddTextEvent(DateTime.Now,"扫码", $"产品品名({barCodeName})格式错误,不做响应!", WarningEnum.Low);
  578. return;
  579. }
  580. //新开始
  581. this.resetUIValue(false);
  582. errCode = 2;
  583. //加载新产品
  584. string pcode = "1-" + codes[2];
  585. if (codes[1] == "0" || Config.SuedeList.Contains(codes[1]))
  586. pcode = "0-" + codes[2];
  587. var productInfo = svcProduct.GetModelNav(pcode); //frmProduct.loadProduct(code);
  588. Records record;
  589. lock (lockCurrKey)
  590. {
  591. errCode = 3;
  592. //保存,这里队列图片可能还未检测完
  593. if (currKey > 0)
  594. {
  595. string szBatchId, szReelId;
  596. double ldErpLen;
  597. szBatchId = txtBatchId.Text.Trim();
  598. szReelId = txtReelId.Text.Trim();
  599. ldErpLen = Convert.ToDouble(numErpLen.Text.Trim());
  600. //BatchId = code,//code[2]
  601. //ReelId = "1",//code[3]
  602. int mykey = currKey;
  603. Task.Run(() => { saveCurrRecord(mykey, szBatchId, szReelId, ldErpLen); });
  604. currKey = 0;
  605. }
  606. if (productInfo == null)
  607. {
  608. AddTextEvent(DateTime.Now,"扫码", $"编码({pcode})对应配方不存在,请先添加产品配方设置,暂停设备!", WarningEnum.High);
  609. warning(WarningEnum.Low);//暂停
  610. this.BeginInvoke(new System.Action(() =>
  611. {
  612. frmProduct.loadProduct(pcode);//转到新建编码
  613. }));
  614. return;
  615. }
  616. errCode = 4;
  617. var now = DateTime.Now;
  618. currKey = now.Hour * 10000 + now.Minute * 100 + now.Second;
  619. //var materialItem = codes[0]+"-"+ codes[1];
  620. var colorItem = Config.getColorItem(int.Parse(codes[2]));
  621. record = new Records()
  622. {
  623. currKey = currKey,
  624. ProductId = productInfo.Id,
  625. ProductInfo = productInfo,//后面计算合格时用
  626. Color = (colorItem == null ? "未知" : colorItem["name"].ToString()),
  627. Material = codes[0] + "-" + codes[1],// (materialItem == null ? "未知" : materialItem["name"].ToString()),
  628. BarCode = barCode,
  629. BarCodeName = barCodeName,
  630. ErpLen=double.Parse(len),
  631. BatchId=batchId,
  632. ReelId=reelId,
  633. ModifyUserCode = Config.loginUser.Code,
  634. CreateUserCode = Config.loginUser.Code,
  635. };
  636. htTask.Add(currKey, record);
  637. }
  638. errCode = 8;
  639. //
  640. errCode = 9;
  641. AddTextEvent(DateTime.Now,"扫码", $"品名({barCodeName}),加载产品信息({productInfo.Code})完成,加载配方(光源={productInfo.LightValue},曝光={productInfo.ExposureTime},增益={productInfo.Gain})...");
  642. errCode = 10;
  643. if (productInfo.LightValue > 0)//光源 - 通道0
  644. devContainer.devLight.setDigitalValue(1, productInfo.LightValue);
  645. errCode = 11;
  646. if (productInfo.ExposureTime > 0 || productInfo.Gain > 0)//相机曝光 增益
  647. {
  648. devContainer.devCamer1.setParam((float)(productInfo.ExposureTime > 0 ? productInfo.ExposureTime : -1), (float)(productInfo.Gain > 0 ? productInfo.Gain : -1));
  649. devContainer.devCamer2.setParam((float)(productInfo.ExposureTime > 0 ? productInfo.ExposureTime : -1), (float)(productInfo.Gain > 0 ? productInfo.Gain : -1));
  650. }
  651. errCode = 15;
  652. AddTextEvent(DateTime.Now,"扫码", $"品名({barCodeName}),配方设置完成:光源={productInfo.LightValue},曝光={productInfo.ExposureTime}");
  653. //注意,这里和修改页共享操作(UI操作),注意冲突
  654. this.Invoke(new System.Action(() =>
  655. {
  656. this.txtBarCode.Text = "";
  657. this.txtBarCodeName.Text= barCodeName;
  658. //txtBatchId.Text = record.BatchId;
  659. //txtReelId.Text = record.ReelId;
  660. this.numErpLen.Text = len;
  661. this.txtBatchId.Text = batchId;
  662. this.txtReelId.Text = reelId;
  663. //暂时全部放开
  664. //this.btnStart.Enabled = true;
  665. //this.btnEnd.Enabled = true;
  666. this.btnPause.Enabled = true;
  667. //
  668. this.swcDefectPauseForUser.Active = this.defectPauseForUser = productInfo.DefectPauseForUser;
  669. }));
  670. //
  671. pStopWatch.Restart();
  672. errCode = 19;
  673. }
  674. catch (Exception ex)
  675. {
  676. AddTextEvent(DateTime.Now,"扫码", $"异常({errCode}):{ex.Message}", WarningEnum.High);
  677. }
  678. };
  679. //相机回调出照片
  680. devContainer.devCamer1.ScanEvent = callBackScanMatEvent;
  681. devContainer.devCamer2.ScanEvent = callBackScanMatEvent;
  682. //devContainer.devScannerGentl1.ScanEventPath = callBackScanPathEvent;
  683. //devContainer.devScannerGentl2.ScanEventPath = callBackScanPathEvent;
  684. devContainer.devCamer1.PhotoNumCacheEvent = (num) =>
  685. {
  686. this.BeginInvoke(new System.Action(() =>
  687. {
  688. this.lblScanner1Cache.Text =num.ToString();
  689. }));
  690. };
  691. devContainer.devCamer2.PhotoNumCacheEvent = (num) =>
  692. {
  693. this.BeginInvoke(new System.Action(() =>
  694. {
  695. this.lblScanner2Cache.Text = num.ToString();
  696. }));
  697. };
  698. devContainer.devPlc.RuningStateChangeEvent = (runningState) =>
  699. {
  700. AddTextEvent(DateTime.Now,"PLC", $"当前状态为:{(runningState ? "运行中" : "暂停")}");
  701. if (runningState)
  702. this.startCommand();
  703. else
  704. this.pauseCommand();
  705. };
  706. //devContainer.devIOCard.INEvent = (i,value) =>
  707. //{
  708. // AddTextEvent(DateTime.Now,"I/O", $"INEvent 当前值:{i}-{value}");
  709. // if (compareIOInput(CMDName.启动按钮))
  710. // {
  711. // this.startCommand();
  712. // }
  713. // else if (compareIOInput(CMDName.暂停按钮))
  714. // {
  715. // this.pauseCommand(true);
  716. // }
  717. //};
  718. }
  719. else
  720. {
  721. AddTextEvent(DateTime.Now,"设备启动", $"启动失败,{msg}", WarningEnum.High);
  722. this.Invoke(new System.Action(() =>
  723. {
  724. this.btnOpen.Enabled = true;
  725. this.btnClose.Enabled = false;
  726. }));
  727. }
  728. };
  729. devContainer.WarningEvent = (now,level, msg) =>
  730. {
  731. AddTextEvent(DateTime.Now,$"设备事件{Thread.CurrentThread.ManagedThreadId}", msg, level);
  732. if (level == WarningEnum.High)
  733. Task.Run(() => warning(level, true));
  734. };
  735. devContainer.start(this.picScanner1, this.picScanner2);
  736. //devContainer.start(IntPtr.Zero,IntPtr.Zero);
  737. }
  738. private DataRow loadErpData(string barCode)
  739. {
  740. var paramList = new List<SugarParameter>() {
  741. new SugarParameter("@code", barCode)
  742. };
  743. Stopwatch stopwatch = Stopwatch.StartNew();
  744. var data = Utils.DBUtils.execSql(Config.ErpSql, paramList);
  745. if (data != null && data.Rows.Count < 1)
  746. {
  747. AddTextEvent(DateTime.Now,"Erp查询结果", $"{barCode}: 时长={stopwatch.ElapsedMilliseconds}ms, 无数据!", WarningEnum.Normal);
  748. return null;
  749. }
  750. AddTextEvent(DateTime.Now,"Erp查询结果", $"{barCode}: 时长={stopwatch.ElapsedMilliseconds}ms, {JsonConvert.SerializeObject(data.Rows[0])}", WarningEnum.Normal);
  751. return data.Rows[0];
  752. //Task.Run(() =>
  753. //{
  754. // try
  755. // {
  756. // var paramList = new List<SugarParameter>() {
  757. // new SugarParameter("@code", code)
  758. // };
  759. // Stopwatch stopwatch = Stopwatch.StartNew();
  760. // var data = Utils.DBUtils.execSql(Config.ErpSql, paramList);
  761. // AddTextEvent(DateTime.Now,"Erp查询结果", $"{code}: 时长={stopwatch.ElapsedMilliseconds}ms, {JsonConvert.SerializeObject(data.Rows[0])}", WarningEnum.Normal);
  762. // if (data != null && data.Rows.Count > 0)
  763. // {
  764. // this.Invoke(new System.Action(() =>
  765. // {
  766. // this.numErpLen.Text = data.Rows[0][0].ToString();
  767. // if (data.Columns.Count > 1) this.txtBatchId.Text = data.Rows[0][1].ToString();
  768. // if (data.Columns.Count > 2) this.txtReelId.Text = data.Rows[0][2].ToString();
  769. // }));
  770. // }
  771. // }
  772. // catch (Exception ex)
  773. // {
  774. // AddTextEvent(DateTime.Now,"Erp查询异常", $"{code}:{ex.Message}", WarningEnum.Low);
  775. // }
  776. //});
  777. }
  778. private class ScanPhotoInfo
  779. {
  780. /// <summary>
  781. ///
  782. /// </summary>
  783. /// <param name="_devIndex"></param>
  784. /// <param name="_photoIndex">1-n 第1张会把1改为0</param>
  785. /// <param name="_path"></param>
  786. public ScanPhotoInfo(int _devIndex,int _photoIndex,string _path)
  787. {
  788. devIndex = _devIndex;
  789. photoIndex = _photoIndex;
  790. path = _path;
  791. }
  792. public ScanPhotoInfo(int _devIndex, int _photoIndex, Mat _mat)
  793. {
  794. devIndex = _devIndex;
  795. photoIndex = _photoIndex;
  796. mat = _mat;
  797. }
  798. public int devIndex { get; set; }
  799. /// <summary>
  800. /// 0-n
  801. /// </summary>
  802. public int photoIndex { get; set; }
  803. public string path { get; set; }
  804. public Mat mat { get; set; }
  805. }
  806. //private List<Queue<ScanPhotoInfo>> scanPhotoQueue1 = new List<Queue<ScanPhotoInfo>>();
  807. /// <summary>
  808. /// 存放相机1/2的实时图像(3-多容错1帧)
  809. /// </summary>
  810. private ScanPhotoInfo[] scanPhotos=new ScanPhotoInfo[3];
  811. private void callBackScanPathEvent(int num, string path, int devIndex) //ScanEvent
  812. {
  813. AddTextEvent(DateTime.Now,"拍照", $"相机({devIndex})采集图索到图像({num}):{path}!");
  814. }
  815. bool firstTest = true;
  816. /// <summary>
  817. ///
  818. /// </summary>
  819. /// <param name="num">1-n</param>
  820. /// <param name="matone"></param>
  821. /// <param name="devIndex"></param>
  822. private void callBackScanMatEvent(int num, Mat matone, int devIndex) //ScanEvent
  823. {
  824. if(Config.Camer_Name== CamerDevNameEnum.埃科)
  825. {
  826. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"采集卡({devIndex}),图像({num})");
  827. Cv2.Flip(matone, matone, FlipMode.XY);//翻转
  828. Bitmap bitmap = BitmapConverter.ToBitmap(matone);
  829. this.BeginInvoke(new Action(() =>
  830. {
  831. //bitmap.Save($"d:\\{devIndex}_{num}.bmp", ImageFormat.Bmp);
  832. //显示图片
  833. if (devIndex == 0)
  834. {
  835. picScanner2.Image = bitmap;
  836. picScanner2.Refresh();
  837. }
  838. else
  839. {
  840. picScanner1.Image = bitmap;
  841. picScanner1.Refresh();
  842. }
  843. }));
  844. }
  845. if (!devContainer.state || (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停))
  846. {
  847. matone.Dispose();
  848. return;
  849. }
  850. lock (lockCurrKey)
  851. {
  852. if (currKey == 0 || !htTask.ContainsKey(currKey))
  853. {
  854. lock (lockScanPhoto)
  855. {
  856. if (scanPhotos[0] != null || scanPhotos[1] != null)
  857. scanPhotos[0] = scanPhotos[1] = scanPhotos[2] = null;
  858. }
  859. matone.Dispose();
  860. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-未扫码,采集卡({devIndex}),图像丢弃!", WarningEnum.Low);
  861. return;
  862. }
  863. }
  864. Records curRecord = Hashtable.Synchronized(htTask)[currKey] as Records;
  865. Device.PhotoLib.PhotoTask task;
  866. Mat myMat = matone;//.Clone();
  867. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-采集卡({devIndex}),size:({myMat.Width}*{myMat.Height})({(myMat == null?"A":"B")})");
  868. //AddTextEvent(DateTime.Now,"拍照", $"Dev={devIndex},图像{num}-2");
  869. lock (lockScanPhoto)//多线程操作
  870. {
  871. if(scanPhotos[2] != null && scanPhotos[2].photoIndex== num)
  872. {
  873. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"相机图像不同步,急停中止(需停机重新开始,不可继续)!!!", WarningEnum.High);
  874. warning(WarningEnum.High);
  875. //重置...
  876. }
  877. if(scanPhotos[devIndex]!=null) //添加一张容错
  878. {
  879. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-采集卡({devIndex}) 容错一张图到队列!", WarningEnum.Low);
  880. scanPhotos[2] = new ScanPhotoInfo(devIndex, num, myMat);
  881. return;
  882. }
  883. //--
  884. scanPhotos[devIndex] = new ScanPhotoInfo(devIndex, num, myMat);
  885. if (scanPhotos[0] == null || scanPhotos[1] == null)
  886. return;
  887. //
  888. //if (scanPhotos[0].photoIndex != scanPhotos[1].photoIndex)
  889. //{
  890. // if (scanPhotos[0].photoIndex > scanPhotos[1].photoIndex)
  891. // {
  892. // AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-索引不一致),丢弃采集卡({scanPhotos[1].devIndex})中较小索引图像({scanPhotos[1].photoIndex})!", WarningEnum.High);
  893. // scanPhotos[1] = null;
  894. // }
  895. // else
  896. // {
  897. // AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{num})-索引不一致),丢弃采集卡({scanPhotos[0].devIndex})中较小索引图像({scanPhotos[0].photoIndex})!", WarningEnum.High);
  898. // scanPhotos[0] = null;
  899. // }
  900. // if (num > 1)
  901. // {
  902. // AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"相机图像不同步,急停中止(需停机重新开始,不可继续)!!!", WarningEnum.High);
  903. // warning(WarningEnum.High);
  904. // //重置...
  905. // }
  906. // return;
  907. //}
  908. //curRecord置新时,相机里最后一张图还是上次的计数器devIndex累计
  909. if (curRecord.ScannerPhotoCount == 0 && num>=1)
  910. {
  911. devContainer.devCamer1.resetScanIndex();
  912. devContainer.devCamer2.resetScanIndex();
  913. scanPhotos[0].photoIndex = scanPhotos[1].photoIndex = 0;
  914. if (scanPhotos[2] != null)
  915. scanPhotos[2].photoIndex = 1;
  916. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"新任务开始,Dev={devIndex},图像({num})重置为图像(0)");
  917. }
  918. task = new Device.PhotoLib.PhotoTask()
  919. {
  920. scanPhotos0 = scanPhotos[0],
  921. scanPhotos1 = scanPhotos[1],
  922. };
  923. scanPhotos[0] = scanPhotos[1] = null;
  924. if (scanPhotos[2] != null)//容错图前移到正常位置
  925. {
  926. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos[2].photoIndex})-采集卡({scanPhotos[2].devIndex}) 容错队列图前移。");
  927. scanPhotos[scanPhotos[2].photoIndex] = scanPhotos[2];
  928. scanPhotos[2] = null;
  929. }
  930. curRecord.ScannerPhotoCount++;
  931. }
  932. //长度剩余提醒
  933. if (Config.residueWarnningLen > 0 && curRecord.ErpLen >0 && Config.residueWarnningLen >= curRecord.ErpLen - curRecord.Len)
  934. {
  935. AddTextEvent(DateTime.Now,$"告警{Thread.CurrentThread.ManagedThreadId}", $"已达剩余长度不足提醒!({curRecord.ErpLen - curRecord.Len}<={Config.residueWarnningLen})", WarningEnum.High);
  936. }
  937. //
  938. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"待处理图像队列:{curRecord.ScannerPhotoCount - curRecord.ScannerPhotoFinishCount}张");
  939. if (task.scanPhotos0 != null && task.scanPhotos1 != null)
  940. {
  941. var scanPhoto = task.scanPhotos0 as ScanPhotoInfo;
  942. curRecord.dicPhoto_Defect.TryAdd(scanPhoto.photoIndex, false);//加入索引,默认无瑕疵
  943. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"图像索引:{scanPhoto.photoIndex},标识数:{curRecord.dicPhoto_Defect.Count}");
  944. //暂停:瑕疵二次判断
  945. if (this.defectPauseForUser)
  946. {
  947. int liPhotoIndex = scanPhoto.photoIndex - Config.defectPauseSkipPhotoCount;
  948. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"Dev={devIndex},图像{scanPhoto.photoIndex} {liPhotoIndex}={scanPhoto.photoIndex}-{Config.defectPauseSkipPhotoCount};{JsonConvert.SerializeObject(curRecord.dicPhoto_Defect) }");
  949. if (liPhotoIndex >= 0 && curRecord.dicPhoto_Defect[liPhotoIndex])
  950. {
  951. List<DefectInfo> lstEditDefect = curRecord.DefectInfoList.Where(m => m.PhotoIndex == liPhotoIndex).ToList();
  952. AddTextEvent(DateTime.Now,$"暂停{Thread.CurrentThread.ManagedThreadId}", $"(图像{liPhotoIndex})已达观察台,瑕疵二次判断=》({string.Join(",", lstEditDefect.Select(m => m.Code).ToArray())})是否包含在({string.Join(",", curRecord.ProductInfo.DefectPauseOption.ToArray())})中。");
  953. //瑕疵选项过滤
  954. if (curRecord.ProductInfo.DefectPauseOption.Count == 0 || lstEditDefect.Where(x => curRecord.ProductInfo.DefectPauseOption.Contains(x.Code)).Count() > 0)
  955. {
  956. AddTextEvent(DateTime.Now,$"暂停{Thread.CurrentThread.ManagedThreadId}", $"(图像{liPhotoIndex})需瑕疵二次判断,已达观察台,进入暂停。");
  957. if (!Config.StopPLC)
  958. this.devContainer.devPlc.pauseDev();
  959. else if (!Config.StopIO && devContainer.devIOCard.IsInit)
  960. {
  961. //只是设备暂停,APP没暂停
  962. devContainer.io_output(CMDName.绿灯输出, false, true, 0);
  963. devContainer.io_output(CMDName.黄灯输出);
  964. devContainer.devIOCard.writeBitState(0, 1, true);
  965. Task.Run(async () =>
  966. {
  967. await Task.Delay(500);
  968. this.devContainer.devIOCard.writeBitState(0, 1, false);
  969. });
  970. }
  971. //不能使用同步Invoke方式,会使相机超时丢帧
  972. this.BeginInvoke(new System.Action(() =>
  973. {
  974. int liDefectCount = lstEditDefect.Count;
  975. FHome_Defect frmDefect = new FHome_Defect(lstEditDefect);
  976. if (frmDefect.ShowDialog() == DialogResult.OK)
  977. {
  978. string oldCode;
  979. for (int i = 0; i < this.uiDataGridView1.Rows.Count; i++)
  980. {
  981. if ((int)this.uiDataGridView1.Rows[i].Cells["colIndex"].Value != liPhotoIndex)
  982. continue;
  983. long uid = (long)this.uiDataGridView1.Rows[i].Cells["colUid"].Value;
  984. foreach (var row in lstEditDefect)
  985. {
  986. AddTextEvent(DateTime.Now,$"暂停{Thread.CurrentThread.ManagedThreadId}", $"修改第({i + 1})行瑕疵名称,{uid} {row.uid}");
  987. if (row.uid == uid)
  988. {
  989. oldCode = this.uiDataGridView1.Rows[i].Cells["colCode"].Value.ToString();
  990. AddTextEvent(DateTime.Now,$"暂停{Thread.CurrentThread.ManagedThreadId}", $"修改第({i + 1})行瑕疵名称 ({this.uiDataGridView1.Rows[i].Cells["colDefectName"].Value})->({row.Name})");
  991. this.uiDataGridView1.Rows[i].Cells["colCode"].Value = row.Code;
  992. this.uiDataGridView1.Rows[i].Cells["colDefectName"].Value = row.Name;
  993. //this.uiDataGridView1.Refresh();
  994. if (!string.IsNullOrWhiteSpace(row.TagFilePath))
  995. File.Move(row.TagFilePath, row.TagFilePath.Replace($"_类别{oldCode}", $"_类别{row.Code}"));//
  996. break;
  997. }
  998. }
  999. foreach (var item in frmDefect.lstDel)
  1000. {
  1001. if (item.uid == uid)
  1002. {
  1003. this.uiDataGridView1.Rows.RemoveAt(i);
  1004. i--;
  1005. break;
  1006. }
  1007. }
  1008. }
  1009. foreach (var item in frmDefect.lstDel)
  1010. {
  1011. curRecord.DefectInfoList.Remove(item);
  1012. //删除忽略瑕疵小图
  1013. //if (!string.IsNullOrWhiteSpace(item.TagFilePath))
  1014. // File.Delete(item.TagFilePath);
  1015. }
  1016. //double len = (double)this.lblLen.Tag;
  1017. //this.reDrawDefectPoints(curRecord.DefectInfoList, new double[] { 0, Math.Round(curRecord.FaceWidthMax + 0.005f, 2) }, new double[] { 0, len });
  1018. AddTextEvent(DateTime.Now,$"二次检测{Thread.CurrentThread.ManagedThreadId}", $"本次忽略{frmDefect.lstDel.Count}个瑕疵,本张图由{liDefectCount} -> {lstEditDefect.Count},总数{curRecord.DefectInfoList.Count}");
  1019. }
  1020. this.uiMiniPagination1.TotalCount = curRecord.DefectTotalCount = curRecord.DefectInfoList.Count;
  1021. //
  1022. //double len = Math.Round((res.photoIndex + 1) * bmpHeight * 1.0d / Config.cm2px_y + 0.005f, 2);
  1023. this.reDrawDefectPoints(curRecord.DefectInfoList);
  1024. //自动继续运行设备(这里临时暂停后不能再急停,否则无法继续)
  1025. if (!Config.StopPLC)
  1026. this.devContainer.devPlc.runDev();
  1027. else if (!Config.StopIO && devContainer.devIOCard.IsInit)
  1028. {
  1029. if (!compareIOInput(CMDName.暂停按钮))
  1030. {
  1031. devContainer.io_output(CMDName.绿灯输出);
  1032. devContainer.io_output(CMDName.黄灯输出, false, true, 0);
  1033. this.devContainer.devIOCard.writeBitState(0, 0, true);
  1034. Task.Run(async () =>
  1035. {
  1036. await Task.Delay(500);
  1037. this.devContainer.devIOCard.writeBitState(0, 0, false);
  1038. });
  1039. }
  1040. }
  1041. }));
  1042. }
  1043. }
  1044. }
  1045. task.record = curRecord;
  1046. task.finishEvent = callBackPhotoEvent;
  1047. devContainer.libPhoto.add(task);
  1048. AddTextEvent(DateTime.Now,$"拍照{Thread.CurrentThread.ManagedThreadId}", $"Dev={devIndex},图像{scanPhoto.photoIndex},已加入图像处理队列");
  1049. }
  1050. }
  1051. private void callBackPhotoEvent(Device.PhotoLib.PhotoTask task)
  1052. {
  1053. int errStep = 0;
  1054. Records curRecord = task.record;
  1055. ScanPhotoInfo scanPhotos0 = task.scanPhotos0 as ScanPhotoInfo;
  1056. ScanPhotoInfo scanPhotos1 = task.scanPhotos1 as ScanPhotoInfo;
  1057. Stopwatch stopWatch = new Stopwatch();
  1058. AddTextEvent(DateTime.Now,$"图像处理{Thread.CurrentThread.ManagedThreadId}", $"图像{scanPhotos0.photoIndex},ThreadId={Thread.CurrentThread.ManagedThreadId}");
  1059. string time = "";
  1060. stopWatch.Start();
  1061. try
  1062. {
  1063. if (scanPhotos0.mat.Height != scanPhotos1.mat.Height)
  1064. {
  1065. AddTextEvent(DateTime.Now,$"警告{Thread.CurrentThread.ManagedThreadId}", $"两相机采集图高度不一致({scanPhotos0.photoIndex}),dev1.Height={scanPhotos0.mat.Height},dev2.Height={scanPhotos1.mat.Height},重新resize...", WarningEnum.Low);
  1066. if (scanPhotos0.mat.Height > scanPhotos1.mat.Height)
  1067. scanPhotos1.mat = OpenCVUtil.resize(scanPhotos1.mat, scanPhotos0.mat.Width, scanPhotos0.mat.Height);
  1068. else
  1069. scanPhotos0.mat = OpenCVUtil.resize(scanPhotos0.mat, scanPhotos1.mat.Width, scanPhotos1.mat.Height);
  1070. }
  1071. //saveMatTest(scanPhotos0.mat, 1);
  1072. //saveMatTest(scanPhotos1.mat, 2);
  1073. //反转+相机索引调换
  1074. Mat mat0 = scanPhotos1.mat;
  1075. Mat mat1 = scanPhotos0.mat;
  1076. if (Config.ScannerReversalX)
  1077. {
  1078. Cv2.Flip(mat0, mat0, FlipMode.X);
  1079. errStep = 1;
  1080. Cv2.Flip(mat1, mat1, FlipMode.X);
  1081. time += $"X翻转({stopWatch.ElapsedMilliseconds})";
  1082. }
  1083. if (Config.ScannerReversalY)
  1084. {
  1085. Cv2.Flip(mat0, mat0, FlipMode.Y);
  1086. errStep = 2;
  1087. Cv2.Flip(mat1, mat1, FlipMode.Y);
  1088. time += $"Y翻转({stopWatch.ElapsedMilliseconds})";
  1089. }
  1090. firstTest = false;
  1091. //记录resize大小
  1092. //var resize = new System.Drawing.Size(mat0.Width * 2, mat0.Height);
  1093. int resizeWidth = devContainer.libDefect.GetWidthForResize(mat0.Width+ mat1.Width - Config.MiddleSuperposition);
  1094. if (resizeWidth == 0)
  1095. throw new Exception("GetWidthForResize result 0 失败!");
  1096. var resize = new System.Drawing.Size(resizeWidth, 4096);//固定8192*2张*4096
  1097. //裁边,两侧和中间重合部分
  1098. if (Config.MiddleSuperposition > 0)//中间重合部分
  1099. {
  1100. errStep = 3;
  1101. int width = mat0.Width - Config.MiddleSuperposition;
  1102. mat0 = OpenCVUtil.cutImage(mat0, 0, 0, width, mat0.Height);
  1103. time += $"->图1去重({stopWatch.ElapsedMilliseconds})";
  1104. }
  1105. AddTextEvent(DateTime.Now,$"裁边{Thread.CurrentThread.ManagedThreadId}",
  1106. $"(图像{scanPhotos0.photoIndex})-左图去重后:{mat0.Width}*{mat0.Height},右图:{mat1.Width}*{mat1.Height}," +
  1107. $"重复值:{Config.MiddleSuperposition},孔洞:{Config.MarginHoleWidth},cm2px:{Config.cm2px_x},resizeWidth:{resizeWidth}");
  1108. errStep = 4;
  1109. //mat0 = OpenCVUtil.getMaxInsetRect(mat0);
  1110. int marginWidth0, marginWidth1;
  1111. mat0 = OpenCVUtil.getMaxInsetRect2(mat0, true, Config.MarginHoleWidth,out marginWidth0);
  1112. //AddTextEvent(DateTime.Now,"裁边", $"(图像{scanPhotos0.photoIndex})-图0裁边后:{mat0.Width}*{mat0.Height}");
  1113. errStep = 5;
  1114. time += $"->图1裁边({stopWatch.ElapsedMilliseconds})";
  1115. //mat1 = OpenCVUtil.getMaxInsetRect(mat1);
  1116. mat1 = OpenCVUtil.getMaxInsetRect2(mat1,false, Config.MarginHoleWidth, out marginWidth1);
  1117. //AddTextEvent(DateTime.Now,"裁边", $"(图像{scanPhotos0.photoIndex})-图1裁边后:{mat1.Width}*{mat1.Height}");
  1118. errStep = 6;
  1119. time += $"->图2裁边({stopWatch.ElapsedMilliseconds})";
  1120. //水平合并l
  1121. Mat mat = OpenCVUtil.mergeImage_sameSize(new Mat[] { mat0, mat1 });//这里相机反装,左右反转下
  1122. AddTextEvent(DateTime.Now,$"裁边{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos0.photoIndex})-边缘宽度:(左图)={marginWidth0},(右图)={marginWidth1}; 裁边去孔洞后:({mat0.Width}+{mat1.Width}={mat0.Width+ mat1.Width});合并后(去孔洞):{mat.Width}*{mat.Height}");
  1123. float widthRatio = mat.Width * 1.0f / resize.Width;//宽度比例
  1124. time += $"->图1+2合并({stopWatch.ElapsedMilliseconds})";
  1125. //门幅更新(含两侧孔洞)x,y cm
  1126. float faceWidthX_cm = (float)Math.Round((scanPhotos0.photoIndex+1) * mat.Height * 1.0f / Config.cm2px_y,2);
  1127. float faceWidthY_cm = (float)Math.Round((mat.Width + Config.MarginHoleWidth * 2) * 1.0f / Config.cm2px_x, 2);
  1128. faceWidthX_cm = (float)Math.Round(faceWidthX_cm, 2);
  1129. faceWidthY_cm = (float)Math.Round(faceWidthY_cm, 2);
  1130. if (curRecord.FaceWidthMin==0 || curRecord.FaceWidthMin > faceWidthY_cm)
  1131. curRecord.FaceWidthMin = faceWidthY_cm;
  1132. if (curRecord.FaceWidthMax < faceWidthY_cm)
  1133. curRecord.FaceWidthMax = faceWidthY_cm;
  1134. var point = new float[] { faceWidthX_cm, faceWidthY_cm };// new System.Drawing.PointF(faceWidthX_cm, faceWidthY_cm);
  1135. AddTextEvent(DateTime.Now,$"门幅{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos0.photoIndex})-({scanPhotos0.photoIndex})位置:{point[0]}; 幅宽:{point[1]}");
  1136. curRecord.FacePointList.Add(point);
  1137. reDrawFaceWidth(curRecord.FacePointList,
  1138. new double[] { 0, Math.Round(point[0] + 0.005f, 2) },
  1139. new double[] { curRecord.FaceWidthMin, Math.Round(curRecord.FaceWidthMax + 0.005f, 2) });
  1140. errStep = 7;
  1141. time += $"->门幅刷新({stopWatch.ElapsedMilliseconds})";
  1142. //去除两侧孔洞(门幅计算时不能去除)
  1143. //if (Config.MarginHoleWidth > 0)
  1144. // mat = OpenCVUtil.cutImage(mat, Config.MarginHoleWidth, 0, mat.Width - Config.MarginHoleWidth * 2, mat.Height);
  1145. //计算速度
  1146. double lenMi = Math.Round(faceWidthX_cm / 100, 2);
  1147. curRecord.Len = lenMi;
  1148. curRecord.TimeLen= pStopWatch.ElapsedMilliseconds / 1000.0d / 60.0d;//总时间 分
  1149. this.BeginInvoke(new System.Action(() =>
  1150. {
  1151. this.lblLen.Text = $"{lenMi}米";
  1152. this.lblLen.Tag = faceWidthX_cm;
  1153. this.lblSpeed.Text = $"速度:{Math.Round(lenMi / curRecord.TimeLen, 2)}米/分";
  1154. }));
  1155. //
  1156. errStep = 9;
  1157. time += $"->速度刷新({stopWatch.ElapsedMilliseconds})";
  1158. //----缺陷队列
  1159. mat = OpenCVUtil.resize(mat, resize.Width, resize.Height);
  1160. AddTextEvent(DateTime.Now,$"图像处理{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos0.photoIndex})-合成图resize后:{mat.Width}*{mat.Height}");
  1161. devContainer.libDefect.add(new Device.DefectLib.DefectTask()
  1162. {
  1163. modelName= curRecord.ProductInfo.ModelName,
  1164. record = curRecord,
  1165. bmp = mat,
  1166. bmpTag = mat.Clone(),
  1167. photoIndex = scanPhotos0.photoIndex,//0-n 首张必需为0,因下面计算长度是从0开始
  1168. widthRatio = widthRatio,
  1169. qualifiedLimitList = curRecord.ProductInfo.QualifiedLimitList,
  1170. finishEvent = callBackDefectEvent,
  1171. });
  1172. errStep = 10;
  1173. time += $"->加入瑕疵待检队列({stopWatch.ElapsedMilliseconds})";
  1174. }
  1175. catch (Exception ex)
  1176. {
  1177. curRecord.ScannerPhotoFinishCount++;//失败时不能因数量不一致无法保存
  1178. AddTextEvent(DateTime.Now,$"图像处理{Thread.CurrentThread.ManagedThreadId}", $"异常({errStep}):(图像{scanPhotos0.photoIndex})-{ex.Message}", WarningEnum.High);
  1179. string dirPath = FileUtil.initFolder($"{Config.ImagePath}{curRecord.BatchId}_{curRecord.ReelId}\\Err\\");
  1180. OpenCvSharp.Extensions.BitmapConverter.ToBitmap(scanPhotos0.mat).Save($"{dirPath}{scanPhotos0.photoIndex}_0_Step{errStep}.bmp", ImageFormat.Bmp);
  1181. OpenCvSharp.Extensions.BitmapConverter.ToBitmap(scanPhotos1.mat).Save($"{dirPath}{scanPhotos1.photoIndex}_1_Step{errStep}.bmp", ImageFormat.Bmp);
  1182. }
  1183. finally
  1184. {
  1185. AddTextEvent(DateTime.Now,$"图像处理{Thread.CurrentThread.ManagedThreadId}", $"(图像{scanPhotos0.photoIndex})-进度计时:{time}");
  1186. scanPhotos0.mat.Dispose();
  1187. scanPhotos1.mat.Dispose();
  1188. scanPhotos0 = scanPhotos1 = null;
  1189. task = null;
  1190. System.GC.Collect();
  1191. }
  1192. }
  1193. private void callBackDefectEvent(Device.DefectLib.DefectTask res)
  1194. {
  1195. {
  1196. int step = 0;
  1197. try
  1198. {
  1199. AddTextEvent(DateTime.Now,$"检测完成{Thread.CurrentThread.ManagedThreadId}", $"图像队列:{res.record.ScannerPhotoFinishCount+1}/{res.record.ScannerPhotoCount} (图像{res.photoIndex})检测结果:{res.isSucceed}");
  1200. string dirPath = FileUtil.initFolder($"{Config.ImagePath}{res.record.BatchId}_{res.record.ReelId}\\");
  1201. string dirSourcePath = FileUtil.initFolder($"{Config.ImagePath}{res.record.BatchId}_{res.record.ReelId}\\源图\\");
  1202. //Cv2.Flip(res.bmp, res.bmp, FlipMode.XY);//翻转
  1203. if (Config.IsSaveAllImage)//保存所有原图
  1204. OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmp).Save($"{dirSourcePath}{res.photoIndex}.bmp", ImageFormat.Bmp);
  1205. if (res.isSucceed)
  1206. {
  1207. step = 1;
  1208. AddTextEvent(DateTime.Now,$"检测完成{Thread.CurrentThread.ManagedThreadId}", $"(图像{res.photoIndex})-瑕疵检测完成,共{res.excelTable.Rows.Count}个瑕疵!各环节用时:{string.Join(",",res.stopwatch)}");
  1209. //AddTextEvent(DateTime.Now,$"打标完成", $"第 ({res.tag}) 张照片,计算过程:{res.resultInfo}");
  1210. if (!Config.IsSaveAllImage && Config.IsSaveDefectSourceImage)
  1211. OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmp).Save($"{dirSourcePath}{res.photoIndex}.bmp", ImageFormat.Bmp);
  1212. step = 2;
  1213. if (res.excelTable.Rows.Count > 0)
  1214. {
  1215. res.record.dicPhoto_Defect[res.photoIndex] = true;//改为此图有瑕疵
  1216. //有瑕疵打标图必需保存 Jpeg
  1217. OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmpTag).Save($"{dirPath}{res.photoIndex}_tag.jpg", ImageFormat.Jpeg);
  1218. step = 3;
  1219. res.record.DefectTotalCount += res.excelTable.Rows.Count;
  1220. if (res.record.DefectInfoList == null)
  1221. res.record.DefectInfoList = new List<DefectInfo>();
  1222. step = 4;
  1223. JObject defectNameInfo;
  1224. DefectInfo defectInfo=null;
  1225. List<object[]> dataRowlist=new List<object[]>();
  1226. long preTicks = pStopWatch.ElapsedMilliseconds;// DateTime.Now.Ticks;
  1227. for (int i = 0; i < res.lstDefectBmp.Count; i++)
  1228. {
  1229. step = 5 + i * 10;
  1230. defectNameInfo = Config.getDefectItem(int.Parse(res.excelTable.Rows[i]["类别"].ToString()));
  1231. defectInfo = new DefectInfo
  1232. {
  1233. PhotoIndex = res.photoIndex,
  1234. Code = defectNameInfo.Value<string>("code"),
  1235. Name = defectNameInfo.Value<string>("name"),
  1236. X = double.Parse(res.excelTable.Rows[i]["X"].ToString()),//cm
  1237. Y = Math.Round((res.photoIndex * res.bmp.Height * 1.0d / Config.cm2px_y + double.Parse(res.excelTable.Rows[i]["Y"].ToString())), 2),//cm
  1238. Width = double.Parse(res.excelTable.Rows[i]["W"].ToString()),//cm
  1239. Height = double.Parse(res.excelTable.Rows[i]["H"].ToString()),//cm
  1240. ZXD = double.Parse(res.excelTable.Rows[i]["置信度"].ToString()),
  1241. Contrast = double.Parse(res.excelTable.Rows[i]["对比度"].ToString()),
  1242. Target = int.Parse(res.excelTable.Rows[i]["目标"].ToString()),
  1243. image = BitmapConverter.ToBitmap(res.lstDefectBmp[i])
  1244. };
  1245. defectInfo.ModifyUserCode = defectInfo.CreateUserCode = res.record.CreateUserCode;
  1246. step = 6 + i * 10;
  1247. res.record.DefectInfoList.Add(defectInfo);
  1248. defectInfo.uid = preTicks++;// res.record.DefectInfoList.Count;//程序中的唯一索引,用于移除用索引
  1249. //AddTextEvent(DateTime.Now,$"打标完成", $"第{i}个缺陷:{ JsonConvert.SerializeObject(defectInfo)}; Y={res.photoIndex * res.bmp.Height * 1.0d / Config.cm2px_y}+{res.excelTable.Rows[i]["Y"].ToString()}");
  1250. step = 7 + i * 10;
  1251. //保存打标小图
  1252. if (Config.IsSaveDefectCutImage)
  1253. {
  1254. string filename = $"{dirPath}\\{res.photoIndex}_X{defectInfo.X}_Y{defectInfo.Y}_W{defectInfo.Width}_H{defectInfo.Height}_目标{defectInfo.Target}_类别{defectInfo.Code}_置信度{defectInfo.ZXD}.jpg";
  1255. OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.lstDefectBmp[i]).Save(filename, ImageFormat.Jpeg);
  1256. defectInfo.TagFilePath = filename;
  1257. }
  1258. step = 8 + i * 10;
  1259. res.lstDefectBmp[i].Dispose();
  1260. dataRowlist.Add(new object[]{ defectInfo.uid,defectInfo.Code, defectInfo.PhotoIndex,defectInfo.Name,
  1261. defectInfo.CentreX, defectInfo.CentreY / 100,defectInfo.Width,defectInfo.Height, defectInfo.Area, defectInfo.ZXD, defectInfo.Contrast});
  1262. //更新UI
  1263. //this.Invoke(new System.Action(() =>
  1264. //{
  1265. // this.uiDataGridView1.Rows.Insert(0, );
  1266. // this.uiMiniPagination1.TotalCount = res.record.DefectInfoList.Count;
  1267. // if (this.uiDataGridView1.Rows.Count == 1)
  1268. // this.picDefectImage.loadImage(defectInfo.image);
  1269. //}));
  1270. step = 9 + i * 10;
  1271. //告警判断???在此还是在收到新照片时触发???
  1272. if (res.record.ProductInfo.DefectAreaLimit > 0 && defectInfo.Area>=res.record.ProductInfo.DefectAreaLimit)
  1273. {
  1274. AddTextEvent(DateTime.Now,$"告警{Thread.CurrentThread.ManagedThreadId}", $"瑕疵面积达到阈值!({defectInfo.Area}>={res.record.ProductInfo.DefectAreaLimit})", WarningEnum.High);
  1275. }
  1276. }
  1277. AddTextEvent(DateTime.Now,$"检测完成{Thread.CurrentThread.ManagedThreadId}", "更新UI");
  1278. //更新UI
  1279. int bmpHeight = res.bmp.Height;
  1280. this.BeginInvoke(new System.Action(() =>
  1281. {
  1282. //这里显示了第1张图的最后一个缺陷
  1283. if (this.uiDataGridView1.Rows.Count == 0 && defectInfo!=null)
  1284. this.picDefectImage.loadImage(defectInfo.image);
  1285. foreach (var rowItem in dataRowlist)
  1286. this.uiDataGridView1.Rows.Insert(0, rowItem);
  1287. this.uiMiniPagination1.TotalCount = res.record.DefectInfoList.Count;
  1288. //
  1289. double len = Math.Round((res.photoIndex + 1) * bmpHeight * 1.0d / Config.cm2px_y+0.005f, 2);
  1290. this.reDrawDefectPoints(res.record.DefectInfoList, new double[] { 0, Math.Round(res.record.FaceWidthMax+ 0.005f, 2) }, new double[] { 0, len });
  1291. }));
  1292. step = 9;
  1293. AddTextEvent(DateTime.Now,$"检测完成{Thread.CurrentThread.ManagedThreadId}", "保存CSV");
  1294. //保存CSV
  1295. bool b = Utils.ExcelUtil.DataTable2CSV($"{dirPath}{res.photoIndex}.csv", res.excelTable);
  1296. //AddTextEvent(DateTime.Now,$"打标完成", $"{res.tag}.xlsx {(b ? "保存成功!" : "保存失败!")}");
  1297. step = 10;
  1298. //每百米告警判断???在此还是在收到新照片时触发???
  1299. if (res.record.ProductInfo.DefectCountLimit > 0 && res.record.DefectTotalCount >= res.record.ProductInfo.DefectCountLimit)
  1300. {
  1301. int compLen = 100 * 100;//每百米 to cm
  1302. int compCount = compLen * Config.cm2px_y / res.bmp.Height;
  1303. //从上次告警后重新开始计算长度及数量
  1304. int defectCount = res.record.DefectInfoList.Where(m => m.PhotoIndex >= res.record.preWarningPhotoIndex && m.PhotoIndex >= res.photoIndex+1 - compCount).Count();
  1305. if (defectCount >= res.record.ProductInfo.DefectCountLimit)
  1306. {
  1307. res.record.preWarningPhotoIndex = res.photoIndex + 1;
  1308. AddTextEvent(DateTime.Now,$"告警{Thread.CurrentThread.ManagedThreadId}", $"每百米瑕疵数量达到阈值!({defectCount}>={res.record.ProductInfo.DefectCountLimit})", WarningEnum.High);
  1309. }
  1310. }
  1311. }
  1312. }
  1313. else
  1314. {
  1315. AddTextEvent(DateTime.Now,$"打标失败{Thread.CurrentThread.ManagedThreadId}", $"(图像{res.photoIndex})-瑕疵检测失败!TId={Thread.CurrentThread.ManagedThreadId}");
  1316. }
  1317. res.bmp.Dispose();
  1318. res.bmpTag.Dispose();
  1319. res.bmps_cut = null;
  1320. res.excelTable.Dispose();
  1321. }
  1322. catch (Exception ex)
  1323. {
  1324. AddTextEvent(DateTime.Now,$"打标失败{Thread.CurrentThread.ManagedThreadId}", $"(图像{res.photoIndex})-瑕疵检测异常({step}):{ex.Message},TId={Thread.CurrentThread.ManagedThreadId}");
  1325. }
  1326. finally
  1327. {
  1328. res.record.ScannerPhotoFinishCount++;
  1329. int liScannerPhotoFinishCount = res.record.ScannerPhotoFinishCount;
  1330. int liScannerPhotoCount = res.record.ScannerPhotoCount;
  1331. AddTextEvent(DateTime.Now,$"检测完成{Thread.CurrentThread.ManagedThreadId}", $"{liScannerPhotoFinishCount}/{liScannerPhotoCount}");
  1332. //this.BeginInvoke(new System.Action(() =>
  1333. //{
  1334. // this.lblWaitImageCount.Text = $"{liScannerPhotoCount - liScannerPhotoFinishCount}";
  1335. //}));
  1336. res = null;
  1337. System.GC.Collect();
  1338. }
  1339. }
  1340. }
  1341. private bool _isDefect = false;
  1342. private async void saveCurrRecord(int key, string batchId, string reelId, double erpLen)
  1343. {
  1344. Records model=null;
  1345. int step = 0;
  1346. try
  1347. {
  1348. _isDefect = true;
  1349. AddTextEvent(DateTime.Now,"入库", $"准备入库key={key}");
  1350. //foreach (int itemKey in htTask.Keys)
  1351. // AddTextEvent(DateTime.Now,"入库", $"htTask {itemKey}");
  1352. step = 1;
  1353. model = Hashtable.Synchronized(htTask)[key] as Records;
  1354. //model = htTask[key] as Records;
  1355. step = 2;
  1356. if (model.Len == 0)
  1357. {
  1358. _isDefect = false;
  1359. return;
  1360. }
  1361. model.BatchId = batchId;
  1362. model.ReelId = reelId;
  1363. model.ErpLen = erpLen;
  1364. while (model.ScannerPhotoCount > model.ScannerPhotoFinishCount)
  1365. await Task.Delay(100);
  1366. step = 3;
  1367. //计算等级标准
  1368. List<GradeLimit> gradeLimitList = model.ProductInfo.GradeLimitList;
  1369. if (gradeLimitList!=null && gradeLimitList.Count > 0)
  1370. {
  1371. step = 4;
  1372. int count;
  1373. foreach(GradeLimit item in gradeLimitList)
  1374. {
  1375. count = model.DefectInfoList.Where(m => m.Code == item.Code).Count();
  1376. if (count <= item.A && model.Grade <= 1) model.Grade = 1;
  1377. else if (count <= item.B && item.B > 0 && model.Grade <= 2) model.Grade = 2;
  1378. else if (count <= item.C && item.C > 0 && model.Grade <= 3) model.Grade = 3;
  1379. else if (count <= item.D && item.D > 0 && model.Grade <= 4) model.Grade = 4;
  1380. else if (count <= item.E && item.E > 0 && model.Grade <= 5) model.Grade = 5;
  1381. else if (count>0) model.Grade = 6;//不合格
  1382. AddTextEvent(DateTime.Now,"标准判断", $"({key}) 批号({model.BatchId}),标准={(char)(model.Grade + 64)} [{item.Code}:{count};A<={item.A};B<={item.B};C<={item.C};D<={item.D};E<={item.E}]");
  1383. }
  1384. step = 5;
  1385. }
  1386. model.Qualified = (model.Grade < 6);//是否合格
  1387. if (!svcRecord.InsertNav(model))
  1388. throw new Exception("写库失败!");
  1389. AddTextEvent(DateTime.Now,"入库完成", $"({key}) 批号({model.BatchId})已完成检测。");
  1390. htTask.Remove(key);
  1391. _isDefect = false;
  1392. }
  1393. catch (Exception ex)
  1394. {
  1395. _isDefect = false;
  1396. if (model==null) AddTextEvent(DateTime.Now,"入库失败", $"记录({key})不存在{step}!" + ex.Message, WarningEnum.High);
  1397. else AddTextEvent(DateTime.Now,"入库失败", $"({key}) 批号({model.BatchId})检测完成,但保存检测记录失败{step}:" + ex.Message, WarningEnum.High);
  1398. warning(WarningEnum.High, true);//暂停
  1399. }
  1400. }
  1401. //停机
  1402. private void btnClose_Click(object sender, EventArgs e)
  1403. {
  1404. if (currentState == CurrentStateEnum.运行中)
  1405. {
  1406. UIMessageTip.ShowWarning("请先结束或暂停设备运行后再停机!", 2000);
  1407. return;
  1408. }
  1409. if(_isDefect)
  1410. {
  1411. UIMessageTip.ShowWarning("还在检测处理中,等待完成再停机!", 2000);
  1412. return;
  1413. }
  1414. AddTextEvent(DateTime.Now,"停机", "停机中...");
  1415. this.btnStart.Enabled = this.btnEnd.Enabled = this.btnPause.Enabled = false;
  1416. this.btnClose.Enabled = false;
  1417. this.btnOpen.Enabled = true;
  1418. tcbarLightValue.Enabled = false;
  1419. if (devContainer.state && devContainer.devIOCard.IsInit)
  1420. {
  1421. devContainer.devIOCard.reset();
  1422. devContainer.io_output(CMDName.IO默认输出);
  1423. }
  1424. devContainer.stop();
  1425. currentState = CurrentStateEnum.初始;//应该是待机
  1426. this.resetUIValue();
  1427. AddTextEvent(DateTime.Now,"停机", "停机完成。");
  1428. }
  1429. //启动
  1430. private void btnStart_Click(object sender, EventArgs e)
  1431. {
  1432. AddTextEvent(DateTime.Now,"启动", "下发启动指令...");
  1433. if (!Config.StopPLC)
  1434. this.devContainer.devPlc.runDev();
  1435. else if (!Config.StopIO && devContainer.devIOCard.IsInit)
  1436. {
  1437. if (!compareIOInput(CMDName.暂停按钮))//硬件急停
  1438. {
  1439. this.devContainer.devIOCard.writeBitState(0, 0, true);
  1440. this.startCommand();
  1441. Task.Run(async () =>
  1442. {
  1443. await Task.Delay(500);
  1444. this.devContainer.devIOCard.writeBitState(0, 0, false);
  1445. });
  1446. }
  1447. }
  1448. }
  1449. private void btnPause_Click(object sender, EventArgs e)
  1450. {
  1451. AddTextEvent(DateTime.Now,"暂停", "下发暂停指令...");
  1452. if (!Config.StopPLC)
  1453. this.devContainer.devPlc.pauseDev();
  1454. else if (!Config.StopIO && devContainer.devIOCard.IsInit)
  1455. {
  1456. //if (!compareIOInput(CMDName.暂停按钮))
  1457. this.devContainer.devIOCard.writeBitState(0, 1, true);
  1458. this.pauseCommand();//输出暂停不会触发输入暂停
  1459. Task.Run(async () =>
  1460. {
  1461. await Task.Delay(500);
  1462. this.devContainer.devIOCard.writeBitState(0, 1, false);
  1463. });
  1464. }
  1465. }
  1466. private void startCommand()
  1467. {
  1468. if (!devContainer.state || currentState == CurrentStateEnum.运行中)
  1469. return;
  1470. if (devContainer.devIOCard.IsInit)
  1471. {
  1472. //devContainer.io_output(CMDName.启动按钮);
  1473. devContainer.io_output(CMDName.绿灯输出);
  1474. devContainer.io_output(CMDName.黄灯输出, false, true, 0);
  1475. devContainer.io_output(CMDName.红灯输出, false, true, 0);
  1476. devContainer.io_output(CMDName.蜂鸣器输出, false, true, 0);
  1477. //devContainer.io_output(CMDName.暂停按钮, false, true, 0);
  1478. //devContainer.io_output(CMDName.黄灯输出, false, true, 0);
  1479. }
  1480. //运行中和暂停时都可正常接收扫描器触发新卷
  1481. //暂停-》继续
  1482. if (currentState == CurrentStateEnum.暂停)
  1483. {
  1484. AddTextEvent(DateTime.Now,"启动", $"暂停 -> 继续");
  1485. currentState = CurrentStateEnum.运行中;
  1486. Task.Run(() =>
  1487. {
  1488. if (currKey > 0)
  1489. pStopWatch.Start();
  1490. //写I/O启动。。。
  1491. //继续读取编译器和门幅等
  1492. //int nextStepId = currProcessIndex;
  1493. //do
  1494. //{
  1495. // currentState = CurrentStateEnum.运行中;
  1496. // nextStepId = nextProcess(currProductModel, nextStepId);
  1497. //} while (nextStepId >= 0 && !isBreakProcessRun());
  1498. });
  1499. }
  1500. else//首次开始/结束后重新开始
  1501. {
  1502. //校正从复位-》运行,不会新启动
  1503. resetUIValue();
  1504. AddTextEvent(DateTime.Now,"启动", "等待扫码...");
  1505. currentState = CurrentStateEnum.运行中;
  1506. }
  1507. this.Invoke(new System.Action(() =>
  1508. {
  1509. this.btnStart.Enabled = false;
  1510. this.btnEnd.Enabled = this.btnPause.Enabled = true;
  1511. }));
  1512. }
  1513. private void pauseCommand(bool buzzer = false)
  1514. {
  1515. if(!devContainer.state || currentState != CurrentStateEnum.运行中)
  1516. return;
  1517. //写IO
  1518. if (devContainer.devIOCard.IsInit)
  1519. {
  1520. devContainer.io_output(CMDName.绿灯输出, false, true, 0);
  1521. if (buzzer)
  1522. {
  1523. devContainer.io_output(CMDName.红灯输出);
  1524. devContainer.io_output(CMDName.蜂鸣器输出);
  1525. }
  1526. else
  1527. devContainer.io_output(CMDName.黄灯输出);
  1528. //devContainer.io_output(CMDName.暂停按钮, false, true, 0);
  1529. //devContainer.io_output(CMDName.黄灯输出, false, true, 0);
  1530. }
  1531. //停止读取
  1532. //AddTextEvent(DateTime.Now,"暂停", "暂停!");
  1533. pStopWatch.Stop();
  1534. currentState = CurrentStateEnum.暂停;
  1535. this.Invoke(new System.Action(() =>
  1536. {
  1537. this.btnStart.Enabled = this.btnEnd.Enabled = true;
  1538. this.btnPause.Enabled = false;
  1539. }));
  1540. }
  1541. //完成
  1542. private void btnEnd_Click(object sender, EventArgs e)
  1543. {
  1544. AddTextEvent(DateTime.Now,"结束验布", "结束验布!");
  1545. if (!Config.StopPLC)
  1546. this.devContainer.devPlc.pauseDev();
  1547. else if (!Config.StopIO)
  1548. {
  1549. this.devContainer.devIOCard.writeBitState(0, 1, true);
  1550. Task.Run(async () =>
  1551. {
  1552. await Task.Delay(500);
  1553. this.devContainer.devIOCard.writeBitState(0, 1, false);
  1554. });
  1555. }
  1556. currentState = CurrentStateEnum.初始;
  1557. UILocalize.OK = "是";
  1558. UILocalize.Cancel = "否";
  1559. if (currKey > 0 && ShowAskDialog("提示", "是否保存当前检测结果?"))
  1560. {
  1561. string szBatchId, szReelId;
  1562. double ldErpLen;
  1563. szBatchId = txtBatchId.Text.Trim();
  1564. szReelId = txtReelId.Text.Trim();
  1565. ldErpLen = numErpLen.IsEmpty ? 0 : Convert.ToDouble(numErpLen.Text.Trim());
  1566. int myKey = currKey;
  1567. Task.Run(() => { saveCurrRecord(myKey, szBatchId, szReelId, ldErpLen); });
  1568. resetUIValue();
  1569. pStopWatch.Stop();
  1570. this.btnStart.Enabled = true;
  1571. this.btnEnd.Enabled = this.btnPause.Enabled = false;//这里有问题,应该是devPlc回调设置
  1572. }
  1573. }
  1574. private void lstboxLog_DrawItem(object sender, DrawItemEventArgs e)
  1575. {
  1576. if (e.Index < 0) return;
  1577. string text = lstboxLog.GetItemText(e.Index);
  1578. //if (text.Contains("D"))
  1579. //{
  1580. // e.Graphics.FillRectangle(UIColor.Green, e.Bounds);
  1581. // e.Graphics.DrawString(text, e.Font, Color.Blue, e.Bounds, ContentAlignment.MiddleLeft);
  1582. //}
  1583. //e.Graphics.FillRectangle(UIColor.Green, e.Bounds);
  1584. switch (text[0])
  1585. {
  1586. case 'R':
  1587. e.DrawBackground();
  1588. e.Graphics.DrawString(text.Substring(1), e.Font, Color.Red, e.Bounds, ContentAlignment.MiddleLeft);
  1589. break;
  1590. case 'Y':
  1591. e.DrawBackground();
  1592. e.Graphics.DrawString(text.Substring(1), e.Font, Color.Orange, e.Bounds, ContentAlignment.MiddleLeft);
  1593. break;
  1594. //default: //B
  1595. // e.Graphics.DrawString(text.Substring(1), e.Font, Color.Black, e.Bounds, ContentAlignment.MiddleLeft);
  1596. // break;
  1597. }
  1598. }
  1599. private void txtBarCode_KeyDown(object sender, KeyEventArgs e)
  1600. {
  1601. if(e.KeyCode == Keys.Enter)
  1602. {
  1603. string barcode=txtBarCode.Text.Trim();
  1604. if (barcode == "")
  1605. return;
  1606. if (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停)
  1607. return;
  1608. devContainer.devCodeScanner.ScanerEvent?.Invoke(barcode);
  1609. }
  1610. }
  1611. private void showImg( Mat mat)
  1612. {
  1613. //把Mat格式的图片转换成Bitmap
  1614. Bitmap bitmap = BitmapConverter.ToBitmap(mat);
  1615. this.Invoke(new System.Action(() =>
  1616. {
  1617. //显示图片
  1618. //picDefectImage.loadImage(bitmap);
  1619. this.picScanner1.Image = bitmap;
  1620. }));
  1621. }
  1622. private void uiSymbolButton1_Click(object sender, EventArgs e)
  1623. {
  1624. try
  1625. {
  1626. }
  1627. catch (Exception ex)
  1628. {
  1629. MessageBox.Show(ex.Message);
  1630. }
  1631. }
  1632. int mynum = 0;
  1633. private void uiSymbolButton1_Click_1(object sender, EventArgs e)
  1634. {
  1635. //UIMessageTip.ShowOk($"{record.Len},{record.ScannerPhotoCount-record.ScannerPhotoFinishCount}", 1500);
  1636. //return;
  1637. try
  1638. {
  1639. // mynum++;
  1640. // int FaceWidth = 200;
  1641. // float faceWidthX_cm = (float)(mynum * 100 * 1.0f / Config.cm2px_y );
  1642. // float faceWidthY_cm = (float)(200 * 1.0f / Config.cm2px_y );
  1643. // var point = new System.Drawing.PointF(faceWidthX_cm, faceWidthY_cm);
  1644. // AddTextEvent(DateTime.Now,"门幅", $"位置:{point.X}; 幅宽:{point.Y}");
  1645. // lstFaceWidth.Add(point);
  1646. // reDrawFaceWidth(lstFaceWidth, new double[] { 0, point.X }, new double[] { faceWidthY_cm, faceWidthY_cm });
  1647. // //Mat mat = new Mat(Application.StartupPath + "\\1.bmp");
  1648. // //devContainer.devScannerGentl1.ScanEvent(1, mat, 1);
  1649. // //---------------------
  1650. // if (record == null)
  1651. // {
  1652. // record = new Records();
  1653. // record.DefectInfoList = new List<DefectInfo>();
  1654. // record.DefectInfoList.Add(new DefectInfo() { Code = "jb", Name = "浆斑", X = 20, Y = 2, Width = 2, Height = 2 });
  1655. // record.DefectInfoList.Add(new DefectInfo() { Code = "jb", Name = "浆斑", X = 20, Y = 5, Width = 2, Height = 2 });
  1656. // record.DefectInfoList.Add(new DefectInfo() { Code = "wy", Name = "污印", X = 50, Y = 2, Width = 4, Height = 4 });
  1657. // record.DefectInfoList.Add(new DefectInfo() { Code = "lj", Name = "垃圾", X = 60, Y = 5, Width = 2, Height = 2 });
  1658. // reDrawDefectPoints(record.DefectInfoList, new double[2] { 0, 10 }, new double[2] { 0, 100 });
  1659. // foreach (DefectInfo info in record.DefectInfoList)
  1660. // {
  1661. // this.uiDataGridView1.Rows.Add(info.Code, info.Name,
  1662. // info.CentreX, info.CentreY, info.Area, info.ZXD, info.Target);
  1663. // }
  1664. // }
  1665. }
  1666. catch (Exception ex)
  1667. {
  1668. MessageBox.Show(ex.Message);
  1669. }
  1670. }
  1671. private void tcbarLightValue_ValueChanged(object sender, EventArgs e)
  1672. {
  1673. if (devContainer.state)
  1674. {
  1675. //bool b1 = devContainer.devScannerGentl1.setParam((float)tcbarExposureTime.Value);
  1676. //bool b2= devContainer.devScannerGentl2.setParam((float)tcbarExposureTime.Value);
  1677. // AddTextEvent(DateTime.Now,"setExposureTime", b1.ToString());
  1678. // AddTextEvent(DateTime.Now,"setExposureTime", b2.ToString());
  1679. var res=devContainer.devLight.setDigitalValue(1, (int)tcbarLightValue.Value);
  1680. //UIMessageTip.ShowOk($"{(int)tcbarLightValue.Value}/255 var={res} {Config.Light_Name}", 1000);
  1681. //tpnlLight.Text = this.tpnlLight.Tag + $" ({(int)tcbarLightValue.Value}/255)";
  1682. }
  1683. }
  1684. private void swcDefectPauseForUser_ValueChanged(object sender, bool value)
  1685. {
  1686. this.defectPauseForUser = this.swcDefectPauseForUser.Active;
  1687. }
  1688. private void button1_Click(object sender, EventArgs e)
  1689. {
  1690. Config.LoadAllConfig();
  1691. DefectLib dl = new DefectLib();
  1692. dl.start();
  1693. string[] files = Directory.GetFiles("E:\\CPL\\个人\\gePic", $"*.bmp", SearchOption.TopDirectoryOnly);
  1694. List<QualifiedLimit> list = new List<QualifiedLimit>();
  1695. list.Add(new QualifiedLimit() { Code = "jb",ZXD = 0.6, Area = 0.04, ContrastLower = 0.98, ContrastTop = 1.02});
  1696. list.Add(new QualifiedLimit() { Code = "wy", ZXD = 0.6, Area = 0.09, ContrastLower = 0.93, ContrastTop = 1.07 });
  1697. list.Add(new QualifiedLimit() { Code = "mj", ZXD = 0.6, Area = 0.04, ContrastLower = 0.96, ContrastTop = 1.04 });
  1698. list.Add(new QualifiedLimit() { Code = "hy", ZXD = 0.67, Area = 0.09, ContrastLower = 0.94, ContrastTop = 1.06 });
  1699. list.Add(new QualifiedLimit() { Code = "lj", ZXD = 0.7, Area = 0.08, ContrastLower = 0.99, ContrastTop = 1.01 });
  1700. list.Add(new QualifiedLimit() { Code = "yss", ZXD = 0.5, Area = 0.04, ContrastLower = 0.99, ContrastTop = 1.01 });
  1701. list.Add(new QualifiedLimit() { Code = "zy", ZXD = 0.8, Area = 2, ContrastLower = 0.99, ContrastTop = 1.01 });
  1702. list.Add(new QualifiedLimit() { Code = "wc", ZXD = 0.6, Area = 0.05, ContrastLower = 0.99, ContrastTop = 1.01 });
  1703. list.Add(new QualifiedLimit() { Code = "cs", ZXD = 0.8, Area = 2, ContrastLower = 0.99, ContrastTop = 1.01 });
  1704. list.Add(new QualifiedLimit() { Code = "cy", ZXD = 1, Area = 0.0, ContrastLower = 0.0, ContrastTop = 0 });
  1705. list.Add(new QualifiedLimit() { Code = "tcy", ZXD = 0.5, Area = 0.0, ContrastLower = 0.99, ContrastTop = 1.01 });
  1706. list.Add(new QualifiedLimit() { Code = "jt", ZXD = 1, Area = 0.0, ContrastLower = 0.0, ContrastTop = 0 });
  1707. for (int i = 0; i < files.Count(); i++)
  1708. {
  1709. Mat mat = new Mat(files[i]);
  1710. dl.add(new Device.DefectLib.DefectTask()
  1711. {
  1712. modelName = "best_0116_bs14.fp16.trt",
  1713. //record = curRecord,
  1714. bmp = mat.Clone(),
  1715. bmpTag = mat.Clone(),
  1716. photoIndex = i,//0-n 首张必需为0,因下面计算长度是从0开始
  1717. widthRatio = 1,
  1718. qualifiedLimitList = list,
  1719. finishEvent = callBackDefectTestEvent,
  1720. });
  1721. }
  1722. string s = DateTime.Now.Ticks.ToString() + "-";
  1723. Thread.Sleep(1);
  1724. s += DateTime.Now.Ticks.ToString() + "-";
  1725. MessageBox.Show(s);
  1726. // 创建SqlSugarClient实例并配置连接字符串
  1727. //var db = new SqlSugarClient(new ConnectionConfig()
  1728. //{
  1729. // // 设置数据库类型为SQLServer
  1730. // DbType = SqlSugar.DbType.SqlServer,
  1731. // // 设置服务器地址、数据库名称以及登录信息等
  1732. // ConnectionString = "Data Source=.;Initial Catalog=testDB;User ID=sa;Password=abc123!@#;"
  1733. //});
  1734. //// 可选:打开调试日志输出
  1735. //db.Ado.IsEnableLogEvent = true;
  1736. //// 查询操作示例
  1737. //if (!db.Ado.IsValidConnection())
  1738. // db.Ado.Open();
  1739. //string sql = "select * from t2";
  1740. //List<SugarParameter> parameters = new List<SugarParameter>();
  1741. //var res= db.Ado.GetDataTable(sql, parameters);
  1742. //loadErpData("20240107492");
  1743. return;
  1744. string code = "SHNY-PX-6-L-100";
  1745. string[] barCodes = code.Split(new char[] { '-' });
  1746. if (barCodes.Length < 4)
  1747. {
  1748. AddTextEvent(DateTime.Now,"扫码", $"产品编码({code})格式错误,不做响应!", WarningEnum.High);
  1749. return;
  1750. }
  1751. //新开始
  1752. //加载新产品
  1753. string pcode = barCodes[0] + "-" + barCodes[1] + "-" + barCodes[2];
  1754. var productInfo = svcProduct.GetModelNav(pcode); //frmProduct.loadProduct(code);
  1755. if (productInfo == null)
  1756. {
  1757. AddTextEvent(DateTime.Now,"扫码", $"编码({code})不存在,请先添加产品,暂停设备!", WarningEnum.High);
  1758. this.BeginInvoke(new System.Action(() =>
  1759. {
  1760. frmProduct.loadProduct(pcode);//转到新建编码
  1761. }));
  1762. return;
  1763. }
  1764. AddTextEvent(DateTime.Now,"扫码", $"编码({code}),加载产品信息({productInfo.Code})完成,加载配方(光源={productInfo.LightValue},曝光={productInfo.ExposureTime},增益={productInfo.Gain})...");
  1765. if (productInfo.LightValue > 0)//光源 - 通道0
  1766. devContainer.devLight.setDigitalValue(1, productInfo.LightValue);
  1767. if (productInfo.ExposureTime > 0 || productInfo.Gain > 0)//相机曝光 增益
  1768. {
  1769. devContainer.devCamer1.setParam((float)(productInfo.ExposureTime > 0 ? productInfo.ExposureTime : -1), (float)(productInfo.Gain > 0 ? productInfo.Gain : -1));
  1770. devContainer.devCamer2.setParam((float)(productInfo.ExposureTime > 0 ? productInfo.ExposureTime : -1), (float)(productInfo.Gain > 0 ? productInfo.Gain : -1));
  1771. }
  1772. AddTextEvent(DateTime.Now,"扫码", $"编码({code}),配方设置完成:光源={productInfo.LightValue},曝光={productInfo.ExposureTime}");
  1773. //ABSCamerCardDev pDev = new CamerCardDevIK();
  1774. //pDev.WarningEvent = (level, msg) =>
  1775. //{
  1776. // AddTextEvent(DateTime.Now,"设备事件", msg, level);
  1777. //};
  1778. //var b = pDev.open(0, 0);
  1779. //MessageBox.Show(b.ToString());
  1780. ////b = pDev.loadConfiguration(@"D:\Debug\DevCfg\wcf.vlcf");
  1781. //b =pDev.start(this.picScanner1,"c:\\");
  1782. //MessageBox.Show(b.ToString());
  1783. //====
  1784. //Mat mat0= new Mat(@"f:\2.bmp");
  1785. //int marginWidth0;
  1786. //mat0 = OpenCVUtil.getMaxInsetRect2(mat0, true, 0, out marginWidth0);
  1787. //Mat mat1 = new Mat(@"f:\1.bmp");
  1788. //mat1 = OpenCVUtil.getMaxInsetRect2(mat1, false, 0, out marginWidth0);
  1789. //lblLen.Text = "0";
  1790. //Task.Run(async () => {
  1791. // for(int i = 0; i < 100; i++)
  1792. // {
  1793. // //选中
  1794. // this.Invoke(new System.Action(() =>
  1795. // {
  1796. // lblLen.Text = Convert.ToInt32(lblLen.Text) + 1 + "";
  1797. // }));
  1798. // await Task.Delay(1000);
  1799. // }
  1800. //});
  1801. //FHome_Defect frm = new FHome_Defect();
  1802. //frm.ShowDialog();
  1803. }
  1804. #region 测试
  1805. private void callBackDefectTestEvent(Device.DefectLib.DefectTask res)
  1806. {
  1807. {
  1808. int step = 0;
  1809. try
  1810. {
  1811. if (res.isSucceed)
  1812. {
  1813. step = 1;
  1814. AddTextEvent(DateTime.Now, $"检测完成{Thread.CurrentThread.ManagedThreadId}", $"(图像{res.photoIndex})-瑕疵检测完成,共{res.excelTable.Rows.Count}个瑕疵!各环节用时:{string.Join(",", res.stopwatch)}");
  1815. //AddTextEvent(DateTime.Now,$"打标完成", $"第 ({res.tag}) 张照片,计算过程:{res.resultInfo}");
  1816. }
  1817. else
  1818. {
  1819. AddTextEvent(DateTime.Now, $"打标失败{Thread.CurrentThread.ManagedThreadId}", $"(图像{res.photoIndex})-瑕疵检测失败!TId={Thread.CurrentThread.ManagedThreadId}");
  1820. }
  1821. res.bmp.Dispose();
  1822. res.bmpTag.Dispose();
  1823. res.bmps_cut = null;
  1824. res.excelTable.Dispose();
  1825. }
  1826. catch (Exception ex)
  1827. {
  1828. AddTextEvent(DateTime.Now, $"打标失败{Thread.CurrentThread.ManagedThreadId}", $"(图像{res.photoIndex})-瑕疵检测异常({step}):{ex.Message},TId={Thread.CurrentThread.ManagedThreadId}");
  1829. }
  1830. finally
  1831. {
  1832. res.record.ScannerPhotoFinishCount++;
  1833. int liScannerPhotoFinishCount = res.record.ScannerPhotoFinishCount;
  1834. int liScannerPhotoCount = res.record.ScannerPhotoCount;
  1835. AddTextEvent(DateTime.Now, $"检测完成{Thread.CurrentThread.ManagedThreadId}", $"{liScannerPhotoFinishCount}/{liScannerPhotoCount}");
  1836. //this.BeginInvoke(new System.Action(() =>
  1837. //{
  1838. // this.lblWaitImageCount.Text = $"{liScannerPhotoCount - liScannerPhotoFinishCount}";
  1839. //}));
  1840. res = null;
  1841. System.GC.Collect();
  1842. }
  1843. }
  1844. }
  1845. #endregion
  1846. private void numErpLen_ValueChanged(object sender, string value)
  1847. {
  1848. //numErpLen_TextChanged(sender, null);
  1849. if (numErpLen.IsEmpty || currKey == 0) return;
  1850. var val = Convert.ToDouble(numErpLen.Text);
  1851. if (val <= 0) return;
  1852. Records record = Hashtable.Synchronized(htTask)[currKey] as Records;
  1853. if (record != null)
  1854. record.ErpLen = val;
  1855. }
  1856. private void numErpLen_KeyUp(object sender, KeyEventArgs e)
  1857. {
  1858. if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
  1859. {
  1860. if (numErpLen.IsEmpty || currKey == 0) return;
  1861. var val = Convert.ToDouble(numErpLen.Text);
  1862. if (val <= 0) return;
  1863. Records record = Hashtable.Synchronized(htTask)[currKey] as Records;
  1864. if (record != null)
  1865. record.ErpLen = val;
  1866. }
  1867. }
  1868. }
  1869. }