M182轴承端盖外观缺陷AOI
kingno
2025-09-18 1894ccc4014d813ea60cf276ac066e5870a82566
src/Bro.M141_AOI1.Process/AOI1Process.cs
@@ -76,10 +76,6 @@
                    Plc1.WriteSingleAddress(ConfigAOI1.Plcover, 1, out _);
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"产品计时结束   plc料号 {pronum} 结果{pronum + (result[0] ? 1 : 2)},{pronum + (result[1] ? 1 : 2)},{pronum + (result[2] ? 1 : 2)}   总用时 {(DateTime.Now - dicplctime[pronum / 10]).TotalMilliseconds}ms  ");
                    Thread.Sleep(100);
                    Plc1.WriteSingleAddress(ConfigAOI1.Finresult1, 0, out _);
                    Plc1.WriteSingleAddress(ConfigAOI1.Finresult2, 0, out _);
                    Plc1.WriteSingleAddress(ConfigAOI1.Finresult3, 0, out _);
                  
                }
                else
@@ -91,586 +87,13 @@
                    Plc1.WriteSingleAddress(ConfigAOI1.Plcover, 1, out _);
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"产品计时结束   plc料号 {pronum} 结果{pronum + 2},{pronum + 2},{pronum + 2}  总用时 {(DateTime.Now - dicplctime[pronum/10]).TotalMilliseconds}ms  ");
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"测试结果数量异常!!!!!!!!");
                    Thread.Sleep(100);
                    Plc1.WriteSingleAddress(ConfigAOI1.Finresult1, 0, out _);
                    Plc1.WriteSingleAddress(ConfigAOI1.Finresult2, 0, out _);
                    Plc1.WriteSingleAddress(ConfigAOI1.Finresult3, 0, out _);
                }
            }
            catch
            {
            }
            //else if (Postion.Contains("2"))
            //{
            //    Plc1.WriteSingleAddress(ConfigAOI1.P2over, result, out _);
            //}
            //else if (Postion.Contains("3"))
            //{
            //    Plc1.WriteSingleAddress(ConfigAOI1.P3over, result, out _);
            //}
            //else
            //{
            //    Plc1.WriteSingleAddress(ConfigAOI1.Finresult, result, out _);
            //}
        }
        //[ProcessMethod("", "PositionCheck_P1_F", "工位1检测飞拍", InvokeType.TestInvoke)]
        //public ResponseMessage PositionCheck_P1_F(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        //{
        //    if (config.TriggerValue == 1)
        //    {
        //        Productcode = DateTime.Now.ToString("yyyyMMddHHmmss");
        //    }
        //    config.TriggerStr = "1#,1,Str," + Productcode;
        //    ResponseMessage msg = new ResponseMessage();
        //    msg.Result = 1;
        //    msg.IsReply = false;
        //    List<MeasureBind> measureBinds = new List<MeasureBind>();
        //    string inputSequence = "";
        //    try
        //    {
        //        RunImageCheckPreTreat_plc(config, out measureBinds, out inputSequence);
        //    }
        //    catch (Exception ex)
        //    {
        //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"检测预处理异常,{ex.GetExceptionMessage()}");
        //        msg.Result = -1;
        //        msg.Message = ex.Message;
        //        msg.IsReply = false;
        //        return msg;
        //    }
        //    int measureNums = measureBinds.Count;
        //    bool WaitSignalstate = true;
        //    //_isLastCheckFlag = false;
        //    measureBinds.GroupBy(u => u.CameraId).AsParallel().ForAll(c =>
        //    {
        //        var camera = DeviceCollection.FirstOrDefault(u => u.Id == c.Key) as CameraBase;
        //        foreach (var i in c.ToList().OrderBy(u => u.ImageIndex).ToList())
        //        {
        //            string index = (i.ImageIndex + 1).ToString();
        //            if (ConfigAOI1.P1SFstate)
        //            {
        //                if (!WaitSignal(index))
        //                {
        //                    WaitSignalstate = false;
        //                    break;
        //                }
        //            }
        //            var products = i.ProductIndices.Select(pi =>
        //            {
        //                return FindProductBySequence($"{inputSequence}_{pi}", true);
        //            }).ToList();
        //            IImageSet imgSet = null;
        //            try
        //            {
        //                imgSet = CollectHImage(camera, i.SnapshotOpConfig);
        //                lock (trigerStr)
        //                {
        //                    trigerStr.Add(index + "over");
        //                }
        //                if (imgSet == null)
        //                {
        //                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位1{camera?.Name}取像{i?.ImageIndex}失败   图像为空");
        //                    break;
        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位1{camera?.Name}取像{i?.ImageIndex}异常,{ex.ToString()}");
        //                break;
        //            }
        //            RunImageCheckAsync(products, config.TriggerStr, config.TriggerSource, imgSet, i).ContinueWith(t =>
        //            {
        //                Interlocked.Decrement(ref measureNums);
        //            });
        //        }
        //    });
        //    if (!WaitSignalstate)
        //    {
        //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"图片采集信息不全");
        //        msg.IsReply = false;
        //        return msg;
        //    }
        //    while (measureNums > 0)
        //    {
        //        Thread.Sleep(50);
        //    }
        //    //检测完成后清理图片缓存
        //    measureBinds.Select(u => u.CameraId).Distinct().ToList().ForEach(u =>
        //    {
        //        var camera = DeviceCollection.FirstOrDefault(c => c.Id == u) as CameraBase;
        //        camera.ClearImageBufferQueue();
        //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{camera.Name}相机工位1检测后清理缓存");
        //    });
        //    string positionName = measureBinds[0].WorkPosition;
        //    var pList = CheckPositionDoneAsync_plc(positionName, inputSequence, config, new List<string>()).GetAwaiter().GetResult();
        //    return msg;
        //}
        //[ProcessMethod("", "PositionCheck_P1_photo", "工位1拍照", InvokeType.TestInvoke)]
        //public ResponseMessage PositionCheck_P1_photo(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        //{
        //    ResponseMessage msg = new ResponseMessage();
        //    msg.IsReply = false;
        //    int index = config.TriggerValue;
        //    if (index == 1)
        //    {
        //        trigerStr.Clear();
        //    }
        //    lock (trigerStr)
        //    {
        //        trigerStr.Add(index.ToString());
        //    }
        //    WaitSignal(index + "over");
        //    ReplyPlcData("P1", index);
        //    return msg;
        //}
        //[ProcessMethod("", "PositionCheck_P2_F", "工位2检测飞拍", InvokeType.TestInvoke)]
        //public ResponseMessage PositionCheck_P2_F(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        //{
        //    config.TriggerStr = "2#,1,Str," + Productcode;
        //    ResponseMessage msg = new ResponseMessage();
        //    msg.Result = 1;
        //    msg.IsReply = false;
        //    List<MeasureBind> measureBinds = new List<MeasureBind>();
        //    string inputSequence = "";
        //    try
        //    {
        //        RunImageCheckPreTreat_plc(config, out measureBinds, out inputSequence);
        //    }
        //    catch (Exception ex)
        //    {
        //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"检测预处理异常,{ex.GetExceptionMessage()}");
        //        msg.Result = -1;
        //        msg.Message = ex.Message;
        //        msg.IsReply = false;
        //        return msg;
        //    }
        //    int measureNums = measureBinds.Count;
        //    bool WaitSignalstate = true;
        //    //_isLastCheckFlag = false;
        //    measureBinds.GroupBy(u => u.CameraId).AsParallel().ForAll(c =>
        //    {
        //        var camera = DeviceCollection.FirstOrDefault(u => u.Id == c.Key) as CameraBase;
        //        foreach (var i in c.ToList().OrderBy(u => u.ImageIndex).ToList())
        //        {
        //            string index = (i.ImageIndex + 1).ToString();
        //            if (ConfigAOI1.P2SFstate)
        //            {
        //                if (!WaitSignal(index, "p2"))
        //                {
        //                    WaitSignalstate = false;
        //                    break;
        //                }
        //            }
        //            var products = i.ProductIndices.Select(pi =>
        //            {
        //                return FindProductBySequence($"{inputSequence}_{pi}", true);
        //            }).ToList();
        //            IImageSet imgSet = null;
        //            try
        //            {
        //                imgSet = CollectHImage(camera, i.SnapshotOpConfig);
        //                lock (trigerStr2)
        //                {
        //                    trigerStr2.Add(index + "over");
        //                }
        //                if (imgSet == null)
        //                {
        //                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位2{camera?.Name}取像{i?.ImageIndex}失败   图像为空");
        //                    break;
        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位2{camera?.Name}取像{i?.ImageIndex}异常,{ex.ToString()}");
        //                break;
        //            }
        //            RunImageCheckAsync(products, config.TriggerStr, config.TriggerSource, imgSet, i).ContinueWith(t =>
        //            {
        //                Interlocked.Decrement(ref measureNums);
        //            });
        //        }
        //    });
        //    if (!WaitSignalstate)
        //    {
        //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"图片采集信息不全");
        //        msg.IsReply = false;
        //        return msg;
        //    }
        //    while (measureNums > 0)
        //    {
        //        Thread.Sleep(50);
        //    }
        //    //检测完成后清理图片缓存
        //    measureBinds.Select(u => u.CameraId).Distinct().ToList().ForEach(u =>
        //    {
        //        var camera = DeviceCollection.FirstOrDefault(c => c.Id == u) as CameraBase;
        //        camera.ClearImageBufferQueue();
        //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{camera.Name}相机工位2检测后清理缓存");
        //    });
        //    string positionName = measureBinds[0].WorkPosition;
        //    var pList = CheckPositionDoneAsync_plc(positionName, inputSequence, config, new List<string>()).GetAwaiter().GetResult();
        //    return msg;
        //}
        //[ProcessMethod("", "PositionCheck_P2_photo", "工位2拍照", InvokeType.TestInvoke)]
        //public ResponseMessage PositionCheck_P2_photo(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        //{
        //    ResponseMessage msg = new ResponseMessage();
        //    msg.IsReply = false;
        //    int index = config.TriggerValue;
        //    if (index == 1)
        //    {
        //        trigerStr2.Clear();
        //    }
        //    lock (trigerStr2)
        //    {
        //        trigerStr2.Add(index.ToString());
        //    }
        //    WaitSignal(index + "over", "p2");
        //    ReplyPlcData("P2", index);
        //    return msg;
        //}
        //[ProcessMethod("", "PositionCheck_P3_F", "工位3检测飞拍", InvokeType.TestInvoke)]
        //public ResponseMessage PositionCheck_P3_F(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        //{
        //    config.TriggerStr = "3#,1,Str," + Productcode;
        //    ResponseMessage msg = new ResponseMessage();
        //    msg.Result = 1;
        //    msg.IsReply = false;
        //    List<MeasureBind> measureBinds = new List<MeasureBind>();
        //    string inputSequence = "";
        //    try
        //    {
        //        RunImageCheckPreTreat_plc(config, out measureBinds, out inputSequence);
        //    }
        //    catch (Exception ex)
        //    {
        //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"检测预处理异常,{ex.GetExceptionMessage()}");
        //        msg.Result = -1;
        //        msg.Message = ex.Message;
        //        msg.IsReply = false;
        //        return msg;
        //    }
        //    int measureNums = measureBinds.Count;
        //    bool WaitSignalstate = true;
        //    //_isLastCheckFlag = false;
        //    measureBinds.GroupBy(u => u.CameraId).AsParallel().ForAll(c =>
        //    {
        //        var camera = DeviceCollection.FirstOrDefault(u => u.Id == c.Key) as CameraBase;
        //        foreach (var i in c.ToList().OrderBy(u => u.ImageIndex).ToList())
        //        {
        //            string index = (i.ImageIndex + 1).ToString();
        //            if (ConfigAOI1.P3SFstate)
        //            {
        //                if (!WaitSignal(index, "p3"))
        //                {
        //                    WaitSignalstate = false;
        //                    break;
        //                }
        //            }
        //            var products = i.ProductIndices.Select(pi =>
        //            {
        //                return FindProductBySequence($"{inputSequence}_{pi}", true);
        //            }).ToList();
        //            IImageSet imgSet = null;
        //            try
        //            {
        //                imgSet = CollectHImage(camera, i.SnapshotOpConfig);
        //                lock (trigerStr3)
        //                {
        //                    trigerStr3.Add(index + "over");
        //                }
        //                if (imgSet == null)
        //                {
        //                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位1{camera?.Name}取像{i?.ImageIndex}失败   图像为空");
        //                    break;
        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位1{camera?.Name}取像{i?.ImageIndex}异常,{ex.ToString()}");
        //                break;
        //            }
        //            RunImageCheckAsync(products, config.TriggerStr, config.TriggerSource, imgSet, i).ContinueWith(t =>
        //            {
        //                Interlocked.Decrement(ref measureNums);
        //            });
        //        }
        //    });
        //    if (!WaitSignalstate)
        //    {
        //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"图片采集信息不全");
        //        msg.IsReply = false;
        //        return msg;
        //    }
        //    while (measureNums > 0)
        //    {
        //        Thread.Sleep(50);
        //    }
        //    //检测完成后清理图片缓存
        //    measureBinds.Select(u => u.CameraId).Distinct().ToList().ForEach(u =>
        //    {
        //        var camera = DeviceCollection.FirstOrDefault(c => c.Id == u) as CameraBase;
        //        camera.ClearImageBufferQueue();
        //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{camera.Name}相机工位1检测后清理缓存");
        //    });
        //    string positionName = measureBinds[0].WorkPosition;
        //    var pList = CheckPositionDoneAsync_plc(positionName, inputSequence, config, new List<string>()).GetAwaiter().GetResult();
        //    return msg;
        //}
        //[ProcessMethod("", "PositionCheck_P3_photo", "工位3拍照", InvokeType.TestInvoke)]
        //public ResponseMessage PositionCheck_P3_photo(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        //{
        //    ResponseMessage msg = new ResponseMessage();
        //    msg.IsReply = false;
        //    int index = config.TriggerValue;
        //    if (index == 1)
        //    {
        //        trigerStr3.Clear();
        //    }
        //    lock (trigerStr3)
        //    {
        //        trigerStr3.Add(index.ToString());
        //    }
        //    WaitSignal(index + "over", "p3");
        //    ReplyPlcData("P3", index);
        //    return msg;
        //}
        //public bool WaitSignal(string index, string p = "p1")
        //{
        //    DateTime dt = DateTime.Now;
        //    while ((DateTime.Now - dt).TotalMilliseconds < ConfigAOI1.PhotolayoutTime)
        //    {
        //        if (p.Equals("p1"))
        //        {
        //            lock (trigerStr)
        //            {
        //                if (trigerStr.Contains(index))
        //                {
        //                    return true;
        //                }
        //            }
        //        }
        //        else if (p == "p2")
        //        {
        //            lock (trigerStr2)
        //            {
        //                if (trigerStr2.Contains(index))
        //                {
        //                    return true;
        //                }
        //            }
        //        }
        //        else if (p == "p3")
        //        {
        //            lock (trigerStr3)
        //            {
        //                if (trigerStr3.Contains(index))
        //                {
        //                    return true;
        //                }
        //            }
        //        }
        //        Thread.Sleep(50);
        //    }
        //    return false;
        //}
        //List<string> trigerStr = new List<string>();
        //List<string> trigerStr2 = new List<string>();
        //List<string> trigerStr3 = new List<string>();
        //[ProcessMethod("", "PositionCheck_P2", "工位2检测", InvokeType.TestInvoke)]
        //public ResponseMessage PositionCheck_P2(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        //{
        //    config.TriggerStr = "2#," + config.TriggerValue + ",Str," + Productcode;
        //    ResponseMessage msg = RunImageCheck_plc(config);
        //    msg.IsReply = false;
        //    if (msg.Result != 1)
        //    {
        //        ReplyPlcData("P2", config.TriggerValue);
        //    }
        //    return msg;
        //}
        //[ProcessMethod("", "PositionCheck_P3", "工位3检测", InvokeType.TestInvoke)]
        //public ResponseMessage PositionCheck_P3(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        //{
        //    config.TriggerStr = "3#," + config.TriggerValue + ",Str," + Productcode;
        //    ResponseMessage msg = RunImageCheck_plc(config);
        //    msg.IsReply = false;
        //    if (msg.Result != 1)
        //    {
        //        ReplyPlcData("P3", config.TriggerValue);
        //    }
        //    return msg;
        //}
        //[ProcessMethod("", "PositionCheck_Fin", "结果反馈plc", InvokeType.TestInvoke)]
        //public ResponseMessage PositionCheck_Fin(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        //{
        //    config.TriggerStr = "#,UpData," + Productcode;
        //    ResponseMessage msg = new ResponseMessage();
        //    var triggerDatas = config.TriggerStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        //    string inputSequence = triggerDatas[triggerDatas.Length - 1];
        //    var productList = new List<int>() { 1 }.Select(u =>
        //    {
        //        string sequence = $"{inputSequence}_{u}";
        //        return FindProductBySequence(sequence, true);
        //    }).ToList();
        //    int waitInterval = 300;
        //    int repeatTime = /*M141Config.DetectTimeout / waitInterval+*/2;
        //    do
        //    {
        //        if (productList.All(p => !string.IsNullOrWhiteSpace(p.Result)))
        //        {
        //            break;
        //        }
        //        else
        //        {
        //            Thread.Sleep(waitInterval);
        //            repeatTime--;
        //        }
        //        if (repeatTime < 0)
        //        {
        //            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{string.Join(",", productList.Select(u => u.PID))}检测获取汇总结果超时,使用当前结果");
        //            break;
        //        }
        //    } while (true);
        //    var Defaultclass = M141Config.DefectClassCollection.FirstOrDefault(u => u.IsDefault);
        //    productList.ForEach(p =>
        //    {
        //        if (Defaultclass == null)
        //        {
        //            if (string.IsNullOrEmpty(p.Result))
        //            {
        //                p.Result = "NG";
        //            }
        //        }
        //        else
        //        {
        //            if (string.IsNullOrEmpty(p.Result))
        //            {
        //                p.Result = Defaultclass.ClassName;
        //            }
        //        }
        //        var specList = p.Details.SelectMany(u => u.ResultList.SelectMany(r => r.Specs)).ToList();
        //        p.Details.SelectMany(u => u.SpecList).ToList().ForEach(s =>
        //        {
        //            if (!specList.Any(u => u.Code == s.Code))
        //            {
        //                specList.Add(s);
        //            }
        //        });
        //        List<Pointdata> Pointdatas11 = new List<Pointdata>();
        //        foreach (var item in specList)
        //        {
        //            try
        //            {
        //                Pointdatas11.Add(new Pointdata()
        //                {
        //                    Code = item.Code,
        //                    Value = Convert.ToDouble(item.GetMeasureValueStr()),
        //                    Stand = item.StandardValue,
        //                    Min = item.StandardValue - item.Tolrenance_Negative,
        //                    Max = item.StandardValue + item.Tolrenance_Positive,
        //                    ispoint = false,
        //                });
        //            }
        //            catch
        //            {
        //            }
        //        }
        //        M141Codelist.Add(Pointdatas11);
        //        ReplyPlcData("Fin", p.Result.ToUpper().Contains("NG") ? 2 : 1);
        //    });
        //    return msg;
        //}