From caf4dd3c752f5982adcd3708d2e8f976f81ff49f Mon Sep 17 00:00:00 2001
From: kingno <30263@KINGNO>
Date: 星期一, 23 六月 2025 09:59:57 +0800
Subject: [PATCH] Merge branch 'master' of http://gitblit.broconcentric.com:8088/r/M200

---
 src/Bro.M141_AOI1.Process/AOI1Process.cs |  185 ++++++++++++++++++++++++++++++++++-----------
 1 files changed, 138 insertions(+), 47 deletions(-)

diff --git a/src/Bro.M141_AOI1.Process/AOI1Process.cs b/src/Bro.M141_AOI1.Process/AOI1Process.cs
index 139b24d..9b8a134 100644
--- a/src/Bro.M141_AOI1.Process/AOI1Process.cs
+++ b/src/Bro.M141_AOI1.Process/AOI1Process.cs
@@ -6,12 +6,14 @@
 using Bro.M141.Process;
 using Bro.UI.Model.Winform;
 using HalconDotNet;
+using Newtonsoft.Json;
 using NPOI.POIFS.Crypt.Dsig;
 using NPOI.SS.Formula.Functions;
 using NPOI.XSSF.Streaming.Values;
 using System.Collections.Concurrent;
 using System.Net.Sockets;
 using System.Text.RegularExpressions;
+using static Bro.Common.Helper.EnumHelper;
 using static NPOI.HSSF.Util.HSSFColor;
 using static Org.BouncyCastle.Crypto.Engines.SM2Engine;
 using static Org.BouncyCastle.Math.EC.ECCurve;
@@ -28,19 +30,28 @@
         #endregion
 
         AOI1Config ConfigAOI1 => Config as AOI1Config;
-        TcpClientWrapBase BarcodeScanner = null;
-
 
         public override void Open()
         {
+
+            //string configPath = @"C:\Users\30263\Desktop\666.txt";
+
+            //string _configBackupStr = "";
+            //using (StreamReader reader = new StreamReader(configPath, System.Text.Encoding.UTF8))
+            //{
+            //    _configBackupStr = reader.ReadToEnd();
+
+            //}
+
+            //ProductModel p = JsonConvert.DeserializeObject<ProductModel>(_configBackupStr, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });
+
             base.Open();
-            BarcodeScanner = DeviceCollection.FirstOrDefault(u => u is TcpClientWrapBase) as TcpClientWrapBase;
         }
+
 
         [ProcessMethod("", "PositionCheck_P1", "宸ヤ綅1妫�娴�", InvokeType.TestInvoke)]
         public ResponseMessage PositionCheck_P1(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
         {
-
             var positionSet = M141Config.WorkPositionCollection.Where(u => u.IsEnabled).FirstOrDefault(u => u.TriggerValue == "1#");
             if (positionSet == null)
             {
@@ -54,6 +65,7 @@
 
             return msg;
         }
+
 
         [ProcessMethod("", "PositionCheck_P2", "宸ヤ綅2妫�娴�", InvokeType.TestInvoke)]
         public ResponseMessage PositionCheck_P2(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
@@ -71,6 +83,7 @@
 
             return msg;
         }
+
 
         [ProcessMethod("", "PositionCheck_P3", "宸ヤ綅3妫�娴�", InvokeType.TestInvoke)]
         public ResponseMessage PositionCheck_P3(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
@@ -90,78 +103,143 @@
         }
 
 
-
         [ProcessMethod("ImageCheck", "ReadBarcode", "璇荤爜", InvokeType.TestInvoke)]
         public ResponseMessage ReadBarcode(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
         {
             ResponseMessage msg = new ResponseMessage();
             if (config is IImageCheckOperationConfig opConfig)
             {
-                var results = opConfig.Products.Select(u =>
+                if (invokeDevice is TcpClientWrapBase BarcodeScanner)
                 {
-                    DetectResult result = new DetectResult();
-                    result.PID = u.PID;
-                    result.Specs = GetSpecListFromConfigSelection(opConfig.SpecCollection);
-                    return result;
-                }).ToList();
+                    var results = opConfig.Products.Select(u =>
+                    {
+                        DetectResult result = new DetectResult();
+                        result.PID = u.PID;
+                        result.Specs = GetSpecListFromConfigSelection(opConfig.SpecCollection);
+                        return result;
+                    }).ToList();
 
-                msg.DataObj = results;
+                    msg.DataObj = results;
 
-                string barcode = BarcodeScannerCommunicate();
+                    string barcode = BarcodeScannerCommunicate(BarcodeScanner);
 
-                List<double> ret = new List<double>();
-                if ("NOREAD".Equals(barcode.ToUpper()))
-                {
-                    ret.Add(999);
+                    List<double> ret = new List<double>();
+                    if ("NOREAD".Equals(barcode.ToUpper()))
+                    {
+                        ret.Add(999);
+                    }
+                    else
+                    {
+                        if (barcode.Split('-').Length > 5)
+                        {
+                            int mh = Plc1.Read(4000, 1, out _)[0];
+                            string codestr = barcode.Split('-')[4];
+                            if (codestr == "L")
+                            {
+                                codestr = "10";
+                            }
+
+                            if (codestr.Equals(mh.ToString()))
+                            {
+                                ret.Add(1);
+                            }
+                            else
+                            {
+                                ret.Add(2);
+                                Plc1.WriteSingleAddress(4010, 1, out _);//缁檖lc鎶ヨ
+                            }
+                        }
+                        else
+                        {
+                            ret.Add(999);
+                        }
+                    }
+
+                    FillSpecResults(results[0].PID, results[0].Specs, ret, opConfig.Products[0].SEQUENCE);
+
+
+                    opConfig.Products[0].SN = barcode;
+                    opConfig.Products[0].PID = barcode + "_1";
+                    opConfig.Products[0].Details.ForEach(u =>
+                    {
+                        u.SN = barcode;
+                        u.PID = barcode + "_1";
+                        u.ResultList.ForEach(x =>
+                    {
+
+                        x.PID = barcode + "_1";
+
+                    });
+                    });
+
+                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"浜у搧{opConfig.Products[0].PID}鏉$爜鑾峰彇涓簕barcode}");
                 }
-                else
-                {
-                    ret.Add(1);
-                }
-
-                FillSpecResults(results[0].PID, results[0].Specs, ret, opConfig.Products[0].SEQUENCE);
-
-
-                opConfig.Products[0].SN = barcode;
-                opConfig.Products[0].Details.ForEach(u => u.SN = barcode);
-
-                LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"浜у搧{opConfig.Products[0].PID}鏉$爜鑾峰彇涓簕barcode}");
-
             }
             return msg;
         }
 
 
+        [ProcessMethod("ImageCheck", "ReadBarcode2", "璇绘爮鍏风爜", InvokeType.TestInvoke)]
+        public ResponseMessage ReadBarcode2(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
+        {
+            ResponseMessage msg = new ResponseMessage();
 
-        private string BarcodeScannerCommunicate()
+            if (invokeDevice is TcpClientWrapBase BarcodeScanner2)
+            {
+                string barcode = BarcodeScannerCommunicate(BarcodeScanner2);
+                if (string.IsNullOrEmpty(barcode) || "noread".Equals(barcode.ToLower()))
+                {
+                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏍忓叿鐮佽幏鍙栧け璐�");
+                    Plc1.WriteSingleAddress(1524, 2, out _);
+                    Plc1.WriteSingleAddress(1514, 1, out _);
+                }
+                else
+                {
+                    Plc1.WriteSingleAddress(1524, 1, out _);
+                    Plc1.WriteSingleAddress(1514, 1, out _);
+                    ConfigAOI1.basketcode = barcode;
+                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"鑾峰彇鍒版爮鍏风爜涓簕barcode}");
+
+                    mysqlhelper.NewBasketcode(barcode);
+                    RerefreshBasketcode();
+
+                }
+            }
+
+            return msg;
+        }
+
+
+        private string BarcodeScannerCommunicate(TcpClientWrapBase client)
         {
             string barcode = "";
 
             for (int i = 0; i < 3; i++)
             {
-                if (BarcodeScanner.WriteAndRead("start", out string error, out barcode, true))
+                if (client.WriteAndRead("start", out string error, out barcode, true))
                 {
                     barcode = barcode.Trim(' ', '\r', '\n');
                     if (CheckBarcodeValid(barcode))
                     {
-                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{BarcodeScanner.Name}鎵爜瀹屾垚锛屽弽棣坽barcode}");
+                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{client.Name}鎵爜瀹屾垚锛屽弽棣坽barcode}");
                         return barcode;
                     }
                     else
                     {
-                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{BarcodeScanner.Name}绗瑊i + 1}鎵爜瀹屾垚锛屽弽棣坽barcode}锛屼笉鏄悎娉曟潯鐮�");
+                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{client.Name}绗瑊i + 1}鎵爜瀹屾垚锛屽弽棣坽barcode}锛屼笉鏄悎娉曟潯鐮�");
                         Thread.Sleep(200);
                     }
                 }
                 else
                 {
-                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"{BarcodeScanner.Name}鎵爜澶辫触锛寋error}");
+                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"{client.Name}鎵爜澶辫触锛寋error}");
                     break;
                 }
             }
 
             return "NOREAD";
         }
+
 
         private bool CheckBarcodeValid(string barcode)
         {
@@ -172,10 +250,6 @@
             }
             return isBarcodeValid;
         }
-
-
-
-
 
 
         [ProcessMethod("ImageCheck", "CheckLineProfile", "妫�娴嬩骇鍝佺嚎杞粨搴�", InvokeType.TestInvoke)]
@@ -321,16 +395,33 @@
         }
 
 
+        [ProcessMethod("ImageCheck", "GetMatrix", "鑾峰彇鐭╅樀", InvokeType.TestInvoke)]
+        public ResponseMessage GetMatrix(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
+        {
+            ResponseMessage msg = new ResponseMessage();
+            if (config is IImageCheckOperationConfig opConfig)
+            {
+                var tool = GetHalconTool(null, "", opConfig.AlgorithemPath);
+                var ret = tool.RunProcedure(null, new Dictionary<string, HalconDotNet.HObject>() { { "INPUT_Image", opConfig.ImageSet.HImage } }, new List<string>() { "OUTPUT_Results" }, null);
 
 
-
-
-
-
-
-
-
-
+                if (ret == null)
+                {
+                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鑴氭湰{opConfig.AlgorithemPath}杩愯寮傚父锛岃繑鍥炲�间负null");
+                }
+                else if (!ret.Item1)
+                {
+                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鑴氭湰{opConfig.AlgorithemPath}杩愯寮傚父锛寋ret.Item4}");
+                }
+                else
+                {
+                    List<double> datas = ret.Item2["OUTPUT_Results"].HTupleToDouble();
+                    opConfig.Products[0].Centermatrix = datas;
+                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"浜у搧{opConfig.Products[0].PID}鐭╅樀鑾峰彇涓� {string.Join(',', datas)}");
+                }
+            }
+            return msg;
+        }
 
 
     }

--
Gitblit v1.8.0