quanzhou
2025-10-21 745d1ce73a3b27d2a3507843c8060755a0c95765
打印机条码格式修改,增添一个点检功能方法
7个文件已修改
307 ■■■■ 已修改文件
src/Bro.M141.Process/M141Config.cs 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141.Process/M141Models.cs 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141.Process/M141Process.cs 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141.Process/M141Process_ImageCheck.cs 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141.Process/MyMQTT.cs 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141.Process/UI/UIPrinter.cs 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141_AOI1.Process/AOI1Process.cs 219 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141.Process/M141Config.cs
@@ -66,6 +66,14 @@
        public List<MeasureBind> MeasureBindCollection { get; set; } = new List<MeasureBind>();
        [Category("点检配置")]
        [Description("点检配置集合,配置检测的工位,图片索引,相机和检测调用项关系")]
        [DisplayName("点检配置集合")]
        [TypeConverter(typeof(CollectionCountConvert))]
        [Editor(typeof(ComplexCollectionEditor<MeasureBind>), typeof(UITypeEditor))]
        public List<MeasureBind> MeasureBindCollection_Check { get; set; } = new List<MeasureBind>();
        [Category("检测超时设置")]
        [Description("检测超时设置,获取检测结果时的允许最大等待时间,单位ms")]
@@ -527,12 +535,6 @@
        public string code { get; set; }
    }
    public class MeasureBind : IComplexDisplay, IHalconToolPath
    {
        [Category("索引配置")]
@@ -1111,14 +1113,10 @@
        [Editor(typeof(FoldDialogEditor), typeof(UITypeEditor))]
        public string ImageFolder { get; set; }
        [Category("检测功能")]
        [Description("图片在批量测试过程中最终图片结果会存储较慢需要设置延时确保图片不被资源自动释放")]
        [DisplayName("图片存图延时")]
        public int SaveImageTime { get; set; } = 0;
        [Category("检测功能")]
        [Description("true:启动OK测试 false:启动NG测试")]
src/Bro.M141.Process/M141Models.cs
@@ -203,9 +203,6 @@
        [Description("该检测项所涉及的所有检测点位集合")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(ComplexCollectionEditor<MeasurePointSelector>), typeof(UITypeEditor))]
        public List<MeasurePointSelector> MeasurePointNameCollection { get; set; } = new List<MeasurePointSelector>();
src/Bro.M141.Process/M141Process.cs
@@ -103,6 +103,7 @@
        public PLCBase Plc1;
        bool devicestate = false;
        PLCBase Plc2;
        public PLCBase Plc3;
        public Thread ThHeartPlc;
        public RabbitMQHelper mqtt;
@@ -322,7 +323,7 @@
                                            AlarmTypeList.Add(item2.alarmtype);
                                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"报警类别{item2.alarmtype}加入队列,队列个数为{AlarmTypeList.Count}");
                                        }
                                    }
                                }
@@ -338,8 +339,7 @@
                                    {
                                        StateDIC[StateDICKey] = new List<int>();
                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"报警类别{StateDICKey}加入字典:StateDIC");
                                    }
@@ -362,7 +362,7 @@
                                            CSVdata = csvdata,
                                        };
                                        AlarmType.Add(item.alarmtype, alarmData);
                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"报警{item.alarmname}类别{key}作为key加入AlarmType字典");
                                    }
                                    alarmData.CSVhead += $",{item.alarmname}";
@@ -642,6 +642,9 @@
                        if (measureBind.WorkPosition == "P1" && M141Config.StationCode == "S5" && detectResults.GetDefectDescList().Count == 0)
                        {
                            var defecttem1 = detectResults.SelectMany(u => u.AllNetResults).SelectMany(m => m.DetectDetails).Select(detail => detail.ClassName).ToList();
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{products[0].SN}  S3S5检测 缺陷名称集合为{string.Join(",", defecttem1)}");
                            var defecttem = detectResults.SelectMany(u => u.AllNetResults.SelectMany(m => m.DetectDetails)).Where(u => u.ClassName == M141Config.defectname).ToList();
                            List<Netdefectdetail> Netdefectdetails = mysqlhelper.GetNetdefectdetails(products[0].SEQUENCE);
@@ -893,7 +896,6 @@
            });
        }
        public async Task RunImageCheckAsync(List<ProductModel> products, string triggerText, string triggerSource, MeasureBind measureBind)
        {
            await Task.Run(() =>
@@ -972,7 +974,6 @@
            });
        }
        private void RunCustomizedMethod(List<ProductModel> products, string triggerText, string triggerSource, IImageSet imgSet, string methodId, List<DetectResult> resultList)
        {
            try
@@ -1019,10 +1020,6 @@
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"自定义检测过程异常,{ex.GetExceptionMessage()}");
            }
        }
        #region plc
        public ResponseMessage RunImageCheck_plc(IOperationConfig config)
@@ -1101,7 +1098,6 @@
            CheckPositionDoneAsync_plc(measureBinds[0].WorkPosition, inputSequence, config, cameraIds);
            return msg;
        }
@@ -1152,8 +1148,6 @@
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"相机{camera.Name}检测前清理缓存完成");
                }
            });
            inputSequence = triggerDatas[triggerDatas.Length - 1];
@@ -1245,7 +1239,6 @@
            {
                try
                {
                    string index = config.TriggerStr.Split(',')[1];
                    var positionSet = M141Config.WorkPositionCollection.FirstOrDefault(u => u.PositionName == positionName);
@@ -1897,10 +1890,6 @@
        }
        #region  打印机相关
        PrintDocument printDocument1 = new PrintDocument();
        string Printmessage = "";
@@ -1909,8 +1898,8 @@
        {
            try
            {
                int ttwith = 320;
                int ttheigh = 160;
                int ttwith = 240;
                int ttheigh = 80;
                Printmessage = str;
                this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240S (300 dpi)";
@@ -1950,7 +1939,7 @@
                writer.Options = options;
                Bitmap map = writer.Write(Printmessage);
                e.Graphics.DrawImage(map, new System.Drawing.Point(90, 10));
                e.Graphics.DrawImage(map, new System.Drawing.Point(90, 4));
                // 在二维码下方画内容文字
                using (Font font = new Font("Arial", 10))
@@ -1959,7 +1948,7 @@
                    // 让文字居中到二维码下方
                    SizeF textSize = e.Graphics.MeasureString(Printmessage, font);
                    float textX = 90 + (map.Width - textSize.Width) / 2;
                    float textY = 10 + map.Height;
                    float textY = 1 + map.Height;
                    e.Graphics.DrawString(Printmessage, font, brush3, textX, textY);
                }
src/Bro.M141.Process/M141Process_ImageCheck.cs
@@ -496,7 +496,6 @@
                Plc1.WriteSingleAddress(1526, mysqlhelper.GetS2Result(productList[0].SEQUENCE) ? 1 : 2, out _);
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{string.Join(",", productList.Select(u => u.PID))}检测反馈{mysqlhelper.GetS2Result(productList[0].SEQUENCE)}");
            }
            else
            {
@@ -988,5 +987,14 @@
            public object data { get; set; }
        }
    }
}
src/Bro.M141.Process/MyMQTT.cs
@@ -148,6 +148,7 @@
                CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 物料 解析异常:{ex.Message}");
            }
        }
        //Dictionary<string, Task> dic_ispass = new Dictionary<string, Task>();///value为收到数据
        public async Task<string> MESForBasketAsync(string ztype, string zlsn, string zlpn, string zstatus, string zversion, int timeoutMes = 30000)
@@ -166,13 +167,18 @@
                    {
                        CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ {keystr}二次接收 返回值正常 {tem.receive}");
                        return tem.receive;
                    }
                    Thread.Sleep(1000);
                }
                MSGClasses.RemoveAll(u => u.key == keystr);
                CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ {keystr}二次接收 返回值为空");
                if (MSGClasses.Count > 10)
                {
                    MSGClasses.RemoveAt(0);
                    CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ二次接收数据超过十条 清除第一条超时数据");
                }
                //MSGClasses.RemoveAll(u => u.key == keystr);
                return null;
            }
src/Bro.M141.Process/UI/UIPrinter.cs
@@ -51,16 +51,16 @@
        PrintDocument printDocument1 = new PrintDocument();
        int ttwith = (int)(80 * 4);
        int ttheigh = (int)(40 * 4);
        int ttwith = (int)(60 * 4);
        int ttheigh = (int)(20 * 4);
        string message = "";
        public void StartPrint(string str)
        {
            try
            {
                ttwith = 320;
                ttheigh = 160;
                ttwith = 240;
                ttheigh = 80;
                message = str;
                if (string.IsNullOrEmpty(message))
@@ -135,7 +135,7 @@
                writer.Options = options;
                Bitmap map = writer.Write(numvalue);
                e.Graphics.DrawImage(map, new System.Drawing.Point(90,10));
                e.Graphics.DrawImage(map, new System.Drawing.Point(90,4));
                // 在二维码下方画内容文字
@@ -145,7 +145,7 @@
                    // 让文字居中到二维码下方
                    SizeF textSize = e.Graphics.MeasureString(numvalue, font);
                    float textX = 90 + (map.Width - textSize.Width) / 2;
                    float textY = 10 + map.Height;
                    float textY = 1 + map.Height;
                    e.Graphics.DrawString(numvalue, font, brush3, textX, textY);
                }
src/Bro.M141_AOI1.Process/AOI1Process.cs
@@ -9,10 +9,12 @@
using HalconDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.OpenXmlFormats.Vml;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
using NPOI.XSSF.Streaming.Values;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using static Bro.Common.Helper.EnumHelper;
@@ -105,6 +107,75 @@
        }
        [ProcessMethod("ImageCheck", "CheckProduction", "产品点检", InvokeType.TestInvoke)]
        public ResponseMessage CheckProduction(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            if (config is IImageCheckOperationConfig opConfig)
            {
                var results1 = GetSpecListFromConfigSelection(opConfig.SpecCollection);
                IImageSet imageSet = null;
                string Head = "时间,";
                string data = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}T,";
                try
                {
                    var messure = M141Config.MeasureBindCollection_Check.FirstOrDefault(u => u.CameraId == invokeDevice.Id);
                    var camera = DeviceCollection.FirstOrDefault(u => u.Id == messure.CameraId) as CameraBase;
                    try
                    {
                        imageSet = CollectHImage(camera, messure.SnapshotOpConfig);
                    }
                    catch (Exception exx)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"点检功能获取图像失败{exx.ToString()}");
                    }
                    var tool = GetHalconTool(null, "", opConfig.AlgorithemPath);
                    var ret = tool.RunProcedure(null, new Dictionary<string, HalconDotNet.HObject>() { { "INPUT_Image", imageSet.HImage } }, new List<string>() { "OUTPUT_Results" }, null);
                    //var ret = tool.RunProcedure(null, null, new List<string>() { "OUTPUT_Results" }, null);
                    List<IShapeElement> eleList = new List<IShapeElement>();
                    TextDisplay text = new TextDisplay();
                    text.LineLimit = M141Config.LineLimit_p;
                    text.FontSize = M141Config.FontSize_p;
                    eleList.Add(text);
                    text.StartX = text.StartY = 0;
                    text.AddText("点检运行成功", Color.Lime, Color.Transparent);
                    var b = ret.Item2["OUTPUT_Results"].HTupleToDouble();
                    if (ret != null&& results1.Count== b.Count())
                    {
                        var itemDict = results1.ToDictionary(u => u.OutputIndex);
                        b.ForEach(u =>
                        {
                            int index = b.IndexOf(u); // 获取当前值的索引
                            if (itemDict.TryGetValue(index, out var item))
                            {
                                double diff = u - item.StandardValue;
                                bool isInTolerance = (diff - item.Tolrenance_Positive) *(diff - item.Tolrenance_Negative) <= 0;
                                text.AddText($"{item.Code} {u}", isInTolerance == true ? Color.Lime: Color.Red, Color.Transparent);
                                 Head += $"{item.Code},";
                                 data += $"{u},";
                            }
                        });
                    }
                    camera.SaveFitImage(eleList, imageSet);
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"点检功能运行成功");
                }
                catch (Exception ex)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"点检功能失败,{ex.ToString()}");
                }
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"点检图片开始释放");
                imageSet.HImage?.Dispose();
                imageSet.HImage = null;
                imageSet.Dispose();
                imageSet = null;
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"图片已释放");
                //报表输出
                CSVRecordAsync($"相机{invokeDevice.Name}_点检数据记录.csv", data, Head);
            }
            return msg;
        }
        [ProcessMethod("ImageCheck", "ReadBarcode", "读码", InvokeType.TestInvoke)]
        public ResponseMessage ReadBarcode(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
@@ -140,7 +211,6 @@
                            {
                                codestr = "10";
                            }
                            if (codestr.Equals(mh.ToString()))
                            {
                                ret.Add(1);
@@ -158,7 +228,6 @@
                    }
                    FillSpecResults(results[0].PID, results[0].Specs, ret, opConfig.Products[0].SEQUENCE);
                    opConfig.Products[0].SN = barcode;
                    opConfig.Products[0].PID = barcode + "_1";
@@ -179,7 +248,6 @@
            }
            return msg;
        }
        [ProcessMethod("ImageCheck", "ReadBarcode2", "读栏具码", InvokeType.TestInvoke)]
        public ResponseMessage ReadBarcode2(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
@@ -295,7 +363,6 @@
            return msg;
        }
        private string BarcodeScannerCommunicate(TcpClientWrapBase client)
        {
            string barcode = "";
@@ -326,7 +393,6 @@
            return "NOREAD";
        }
        private bool CheckBarcodeValid(string barcode)
        {
            bool isBarcodeValid = true;
@@ -336,7 +402,6 @@
            }
            return isBarcodeValid;
        }
        [ProcessMethod("ImageCheck", "CheckLineProfile", "检测产品线轮廓度", InvokeType.TestInvoke)]
        public ResponseMessage CheckLineProfile(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
@@ -500,7 +565,6 @@
            return msg;
        }
        [ProcessMethod("ImageCheck", "GetMatrix", "获取矩阵", InvokeType.TestInvoke)]
        public ResponseMessage GetMatrix(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
@@ -529,10 +593,147 @@
            return msg;
        }
        [ProcessMethod("", "MESupdata", "MES正常上传OK产品获取条码", InvokeType.TestInvoke)]
        public ResponseMessage MESupdata(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            Plc3 = invokeDevice as PLCBase;
            ///产品1上传
            var plcnum = Plc3.Read(2100, 1, out _);
            if (plcnum[0] != 0)
            {
                var plist = mysqlhelper.GetProductList(plcnum + "_1");
                ProductModel newp = new ProductModel();
                newp.SEQUENCE = plist[0].SEQUENCE;
                newp.PID = plist[0].PID;
                newp.BasketCode = plist[0].BasketCode;
                newp.Zword = plist[0].Zword;
                newp.Result = "OK";
                newp.SN = plist[0].SN;
                var tems = Task.Run(() => mqtt.MESForProduceAsync(newp, M141Config.mesnum2.ToString(), M141Config.numpro)).Result;
                M141Config.mesnum2++;
                if (tems == null)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES异常 返回数据为null");
                }
                else
                {
                    try
                    {
                        var obj = JsonConvert.DeserializeObject<AutoLineMacBarcodeQueueBak>(tems);
                        if (obj.zstatus == "200")
                        {
                            Plc1.WriteSingleAddress(2120, 1, out _);
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{plist[0].PID}数据上传MES成功  {tems}");
                        }
                        else
                        {
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                        }
                    }
                    catch (Exception)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                    }
                }
                ///产品1上传
                var plcnum1 = Plc3.Read(2102, 1, out _);
                if (plcnum1[0] != 0)
                {
                    var plist1 = mysqlhelper.GetProductList(plcnum1 + "_1");
                    ProductModel newp1 = new ProductModel();
                    newp1.SEQUENCE = plist[0].SEQUENCE;
                    newp1.PID = plist[0].PID;
                    newp1.BasketCode = plist[0].BasketCode;
                    newp1.Zword = plist[0].Zword;
                    newp1.Result = "OK";
                    newp1.SN = plist[0].SN;
                    var tems1 = Task.Run(() => mqtt.MESForProduceAsync(newp1, M141Config.mesnum2.ToString(), M141Config.numpro)).Result;
                    M141Config.mesnum2++;
                    if (tems1 == null)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES异常 返回数据为null");
                    }
                    else
                    {
                        try
                        {
                            var obj1 = JsonConvert.DeserializeObject<AutoLineMacBarcodeQueueBak>(tems1);
                            if (obj1.zstatus == "200")
                            {
                                Plc1.WriteSingleAddress(2120, 1, out _);
                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{plist[0].PID}数据上传MES成功  {tems}");
                            }
                            else
                            {
                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                            }
                        }
                        catch (Exception)
                        {
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                        }
                    }
                }
            }
            return msg;
        }
        [ProcessMethod("", "MESupFinallydata", "MES上传尾盘OK产品获取条码", InvokeType.TestInvoke)]
        public ResponseMessage MESupFinallydata(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            Plc3 = invokeDevice as PLCBase;
            ///产品1上传
            var plcnum = Plc3.Read(2100, 1, out _);
            var plist = mysqlhelper.GetProductList(plcnum+"_1");
            ProductModel newp = new ProductModel();
            newp.SEQUENCE = plist[0].SEQUENCE;
            newp.PID = plist[0].PID;
            newp.BasketCode = plist[0].BasketCode;
            newp.Zword = plist[0].Zword;
            newp.Result = "OK";
            newp.SN = plist[0].SN;
            //上传尾盘数据
            var tems = Task.Run(() => mqtt.MESForProduceAsync(newp, M141Config.mesnum2.ToString(), M141Config.numpro,"Y")).Result;
            M141Config.mesnum2 = 0;
            if (tems == null)
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES异常 返回数据为null");
            }
            else
            {
                try
                {
                    var obj = JsonConvert.DeserializeObject<AutoLineMacBarcodeQueueBak>(tems);
                    if (obj.zstatus == "200")
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{plist[0].PID}数据上传MES成功  {tems}");
                        //打印获取的条码
                        if (M141Config.Isprint && !string.IsNullOrEmpty(obj.tary_label))
                        {
                            StartPrint(obj.tary_label);
                        }
                    }
                    else
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                    }
                }
                catch (Exception)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                }
            }
            return msg;
        }
        [ProcessMethod("", "MESup1", "MES上传进料口空篮", InvokeType.TestInvoke)]