#define OnLine
#define JM
//#define Oracle //禾欣使用oracle数据库
#define NT //新流程
//#define UPDATA
using Automation.BDaq;
using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using HalconDotNet;
using HZH_Controls;
using IKapC.NET;
using Irony.Parsing;
using LeatherApp.Device;
using LeatherApp.Device.CamerUtil;
using LeatherApp.Interface;
using LeatherApp.UIExtend;
using LeatherApp.Utils;
using Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using S7.Net;
using Service;
using SqlSugar;
using SqlSugar.DbConvert;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
namespace LeatherApp.Page
{
public partial class FHome : UIPage
{
private double LineDataLen = 50;
private int delayTime = 65000;
private WarningEnum warningLevel;//警告等级
private CurrentStateEnum currentState;//当前状态
private DevContainer devContainer = new DevContainer();
private Service.ProductService svcProduct = new Service.ProductService();
private Service.RecordsService svcRecord = new Service.RecordsService();
private bool defectPauseForUser = false;
//private Models.Product currProductModel = null;//当前产品
//private Models.Records record = null;
private Stopwatch pStopWatch=new Stopwatch();//计算速度用,暂停时停止计数
private Stopwatch ptStopWatch = new Stopwatch();//计算速度用,暂停时停止计数
private double ptTime = 0;
private double ptLen = 0;
private object lockScanPhoto = new object();
private object lockCurrKey = new object();
private int currKey=0;
private Hashtable htTask = new Hashtable();//默认单线程写入不用lock, 多线程安全同步读取用Synchronized
//无产品编码时加载
FProductInfo frmProduct;
private double ThnDieLen = 0;
///
/// 分段长度计算量
///
private double SectioningLen = 0;
//private Dictionary defectTag = new Dictionary();
private Dictionary defectTag = new Dictionary();
//裁切偏移
private double OffsetCut = 0.2;
//云端
private CloudMgr cloudMgr = new CloudMgr();
private bool init_Cloud;
//
private bool _residueWarnningLenStop;
private bool _orderWarnningLenStop;
//判断是否已经扫码获取erp信息
private bool _IsGetErpCode = false;
///
/// 当前需要上传的数据
///
private UploadDataRecords CurrUploadDataRecords;
#region 处理类型
private class tScanPhotoInfo
{
///
///
///
///
/// 1-n 第1张会把1改为0
///
public tScanPhotoInfo(int _devIndex, int _photoIndex, string _path)
{
devIndex = _devIndex;
photoIndex = _photoIndex;
path = _path;
}
public tScanPhotoInfo(int _devIndex, int _photoIndex, Mat _mat, double dis = 0)
{
devIndex = _devIndex;
photoIndex = _photoIndex;
mat = _mat;
CurrDis = dis;
}
public int devIndex { get; set; }
///
/// 0-n
///
public int photoIndex { get; set; }
public string path { get; set; }
public Mat mat { get; set; }
public double CurrDis { get; set; }
}
#endregion
//主流程
private CancellationTokenSource _cts;
private ConcurrentQueue _matList1 = new ConcurrentQueue();
private ConcurrentQueue _matList2 = new ConcurrentQueue();
public FHome(FProductInfo frm)
{
InitializeComponent();
frmProduct = frm;
if(Config.Camer_Name == CamerDevNameEnum.埃科)
IKapCLib.ItkManInitialize();
#region dataGridView设置
uiDataGridView1.AllowUserToAddRows = uiDataGridView1.AllowUserToDeleteRows = false;//用户添加删除行
uiDataGridView1.AllowUserToResizeRows = false;//用户调整行大小
uiDataGridView1.AllowUserToResizeColumns = false;//用户调整列大小
uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//只可选中整行,不是单元格
//显示行号与列宽度自动调整
uiDataGridView1.RowHeadersVisible = true;
uiDataGridView1.RowHeadersWidth = 60;
uiDataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
uiDataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
uiDataGridView1.RowPostPaint += (sender, e) =>//序号列头
{
Utils.Util.showRowNum_onDataGrid_RowPostPaint(this.uiDataGridView1, sender, e);
//Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, uiDataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
//TextRenderer.DrawText(e.Graphics, (e.RowIndex).ToString(), uiDataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, uiDataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
};
for (int i = 0; i < uiDataGridView1.Columns.Count; i++)//禁止点击列头排序
uiDataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
////行列交叉处标题
//if (dataGridView1.RowHeadersVisible) dataGridView1.TopLeftHeaderCell.Value = "SPH/CYL";
//事件
//this.uiDataGridView1.DataBindingComplete += this.uiDataGridView1_DataBindingComplete;//bing data时发生,可修改单元格内容
//this.uiDataGridView1.SelectIndexChange += uiDataGridView1_SelectIndexChange;//选择行时发行
this.uiDataGridView1.CellClick += uiDataGridView1_CellClick; ;
#endregion
this.ucColorListDefect.ColorChanged = (xcode, xcolor) =>
{
try
{
//var item = Config.getDefectItem(xcode);
//if (item != null)
//{
// item["color"] = xcolor;
//Config.SaveDefectItemList(Config.defectItemList);
//}
}
catch (Exception ex)
{
this.AddTextEvent(DateTime.Now, "事件", "缺陷颜色修改后保存失败!");
}
};
if (Config.OpenHouDuJiLu)
this.uilbHD.Visible = true;
else
this.uilbHD.Visible = false;
if (Config.CustomerName != "XCL")
{
btnHeight.Visible = false;
btnCut.Visible = false;
btnFenJuan.Visible = false;
radioButton1.Visible = false;
radioButton2.Visible = false;
}
if(Config.CustomerName == "XCL")
{
uiLabel3.Visible = false;
txtBatchId.Visible = false;
radioButton1.Visible = true;
radioButton2.Visible = true;
}
}
private void uiDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
AddTextEvent(DateTime.Now, "表格查询", $"Row:{e.RowIndex} currKey={currKey}", WarningEnum.Normal,false);
if (e.RowIndex < 0 || currKey == 0)
{
UIMessageTip.ShowError($"无记录{e.RowIndex}-{currKey}!", 1000);
return;
}
try
{
Records record = Hashtable.Synchronized(htTask)[currKey] as Records;
//var defectInfo = record.DefectInfoList[record.DefectInfoList.Count - e.RowIndex-1];//按顺序索引引用
//var defectInfo = record.DefectInfoList.FirstOrDefault(m => m.uid == (long)this.uiDataGridView1.CurrentRow.Cells["colUid"].Value);
var defectInfo = record.DefectInfoList.FirstOrDefault(m => m.CentreX == (double)this.uiDataGridView1.CurrentRow.Cells["colX"].Value
&& (m.CentreY/100) == (double)this.uiDataGridView1.CurrentRow.Cells["colY"].Value);
if (defectInfo == null)
{
UIMessageTip.ShowError("此记录已不存在!", 1000);
return;
}
if (defectInfo.imageID != null)
{
try
{
string filename = Path.Combine(Config.appBasePath + "\\TempPic\\", $"{defectInfo.imageID}.jpg");
var pmat = new Mat(filename);//defectPuaseImgList[liPhotoIndex].Clone();
var pimage = (Bitmap)pmat.ToBitmap().Clone();
this.picDefectImage.loadImage(pimage);
this.picDefectImage.Refresh();
}
catch (Exception ex)
{
UIMessageTip.ShowError($"此记录临时图片已不存在!{ex.Message}", 1000);
}
}
else
UIMessageTip.ShowError("此记录图片已不存在!", 1000);
//选中
this.Invoke(new System.Action(() =>
{
if (lineChartDefect.Option.Series != null && lineChartDefect.Option.Series.ContainsKey("SELECT"))
{
lineChartDefect.Option.Series["SELECT"].Clear();
lineChartDefect.Option.Series["SELECT"].Add(defectInfo.CentreX, defectInfo.CentreY / 100);
lineChartDefect.Refresh();
}
}));
AddTextEvent(DateTime.Now, "表格查询", $"查询完成 Row:{this.uiDataGridView1.CurrentRow.Index} Uid={this.uiDataGridView1.CurrentRow.Cells["colUid"].Value} X={(double)this.uiDataGridView1.CurrentRow.Cells["colX"].Value} Y={(double)this.uiDataGridView1.CurrentRow.Cells["colY"].Value}", WarningEnum.Normal, false);
}
catch (Exception ex)
{
UIMessageTip.ShowError($"记录出错{ex.Message}!", 2000);
return;
}
}
private void lineChartDefect_Click(object sender, EventArgs e)
{
if (lineChartDefect.Option.Series != null && lineChartDefect.Option.Series.ContainsKey("SELECT") && lineChartDefect.Option.Series["SELECT"].DataCount > 0)
{
lineChartDefect.Option.Series["SELECT"].Clear();
lineChartDefect.Refresh();
}
}
#region 内部方法
private void resetUIValue(bool resetCurrKey=true)
{
firstTest = true;
//if(resetCurrKey)
// currKey = 0;
pStopWatch.Reset();
ptStopWatch.Reset();
this.Invoke(new System.Action(() =>
{
lblLen.Text = "0米";
lblSpeed.Text = "速度:0米/分";
this.uilbKF.Text = $"当前幅宽:0cm";
this.uilbHD.Text = $"当前厚度:0,0,0";
//txtBarCodeName.Text = txtBatchId.Text = txtReelId.Text = "";
if (string.IsNullOrEmpty(numErpLen.Text))
numErpLen.Text = "0";
this.lineChartDefect.SetOption(new UILineOption());
this.lineChartFaceWidth.SetOption(new UILineOption());
this.lineChartHouDu.SetOption(new UILineOption());
this.uiDataGridView1.DataSource = null;
this.uiDataGridView1.Rows.Clear();
this.lstboxLog.Items.Clear();
this.picDefectImage.clear();
this.picScanner1.Image = this.picScanner2.Image = null;
picScanner1.Refresh();
picScanner2.Refresh();
radioButton1.Checked = false;
radioButton2.Checked = false;
}));
}
///
/// 全局中断
///
private void globalBreakEvent(int portIndex, byte data)
{
AddTextEvent(DateTime.Now,"I/0状态", $"{portIndex}:{Convert.ToString(data, 2)}", WarningEnum.Normal, false);
if (compareIOInput(CMDName.启动按钮) && this.btnStart.Enabled)
{
if (!_IsGetErpCode)
{
AddTextEvent(DateTime.Now, "启动", "还未扫码获取检测信息!", WarningEnum.Low);
return;
}
this.devContainer.devIOCard.writeBitState(0, 0, true);
this.startCommand();
//Task.Run(async () =>
//{
// await Task.Delay(500);
Thread.Sleep(200);
this.devContainer.devIOCard.writeBitState(0, 0, false);
//});
}
else if (compareIOInput(CMDName.暂停按钮) && this.btnPause.Enabled)
{
this.devContainer.devIOCard.writeBitState(0, 1, true);
this.pauseCommand();//true 输出暂停不会触发输入暂停
//Task.Run(async () =>
//{
// await Task.Delay(500);
Thread.Sleep(200);
this.devContainer.devIOCard.writeBitState(0, 1, false);
//});
}
//else if (compareIOInput(CMDName.金属检测输入) && this.btnPause.Enabled)
//{
// //在可暂停的情况下检测金属才记录数据
//}
//else if (compareIOInput(CMDName.复位按钮) && this.tsbtnReset.Enabled)
// resetCommand();
//else if (!this.disableBuzzer && compareIOInput(CMDName.门磁告警输入))
// warning(WarningEnum.Low, true);
//else if (!this.disableBuzzer && compareIOInput(CMDName.喷墨告警输入))
// warning(WarningEnum.Low, true);
}
private bool compareIOInput(CMDName key)
{
JObject joJson = Config.CMDProcess[key];
IODirectionEnum direction = (IODirectionEnum)joJson.Value("Direction");
if (direction == IODirectionEnum.输入 || direction == IODirectionEnum.输入输出)
{
return Util.compareIOInput(
joJson.Value("IN_OP_SHOW").ToObject>().ToArray(),
devContainer.devIOCard.DIData);
}
return false;
}
///
/// 报警,只响应low,high
///
private void warning(WarningEnum level, bool buzzer = true)
{
if (level == WarningEnum.Normal)
return;
//lock (myLock)
warningLevel = level;
//if ((int)level >= (int)WarningEnum.Low)//暂停
{
//currentState = CurrentStateEnum.暂停;
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
this.devContainer.devIOCard.writeBitState(0, 1, true);
//Task.Run(async () =>
//{
// await Task.Delay(500);
Thread.Sleep(200);
this.devContainer.devIOCard.writeBitState(0, 1, false);
//});
}
pauseCommand(buzzer);
}
//else if (level == WarningEnum.High)//急停
//{
// currentState = CurrentStateEnum.急停;
// devContainer.devAxis.stopNow();
// stopNowCommand();
//}
//启用报警消除按钮
//this.Invoke(new System.Action(() =>
//{
// tsbtnWarning.Enabled = true;
//}));
}
object _lockDrawPoints = new object();
///
/// 重新生成缺陷分布(cm2M在内部转换)
///
/// Records.DefectInfoList
/// 幅宽
/// 卷长度
private void reDrawDefectPoints(string modelName, List lstDefectInfo, double[] XSizeRange=null, double[] YSizeRange=null,bool addSelRect=true)
{
lock (_lockDrawPoints)
{
try
{
UILineOption option;
//AddTextEvent(DateTime.Now,$"绘图", $"缺陷分布, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(lstDefectInfo[lstDefectInfo.Count - 1])}");
var lstData = lstDefectInfo.OrderBy(m => m.Code).ThenBy(m => m.Code).ToList();
if (lstData == null)
return;
if (XSizeRange == null || YSizeRange == null)
option = this.lineChartDefect.Option;
else
{
if (YSizeRange[0] == YSizeRange[1])
{
YSizeRange[0] -= YSizeRange[0] / 10f;
YSizeRange[1] += YSizeRange[1] / 10f;
}
YSizeRange[0] /= 100;
YSizeRange[1] /= 100;
option = new UILineOption();
option.XAxis.Name = "面宽(cm)";
option.YAxis.Name = "长度(米)";
//option.Grid.Top = 20;//边距
option.Grid.Right = 20;//边距
//X轴数据类型
option.XAxisType = UIAxisType.Value;
//设置X/Y轴显示范围
option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
//坐标轴显示小数位数
option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1;
//X/Y轴画参考线
//option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
//option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));
option.ToolTip.Visible = true;
//option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}";
option.Title = new UITitle();
option.Title.Text = "";
option.Title.SubText = "";
}
option.Series.Clear();
string preCode = "";
UILineSeries series = null;
foreach (var item in lstData)
{
try
{
if (preCode != item.Code)//加一组新类型及样式
{
preCode = item.Code;
var one = Config.getDefectItem(modelName, item.Code);
if (one == null)
{
AddTextEvent(DateTime.Now, $"绘图", $"getDefectItem({item.Code}) is null!");
continue;
}
JObject objItem = one as JObject;
Color color = ColorTranslator.FromHtml(objItem.Value("color"));
series = option.AddSeries(new UILineSeries(objItem.Value("name"), color));//加一组
series.Symbol = UILinePointSymbol.Star;
series.SymbolSize = 4;
series.SymbolLineWidth = 2;
series.ShowLine = false;
series.SymbolColor = color;
//数据点显示小数位数(针对当前UILineSeries)
series.XAxisDecimalPlaces = 1;
series.YAxisDecimalPlaces = 2;
//series.Smooth = false;
}
series.Add(item.CentreX, item.CentreY / 100); //cm -> m
}
catch (Exception ex)
{
AddTextEvent(DateTime.Now, $"绘图", $"getDefectItem({item.Code}) is {ex.Message}!");
}
}
////加一组框选
if (addSelRect && !option.Series.ContainsKey("SELECT"))
{
series = option.AddSeries(new UILineSeries("SELECT", Color.Red));
series.Symbol = UILinePointSymbol.Circle;
series.SymbolSize = 6;
series.SymbolLineWidth = 3;
series.ShowLine = false;
series.SymbolColor = Color.Red;
//数据点显示小数位数(针对当前UILineSeries)
series.XAxisDecimalPlaces = 1;
series.YAxisDecimalPlaces = 2;
//series.Add(1, 1);
}
//====
//option.GreaterWarningArea = new UILineWarningArea(3.5);
//option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
this.BeginInvoke(new System.Action(() =>
{
this.lineChartDefect.SetOption(option);
//series.UpdateYData();//按序号更新Y轴值(可设置值超出范围用于闪烁)
}));
}
catch { }
}
}
///
/// 重新门幅宽度
///
///
/// 卷长度
/// 幅宽
object fklock=new object();
private void reDrawFaceWidth(List points, double[] XSizeRange, double[] YSizeRange)
{
//AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(points[points.Count-1])}");
if(YSizeRange[0]== YSizeRange[1])
{
YSizeRange[0] -= YSizeRange[0] / 10f;
YSizeRange[1] += YSizeRange[1] / 10f;
}
XSizeRange[0] /= 100;
XSizeRange[1] /= 100;
//防止超限
XSizeRange[1] += 0.01;
YSizeRange[1] += 0.1;
UILineOption option = new UILineOption();
option.XAxis.Name = "长度(米)";
option.YAxis.Name = "面宽(cm)";
option.Grid.Top = 20;
option.Grid.Right = 20;
//X轴数据类型
option.XAxisType = UIAxisType.Value;
//设置X/Y轴显示范围
option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
//坐标轴显示小数位数
option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1;
//X/Y轴画参考线
//option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
//option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));
option.ToolTip.Visible = true;
//option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}";
option.Title = new UITitle();
option.Title.Text = "";
option.Title.SubText = "";
Color color = Color.Blue;
UILineSeries series = null;
series = option.AddSeries(new UILineSeries("面宽", color));
series.Symbol = UILinePointSymbol.Circle;
series.ShowLine = true;
series.SymbolSize = 1;//4
series.SymbolLineWidth = 1;//2
series.SymbolColor = color;
//数据点显示小数位数(针对当前UILineSeries)
series.XAxisDecimalPlaces = 2;
series.YAxisDecimalPlaces = 1;
float x;
foreach (var item in points)
{
x = item[0] / 100; //cm -> m
series.Add(x, item[1]);
if (x < XSizeRange[0]) break;// AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 1!!!! {x}<{XSizeRange[0]}",WarningEnum.High);
if (x > XSizeRange[1]) break;// AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 2!!!! {x}>{XSizeRange[1]}", WarningEnum.High);
//if (item[1] < YSizeRange[0])
// AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 3!!!! {item[1]}<{YSizeRange[0]}", WarningEnum.High);
//if (item[1] > YSizeRange[1])
// AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 4!!!! {item[1]}>{YSizeRange[1]}", WarningEnum.High);
}
//====
//option.GreaterWarningArea = new UILineWarningArea(3.5);
//option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
this.BeginInvoke(new System.Action(() =>
{
//lock (fklock)
{
this.lineChartFaceWidth.SetOption(option);
}
}));
}
private void reDrawHouDu(List HDpoints, double[] XSizeRange, double[] YSizeRange)
{
//AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(points[points.Count-1])}");
if (YSizeRange[0] == YSizeRange[1])
{
YSizeRange[0] -= YSizeRange[0] / 10f;
YSizeRange[1] += YSizeRange[1] / 10f;
}
XSizeRange[0] /= 100;
XSizeRange[1] /= 100;
//防止超限
XSizeRange[1] += 0.01;
YSizeRange[1] += 0.1;
this.BeginInvoke(new System.Action(() =>
{
UILineOption option;
UILineSeries series1, series2, series3;
//if (this.lineChartHouDu.Option.Series.Count > 0)
//{
// option = this.lineChartHouDu.Option;
// series1 = this.lineChartHouDu.Option.Series["厚度1"];
// series2 = this.lineChartHouDu.Option.Series["厚度2"];
// series3 = this.lineChartHouDu.Option.Series["厚度3"];
// option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
// option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
//}
//else
{
option = new UILineOption();
// 设置图例
option.Legend = new UILegend();
// 图例水平布局
option.Legend.Orient = UIOrient.Horizontal;
// 图例放置在左上角
option.Legend.Top = UITopAlignment.Top;
option.Legend.Left = UILeftAlignment.Left;
// 两个图例分别是Bar1和Bar2
option.Legend.AddData(Config.ThicknessNames.Split(',')[0], Color.Blue);
option.Legend.AddData(Config.ThicknessNames.Split(',')[1], Color.Red);
option.Legend.AddData(Config.ThicknessNames.Split(',')[2], Color.Green);
option.XAxis.Name = "长度(米)";
option.YAxis.Name = "厚度(mm)";
option.Grid.Top = 40;
option.Grid.Right = 20;
//X轴数据类型
option.XAxisType = UIAxisType.Value;
//设置X/Y轴显示范围
option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]);
option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]);
//坐标轴显示小数位数
option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 2;
//X/Y轴画参考线
//option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
//option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
//option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));
option.ToolTip.Visible = true;
//option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}";
option.Title = new UITitle();
option.Title.Text = "";
option.Title.SubText = "";
Color color1 = Color.Blue;
series1 = null;
series1 = option.AddSeries(new UILineSeries(Config.ThicknessNames.Split(',')[0], color1));
series1.Symbol = UILinePointSymbol.Circle;
series1.ShowLine = true;
series1.SymbolSize = 1;//4
series1.SymbolLineWidth = 1;//2
series1.SymbolColor = color1;
series1.XAxisDecimalPlaces = 2;
series1.YAxisDecimalPlaces = 2;
Color color2 = Color.Red;
series2 = null;
series2 = option.AddSeries(new UILineSeries(Config.ThicknessNames.Split(',')[1], color2));
series2.Symbol = UILinePointSymbol.Circle;
series2.ShowLine = true;
series2.SymbolSize = 1;//4
series2.SymbolLineWidth = 1;//2
series2.SymbolColor = color2;
series2.XAxisDecimalPlaces = 2;
series2.YAxisDecimalPlaces = 2;
Color color3 = Color.Green;
series3 = null;
series3 = option.AddSeries(new UILineSeries(Config.ThicknessNames.Split(',')[2], color3));
series3.Symbol = UILinePointSymbol.Circle;
series3.ShowLine = true;
series3.SymbolSize = 1;//4
series3.SymbolLineWidth = 1;//2
series3.SymbolColor = color3;
series3.XAxisDecimalPlaces = 2;
series3.YAxisDecimalPlaces = 2;
//判断第一次数据是否为0开始
//if (HDpoints[0].Y_Dis / 100.0 > 0.5)
//{
// HDpoints[0].Y_Dis = 0;
//}
double x;
DateTime dt = DateTime.Now;
foreach (var item in HDpoints)
{
x = item.Y_Dis / 100.0 >= 0 ? item.Y_Dis / 100.0 : 1; //cm -> m
series1.Add(x, item.Value1);
series2.Add(x, item.Value2);
series3.Add(x, item.Value3);
if (x < XSizeRange[0]) break;// AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 1!!!! {x}<{XSizeRange[0]}",WarningEnum.High);
if (x > XSizeRange[1]) break;// AddTextEvent(DateTime.Now,$"绘图", $"门幅宽度超限 2!!!! {x}>{XSizeRange[1]}", WarningEnum.High);
//if (item.Value1 < YSizeRange[0]) AddTextEvent(DateTime.Now, $"绘图", $"测厚超限 3!!!! {item.Value1}<{YSizeRange[0]}", WarningEnum.High);
//if (item.Value1 > YSizeRange[1]) AddTextEvent(DateTime.Now, $"绘图", $"测厚超限 4!!!! {item.Value1}>{YSizeRange[1]}", WarningEnum.High);
//if (item.Value2 < YSizeRange[0]) AddTextEvent(DateTime.Now, $"绘图", $"测厚超限 5!!!! {item.Value2}<{YSizeRange[0]}", WarningEnum.High);
//if (item.Value2 > YSizeRange[1]) AddTextEvent(DateTime.Now, $"绘图", $"测厚超限 6!!!! {item.Value2}>{YSizeRange[1]}", WarningEnum.High);
//if (item.Value3 < YSizeRange[0]) AddTextEvent(DateTime.Now, $"绘图", $"测厚超限 7!!!! {item.Value3}<{YSizeRange[0]}", WarningEnum.High);
//if (item.Value3 > YSizeRange[1]) AddTextEvent(DateTime.Now, $"绘图", $"测厚超限 8!!!! {item.Value3}>{YSizeRange[1]}", WarningEnum.High);
if ((DateTime.Now - dt).Seconds > 1)
{
AddTextEvent(DateTime.Now, $"绘图", $"测厚超时!!!!", WarningEnum.High, false);
break;
}
}
}
//double x;
//DateTime dt = DateTime.Now;
//var item = HDpoints;
//x = item.Y_Dis / 100.0; //cm -> m
//series1.Add(x, item.Value1);
//series2.Add(x, item.Value2);
//series3.Add(x, item.Value3);
//====
//option.GreaterWarningArea = new UILineWarningArea(3.5);
//option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
this.lineChartHouDu.SetOption(option);
}));
}
private delegate void AddTextDelegate(DateTime time,string tag, string msg, WarningEnum level, bool Show);
private void AddTextEvent(DateTime now, string tag, string msg, WarningEnum level = WarningEnum.Normal, bool Show = true)
{
try
{
if (InvokeRequired)
{
Invoke(new AddTextDelegate(AddTextEvent), new object[]
{
now,
tag,
msg,
level,
Show
});
}
else
{
if (tag != null && tag != "")
tag = $" - [{tag}]";
//var now = DateTime.Now;
msg = now.ToString("HH:mm:ss fff") + tag + " - " + msg;
//cont = MyHelper.subString(cont, 300);
//写日志,warn和error日志直接写
writeLog(now, level, msg);
//
//if (type > 0)
// cont = $"{cont}";
if (Show)
{
//msg = (level == WarningEnum.Normal ? "B" : level == WarningEnum.Low ? "Y" : "R") + msg;
msg = (level == WarningEnum.Normal ? "" : level == WarningEnum.Low ? "Y" : "R") + msg;
//this.Invoke(new System.Action(() =>
//{
if (this.lstboxLog.Items.Count > 1000)
this.lstboxLog.Items.Clear();
lstboxLog.Items.Insert(0, msg);
//}));
}
//日志滚动
//lstLog.SelectedIndex = lstLog.Items.Count - 1;
//开启云端
if ((init_Cloud) && (level != WarningEnum.Normal))
{
//上传报警状态和信息
string statusStr = level == WarningEnum.Normal ? "正常" : level == WarningEnum.Low ? "警告" : "系统报警";
cloudMgr.SendTopic("device/attributes", $"{{\"status\": \"{statusStr}\", \"alm\": \"{tag}-{msg}\", " +
$"\"name\": \"{Config.CloudThisName}\"}}");
}
}
}
catch (Exception ex)
{
//MessageBox.Show("AddTextEvent ex=(" + ex.Message + ")", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
}
}
private void writeLog(DateTime now, WarningEnum level, string text)
{
string directory = Config.LogPath + "\\" + DateTime.Now.ToString("yyyyMM") + "\\";
//if (type == 0) directory = Application.StartupPath + "\\Log\\Info\\";
//else if (type == 1) directory = Application.StartupPath + "\\Log\\Warn\\";
//else directory = Application.StartupPath + "\\Log\\Error\\";
if (!System.IO.Directory.Exists(directory))
System.IO.Directory.CreateDirectory(directory);
File.AppendAllText(directory + now.ToString("yyyyMMdd") + ".log", text + "\r\n");
}
#endregion
private void FHome_Load(object sender, EventArgs e)
{
var defectList = Config.LoadDefectItemList();
ucColorListDefect.initData(defectList);
this.lineChartDefect.SetOption(new UILineOption());
this.lineChartFaceWidth.SetOption(new UILineOption());
this.lineChartHouDu.SetOption(new UILineOption());
//判断是否连接云端
if (Config.cloud_open >0)
{
AddTextEvent(DateTime.Now, "设备启动", $"连接云端");
if (cloudMgr.ConnectCloud(Config.cloud_ip, Config.cloud_port,
Config.cloud_username, Config.cloud_password))
{
init_Cloud = true;
AddTextEvent(DateTime.Now, "云端数据", $"开启云端连接");
//开启云端
if (init_Cloud)
{
//上传报警状态和信息
string statusStr = "正常" ;
cloudMgr.SendTopic("device/attributes", $"{{\"status\": \"{statusStr}\", \"alm\": \"系统运行-正常启动软件\", " +
$"\"name\": \"{Config.CloudThisName}\"}}");
}
}
else
AddTextEvent(DateTime.Now, "云端数据", "云端连接失败!", WarningEnum.Low);
}
//if (Config.OpenClearPic)
// ClearPic(Config.ImagePath, Config.ClearDays);
}
private void FHome_Shown(object sender, EventArgs e)
{
//picScanner1.BackColor = Color.Red;
//picScanner2.BackColor = Color.Green;
picScanner1.Width = picScanner2.Width = pnlScannerImg.ClientSize.Width / 2 - 5;
picScanner2.Left = picScanner1.Width + 5;
uilbKF.Top = 8;
uilbHD.Top = 8;
this.btnHeight.Left = this.btnClearAlm.Left + (this.btnClearAlm.Left - this.btnPause.Left);
this.btnCut.Left = this.btnHeight.Left + (this.btnHeight.Left - this.btnClearAlm.Left);
this.btnFenJuan.Left = this.btnCut.Left + (this.btnCut.Left - this.btnHeight.Left);
if (Config.OpenHouDuJiLu)
{
uiTitlePanel4.Left = uiTitlePanel3.Left;
uiTitlePanel4.Width = (uiTitlePanel3.Width - 5) / 2;
uiTitlePanel8.Left = uiTitlePanel4.Left + uiTitlePanel4.Width + 5;
uiTitlePanel8.Width = uiTitlePanel4.Width;
}
else
{
uiTitlePanel4.Left = uiTitlePanel3.Left;
uiTitlePanel4.Width = uiTitlePanel3.Width;
uiTitlePanel8.Visible = false;
}
numBzLen.Text = Config.orderWarnningLen.ToString();
}
//开机
private Thread t_test0;
private Thread t_test1;
private Thread t_test2;
private Thread t_test3;
private string _prebathid = "";
private int _reelno = 0;
//当前产品需裁孔位
//private int MarginHoleWidth = 0;
private void btnOpen_Click(object sender, EventArgs e)
{
this.Activate();
//this.txtNote.Text = (currProductModel == null ? "NO" : currProductModel.Name);
Config.LoadAllConfig();
//设置程序最小/大线程池
// Get the current settings.
int minWorker, minIOC;
ThreadPool.GetMinThreads(out minWorker, out minIOC);
ThreadPool.SetMinThreads(25, minIOC);
//ThreadPool.SetMaxThreads(256, 256);
//DefectLib dl = new DefectLib();
//dl.start();
//加载寻边模型
OpenCVUtil.LoadEdgeMode();
this.btnOpen.Enabled = false;
_cts = new CancellationTokenSource();
this.resetUIValue();
AddTextEvent(DateTime.Now,"设备启动", $"正在启动硬件设备(版本:v{ Assembly.GetExecutingAssembly().GetName().Version.ToString()})。。。");
warningLevel = WarningEnum.Normal;
this.btnStart.Enabled = this.btnEnd.Enabled = this.btnPause.Enabled = false;
//后台线程回调事件
devContainer = new DevContainer();
devContainer.StateChange = (state, msg) =>
{
if (state)
{
currentState = CurrentStateEnum.初始;
AddTextEvent(DateTime.Now,"设备启动", $"启动成功,请点开始验布按钮!");
//全局中断
devContainer.devIOCard.INEvent = globalBreakEvent;
// I/O reset后,输出默认状态
if (devContainer.devIOCard.IsInit)
{
AddTextEvent(DateTime.Now,"复位", $"I/O复位...");
if (!devContainer.devIOCard.reset())
{
AddTextEvent(DateTime.Now,"复位", "I/O板卡复位失败!", WarningEnum.High);
return;
}
if (!devContainer.io_output(CMDName.IO默认输出))
{
//AddTextEvent(DateTime.Now,"复位", "I/O板卡复位默认值失败!", WarningEnum.High);
//return;
}
int portIndex = 0;
foreach(var data in devContainer.devIOCard.DIData)
AddTextEvent(DateTime.Now,"I/0状态", $"DIData: {portIndex++}-{Convert.ToString(data, 2)}");
}
devContainer.OutDebugEvent = (tag, debugInfo) =>
{
AddTextEvent(DateTime.Now,tag, debugInfo);
};
//
this.Invoke(new System.Action(() =>
{
this.btnOpen.Enabled = false;
this.btnClose.Enabled = true;
this.btnStart.Enabled = true;
this.tcbarLightValue.Enabled = true;
tcbarLightValue.Value=devContainer.devLight.getDigitalValue(1);
}));
devContainer.libPhoto.QueueCountEvent = (count) =>
{
this.BeginInvoke(new System.Action(() =>
{
this.lblWaitImageCount.Text = count.ToString();
}));
};
devContainer.libDefect.QueueCountEvent = (type, count) =>
{
this.BeginInvoke(new System.Action(() =>
{
switch (type)
{
case 0: this.lblDefectQueue0.Text = count.ToString(); break;
case 1: this.lblDefectQueue1.Text = count.ToString(); break;
case 2: this.lblDefectQueue2.Text = count.ToString(); break;
}
}));
};
//在【暂停】和【运行中】时都可以扫码,扫码后即开始即结束上一卷(如存在),自动开始下一卷,等待相机照片回调
devContainer.devCodeScanner.ScanerEvent = (barCode) =>
{
int errCode = 0;
try
{
//AddTextEvent(DateTime.Now,"扫码", $"{code}:{currentState.ToString()}");
//if (!devContainer.state || string.IsNullOrWhiteSpace(barCode)
// || (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停))
// return;
if (!devContainer.state || string.IsNullOrWhiteSpace(barCode)
|| (currentState != CurrentStateEnum.初始 ))
return;
//明新二维码解析
if (Config.CustomerName == "MX")
{
try
{
JObject codeJson = JObject.Parse(barCode);
barCode = codeJson.Value("id");
}
catch (Exception ex)
{
AddTextEvent(DateTime.Now, "扫码", $"产品条码({barCode})解析失败!{ex.Message}", WarningEnum.High);
return;
}
}
string barCodeName="",len = "0", batchId = "", reelId = "", erpID = "", sDefectType = "";
string PJXTBH = "";
string mXCL = "", ysXCL = "";
string showModelname;
if (!string.IsNullOrWhiteSpace(Config.ErpDBConStr) && !string.IsNullOrWhiteSpace(Config.ErpSql) && !string.IsNullOrWhiteSpace(barCode))
{
AddTextEvent(DateTime.Now,"扫码", $"产品条码({barCode})到ERP查询对应数据...", WarningEnum.Normal);
DataRow rowData = null; ;
if (Config.CustomerName == "XCL")
{
var datatb = this.loadErpDataTable(barCode);
if (datatb == null || datatb.Rows.Count == 0)
{
AddTextEvent(DateTime.Now, "扫码", $"产品条码({barCode})无对应ERP数据,不做响应!", WarningEnum.Low);
return;
}
if (datatb.Rows.Count > 1)
{
SelectReelFrm srf = new SelectReelFrm(datatb);
srf.Render();
srf.Text = "ERP卷号选择";
srf.ShowDialog();
if (srf.IsOK)
{
PJXTBH = datatb.Rows[srf.RowIndex][0].ToString();
datatb.Columns.RemoveAt(0);
datatb.Columns.RemoveAt(0);
rowData = datatb.Rows[srf.RowIndex];
AddTextEvent(DateTime.Now, "卷号选择", $"Index({srf.RowIndex})-批号:{srf.SelectBatch},卷号:{srf.SelectReel},长度:{srf.SelectLen}!");
}
else
{
AddTextEvent(DateTime.Now, "卷号选择", $"未选择卷号!", WarningEnum.High);
srf.Dispose();
return;
}
srf.Dispose();
}
else
{
PJXTBH = datatb.Rows[0][0].ToString();
datatb.Columns.RemoveAt(0);
datatb.Columns.RemoveAt(0);
rowData = datatb.Rows[0];
}
}
else
{
rowData = this.loadErpData(barCode);
if (rowData == null)
{
AddTextEvent(DateTime.Now, "扫码", $"产品条码({barCode})无对应ERP数据,不做响应!", WarningEnum.Low);
return;
}
}
barCodeName = rowData[0].ToString();
if (rowData.ItemArray.Length > 1) len = rowData[1].ToString();
if (rowData.ItemArray.Length > 2) batchId = rowData[2].ToString();
if (rowData.ItemArray.Length > 3) reelId = rowData[3].ToString();
if (Config.CustomerName == "MX")
{
if (rowData.ItemArray.Length > 4) erpID = $"{rowData[4]}@{barCodeName}";
}
else if (Config.CustomerName == "XCL")
{
//获取材质 颜色
if (rowData["SFMM"].ToString() == "Y")
mXCL = "磨毛";
else if (rowData["SFQC"].ToString() == "Y")
mXCL = "绒面";
else
mXCL = "光面";
}
}
else
barCodeName = barCode;//没有ERP对应关系时直接使用条码做为品名
errCode = 1;
Product productInfo = null;
string pcode = "";
string[] codes = new string[5];
if (Config.CustomerName != "MX" && Config.CustomerName != "XCL")
{
//SHNY-PX-6-***
codes = barCodeName.Split(new char[] { '-' });
if (codes.Length < 4)
{
AddTextEvent(DateTime.Now, "扫码", $"产品品名({barCodeName})格式错误,不做响应!", WarningEnum.Low);
return;
}
//新开始
this.resetUIValue(false);
errCode = 2;
//加载新产品
pcode = "1-" + codes[2];
if (codes[1] == "0" || Config.SuedeList1.Contains(codes[1]))
pcode = "0-" + codes[2];
#if false
else
pcode = codes[1] + "-" + codes[2];
#elif HX_LOD
else
pcode = "1-" + codes[2];
#else
else if (codes[1] == "1" || Config.SuedeList2.Contains(codes[1]))
pcode = "1-" + codes[2];
else if (codes[1] == "2" || Config.SuedeList3.Contains(codes[1]))
pcode = "2-" + codes[2];
else if (codes[1] == "3" || Config.SuedeList4.Contains(codes[1]))
pcode = "3-" + codes[2];
else if (codes[1] == "4" || Config.SuedeList5.Contains(codes[1]))
pcode = "4-" + codes[2];
else if (codes[1] == "5" || Config.SuedeList6.Contains(codes[1]))
pcode = "5-" + codes[2];
else
pcode = "1-" + codes[2];
#endif
productInfo = svcProduct.GetModelNav(pcode); //frmProduct.loadProduct(code);
}
else if(Config.CustomerName == "MX")
{
//新开始
this.resetUIValue(false);
AddTextEvent(DateTime.Now, "扫码", $"产品品名({barCodeName}),ERP-ID:{erpID}!", WarningEnum.Normal);
productInfo = svcProduct.GetModelNavByErpID(erpID);
if(productInfo == null)
{
//添加配方
AddErpFrm frm = new AddErpFrm(erpID);
frm.Render();
frm.Text = "绑定ERP信息";
frm.ShowDialog();
if (frm.IsOK)
{
AddTextEvent(DateTime.Now, "扫码", $"产品品名({barCodeName})-ERP_ID({erpID})对应配方添加!");
productInfo = svcProduct.GetModelNavByErpID(erpID);
}
else
{
AddTextEvent(DateTime.Now, "扫码", $"产品品名({barCodeName})-ERP_ID({erpID})对应配方不存在,请先添加ERP绑定设置,暂停设备!", WarningEnum.High);
warning(WarningEnum.Low);//暂停
return;
}
frm.Dispose();
}
pcode = barCodeName;
codes[0] = "MX";
codes[1] = productInfo.Material;
codes[2] = productInfo.Color.ToString();
}
else if (Config.CustomerName == "XCL")
{
erpID = barCodeName;
//新开始
this.resetUIValue(false);
AddTextEvent(DateTime.Now, "扫码", $"产品品名({barCodeName}),ERP-ID:{erpID}!", WarningEnum.Normal);
productInfo = svcProduct.GetModelNavByErpID(erpID);
if (productInfo == null)
{
//无配方根据材质,颜色自动绑定
var getColor = barCodeName.Split(new char[] { '-' });
if (getColor[2].Trim() == "0096" || getColor[2] == "0097" || getColor[2] == "0098")
ysXCL = "黑色";
else if (getColor[2].Trim() == "0095")
ysXCL = "灰色";
else if (getColor[2].Trim() == "01" || getColor[2].Trim() == "02" ||
getColor[2].Trim() == "0001" || getColor[2].Trim() == "0002")
ysXCL = "白色";
//获取材质
string[] Material = new string[6];
Material[0] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 0).Value("name");
Material[1] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 1).Value("name");
Material[2] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 2).Value("name");
Material[3] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 3).Value("name");
Material[4] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 4).Value("name");
Material[5] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 5).Value("name");
var lstColor = Config.colorNameList.Select(x => new { code = x.Value("code"), name = x.Value("name") }).ToList();
string mCode = "";
if (mXCL == Material[0])
mCode = "0";
else if (mXCL == Material[1])
mCode = "1";
else if (mXCL == Material[2])
mCode = "2";
else if (mXCL == Material[3])
mCode = "3";
else if (mXCL == Material[4])
mCode = "4";
else if (mXCL == Material[5])
mCode = "5";
//获取颜色
int yscode = -1;
var tp = lstColor.Find(x => x.name == ysXCL);
if (tp != null)
yscode = tp.code;
AddTextEvent(DateTime.Now, "新材料ERP解析", $"色号:({getColor[2]}-{ysXCL}-{yscode}),材质:{mXCL}-{mCode}!", WarningEnum.Normal);
var allList = svcProduct.GetListNav();
var find = allList.Find(x => x.Color == yscode && x.Material == mCode);
if (find == null)
{
//添加配方
AddErpFrm frm = new AddErpFrm(erpID);
frm.Render();
frm.Text = "绑定ERP信息";
frm.ShowDialog();
if (frm.IsOK)
{
AddTextEvent(DateTime.Now, "扫码", $"产品品名({barCodeName})-ERP_ID({erpID})对应配方添加!");
productInfo = svcProduct.GetModelNavByErpID(erpID);
}
else
{
AddTextEvent(DateTime.Now, "扫码", $"产品品名({barCodeName})-ERP_ID({erpID})对应配方不存在,请先添加ERP绑定设置,暂停设备!", WarningEnum.High);
warning(WarningEnum.Low);//暂停
return;
}
frm.Dispose();
}
else
productInfo = find;
}
pcode = barCodeName;
codes[0] = "XCL";
codes[1] = productInfo.Material;
codes[2] = productInfo.Color.ToString();
}
Records record;
lock (lockCurrKey)
{
errCode = 3;
//保存,这里队列图片可能还未检测完
if (currKey > 0)
{
string szBatchId, szReelId;
double ldErpLen ;
szBatchId = txtBatchId.Text.Trim();
szReelId = txtReelId.Text.Trim();
ldErpLen = Convert.ToDouble(numErpLen.Text.Trim());
if (radioButton1.Checked)
sDefectType = "成检";
else
sDefectType = "半检";
//BatchId = code,//code[2]
//ReelId = "1",//code[3]
int mykey = currKey;
//Task.Run(() => { saveCurrRecord(mykey, szBatchId, szReelId, ldErpLen); });
saveCurrRecord(mykey, szBatchId, szReelId, ldErpLen);
currKey = 0;
}
if (productInfo == null)
{
AddTextEvent(DateTime.Now,"扫码", $"编码({pcode})对应配方不存在,请先添加产品配方设置,暂停设备!", WarningEnum.High);
warning(WarningEnum.Low);//暂停
this.BeginInvoke(new System.Action(() =>
{
frmProduct.loadProduct(pcode);//转到新建编码
}));
return;
}
//加载model对应label
JArray defectItemList = new JArray();
if(Config.LoadModelDefectItemList(productInfo.ModelName, out defectItemList))
AddTextEvent(DateTime.Now, "标签", $"加载模型({productInfo.ModelName})对应的label!", WarningEnum.Normal);
else
AddTextEvent(DateTime.Now, "标签", $"模型({productInfo.ModelName})无对应的label,使用默认!", WarningEnum.Normal);
ucColorListDefect.initData(defectItemList);
//界面显示当前模型标准
showModelname = GetModelShowName(productInfo);
errCode = 4;
var now = DateTime.Now;
currKey = now.Hour * 10000 + now.Minute * 100 + now.Second;
_residueWarnningLenStop = _orderWarnningLenStop = false;
//var materialItem = codes[0]+"-"+ codes[1];
var colorItem = Config.getColorItem(int.Parse(codes[2]));
if (!string.IsNullOrEmpty(batchId) && _prebathid == batchId)
_reelno++;
else
_reelno = 0;
//MarginHoleWidth = productInfo.MarginHoleWidth;
record = new Records()
{
currKey = currKey,
ProductId = productInfo.Id,
ProductInfo = productInfo,//后面计算合格时用
Color = (colorItem == null ? "未知" : colorItem["name"].ToString()),
Material = codes[0] + "-" + codes[1],// (materialItem == null ? "未知" : materialItem["name"].ToString()),
BarCode = barCode,
BarCodeName = barCodeName,
ErpLen = double.Parse(len),
BatchId = batchId,
ReelId = reelId,
ReelNo = _reelno,
PartReelId = "",
ModifyUserCode = Config.loginUser.Code,
CreateUserCode = Config.loginUser.Code,
DefectInfoList = new List(),
UserName = Config.loginUser.Name,
WorkTeam = Config.loginUser.WorkTeam,
PJXTBH = PJXTBH,
DefectType = sDefectType,
};
//加入上传数据
CurrUploadDataRecords = new UploadDataRecords()
{
Color = (colorItem == null ? "未知" : colorItem["name"].ToString()),
Material = codes[0] + "-" + codes[1],// (materialItem == null ? "未知" : materialItem["name"].ToString()),
BarCode = barCode,
BarCodeName = barCodeName,
ErpLen = double.Parse(len),
BatchId = batchId,
ReelId = reelId,
Name = productInfo.Name,
Spec = productInfo.Spec,
LightValue = productInfo.LightValue,
ExposureTime = productInfo.ExposureTime,
Gain = productInfo.Gain,
QualifiedLimitList = productInfo.QualifiedLimitList,
GradeLimitList = productInfo.GradeLimitList,
ModelName = productInfo.ModelName,
DefectAreaLimit = productInfo.DefectAreaLimit,
DefectCountLimit = productInfo.DefectCountLimit,
DefectPauseForUser = productInfo.DefectPauseForUser,
DefectPauseOption = productInfo.DefectPauseOption,
DefectCntLength = productInfo.DefectCntLength,
WarnDefect = productInfo.WarnDefect,
ClassType = productInfo.ClassType,
HalconAreaThr = productInfo.HalconAreaThr,
ModifyUserCode = Config.loginUser.Code,
CreateUserCode = Config.loginUser.Code,
DefectInfoList = new List(),
cm2px_x = Config.cm2px_x,
cm2px_y = Config.cm2px_y,
UserName = Config.loginUser.Name,
WorkTeam = Config.loginUser.WorkTeam
};
#if UPDATA
if (!svcRecord.InsertNav(record))
{
AddTextEvent(DateTime.Now, "数据预存", $"数据预存失败!", WarningEnum.High);
return;
}
var trecord = svcRecord.GetRecordsNav(record.BarCode, record.BarCodeName);
if(trecord == null)
{
AddTextEvent(DateTime.Now, "数据预存", $"预存数据获取失败失败!", WarningEnum.High);
return;
}
else
{
AddTextEvent(DateTime.Now, "数据预存", $"产品条码({trecord.BarCode}),创建时间({trecord.CreateTime})", WarningEnum.Normal);
}
trecord.ProductInfo = productInfo;
htTask.Add(currKey, trecord);
#else
htTask.Add(currKey, record);
#endif
}
errCode = 8;
//
errCode = 9;
AddTextEvent(DateTime.Now,"扫码", $"品名({barCodeName}),加载产品信息({productInfo.Code})完成,加载配方(光源={productInfo.LightValue},曝光={productInfo.ExposureTime},增益={productInfo.Gain})...");
errCode = 10;
if (productInfo.LightValue > 0)//光源 - 通道0
devContainer.devLight.setDigitalValue(1, productInfo.LightValue);
errCode = 11;
if (productInfo.ExposureTime > 0 || productInfo.Gain > 0)//相机曝光 增益
{
devContainer.devCamer1.setParam((float)(productInfo.ExposureTime > 0 ? productInfo.ExposureTime : -1), (float)(productInfo.Gain > 0 ? productInfo.Gain : -1));
devContainer.devCamer2.setParam((float)(productInfo.ExposureTime > 0 ? productInfo.ExposureTime : -1), (float)(productInfo.Gain > 0 ? productInfo.Gain : -1));
}
errCode = 15;
AddTextEvent(DateTime.Now,"扫码", $"品名({barCodeName}),配方设置完成:光源={productInfo.LightValue},曝光={productInfo.ExposureTime}");
//加载模型
if(!devContainer.libDefect.LoadModel(productInfo.ModelName))
{
AddTextEvent(DateTime.Now, "模型预加载", $"模型加载异常({errCode})请检测模型路径:{productInfo.ModelName}", WarningEnum.High);
return;
}
//注意,这里和修改页共享操作(UI操作),注意冲突
this.Invoke(new System.Action(() =>
{
if (Config.CustomerName != "XCL")
this.txtBarCode.Text = "";
else
this.txtBarCode.Text = batchId;
this.txtBarCodeName.Text= barCodeName;
//txtBatchId.Text = record.BatchId;
//txtReelId.Text = record.ReelId;
this.numErpLen.Text = len;
this.txtBatchId.Text = batchId;
this.txtReelId.Text = reelId;
this.txtDefectName.Text = showModelname;
//暂时全部放开
//this.btnStart.Enabled = true;
//this.btnEnd.Enabled = true;
//this.btnPause.Enabled = true;
//
this.swcDefectPauseForUser.Active = this.defectPauseForUser = productInfo.DefectPauseForUser;
}));
//
//ptStopWatch.Reset();
pStopWatch.Restart();
errCode = 19;
//扫码成功
_IsGetErpCode = true;
}
catch (Exception ex)
{
AddTextEvent(DateTime.Now,"扫码", $"异常({errCode}):{ex.Message}", WarningEnum.High);
}
};
//相机回调出照片
#if NT
#else
devContainer.devCamer1.ScanEvent = callBackScanMatEvent;
devContainer.devCamer2.ScanEvent = callBackScanMatEvent;
#endif
//devContainer.devScannerGentl1.ScanEventPath = callBackScanPathEvent;
//devContainer.devScannerGentl2.ScanEventPath = callBackScanPathEvent;
devContainer.devCamer1.PhotoNumCacheEvent = (num) =>
{
this.BeginInvoke(new System.Action(() =>
{
this.lblScanner1Cache.Text =num.ToString();
}));
};
devContainer.devCamer2.PhotoNumCacheEvent = (num) =>
{
this.BeginInvoke(new System.Action(() =>
{
this.lblScanner2Cache.Text = num.ToString();
}));
};
devContainer.devPlc.RuningStateChangeEvent = (runningState) =>
{
AddTextEvent(DateTime.Now,"PLC", $"当前状态为:{(runningState ? "运行中" : "暂停")}");
if (runningState)
this.startCommand();
else
this.pauseCommand();
};
//devContainer.devIOCard.INEvent = (i,value) =>
//{
// AddTextEvent(DateTime.Now,"I/O", $"INEvent 当前值:{i}-{value}");
// if (compareIOInput(CMDName.启动按钮))
// {
// this.startCommand();
// }
// else if (compareIOInput(CMDName.暂停按钮))
// {
// this.pauseCommand(true);
// }
//};
}
else
{
AddTextEvent(DateTime.Now,"设备启动", $"启动失败,{msg}", WarningEnum.High);
this.Invoke(new System.Action(() =>
{
this.btnOpen.Enabled = true;
this.btnClose.Enabled = false;
}));
}
};
devContainer.WarningEvent = (now,level, msg) =>
{
AddTextEvent(DateTime.Now,$"设备事件", msg, level, false);
if (level == WarningEnum.High)
Task.Run(() => warning(level, true));
};
devContainer.start(this.picScanner1, this.picScanner2);
//devContainer.start(IntPtr.Zero,IntPtr.Zero);
#if NT
t_test0 = new System.Threading.Thread(GetLenAndSpd);
t_test0.IsBackground = true;
t_test0.Start();
t_test1 = new System.Threading.Thread(Cam1ThreadFunction);
t_test1.IsBackground = true;
t_test1.Start();
t_test2 = new System.Threading.Thread(Cam2ThreadFunction);
t_test2.IsBackground = true;
t_test2.Start();
t_test3 = new System.Threading.Thread(MainThreadFunction);
t_test3.IsBackground = true;
t_test3.Start();
#endif
}
private string GetModelShowName(Product productInfo)
{
string[] Material = new string[6];
string pdtName = "";
Material[0] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 0).Value("name");
Material[1] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 1).Value("name");
Material[2] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 2).Value("name");
Material[3] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 3).Value("name");
Material[4] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 4).Value("name");
Material[5] = Config.materialNameList.FirstOrDefault(x => x.Value("code") == 5).Value("name");
var lstColor = Config.colorNameList.Select(x => new { code = x.Value("code"), name = x.Value("name") }).ToList();
if (productInfo.Material == "0")
pdtName += Material[0];
else if (productInfo.Material == "1")
pdtName += Material[1];
else if (productInfo.Material == "2")
pdtName += Material[2];
else if (productInfo.Material == "3")
pdtName += Material[3];
else if (productInfo.Material == "4")
pdtName += Material[4];
else if (productInfo.Material == "5")
pdtName += Material[5];
else
pdtName += "错误材质";
var tp = lstColor.Find(x => x.code == productInfo.Color);
if (tp != null)
pdtName += "_" + tp.name;
else
pdtName += "_无效颜色";
return pdtName;
}
private DataRow loadErpData(string barCode)
{
var paramList = new List() {
new SugarParameter("@code", barCode)
};
Stopwatch stopwatch = Stopwatch.StartNew();
#if Oracle
var data = Utils.DBUtils.execSql(Config.ErpSql, paramList, SqlSugar.DbType.Oracle);
#else
var data = Utils.DBUtils.execSql(Config.ErpSql, paramList);
#endif
if (data == null || data.Rows.Count < 1)
{
AddTextEvent(DateTime.Now,"Erp查询结果", $"{barCode}: 时长={stopwatch.ElapsedMilliseconds}ms, 无数据!", WarningEnum.Normal);
return null;
}
AddTextEvent(DateTime.Now,"Erp查询结果", $"{barCode}: 时长={stopwatch.ElapsedMilliseconds}ms, {JsonConvert.SerializeObject(data.Rows[0])}", WarningEnum.Normal);
return data.Rows[0];
//Task.Run(() =>
//{
// try
// {
// var paramList = new List() {
// new SugarParameter("@code", code)
// };
// Stopwatch stopwatch = Stopwatch.StartNew();
// var data = Utils.DBUtils.execSql(Config.ErpSql, paramList);
// AddTextEvent(DateTime.Now,"Erp查询结果", $"{code}: 时长={stopwatch.ElapsedMilliseconds}ms, {JsonConvert.SerializeObject(data.Rows[0])}", WarningEnum.Normal);
// if (data != null && data.Rows.Count > 0)
// {
// this.Invoke(new System.Action(() =>
// {
// this.numErpLen.Text = data.Rows[0][0].ToString();
// if (data.Columns.Count > 1) this.txtBatchId.Text = data.Rows[0][1].ToString();
// if (data.Columns.Count > 2) this.txtReelId.Text = data.Rows[0][2].ToString();
// }));
// }
// }
// catch (Exception ex)
// {
// AddTextEvent(DateTime.Now,"Erp查询异常", $"{code}:{ex.Message}", WarningEnum.Low);
// }
//});
}
private DataTable loadErpDataTable(string barCode)
{
var paramList = new List() {
new SugarParameter("@code", barCode)
};
Stopwatch stopwatch = Stopwatch.StartNew();
#if Oracle
var data = Utils.DBUtils.execSql(Config.ErpSql, paramList, SqlSugar.DbType.Oracle);
#else
var data = Utils.DBUtils.execSql(Config.ErpSql, paramList);
#endif
if (data == null || data.Rows.Count < 1)
{
AddTextEvent(DateTime.Now, "Erp查询结果", $"{barCode}: 时长={stopwatch.ElapsedMilliseconds}ms, 无数据!", WarningEnum.Normal);
return null;
}
AddTextEvent(DateTime.Now, "Erp查询结果", $"{barCode}: 时长={stopwatch.ElapsedMilliseconds}ms, {JsonConvert.SerializeObject(data.Rows[0])}", WarningEnum.Normal);
return data;
}
private class ScanPhotoInfo
{
///
///
///
///
/// 1-n 第1张会把1改为0
///
public ScanPhotoInfo(int _devIndex,int _photoIndex,string _path)
{
devIndex = _devIndex;
photoIndex = _photoIndex;
path = _path;
}
public ScanPhotoInfo(int _devIndex, int _photoIndex, Mat _mat)
{
devIndex = _devIndex;
photoIndex = _photoIndex;
mat = _mat;
}
public int devIndex { get; set; }
///
/// 0-n
///
public int photoIndex { get; set; }
public string path { get; set; }
public Mat mat { get; set; }
}
//private List> scanPhotoQueue1 = new List>();
///
/// 存放相机1/2的实时图像(3-多容错1帧)
///
private ScanPhotoInfo[] scanPhotos=new ScanPhotoInfo[3];
private void callBackScanPathEvent(int num, string path, int devIndex) //ScanEvent
{
AddTextEvent(DateTime.Now,"拍照", $"相机({devIndex})采集图索到图像({num}):{path}!");
}
bool firstTest = true;
#region 中断
///
/// 中断工序运行
///
///
private bool isBreakProcessRun()
{
//if (!devContainer.state || (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停))
return (!devContainer.state || (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停));
}
#endregion
#region 长度与速度流程
#region 计米器位置和速度
///
/// 分卷上次长度
///
private double ActiveSpeed = 0;
//实时位置
private double ActiveDis = 0;
private object LockSpd = new object();
private object LockDis = new object();
private void SetSpd(double spd)
{
lock (LockSpd)
{
ActiveSpeed = spd;
}
}
private double GetSpd()
{
double spd;
lock (LockSpd)
{
spd = ActiveSpeed;
}
return spd;
}
private void SetDis(double dis)
{
lock (LockDis)
{
ActiveDis = dis;
}
}
private double GetDis()
{
double dis;
lock (LockDis)
{
dis = ActiveDis;
}
return dis;
}
#endregion
#region 计米停车缺陷管理
private object lock_defectPuase = new object();
///
/// 二次判定缺陷
///
private List defectPuaseList = new List();
///
/// 待定缺陷
///
private List defectWaitList = new List();
public void DelFirstDefect(DefectInfo df)
{
lock (lock_defectPuase)
{
defectPuaseList.RemoveAll(m => m.PhotoIndex == df.PhotoIndex && m.PicY < (df.PicY + 10));
}
return;
}
private DefectInfo GetDefectInfoByIndex(int index)
{
DefectInfo dt = null ;
lock (lock_defectPuase)
{
if (defectPuaseList.Count > 0)
dt = defectPuaseList[index].CloneModel();
}
return dt;
}
private int GetDefectPuaseListCnt()
{
int cnt = 0;
lock (lock_defectPuase)
{
cnt = defectPuaseList.Count;
}
return cnt;
}
private List GetDefectPuaseListByIndex(int Pindex)
{
List < DefectInfo >list = null;
lock (lock_defectPuase)
{
//return defectPuaseList.Where(m => m.PhotoIndex == Pindex).ToList().CloneModel();
list = defectPuaseList.Where(m => m.PhotoIndex == Pindex).ToList();
}
return list;
}
#endregion
///
/// 第一次计米启用
///
private bool JmFtStart = false;
//计算速度用,计算实时速度
private Stopwatch pRunSpeedWatch = new Stopwatch();
//报警上升沿
GetPN pn = new GetPN();
//模拟计米数据
private double jmTest = 0;
private bool IsTopAlm = false;
///
/// 获取速度和长度
///
private void GetLenAndSpd()
{
int errStep = 0;
double stl = 0; //计米起始位置
double etl = 0; //计米结束位置
double spl = 0; //计算速度计米位置
double yqjimi = 0; //计米长度
double cehouDis = 0;//测厚位置记录
double UseTime = 1;
double preSpd = 0;//上次速度,防止速度出差
double rioSpd = 0.3;//过冲
bool haveNG = false;
double hdJMDis = 0; //厚度计米间隔计米结束位置
AddTextEvent(DateTime.Now, "传感器流程", $"流程启动!");
while (true)
{
int spdcnt = 0;
int overspd = 0;
try
{
if (_cts.IsCancellationRequested)
break;
if (true)
{
////暂停开始
//stopWatch.Start();
do
{
if (currKey > 0 && currentState == CurrentStateEnum.运行中)
{
Records curRecord = Hashtable.Synchronized(htTask)[currKey] as Records;
if(curRecord == null)
{
AddTextEvent(DateTime.Now, $"流程运行", $"key={currKey},无数据!", WarningEnum.Normal, false);
continue;
}
#region 实时速度
#if JM
errStep = 1;
#if OnLine
double length = devContainer.GetLength();
#else
//模拟获取计米数据
double length = jmTest;
jmTest += 0.2;
#endif
if (length < 9999)
{
haveNG = false;
curRecord.Len = length;
if (!JmFtStart)
{
//第一次记录起始数据
stl = 0;
spl = 0;
cehouDis = 0;
hdJMDis = 0;
JmFtStart = true;
yqjimi = 0;
pRunSpeedWatch.Restart();
}
else
{
errStep = 2;
//1s计算一次实时速度
double dt = pRunSpeedWatch.ElapsedMilliseconds;
if (dt / 1000.0d > UseTime)
{
double spddis = Math.Abs(length - spl);
spl = length;
double runOnePicTime = dt / 1000.0d / (60.0d);//总时间 分
double spd = Math.Round(spddis / runOnePicTime, 2);
//if (spd !=0&&((Math.Abs(spd - preSpd) / preSpd) > rioSpd))
//{
// spd = (Math.Abs(spd - preSpd))/4 + preSpd;
//}
errStep = 3;
preSpd = spd;
SetSpd(spd);
if (spdcnt > 2)
{
AddTextEvent(DateTime.Now, $"计米对比", $"计米器距离{Math.Round(spddis, 2)},计米速度{Math.Round(spd, 2)},计米时间{Math.Round(runOnePicTime, 4)}", WarningEnum.Normal, false);
spdcnt = 0;
}
spdcnt++;
//重新计时
pRunSpeedWatch.Restart();
}
errStep = 4;
etl = length;//记录当前读数
yqjimi = Math.Round((etl - stl) / 1.00, 2); //记录长度
SetDis(yqjimi);
}
lock (lockCurrKey)
{
errStep = 5;
curRecord.Len = yqjimi + Config.JMOffset;
curRecord.TimeLen = pStopWatch.ElapsedMilliseconds / 1000.0d / 60.0d;//总时间 分
}
double tRunSpd = GetSpd();//1s计算的实时速度
this.BeginInvoke(new System.Action(() =>
{
AddTextEvent(DateTime.Now, $"记录", $"(计米{yqjimi}米), 速度:{tRunSpd}米/分", WarningEnum.Normal, false);
this.lblLen.Text = $"{yqjimi + Config.JMOffset}米";//$"{lenMi}米";
this.lblSpeed.Text = $"速度:{tRunSpd}米/分";//$"速度:{Math.Round(lenMi / curRecord.TimeLen, 2)}米/分";
}));
//速度超速 65m/min 切多次 进行报警
if(Config.OpenOverSpeed)
{
if(overspd > 5 && tRunSpd >(65))
{
overspd = 0;
//报警
Task.Run(async () =>
{
devContainer.io_output(CMDName.蜂鸣器输出, true, true, 5000);
await Task.Delay(5);
});
}
overspd++;
}
#region 二次判定
errStep = 6;
//处理2次判定
//计米器判断暂停:瑕疵二次判断 存在问题
try
{
//Log($"计米二次判断", $"开启{DefectPauseForUser},计米停车{confMgr.SysConfigParams.OpenLengthCountStop},缺陷数量{defectPuaseList.Count}", WarningEnum.Normal, false);
if (Config.OpenJMStop && defectPauseForUser && GetDefectPuaseListCnt() > 0)
{
var dtInfo = GetDefectInfoByIndex(0);
int pindex = dtInfo.PhotoIndex;
double XiaCiStopDis = dtInfo.PicY;
double pdis = dtInfo.CurrDis - ((4096 / Config.cm2px_y - dtInfo.PicY) / 100);
AddTextEvent(DateTime.Now, $"二次判断", $"计米器{yqjimi},瑕疵位置{pdis},停车距离{Config.StopLookDis}m,实时速度{tRunSpd}", WarningEnum.Normal, false);
//Log($"计米二次判断", $"计米位置{yqjimi},图片位置{pdis},停车距离{confMgr.SysConfigParams.StopLookDis}", WarningEnum.Normal, false);
if (yqjimi > (pdis + Config.StopLookDis)) //图片位置超过计米器
{
errStep = 7;
{
List lstEditDefect0 = GetDefectPuaseListByIndex(pindex);
AddTextEvent(DateTime.Now, $"暂停", $"(图像{pindex})已达观察台,瑕疵二次判断=》({string.Join(",", lstEditDefect0.Select(m => m.Code).ToArray())})是否包含在({string.Join(",", curRecord.ProductInfo.DefectPauseOption.ToArray())})中。", WarningEnum.Normal, false);
//瑕疵选项过滤 curRecord.ProductInfo.DefectPauseOption.Count == 0 || lstEditDefect.Where(x => curRecord.ProductInfo.DefectPauseOption.Contains(x.Code)).Count() > 0
if (curRecord.ProductInfo.DefectPauseOption.Count == 0 || lstEditDefect0.Where(x => curRecord.ProductInfo.DefectPauseOption.Contains(x.Code)).Count() > 0)
{
{
errStep = 8;
try
{
//暂停
AddTextEvent(DateTime.Now, $"暂停", $"(图像{pindex})需瑕疵二次判断,已达观察台,进入暂停。",WarningEnum.Normal, false);
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
//只是设备暂停,APP没暂停
devContainer.io_output(CMDName.绿灯输出, false, true, 0);
devContainer.io_output(CMDName.黄灯输出);
devContainer.devIOCard.writeBitState(0, 1, true);
Task.Run(async () =>
{
await Task.Delay(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
});
if (Config.OpenBeep && !IsTopAlm)
{
Task.Run(async () =>
{
devContainer.io_output(CMDName.蜂鸣器输出, false, true, Config.BeepTime);
await Task.Delay(5);
});
}
}
//获取完整数据
Thread.Sleep(500);
var lstEditDefect = GetDefectPuaseListByIndex(pindex);
#region 二级界面
int liPhotoIndex = pindex;
#region 加载界面
//if (!GetDefectPuaseIsShow())
//不能使用同步Invoke方式,会使相机超时丢帧
this.BeginInvoke(new System.Action(() =>
{
int liDefectCount = lstEditDefect.Count;
Mat pmat = null; ;
if (defectTag.ContainsKey(liPhotoIndex))
{
string filename = Path.Combine(Config.appBasePath + "\\TempPic\\", defectTag[liPhotoIndex]);
pmat = new Mat(filename);
}
else
AddTextEvent(DateTime.Now, $"二次检测", $"图像{liPhotoIndex}丢失", WarningEnum.Low, true);
FHome_Defect frmDefect = new FHome_Defect(curRecord.ProductInfo.ModelName, lstEditDefect, pmat, curRecord, defectWaitList, SectioningLen);
var sd = frmDefect.ShowDialog();
if (sd == DialogResult.OK || sd == DialogResult.Yes)
{
SectioningLen = frmDefect.SectioningLen;
if (sd == DialogResult.Yes)
{
//待定缺陷进行了放码
defectWaitList = frmDefect.lstWait;
}
else
{
//判断是否有待定缺陷,并加入列表
if (frmDefect.lstWait != null && frmDefect.lstWait.Count > 0)
defectWaitList.AddRange(frmDefect.lstWait);
}
//判断是否有放码信息
if (!string.IsNullOrEmpty(frmDefect.FMInfo[0]))
{
curRecord.FMInformation.Add(frmDefect.FMInfo);
}
//二次判定处理
defectTag.Remove(liPhotoIndex);
string oldCode;
for (int i = 0; i < this.uiDataGridView1.Rows.Count; i++)
{
if ((int)this.uiDataGridView1.Rows[i].Cells["colIndex"].Value != liPhotoIndex)
continue;
long uid = (long)this.uiDataGridView1.Rows[i].Cells["colUid"].Value;
foreach (var row in lstEditDefect)
{
AddTextEvent(DateTime.Now, $"暂停", $"修改第({i + 1})行瑕疵名称,{uid} {row.uid}", WarningEnum.Low, false);
if (row.uid == uid)
{
oldCode = this.uiDataGridView1.Rows[i].Cells["colCode"].Value.ToString();
AddTextEvent(DateTime.Now, $"暂停", $"修改第({i + 1})行瑕疵名称 ({this.uiDataGridView1.Rows[i].Cells["colDefectName"].Value})->({row.Name})", WarningEnum.Normal, false);
this.uiDataGridView1.Rows[i].Cells["colCode"].Value = row.Code;
this.uiDataGridView1.Rows[i].Cells["colDefectName"].Value = row.Name;
//this.uiDataGridView1.Refresh();
if (!string.IsNullOrWhiteSpace(row.TagFilePath))
File.Move(row.TagFilePath, row.TagFilePath.Replace($"_类别{oldCode}", $"_类别{row.Code}"));//
break;
}
}
foreach (var item in frmDefect.lstDel)
{
if (item.uid == uid)
{
this.uiDataGridView1.Rows.RemoveAt(i);
i--;
break;
}
}
}
foreach (var item in frmDefect.lstDel)
{
curRecord.DefectInfoList.Remove(item);
//删除忽略瑕疵小图
//if (!string.IsNullOrWhiteSpace(item.TagFilePath))
// File.Delete(item.TagFilePath);
}
//double len = (double)this.lblLen.Tag;
//this.reDrawDefectPoints(curRecord.DefectInfoList, new double[] { 0, Math.Round(curRecord.FaceWidthMax + 0.005f, 2) }, new double[] { 0, len });
AddTextEvent(DateTime.Now, $"二次检测", $"本次忽略{frmDefect.lstDel.Count}个瑕疵,本张图由{liDefectCount} -> {lstEditDefect.Count},总数{curRecord.DefectInfoList.Count}", WarningEnum.Normal, false);
}
this.uiMiniPagination1.TotalCount = curRecord.DefectTotalCount = curRecord.DefectInfoList.Count;
//
//double len = Math.Round((res.photoIndex + 1) * bmpHeight * 1.0d / Config.cm2px_y + 0.005f, 2);
this.reDrawDefectPoints(curRecord.ProductInfo.ModelName, curRecord.DefectInfoList);
errStep = 10;
//自动继续运行设备(这里临时暂停后不能再急停,否则无法继续)
if (!Config.StopPLC)
this.devContainer.devPlc.runDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
if (!compareIOInput(CMDName.暂停按钮))
{
devContainer.io_output(CMDName.绿灯输出);
devContainer.io_output(CMDName.黄灯输出, false, true, 0);
this.devContainer.devIOCard.writeBitState(0, 0, true);
Thread.Sleep(200);
this.devContainer.devIOCard.writeBitState(0, 0, false);
//Task.Run(async () =>
//{
// await Task.Delay(500);
// this.devContainer.devIOCard.writeBitState(0, 0, false);
//});
}
}
lineChartDefect.Focus();
frmDefect.Dispose();
}));
#endregion
{
lock (lock_defectPuase)
{
errStep = 12;
//删除同图所有缺陷
defectPuaseList.RemoveAll(m => m.PhotoIndex == pindex);
//删除当前缺陷10cm内缺陷
//defectPuaseList.RemoveAll(m => m.PhotoIndex == pindex && m.PicY < (XiaCiStopDis + 10));
}
}
//OnAutoRuning(new RunEventArgs(liPhotoIndex, lstEditDefect, curRecord, defectPuaseImgList[liPhotoIndex]));
#endregion
}
catch (Exception b)
{
//SetDefectPuaseIsShow(false);
AddTextEvent(DateTime.Now, "运行报警", $"缺陷列表警告1:{errStep}:" + b.Message, WarningEnum.Low);
}
}
}
errStep = 20;
}
}
}
//二次判断手动关闭状态,清除所有
if (!defectPauseForUser)
{
errStep = 21;
if (defectPuaseList.Count > 0)
defectPuaseList.Clear();
}
}
catch (Exception b)
{
AddTextEvent(DateTime.Now, "运行报警", $"缺陷列表警告2:{errStep}:" + b.Message, WarningEnum.Low);
}
#endregion
//金属探测
if (Config.OpenJinShuJianCe)
{
#if OnLine
if (pn.P(compareIOInput(CMDName.金属检测输入)))
{
errStep = 22;
AddTextEvent(DateTime.Now, $"金属记录", $"(计米{yqjimi}米)", WarningEnum.Normal, false);
curRecord.DefectTotalCount += 1;
if (curRecord.DefectInfoList == null)
curRecord.DefectInfoList = new List();
DefectInfo defectInfo = new DefectInfo
{
PhotoIndex = -1,
Code = "JSYC",
Name = "金属",
X = 5,//cm未知
Y = Math.Round(yqjimi * 100, 2),//cm
Width = 0.1,//cm
Height = 0.1,//cm
ZXD = 1,
Contrast = 1,
Target = 1,
imageID = "",//res.lstDefectBmp[i].Clone(),
};
errStep = 23;
defectInfo.ModifyUserCode = defectInfo.CreateUserCode = curRecord.CreateUserCode;
curRecord.DefectInfoList.Add(defectInfo);
//更新UI
object[] rowItem = new object[]{ defectInfo.uid, defectInfo.Code, defectInfo.PhotoIndex,defectInfo.Name,
defectInfo.CentreX, defectInfo.CentreY / 100,defectInfo.Width * 10,
defectInfo.Height * 10, defectInfo.Area * 100, defectInfo.ZXD, defectInfo.Contrast};
this.BeginInvoke(new System.Action(() =>
{
this.uiDataGridView1.Rows.Insert(0, rowItem);
this.uiMiniPagination1.TotalCount = curRecord.DefectInfoList.Count;
//
double len = Math.Round(yqjimi * 100 + 0.05f, 2);
double fk = 0;
if (curRecord.FaceWidthMax == 0)
fk = 100;
else
fk = curRecord.FaceWidthMax;
this.reDrawDefectPoints(curRecord.ProductInfo.ModelName, curRecord.DefectInfoList, new double[] { 0, Math.Round(fk + 0.005f, 2) }, new double[] { 0, len });
}));
}
#else
;
#endif
}
//厚度数据读取
if (Config.OpenHouDuJiLu)
{
errStep = 24;
double d1 = 0, d2 = 0, d3 = 0;
if (Config.CustomerName != "XCL")
{
#if OnLine
if (devContainer.GetThicknessValue(out d1, out d2, out d3))
#else
Random rand = new Random();
double sd1 = rand.NextDouble();
double sd2 = rand.NextDouble();
double sd3 = rand.NextDouble();
d1 = 1.1 + sd1; d2 = 1.2 + sd2; d3 = 1.3 + sd3;
if(true)
#endif
{
if (Math.Abs(yqjimi - hdJMDis) * 100 > 10) //在10cm以内不做记录
{
//加入偏差计算
d1 = Math.Round(d1 + Config.DataOffset1, 2);
d2 = Math.Round(d2 + Config.DataOffset2, 2);
d3 = Math.Round(d3 + Config.DataOffset3, 2);
//限制0-5mm范围
d1 = d1 > 5 ? 5 : d1;
d2 = d2 > 5 ? 5 : d2;
d3 = d3 > 5 ? 5 : d3;
this.BeginInvoke(new System.Action(() =>
{
this.uilbHD.Text = $"当前厚度:{d1}, {d2}, {d3}";
}));
AddTextEvent(DateTime.Now, $"厚度数据读取", $"厚度数据读取{yqjimi}-{hdJMDis}-{d1}-{d2}-{d3}", WarningEnum.Normal, false);
if (curRecord.ThicknessList == null)
curRecord.ThicknessList = new List();
Thickness ThicknessInfo = new Thickness
{
Y_Dis = Math.Round(yqjimi * 100, 2),//cm
Value1 = d1,
Value2 = d2,
Value3 = d3,
};
ThicknessInfo.ModifyUserCode = ThicknessInfo.CreateUserCode = curRecord.CreateUserCode;
curRecord.ThicknessList.Add(ThicknessInfo);
hdJMDis = yqjimi;
double yStart = (ThicknessInfo.Y_Dis / 100.0 - LineDataLen) > 0 ? (ThicknessInfo.Y_Dis / 100.0 - LineDataLen) : 0;
List hdMax = new List(){
curRecord.ThicknessList.Where(x => (x.Y_Dis / 100.0) >= yStart).ToList().Select(t=> t.Value1).ToList().Max(),
curRecord.ThicknessList.Where(x => (x.Y_Dis / 100.0) >= yStart).ToList().Select(t => t.Value2).ToList().Max(),
curRecord.ThicknessList.Where(x => (x.Y_Dis / 100.0) >= yStart).ToList().Select(t => t.Value3).ToList().Max(),
};
List hdMin = new List(){
curRecord.ThicknessList.Where(x => (x.Y_Dis / 100.0) >= yStart).ToList().Select(t=> t.Value1).ToList().Min(),
curRecord.ThicknessList.Where(x => (x.Y_Dis / 100.0) >= yStart).ToList().Select(t => t.Value2).ToList().Min(),
curRecord.ThicknessList.Where(x => (x.Y_Dis / 100.0) >= yStart).ToList().Select(t => t.Value3).ToList().Min(),
};
var ShowThicknessList = curRecord.ThicknessList.Where(x => (x.Y_Dis / 100.0) >= yStart).ToList();
//显示100m数据
reDrawHouDu(ShowThicknessList,
new double[] { yStart * 100, Math.Round(yqjimi * 100 + 0.005f, 2) },
new double[] { (hdMin.Min()-0.1) <=0? 0: (hdMin.Min() - 0.1),
(hdMax.Max() + 0.1)> 5?5: (hdMax.Max() + 0.1) });
}
}
else
AddTextEvent(DateTime.Now, $"厚度数据读取", $"厚度数据读取失败{d1}-{d2}-{d3}", WarningEnum.Low, true);
}
else
{
if (curRecord.ThicknessList != null && curRecord.ThicknessList.Count > 0)
{
d1 = curRecord.ThicknessList[curRecord.ThicknessList.Count - 1].Value1;
d2 = curRecord.ThicknessList[curRecord.ThicknessList.Count - 1].Value2;
d3 = curRecord.ThicknessList[curRecord.ThicknessList.Count - 1].Value3;
//新材料厚度显示
this.BeginInvoke(new System.Action(() =>
{
this.uilbHD.Text = $"当前厚度:{d1}, {d2}, {d3}";
}));
//var ShowThicknessList = curRecord.ThicknessList.Where(x => (x.Y_Dis / 100.0) >= yStart).ToList();
List hdMax = new List(){
curRecord.ThicknessList.Select(t=> t.Value1).ToList().Max(),
curRecord.ThicknessList.Select(t => t.Value2).ToList().Max(),
curRecord.ThicknessList.Select(t => t.Value3).ToList().Max(),
};
List hdMin = new List(){
curRecord.ThicknessList.Select(t=> t.Value1).ToList().Min(),
curRecord.ThicknessList.Select(t => t.Value2).ToList().Min(),
curRecord.ThicknessList.Select(t => t.Value3).ToList().Min(),
};
//显示100m数据
reDrawHouDu(curRecord.ThicknessList,
new double[] { 0, Math.Round(curRecord.ThicknessList[curRecord.ThicknessList.Count - 1].Y_Dis / 100 + 0.005f, 2) },
new double[] { (hdMin.Min()-0.1) <=0? 0: (hdMin.Min() - 0.1),
(hdMax.Max() + 0.1)> 5?5: (hdMax.Max() + 0.1) });
}
}
}
//定单长度提醒
if (Config.orderWarnningLen > 0 && curRecord.Len > 0 && Config.orderWarnningLen <= Math.Abs(curRecord.Len))
{
#region 剩余提示暂停
if (!_orderWarnningLenStop)
{
AddTextEvent(DateTime.Now, $"告警", $"订单长度已经达到设置!({curRecord.Len}>={Config.orderWarnningLen})", WarningEnum.High);
_orderWarnningLenStop = true;
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
//只是设备暂停,APP没暂停
devContainer.io_output(CMDName.绿灯输出, false, true, 0);
devContainer.io_output(CMDName.黄灯输出);
devContainer.devIOCard.writeBitState(0, 1, true);
Thread.Sleep(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
}
this.BeginInvoke(new System.Action(() =>
{
MessageBox.Show($"订单长度已经达到设置!({curRecord.Len}>={Config.orderWarnningLen}),进入暂停。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}));
}
#endregion
}
}
else
{
if (haveNG)
{
AddTextEvent(DateTime.Now, "运行报警", $"计米器无数据", WarningEnum.Low);
haveNG = true;
}
// stLen = lenMi;
// curRecord.Len = lenMi;
// this.BeginInvoke(new System.Action(() =>
// {
// AddTextEvent(DateTime.Now, $"计米无数据记录", $"(计米{stLen}米), 速度:{Math.Round(lenMi / curRecord.TimeLen, 2)}米/分", WarningEnum.Normal, false);
// this.lblLen.Text = $"@ {stLen}米";//$"{lenMi}米";
// this.lblLen.Tag = faceWidthX_cm;
// this.lblSpeed.Text = $"@速度:{Math.Round(lenMi / curRecord.TimeLen, 2)}米/分";
// this.uilbKF.Text = $"当前幅宽:{faceWidthY_cm}cm";
// }));
}
#endif
}
#endregion
#if Online //在图像中实现
lock (lockCurrKey)
{
errStep = 15;
#region 换卷长度提示
if (confMgr.SysConfigParams.OpenLengthCount)
{
if (curRecord.ProductInfo.residueWarnningLen > 0 && yqjimi > curRecord.ProductInfo.residueWarnningLen)
{
Log($"告警", $"已经达到换卷长度最大值{curRecord.ProductInfo.residueWarnningLen}-当前长度{yqjimi}-", WarningEnum.High);
}
}
#endregion
#region 测厚停止
if (confMgr.SysConfigParams.OpenLengthCount && curRecord.ProductInfo.OpenThicknessDetection)
{
errStep = 16;
if (curRecord.ProductInfo.ThicknessDetectionStopDis > 0 && (yqjimi - cehouDis) > curRecord.ProductInfo.ThicknessDetectionStopDis)
{
Stop();
Log($"测厚提示", $"已经达到测厚位置{curRecord.ProductInfo.ThicknessDetectionStopDis}-当前前进长度{(yqjimi - cehouDis)}-", WarningEnum.High);
}
}
#endregion
}
}
#endif
Thread.Sleep(300);
} while (!isBreakProcessRun());
//暂停中断
//stopWatch.Stop();
//pStopWatch.Stop();
//_isRuning = false;
}
Thread.Sleep(10);
}
catch (Exception e)
{
AddTextEvent( DateTime.Now, "运行报警", $"传感器流程运行出错{errStep}:" + e.Message , WarningEnum.High);
}
}
}
#endregion
#region 相机线程1 && 二次判定
int indexTest1 = 0;
///
/// 相机1采图预处理
///
private void Cam1ThreadFunction()
{
int errStep = 0;
AddTextEvent(DateTime.Now, "相机流程1", $"流程启动!");
#if OnLine
#else
string imgfilePath = "D:\\CPL\\img\\L1.bmp";
Mat timg = new Mat(imgfilePath);
//timg = timg.Flip(FlipMode.XY);
#endif
while (true)
{
try
{
if (_cts.IsCancellationRequested)
break;
if (currentState == CurrentStateEnum.运行中)
{
////暂停开始
//stopWatch.Start();
do
{
#region 实时采图
#if OnLine
errStep = 1112;
int index;
Mat Image;
int devNo;
//采集图片
if(devContainer.devCamer1 == null)
{
Thread.Sleep(50);
continue;
}
bool gsts = devContainer.devCamer1.readDataImage(out index, out Image, out devNo);
//AcquisitionMat acq = _LinecamDev1.GetFrames(100);
#else
Thread.Sleep(delayTime);
bool gsts = true;
int index = indexTest1;
indexTest1++;
Mat Image = timg.Clone();
int devNo = 0;
#endif
if (gsts)
{
//double stLen = devContainer.GetLength();
errStep = 1113;
Cv2.Flip(Image, Image, FlipMode.XY);//翻转
AddTextEvent(DateTime.Now, $"拍照", $"采集卡({devNo}),图像({index})", WarningEnum.Normal, false);
Bitmap bitmap = (Bitmap)BitmapConverter.ToBitmap(Image).Clone();
this.BeginInvoke(new Action(() =>
{
//Bitmap bitmap = Image.ToBitmap();
//显示图片
if (devNo == 0)
{
picScanner2.Image = bitmap;
//picScanner2.Refresh();
}
else
{
picScanner1.Image = bitmap;
//picScanner1.Refresh();
}
}));
//裁边
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
int marginWidth;
Image = OpenCVUtil.getMaxInsetRect2(Image, devNo == 1 ? true : false, Config.MarginHoleWidth, out marginWidth);
//AddTextEvent(DateTime.Now,"裁边", $"(图像{scanPhotos0.photoIndex})-图0裁边后:{mat0.Width}*{mat0.Height}");
errStep = 55;
string time = $"dev{devNo}-图{index}裁边({stopWatch.ElapsedMilliseconds})";
AddTextEvent(DateTime.Now, $"拍照", time, WarningEnum.Normal, false);
errStep = 1;
if (!devContainer.state || (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停))
{
if(currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停)
AddTextEvent(DateTime.Now, $"系统", $"不在运行状态-{currentState}", WarningEnum.Normal, true);
else
AddTextEvent(DateTime.Now, $"系统", $"硬件状态出错-{devContainer.state}", WarningEnum.Normal, true);
Image.Dispose();
continue;
}
if (currKey != 0 || htTask.ContainsKey(currKey))
{
Records curRecord = Hashtable.Synchronized(htTask)[currKey] as Records;
//长度剩余提醒
if (Config.residueWarnningLen > 0 && curRecord.ErpLen > 0 && Config.residueWarnningLen >= Math.Abs(curRecord.ErpLen - curRecord.Len))
{
AddTextEvent(DateTime.Now, $"告警", $"已达剩余长度不足提醒!(({curRecord.ErpLen} -{ curRecord.Len})<={Config.residueWarnningLen})", WarningEnum.High);
#region 剩余提示暂停
if (!_residueWarnningLenStop)
{
_residueWarnningLenStop = true;
AddTextEvent(DateTime.Now, $"暂停", $"已达剩余长度不足提醒!(({curRecord.ErpLen} - {curRecord.Len})<={Config.residueWarnningLen}),进入暂停。");
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
//只是设备暂停,APP没暂停
devContainer.io_output(CMDName.绿灯输出, false, true, 0);
devContainer.io_output(CMDName.黄灯输出);
devContainer.devIOCard.writeBitState(0, 1, true);
//Task.Run(async () =>
//{
// await Task.Delay(500);
Thread.Sleep(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
//});
}
this.BeginInvoke(new System.Action(() =>
{
//warning(WarningEnum.Low, true);//暂停
MessageBox.Show($"已达剩余长度不足提醒!({Math.Abs(curRecord.ErpLen - curRecord.Len)}<={Config.residueWarnningLen}),进入暂停。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}));
}
#endregion
}
errStep = 6;
//厚度检测提示
if (curRecord.ProductInfo.OpenThicknessDetection)
{
if ((curRecord.Len - ThnDieLen) > curRecord.ProductInfo.ThicknessDetectionStopDis)
{
ThnDieLen = curRecord.Len;
AddTextEvent(DateTime.Now, $"厚度检测", $"位置:{curRecord.Len}; 上次位置:{ThnDieLen}");
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
//只是设备暂停,APP没暂停
//devContainer.io_output(CMDName.绿灯输出, false, true, 0);
//devContainer.io_output(CMDName.黄灯输出);
devContainer.devIOCard.writeBitState(0, 1, true);
Task.Run(async () =>
{
await Task.Delay(500);
//Thread.Sleep(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
});
}
}
}
//二次判断
curRecord.dicPhoto_Defect.TryAdd(index, false);//加入索引,默认无瑕疵
errStep = 7;
//暂停:瑕疵二次判断
if (this.defectPauseForUser && !Config.OpenJMStop)
{
AddTextEvent(DateTime.Now, $"拍照", $"图像索引:{index},标识数:{curRecord.dicPhoto_Defect.Count}", WarningEnum.Normal, false);
int liPhotoIndex = index - Config.defectPauseSkipPhotoCount;
//新材料代码,因为刹车垃圾所以特制化
if(Config.CustomerName == "XCL")
{
double nowSpd = GetSpd();//获取当前速度
if (nowSpd < 30)
liPhotoIndex = index - 14;
else if (nowSpd < 40)
liPhotoIndex = index - 13;
else if (nowSpd < 45)
liPhotoIndex = index - 12;
else if (nowSpd < 55)
liPhotoIndex = index - 11;
else if (nowSpd < 65)
liPhotoIndex = index - 10;
else
liPhotoIndex = index - 10;
}
AddTextEvent(DateTime.Now, $"拍照", $"Dev={devNo},图像{index} {liPhotoIndex}={index}-{Config.defectPauseSkipPhotoCount};", WarningEnum.Normal, false);
if (liPhotoIndex >= 0 && curRecord.dicPhoto_Defect.ContainsKey(liPhotoIndex) && curRecord.dicPhoto_Defect[liPhotoIndex])
{
errStep = 8;
for (int fi = 0; fi < 3; fi++)
{
//循环等待数据后期处理完成
Thread.Sleep(500);
List lstEditDefect = curRecord.DefectInfoList.Where(m => m.PhotoIndex == liPhotoIndex).ToList();
AddTextEvent(DateTime.Now, $"暂停", $"(图像{liPhotoIndex})已达观察台,瑕疵二次判断=》({string.Join(",", lstEditDefect.Select(m => m.Code).ToArray())})是否包含在({string.Join(",", curRecord.ProductInfo.DefectPauseOption.ToArray())})中。", WarningEnum.Normal, false);
//瑕疵选项过滤
if (Config.OpenHalconDefect || curRecord.ProductInfo.DefectPauseOption.Count == 0 || lstEditDefect.Where(x => curRecord.ProductInfo.DefectPauseOption.Contains(x.Code)).Count() > 0)
{
//暂停
AddTextEvent(DateTime.Now, $"暂停", $"(图像{liPhotoIndex})需瑕疵二次判断,已达观察台,进入暂停。", WarningEnum.Normal, false);
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
//只是设备暂停,APP没暂停
devContainer.io_output(CMDName.绿灯输出, false, true, 0);
devContainer.io_output(CMDName.黄灯输出);
devContainer.devIOCard.writeBitState(0, 1, true);
Task.Run(async () =>
{
await Task.Delay(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
});
if (Config.OpenBeep && !IsTopAlm)
{
Task.Run(async () =>
{
devContainer.io_output(CMDName.蜂鸣器输出, false, true, Config.BeepTime);
await Task.Delay(5);
});
}
}
errStep = 9;
//不能使用同步Invoke方式,会使相机超时丢帧
this.BeginInvoke(new System.Action(() =>
{
int liDefectCount = lstEditDefect.Count;
Mat pmat = null;
if (defectTag.ContainsKey(liPhotoIndex))
{
string filename = Path.Combine(Config.appBasePath + "\\TempPic\\", defectTag[liPhotoIndex]);
pmat = new Mat(filename);
}
else
AddTextEvent(DateTime.Now, $"二次检测", $"图像{liPhotoIndex}丢失", WarningEnum.Low, true);
//var pimage = (Bitmap)pmat.ToBitmap().Clone();
FHome_Defect frmDefect = new FHome_Defect(curRecord.ProductInfo.ModelName, lstEditDefect, pmat, curRecord, defectWaitList, SectioningLen);
var sd = frmDefect.ShowDialog();
if (sd == DialogResult.OK || sd == DialogResult.Yes)
{
SectioningLen = frmDefect.SectioningLen;
if (sd == DialogResult.Yes)
{
//待定缺陷进行了放码
defectWaitList = frmDefect.lstWait;
}
else
{
//判断是否有待定缺陷,并加入列表
if (frmDefect.lstWait != null && frmDefect.lstWait.Count > 0)
defectWaitList.AddRange(frmDefect.lstWait);
}
//判断是否有放码信息
if (!string.IsNullOrEmpty(frmDefect.FMInfo[0]))
{
curRecord.FMInformation.Add(frmDefect.FMInfo);
}
//二次判定处理
defectTag.Remove(liPhotoIndex);
string oldCode;
for (int i = 0; i < this.uiDataGridView1.Rows.Count; i++)
{
if ((int)this.uiDataGridView1.Rows[i].Cells["colIndex"].Value != liPhotoIndex)
continue;
long uid = (long)this.uiDataGridView1.Rows[i].Cells["colUid"].Value;
foreach (var row in lstEditDefect)
{
AddTextEvent(DateTime.Now, $"暂停", $"修改第({i + 1})行瑕疵名称,{uid} {row.uid}", WarningEnum.Low, false);
if (row.uid == uid)
{
oldCode = this.uiDataGridView1.Rows[i].Cells["colCode"].Value.ToString();
AddTextEvent(DateTime.Now, $"暂停", $"修改第({i + 1})行瑕疵名称 ({this.uiDataGridView1.Rows[i].Cells["colDefectName"].Value})->({row.Name})", WarningEnum.Normal, false);
this.uiDataGridView1.Rows[i].Cells["colCode"].Value = row.Code;
this.uiDataGridView1.Rows[i].Cells["colDefectName"].Value = row.Name;
//this.uiDataGridView1.Refresh();
if (!string.IsNullOrWhiteSpace(row.TagFilePath))
File.Move(row.TagFilePath, row.TagFilePath.Replace($"_类别{oldCode}", $"_类别{row.Code}"));//
break;
}
}
foreach (var item in frmDefect.lstDel)
{
if (item.uid == uid)
{
this.uiDataGridView1.Rows.RemoveAt(i);
i--;
break;
}
}
}
foreach (var item in frmDefect.lstDel)
{
curRecord.DefectInfoList.Remove(item);
//删除忽略瑕疵小图
//if (!string.IsNullOrWhiteSpace(item.TagFilePath))
// File.Delete(item.TagFilePath);
}
//double len = (double)this.lblLen.Tag;
//this.reDrawDefectPoints(curRecord.DefectInfoList, new double[] { 0, Math.Round(curRecord.FaceWidthMax + 0.005f, 2) }, new double[] { 0, len });
AddTextEvent(DateTime.Now, $"二次检测", $"本次忽略{frmDefect.lstDel.Count}个瑕疵,本张图由{liDefectCount} -> {lstEditDefect.Count},总数{curRecord.DefectInfoList.Count}", WarningEnum.Normal, false);
}
this.uiMiniPagination1.TotalCount = curRecord.DefectTotalCount = curRecord.DefectInfoList.Count;
//
//double len = Math.Round((res.photoIndex + 1) * bmpHeight * 1.0d / Config.cm2px_y + 0.005f, 2);
this.reDrawDefectPoints(curRecord.ProductInfo.ModelName, curRecord.DefectInfoList);
errStep = 10;
//自动继续运行设备(这里临时暂停后不能再急停,否则无法继续)
if (!Config.StopPLC)
this.devContainer.devPlc.runDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
if (!compareIOInput(CMDName.暂停按钮))
{
devContainer.io_output(CMDName.绿灯输出);
devContainer.io_output(CMDName.黄灯输出, false, true, 0);
this.devContainer.devIOCard.writeBitState(0, 0, true);
Thread.Sleep(200);
this.devContainer.devIOCard.writeBitState(0, 0, false);
//Task.Run(async () =>
//{
// await Task.Delay(500);
// this.devContainer.devIOCard.writeBitState(0, 0, false);
//});
}
}
lineChartDefect.Focus();
frmDefect.Dispose();
}));
break;
}
}
}
}
#if false //未在改时间进行判定,改为检出时候
else if(Config.OpenWarnBeep)
{
AddTextEvent(DateTime.Now, $"拍照", $"图像索引:{index},标识数:{curRecord.dicPhoto_Defect.Count}", WarningEnum.Normal, false);
int liPhotoIndex = index - Config.defectPauseSkipPhotoCount;
AddTextEvent(DateTime.Now, $"拍照", $"Dev={devNo},图像{index} {liPhotoIndex}={index}-{Config.defectPauseSkipPhotoCount};", WarningEnum.Normal, false);
if (liPhotoIndex >= 0 && curRecord.dicPhoto_Defect.ContainsKey(liPhotoIndex) && curRecord.dicPhoto_Defect[liPhotoIndex])
{
errStep = 8;
//for (int fi = 0; fi < 3; fi++)
{
//循环等待数据后期处理完成
//Thread.Sleep(500);
List lstEditDefect = curRecord.DefectInfoList.Where(m => m.PhotoIndex == liPhotoIndex).ToList();
AddTextEvent(DateTime.Now, $"准备报警", $"(图像{liPhotoIndex})已达观察台,瑕疵二次判断=》({string.Join(",", lstEditDefect.Select(m => m.Code).ToArray())})是否包含在({string.Join(",", curRecord.ProductInfo.DefectPauseOption.ToArray())})中。");
//瑕疵选项过滤
if (Config.OpenHalconDefect || curRecord.ProductInfo.DefectPauseOption.Count == 0 || lstEditDefect.Where(x => curRecord.ProductInfo.DefectPauseOption.Contains(x.Code)).Count() > 0)
{
//暂停
AddTextEvent(DateTime.Now, $"蜂鸣报警", $"(图像{liPhotoIndex})需瑕疵报警,已达观察台。");
if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
if (Config.OpenBeep)
{
Task.Run(async () =>
{
devContainer.io_output(CMDName.蜂鸣器输出, false, true, Config.BeepTime);
await Task.Delay(5);
});
}
}
errStep = 9;
//break;
}
}
}
}
#endif
errStep = 10;
//以下判定放置于二次判定之后,因为二次判定可能会忽略部分检测项!!
//暂停:缺陷超标
//每百米告警判断???在此还是在收到新照片时触发???
if (curRecord.ProductInfo.DefectCountLimit > 0 && curRecord.DefectTotalCount >= curRecord.ProductInfo.DefectCountLimit && curRecord.DefectInfoList != null)
{
int liPhotoIndex =index - Config.defectPauseSkipPhotoCount; //二次判定完的图片index
int compLen = 100 * 100;//每百米 to cm
int compCount = compLen * Config.cm2px_y / Image.Height; //100m图像张数
//从上次告警后重新开始计算长度及数量
int defectCount = curRecord.DefectInfoList.Where(m => m.PhotoIndex >= curRecord.preWarningPhotoIndex && m.PhotoIndex >= (liPhotoIndex + 1 - compCount) && m.PhotoIndex <= liPhotoIndex).Count();
if (defectCount >= curRecord.ProductInfo.DefectCountLimit)
{
curRecord.preWarningPhotoIndex = liPhotoIndex + 1;
AddTextEvent(DateTime.Now, $"告警", $"每百米瑕疵数量达到阈值!({defectCount}>={curRecord.ProductInfo.DefectCountLimit})", WarningEnum.High);
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
//只是设备暂停,APP没暂停
devContainer.io_output(CMDName.绿灯输出, false, true, 0);
devContainer.io_output(CMDName.黄灯输出);
devContainer.devIOCard.writeBitState(0, 1, true);
Task.Run(async () =>
{
await Task.Delay(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
});
if (Config.OpenBeep && !IsTopAlm)
{
Task.Run(async () =>
{
devContainer.io_output(CMDName.蜂鸣器输出, false, true, Config.BeepTime);
await Task.Delay(5);
});
}
}
}
errStep = 11;
}
//暂停:每个缺陷项不同长度检测数量报警
if (curRecord.DefectTotalCount > 0 && curRecord.DefectInfoList != null)
{
#if true
//按缺陷计算没X米多少缺陷报警
for (int i = 0; i < curRecord.ProductInfo.QualifiedLimitList.Count; i++)
{
var defectWarn = curRecord.ProductInfo.QualifiedLimitList[i];
if (defectWarn.DefectWarnLength > 0 || defectWarn.DefectWarnCnt > 0)
{
errStep = 12;
//int liPhotoIndex = index - Config.defectPauseSkipPhotoCount; //二次判定完的图片index
int liPhotoIndex = index;
int warnLen = defectWarn.DefectWarnLength * 100;//每米 to cm
int warnCount = warnLen * Config.cm2px_y / Image.Height; //计算图像张数
//从上次告警后重新开始计算长度及数量
int warnDefectCount = curRecord.DefectInfoList.Where(m => m.PhotoIndex >= curRecord.preWarningPhotoIndexByLabel[i] && m.PhotoIndex >= (liPhotoIndex + 1 - warnCount) && m.PhotoIndex <= liPhotoIndex && m.Code == defectWarn.Code).Count();
if (warnDefectCount >= defectWarn.DefectWarnCnt)
{
curRecord.preWarningPhotoIndexByLabel[i] = liPhotoIndex + 1;
AddTextEvent(DateTime.Now, $"告警", $"每{defectWarn.DefectWarnLength}米{Config.getDefectName(curRecord.ProductInfo.ModelName, defectWarn.Code)}瑕疵数量达到阈值!({warnDefectCount}>={defectWarn.DefectWarnCnt})", WarningEnum.High);
if (!Config.StopPLC)
this.devContainer.devPlc.pauseDev();
else if (!Config.StopIO && devContainer.devIOCard.IsInit)
{
//只是设备暂停,APP没暂停
devContainer.io_output(CMDName.绿灯输出, false, true, 0);
devContainer.io_output(CMDName.黄灯输出);
devContainer.devIOCard.writeBitState(0, 1, true);
Task.Run(async () =>
{
await Task.Delay(500);
this.devContainer.devIOCard.writeBitState(0, 1, false);
});
if (Config.OpenBeep)
{
IsTopAlm = true;
Task.Run(async () =>
{
devContainer.io_output(CMDName.蜂鸣器输出, true, false, Config.BeepTime);
//devContainer.io_output(CMDName.蜂鸣器输出, false, true, Config.BeepTime);
await Task.Delay(5);
});
}
}
}
}
}
#endif
}
errStep = 21;
//Application.DoEvents();
}
_matList1.Enqueue(new tScanPhotoInfo(1, index, Image.Clone()));
}
#endregion
//System.GC.Collect();
Thread.Sleep(50);
} while (!isBreakProcessRun());
//暂停中断
//stopWatch.Stop();
pStopWatch.Stop();
//_isRuning = false;
}
else
{
int index;
Mat Image;
int devNo;
//采集图片
if (devContainer.devCamer1 == null)
{
Thread.Sleep(50);
continue;
}
//防止内存爆满
devContainer.devCamer1.readDataImage(out index, out Image, out devNo);
}
Thread.Sleep(50);
}
catch (Exception e)
{
//_isRuning = false;
AddTextEvent(DateTime.Now, $"运行报警", $"相机1流程运行出错{errStep}:" + e.Message + "\n", WarningEnum.High, true);
//Log("运行报警", $"相机1流程运行出错{errStep}:" + e.Message + "\n", WarningEnum.High);
}
}
}
#endregion
#region 相机线程2 && 图片位置
int indexTest2 = 0;
///
/// 相机2采图预处理
///
private void Cam2ThreadFunction()
{
int errStep = 0;
AddTextEvent(DateTime.Now, "相机流程2", $"流程启动!");
#if OnLine
#else
string imgfilePath = "D:\\CPL\\img\\R1.bmp";
Mat timg = new Mat(imgfilePath);
//timg = timg.Flip(FlipMode.XY);
#endif
while (true)
{
try
{
if (_cts.IsCancellationRequested)
break;
if (currentState == CurrentStateEnum.运行中)
{
////暂停开始
//stopWatch.Start();
do
{
#region 实时采图
#if OnLine
int index;
Mat Image;
int devNo;
//采集图片
if (devContainer.devCamer2 == null)
{
Thread.Sleep(50);
continue;
}
bool gsts = devContainer.devCamer2.readDataImage(out index, out Image, out devNo);
#else
Thread.Sleep(delayTime);
int index = indexTest2;
indexTest2++;
Mat Image = timg.Clone();
int devNo = 1;
bool gsts = true;
#endif
//AcquisitionMat acq = _LinecamDev1.GetFrames(100);
if (gsts)
{
//获取图片成功,说明正好拍完照片,获取当前计米器位置
double picDis = GetDis();
Cv2.Flip(Image, Image, FlipMode.XY);//翻转
AddTextEvent(DateTime.Now, $"拍照", $"采集卡({devNo}),图像({index})", WarningEnum.Normal, false);
Bitmap bitmap = (Bitmap)BitmapConverter.ToBitmap(Image).Clone();
this.BeginInvoke(new Action(() =>
{
//Bitmap bitmap = Image.ToBitmap();
//显示图片
if (devNo == 0)
{
picScanner2.Image = bitmap;
//picScanner2.Refresh();
}
else
{
picScanner1.Image = bitmap;
//picScanner1.Refresh();
}
}));
//裁边
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
int marginWidth;
Image = OpenCVUtil.getMaxInsetRect2(Image, devNo == 1 ? true : false, Config.MarginHoleWidth, out marginWidth);
//AddTextEvent(DateTime.Now,"裁边", $"(图像{scanPhotos0.photoIndex})-图0裁边后:{mat0.Width}*{mat0.Height}");
errStep = 55;
string time = $"dev{devNo}-图{index}裁边({stopWatch.ElapsedMilliseconds})";
AddTextEvent(DateTime.Now, $"拍照", time, WarningEnum.Normal, false);
errStep = 1;
if (!devContainer.state || (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停))
{
if (currentState != CurrentStateEnum.运行中 && currentState != CurrentStateEnum.暂停)
AddTextEvent(DateTime.Now, $"系统", $"不在运行状态-{currentState}", WarningEnum.Normal, true);
else
AddTextEvent(DateTime.Now, $"系统", $"硬件状态出错-{devContainer.state}", WarningEnum.Normal, true);
Image.Dispose();
continue;
}
_matList2.Enqueue(new tScanPhotoInfo(1, index, Image.Clone(), picDis));
}
#endregion
//System.GC.Collect();
Thread.Sleep(50);
} while (!isBreakProcessRun());
//暂停中断
//stopWatch.Stop();
pStopWatch.Stop();
//_isRuning = false;
}
else
{
//防止内存爆满
int index;
Mat Image;
int devNo;
//采集图片
if (devContainer.devCamer2 == null)
{
Thread.Sleep(50);
continue;
}
bool gsts = devContainer.devCamer2.readDataImage(out index, out Image, out devNo);
}
Thread.Sleep(50);
}
catch (Exception e)
{
//_isRuning = false;
AddTextEvent(DateTime.Now, $"运行报警", $"相机1流程运行出错{errStep}:" + e.Message + "\n", WarningEnum.High, true);
//Log("运行报警", $"相机1流程运行出错{errStep}:" + e.Message + "\n", WarningEnum.High);
}
}
}
#endregion
#region 处理线程
int testCnt = 0;
///
/// 后台运行主线程
///
private void MainThreadFunction()
{
AddTextEvent(DateTime.Now, "处理流程流程", $"流程启动!");
Mat acq1 = new Mat();
Mat acq2 = new Mat();
#if OnLine
#else
string imgfilePath1 = "D:\\CPL\\img\\L1.bmp";
string imgfilePath2 = "D:\\CPL\\img\\R1.bmp";
Mat timg1 = new Mat(imgfilePath1);
timg1 = timg1.Flip(FlipMode.XY);
Mat timg2 = new Mat(imgfilePath2);
timg2 = timg1.Flip(FlipMode.XY);
#endif
while (true)
{
int errStep = 0;
try
{
if (_cts.IsCancellationRequested)
break;
if (true)
{
////暂停开始
//stopWatch.Start();
do
{
tScanPhotoInfo peek1;
tScanPhotoInfo peek2;
if (_matList1.Count > 0 && _matList2.Count > 0)
{
_matList1.TryPeek(out peek1);
_matList2.TryPeek(out peek2);
if (peek1 != null && peek2 != null && peek1.photoIndex != peek2.photoIndex)
{
if (peek1.photoIndex != peek2.photoIndex)
{
AddTextEvent(DateTime.Now, $"图像等待", $"图像{peek1.photoIndex}-{peek2.photoIndex}未对齐", WarningEnum.Low, false);
if(peek1.photoIndex < peek2.photoIndex)
{
tScanPhotoInfo tout;
_matList1.TryDequeue(out tout);
AddTextEvent(DateTime.Now, $"图像等待", $"相机1-图像{tout.photoIndex}出队", WarningEnum.Low, false);
}
else
{
tScanPhotoInfo tout;
_matList2.TryDequeue(out tout);
AddTextEvent(DateTime.Now, $"图像等待", $"相机2-图像{tout.photoIndex}出队", WarningEnum.Low, false);
}
}
continue;
}
tScanPhotoInfo tscanPhotos0;
if (!_matList1.TryDequeue(out tscanPhotos0))
AddTextEvent(DateTime.Now, $"图像出队列", $"图像{tscanPhotos0.photoIndex},异常相机1队列出队失败", WarningEnum.High, true);
tScanPhotoInfo tscanPhotos1;
if (!_matList2.TryDequeue(out tscanPhotos1))
AddTextEvent(DateTime.Now, $"图像出队列", $"图像{tscanPhotos0.photoIndex},异常相机2队列出队失败", WarningEnum.High, true);
if (currKey > 0)
{
Records curRecord = Hashtable.Synchronized(htTask)[currKey] as Records;
ScanPhotoInfo scanPhotos0 = new ScanPhotoInfo(tscanPhotos0.devIndex, tscanPhotos0.photoIndex, tscanPhotos0.mat);
ScanPhotoInfo scanPhotos1 = new ScanPhotoInfo(tscanPhotos1.devIndex, tscanPhotos1.photoIndex, tscanPhotos1.mat);
Stopwatch stopWatch = new Stopwatch();
curRecord.ScannerPhotoCount++;
AddTextEvent(DateTime.Now, $"图像处理", $"图像{scanPhotos0.photoIndex}", WarningEnum.Low, false);
string time = "";
stopWatch.Start();
try
{
if (scanPhotos0.mat.Height != scanPhotos1.mat.Height)
{
int xw, xh;
AddTextEvent(DateTime.Now, $"警告", $"两相机采集图高度不一致({scanPhotos0.photoIndex}),dev1.Height={scanPhotos0.mat.Height},dev2.Height={scanPhotos1.mat.Height},重新resize...", WarningEnum.Low);
if (scanPhotos0.mat.Height > scanPhotos1.mat.Height)
scanPhotos1.mat = OpenCVUtil.resize(scanPhotos1.mat, scanPhotos0.mat.Width, scanPhotos0.mat.Height, out xw, out xh);
else
scanPhotos0.mat = OpenCVUtil.resize(scanPhotos0.mat, scanPhotos1.mat.Width, scanPhotos1.mat.Height, out xw, out xh);
}
//saveMatTest(scanPhotos0.mat, 1);
//saveMatTest(scanPhotos1.mat, 2);
//反转+相机索引调换
Mat mat0 = scanPhotos1.mat;
Mat mat1 = scanPhotos0.mat;
firstTest = false;
//记录resize大小
//var resize = new System.Drawing.Size(mat0.Width * 2, mat0.Height);
int resizeWidth = devContainer.libDefect.GetWidthForResize(mat0.Width + mat1.Width - Config.MiddleSuperposition);
if (resizeWidth == 0)
throw new Exception("GetWidthForResize result 0 失败!");
var resize = new System.Drawing.Size(resizeWidth, 4096);//固定8192*2张*4096
//裁边,两侧和中间重合部分
if (Config.MiddleSuperposition > 0)//中间重合部分
{
errStep = 3;
int width = mat0.Width - Config.MiddleSuperposition / 2;
mat0 = OpenCVUtil.cutImage(mat0, 0, 0, width, mat0.Height);
time += $"->图1去重({stopWatch.ElapsedMilliseconds})";
width = mat1.Width - Config.MiddleSuperposition / 2;
mat1 = OpenCVUtil.cutImage(mat1, Config.MiddleSuperposition / 2, 0, width, mat1.Height);
time += $"->图2去重({stopWatch.ElapsedMilliseconds})";
}
AddTextEvent(DateTime.Now, $"裁边",
$"(图像{scanPhotos0.photoIndex})-左图去重后:{mat0.Width}*{mat0.Height},右图:{mat1.Width}*{mat1.Height}," +
$"重复值:{Config.MiddleSuperposition},孔洞:{Config.MarginHoleWidth},cm2px:{Config.cm2px_x},resizeWidth:{resizeWidth}", WarningEnum.Low, false);
errStep = 4;
//水平合并l
Mat mat = OpenCVUtil.mergeImage_sameSize(new Mat[] { mat0, mat1 });//这里相机反装,左右反转下
//AddTextEvent(DateTime.Now, $"裁边", $"(图像{scanPhotos0.photoIndex})-边缘宽度:(左图)={marginWidth0},(右图)={marginWidth1}; 裁边去孔洞后:({mat0.Width}+{mat1.Width}={mat0.Width + mat1.Width});合并后(去孔洞):{mat.Width}*{mat.Height}", WarningEnum.Low, false);
AddTextEvent(DateTime.Now, $"裁边", $"(图像{scanPhotos0.photoIndex}); 裁边去孔洞后:({mat0.Width}+{mat1.Width}={mat0.Width + mat1.Width});合并后(去孔洞):{mat.Width}*{mat.Height}", WarningEnum.Normal, false);
float widthRatio = mat.Width * 1.0f / resize.Width;//宽度比例
time += $"->图1+2合并({stopWatch.ElapsedMilliseconds})";
errStep = 5;
//门幅更新(含两侧孔洞)x,y cm
float faceWidthX_cm = (float)Math.Round((scanPhotos0.photoIndex + 1) * mat.Height * 1.0f / Config.cm2px_y, 2);
float faceWidthY_cm = (float)Math.Round((mat.Width + Config.MarginHoleWidth * 2) * 1.0f / Config.cm2px_x + Config.EdgeOffset, 2);
faceWidthX_cm = (float)Math.Round(faceWidthX_cm, 2);
#if OnLine
faceWidthY_cm = (float)Math.Round(faceWidthY_cm, 2);
#else
Random rd = new Random();
faceWidthY_cm = (float)Math.Round(faceWidthY_cm + rd.NextDouble(), 2);
#endif
if (curRecord.FaceWidthMin == 0 || curRecord.FaceWidthMin > faceWidthY_cm)
curRecord.FaceWidthMin = faceWidthY_cm;
if (curRecord.FaceWidthMax < faceWidthY_cm)
curRecord.FaceWidthMax = faceWidthY_cm;
var point = new float[] { faceWidthX_cm, faceWidthY_cm };// new System.Drawing.PointF(faceWidthX_cm, faceWidthY_cm);
AddTextEvent(DateTime.Now, $"门幅", $"(图像{scanPhotos0.photoIndex})-({scanPhotos0.photoIndex})位置:{point[0]}; 幅宽:{point[1]}", WarningEnum.Normal, false);
curRecord.FacePointList.Add(point);
errStep = 6;
List mfdata = new List();
//显示100m内幅宽
double xStary = (point[0] / 100.0 - LineDataLen) > 0 ? (point[0] / 100.0 - LineDataLen) : 0;
mfdata = curRecord.FacePointList.Where(x => x[0] / 100.0 >= xStary).ToList();
double fmin = curRecord.FacePointList.Where(x => x[0] / 100.0 >= xStary).ToList().Select(x => x[1]).ToList().Min();
double fmax = curRecord.FacePointList.Where(x => x[0] / 100.0 >= xStary).ToList().Select(x => x[1]).ToList().Max();
//for (int i = 0; i < curRecord.FacePointList.Count; i++)
// mfdata.Add(curRecord.FacePointList[i]);
reDrawFaceWidth(mfdata,
new double[] { xStary * 100, Math.Round(point[0] + 0.005f, 2) },
new double[] { Math.Round(fmin - 0.005f,2), Math.Round(fmax + 0.005f, 2) });
errStep = 7;
time += $"->门幅刷新({stopWatch.ElapsedMilliseconds})";
//去除两侧孔洞(门幅计算时不能去除)
//if (Config.MarginHoleWidth > 0)
// mat = OpenCVUtil.cutImage(mat, Config.MarginHoleWidth, 0, mat.Width - Config.MarginHoleWidth * 2, mat.Height);
//计算速度
//
//
curRecord.TimeLen = pStopWatch.ElapsedMilliseconds / 1000.0d / 60.0d;//总时间 分
#if JM
#else
double lenMi = Math.Round(faceWidthX_cm / 100, 2);
double stLen = lenMi;
curRecord.Len = lenMi;
#endif
this.BeginInvoke(new System.Action(() =>
{
#if JM
#else
this.lblLen.Text = $"{stLen}米";
this.lblSpeed.Text = $"速度:{Math.Round(lenMi / curRecord.TimeLen, 2)}米/分";
#endif
this.uilbKF.Text = $"当前幅宽:{faceWidthY_cm}cm";
}));
#if JM
#endif
//
errStep = 9;
time += $"->速度刷新({stopWatch.ElapsedMilliseconds})";
//----缺陷队列
int oxw, oxh;
#if OnLine
mat = OpenCVUtil.resize(mat, resize.Width, resize.Height, out oxw, out oxh);
#else
mat = new Mat("C:\\Users\\fang\\Desktop\\新建文件夹\\华锋\\126.bmp");
mat = OpenCVUtil.resize(mat, resize.Width, resize.Height, out oxw, out oxh);
#endif
AddTextEvent(DateTime.Now, $"图像处理", $"(图像{scanPhotos0.photoIndex})-合成图resize后:{mat.Width}*{mat.Height}-{oxw}*{oxh}", WarningEnum.Normal, false);
devContainer.libDefect.add(new Device.DefectLib.DefectTask()
{
modelName = curRecord.ProductInfo.ModelName,
record = curRecord,
bmp = mat,
bmpTag = mat.Clone(),
photoIndex = scanPhotos0.photoIndex,//0-n 首张必需为0,因下面计算长度是从0开始
widthRatio = widthRatio,
qualifiedLimitList = curRecord.ProductInfo.QualifiedLimitList,
finishEvent = callBackDefectEvent,
xw = oxw,
CurrDis = tscanPhotos1.CurrDis,
});
errStep = 10;
time += $"->加入瑕疵待检队列({stopWatch.ElapsedMilliseconds})";
}
catch (Exception ex)
{
curRecord.ScannerPhotoFinishCount++;//失败时不能因数量不一致无法保存
AddTextEvent(DateTime.Now, $"图像处理", $"异常({errStep}):(图像{scanPhotos0.photoIndex})-{ex.Message}", WarningEnum.High);
string dirPath = FileUtil.initFolder($"{Config.ImagePath}{curRecord.BatchId}_{curRecord.ReelId}\\Err\\");
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(scanPhotos0.mat).Save($"{dirPath}{scanPhotos0.photoIndex}_0_Step{errStep}.bmp", ImageFormat.Bmp);
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(scanPhotos1.mat).Save($"{dirPath}{scanPhotos1.photoIndex}_1_Step{errStep}.bmp", ImageFormat.Bmp);
}
finally
{
AddTextEvent(DateTime.Now, $"图像处理", $"(图像{scanPhotos0.photoIndex})-进度计时:{time}", WarningEnum.Normal, false);
scanPhotos0.mat.Dispose();
scanPhotos1.mat.Dispose();
scanPhotos0 = scanPhotos1 = null;
//System.GC.Collect();
}
}
}
Thread.Sleep(20);
} while (!isBreakProcessRun());
//暂停中断
//stopWatch.Stop();
pStopWatch.Stop();
//_isRuning = false;
}
#if Online_One
else
{
//防止内存爆满
AcquisitionMat tacq1 = _LinecamDev1.GetFrames(10);
AcquisitionMat tacq2 = _LinecamDev2.GetFrames(10);
}
#endif
Thread.Sleep(20);
}
catch (Exception e)
{
//_isRuning = false;
AddTextEvent(DateTime.Now, $"运行报警", $"流程运行出错{errStep}:" + e.Message , WarningEnum.High, true);
//Log("运行报警", $"流程运行出错{errStep}:" + e.Message + "\n", WarningEnum.High);
}
}
}
#endregion
///
/// 处理完成回调
///
///
private void callBackDefectEvent(Device.DefectLib.DefectTask res)
{
{
int step = 0;
try
{
AddTextEvent(DateTime.Now,$"检测完成", $"图像队列:{res.record.ScannerPhotoFinishCount+1}/{res.record.ScannerPhotoCount} (图像{res.photoIndex})检测结果:{res.isSucceed}", WarningEnum.Normal, false);
string dirPath = FileUtil.initFolder($"{Config.ImagePath}{res.record.BatchId}_{res.record.ReelId}_{StartTime}\\");
string dirSourcePath = FileUtil.initFolder($"{Config.ImagePath}{res.record.BatchId}_{res.record.ReelId}_{StartTime}\\源图\\");
try
{
if (Config.IsSaveAllImage)//保存所有原图
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmp).Save($"{dirSourcePath}{res.photoIndex}.bmp", ImageFormat.Bmp);
}
catch (Exception e)
{
AddTextEvent(DateTime.Now, $"存图失败1", $"检测硬盘容量或者保存路径:{dirSourcePath}{res.photoIndex}.bmp!{e.Message}", WarningEnum.Low, true);
}
if (res.isSucceed)
{
step = 1;
AddTextEvent(DateTime.Now,$"检测完成", $"(图像{res.photoIndex})-瑕疵检测完成,共{res.excelTable.Rows.Count}个瑕疵!各环节用时:{string.Join(",",res.stopwatch)}", WarningEnum.Normal, false);
//AddTextEvent(DateTime.Now,$"打标完成", $"第 ({res.photoIndex}) 张照片,计算过程:{res.resultInfo}");
//if (!Config.IsSaveAllImage && Config.IsSaveDefectSourceImage)
// OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmp).Save($"{dirSourcePath}{res.photoIndex}.bmp", ImageFormat.Bmp);
step = 2;
if (res.excelTable.Rows.Count > 0)
{
res.record.dicPhoto_Defect[res.photoIndex] = true;//改为此图有瑕疵
//有瑕疵打标图必需保存 Jpeg
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmpTag).Save($"{dirPath}{res.photoIndex}_tag.jpg", ImageFormat.Jpeg);
if (!Config.IsSaveAllImage && Config.IsSaveDefectSourceImage)
OpenCvSharp.Extensions.BitmapConverter.ToBitmap(res.bmp).Save($"{dirSourcePath}{res.photoIndex}.bmp", ImageFormat.Bmp);
step = 3;
res.record.DefectTotalCount += res.excelTable.Rows.Count;
if (res.record.DefectInfoList == null)
res.record.DefectInfoList = new List();
step = 4;
JObject defectNameInfo;
DefectInfo defectInfo=null;
List