革博士程序V1仓库
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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