From 3da5303554619f46b131ef8e115fc0bc83618548 Mon Sep 17 00:00:00 2001 From: liuwf Date: Wed, 20 Mar 2024 15:42:40 +0800 Subject: [PATCH] =?UTF-8?q?change-=E6=9D=A1=E7=A0=81=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E6=89=AB=E7=A0=81=E5=99=A8=EF=BC=8C=E6=89=AB=E7=A0=81=E6=9E=AA?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=94=B1=E4=B8=80=E4=B8=AA=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E4=BC=A0=E6=94=B9=E6=88=90=E4=B8=A4=E4=B8=AA=E4=B8=80=E8=B5=B7?= =?UTF-8?q?=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Aucma.Core.CodeBinding.csproj | 2 +- .../Business/GunBusiness.cs | 13 +- .../Business/MvCodeHelper.cs | 119 ++++++++++++------ .../ViewModels/IndexPageViewModel.cs | 88 ++++++------- 4 files changed, 136 insertions(+), 86 deletions(-) diff --git a/Aucma.Core.CodeBinding/Aucma.Core.CodeBinding.csproj b/Aucma.Core.CodeBinding/Aucma.Core.CodeBinding.csproj index 499c8941..7edf5cca 100644 --- a/Aucma.Core.CodeBinding/Aucma.Core.CodeBinding.csproj +++ b/Aucma.Core.CodeBinding/Aucma.Core.CodeBinding.csproj @@ -1,7 +1,7 @@  - Exe + WinExe net6.0-windows enable true diff --git a/Aucma.Core.CodeBinding/Business/GunBusiness.cs b/Aucma.Core.CodeBinding/Business/GunBusiness.cs index ff270e06..9883ed22 100644 --- a/Aucma.Core.CodeBinding/Business/GunBusiness.cs +++ b/Aucma.Core.CodeBinding/Business/GunBusiness.cs @@ -25,11 +25,13 @@ namespace Aucma.Core.CodeBinding.Business // 箱壳扫码器ip private static string SNScannerIp = allScanners.First(x => x.Name == "sn扫码器").Ip; + private static string SNCode = string.Empty; + private static string MESCode = string.Empty; /// /// code1扫码信息刷新 /// /// - public delegate void BindingReceiveCode(string code, string ip); + public delegate void BindingReceiveCode(string MesCode, string SnCode); public static event BindingReceiveCode? BindingReceiveCodeEvent; #region 单例实现 @@ -103,13 +105,22 @@ namespace Aucma.Core.CodeBinding.Business if (str.Substring(0, 1) == "B") { // mes条码 + MESCode = str; RefreshMaterialCodeStrEvent?.Invoke(str, MesScannerIp); } else { //SN码 + SNCode = str; RefreshMaterialCodeStrEvent?.Invoke(str,SNScannerIp); } + if(!string.IsNullOrEmpty(MESCode) && !string.IsNullOrEmpty(SNCode)) + { + // 委托 + BindingReceiveCodeEvent?.Invoke(MESCode, SNCode); + SNCode = string.Empty; + MESCode = string.Empty; + } sb.Clear(); } diff --git a/Aucma.Core.CodeBinding/Business/MvCodeHelper.cs b/Aucma.Core.CodeBinding/Business/MvCodeHelper.cs index a7cafecc..2cb4492e 100644 --- a/Aucma.Core.CodeBinding/Business/MvCodeHelper.cs +++ b/Aucma.Core.CodeBinding/Business/MvCodeHelper.cs @@ -32,6 +32,10 @@ namespace Aucma.Core.CodeBinding.Business public static MvCodeReader LinerDevice = null; public static DateTime ShellLiveTime = DateTime.Now; public static DateTime LinerLiveTime = DateTime.Now; + + private static string MesCode = string.Empty; + private static string SNCode = string.Empty; + #endregion @@ -40,7 +44,7 @@ namespace Aucma.Core.CodeBinding.Business #region 条码绑定上位机委托事件 - + /// /// 刷新扫码器状态 @@ -56,8 +60,15 @@ namespace Aucma.Core.CodeBinding.Business /// code1扫码信息刷新 /// /// - public delegate void BindingReceiveCode(string code, string ip); - public static event BindingReceiveCode? BindingReceiveCodeEvent; + public delegate void ReceiveCode(string code, string ip); + public static event ReceiveCode? ReceiveCodeEvent; + + /// + /// 条码绑定 + /// + /// + public delegate void BindingCode(string mesCode, string snCode); + public static event BindingCode? BindingCodeEvent; /// /// NoRead通知 @@ -91,6 +102,8 @@ namespace Aucma.Core.CodeBinding.Business log.Info(DateTime.Now + ":SN--->No Read 处理"); //更新扫码器存活时间 ShellLiveTime = DateTime.Now; + SNCode = string.Empty; + MesCode = string.Empty; } for (Int32 i = 0; i < stBcrResult.nCodeNum; i++) { @@ -106,12 +119,14 @@ namespace Aucma.Core.CodeBinding.Business string strCode = Encoding.GetEncoding("GB2312").GetString(stBcrResult.stBcrInfoEx2[i].chCode); Console.WriteLine(DateTime.Now + ":SN扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]"); log.Info(DateTime.Now + ":SN扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]"); - - //更新扫码器存活时间 - ShellLiveTime = DateTime.Now; - BindingReceiveCodeEvent?.Invoke(strCode.Trim().TrimEnd('\0'), ShellScannerIp); - // 业务处理 - + + //更新扫码器存活时间 + ShellLiveTime = DateTime.Now; + + SNCode = strCode.Trim().TrimEnd('\0'); + ReceiveCodeEvent?.Invoke(strCode.Trim().TrimEnd('\0'), ShellScannerIp); + + } } @@ -127,51 +142,73 @@ namespace Aucma.Core.CodeBinding.Business public static void LinerImageCallbackFunc(IntPtr pData, IntPtr pstFrameInfoEx2, IntPtr pUser) { - var stFrameInfo = (MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2)Marshal.PtrToStructure(pstFrameInfoEx2, typeof(MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2)); + try + { + var stFrameInfo = (MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2)Marshal.PtrToStructure(pstFrameInfoEx2, typeof(MvCodeReader.MV_CODEREADER_IMAGE_OUT_INFO_EX2)); - MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2 stBcrResult = (MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2)Marshal.PtrToStructure(stFrameInfo.UnparsedBcrList.pstCodeListEx2, typeof(MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2)); + MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2 stBcrResult = (MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2)Marshal.PtrToStructure(stFrameInfo.UnparsedBcrList.pstCodeListEx2, typeof(MvCodeReader.MV_CODEREADER_RESULT_BCR_EX2)); - // Console.WriteLine("CodeNum[" + Convert.ToString(stBcrResult.nCodeNum) + "]"); - if (stBcrResult.nCodeNum == 0) - { - NoReadEvent?.Invoke(LinerScannerIp); - Console.WriteLine(DateTime.Now + ":mes--->No Read 处理"); - log.Info(DateTime.Now + ":mes--->No Read 处理"); - // 更新存活时间 - LinerLiveTime = DateTime.Now; - } - for (Int32 i = 0; i < stBcrResult.nCodeNum; i++) - { - bool bIsValidUTF8 = IsTextUTF8(stBcrResult.stBcrInfoEx2[i].chCode); - if (bIsValidUTF8) + // Console.WriteLine("CodeNum[" + Convert.ToString(stBcrResult.nCodeNum) + "]"); + if (stBcrResult.nCodeNum == 0) { - string strCode = Encoding.UTF8.GetString(stBcrResult.stBcrInfoEx2[i].chCode); - Console.WriteLine(DateTime.Now + ":Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]"); + NoReadEvent?.Invoke(LinerScannerIp); + Console.WriteLine(DateTime.Now + ":mes--->No Read 处理"); + log.Info(DateTime.Now + ":mes--->No Read 处理"); + // 更新存活时间 + LinerLiveTime = DateTime.Now; + SNCode = string.Empty; + MesCode = string.Empty; } - else + for (Int32 i = 0; i < stBcrResult.nCodeNum; i++) { - string strCode = Encoding.GetEncoding("GB2312").GetString(stBcrResult.stBcrInfoEx2[i].chCode); - Console.WriteLine(DateTime.Now + ":mes扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]"); - log.Info(DateTime.Now + ":mes扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]"); - + bool bIsValidUTF8 = IsTextUTF8(stBcrResult.stBcrInfoEx2[i].chCode); + if (bIsValidUTF8) + { + string strCode = Encoding.UTF8.GetString(stBcrResult.stBcrInfoEx2[i].chCode); + Console.WriteLine(DateTime.Now + ":Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]"); + } + else + { + string strCode = Encoding.GetEncoding("GB2312").GetString(stBcrResult.stBcrInfoEx2[i].chCode); + Console.WriteLine(DateTime.Now + ":mes扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]"); + log.Info(DateTime.Now + ":mes扫码器==>Get CodeNum: " + "CodeNum[" + i.ToString() + "], CodeString[" + strCode.Trim().TrimEnd('\0') + "]"); + // 更新存活时间 LinerLiveTime = DateTime.Now; - // 业务处理 - BindingReceiveCodeEvent?.Invoke(strCode.Trim().TrimEnd('\0'), LinerScannerIp); - + + MesCode = strCode.Trim().TrimEnd('\0'); + ReceiveCodeEvent?.Invoke(strCode.Trim().TrimEnd('\0'), LinerScannerIp); + + if (!string.IsNullOrEmpty(MesCode) && !string.IsNullOrEmpty(SNCode)) + { + BindingCodeEvent?.Invoke(MesCode, SNCode); + MesCode = string.Empty; + SNCode = string.Empty; + } + + } } - } - MvCodeReader.MV_CODEREADER_OCR_INFO_LIST stOcrInfo = (MvCodeReader.MV_CODEREADER_OCR_INFO_LIST)Marshal.PtrToStructure(stFrameInfo.UnparsedOcrList.pstOcrList, typeof(MvCodeReader.MV_CODEREADER_OCR_INFO_LIST)); + MvCodeReader.MV_CODEREADER_OCR_INFO_LIST stOcrInfo = (MvCodeReader.MV_CODEREADER_OCR_INFO_LIST)Marshal.PtrToStructure(stFrameInfo.UnparsedOcrList.pstOcrList, typeof(MvCodeReader.MV_CODEREADER_OCR_INFO_LIST)); - Console.WriteLine("ocrAllNum[" + Convert.ToString(stOcrInfo.nOCRAllNum) + "]"); - for (int i = 0; i < stOcrInfo.nOCRAllNum; i++) + Console.WriteLine("ocrAllNum[" + Convert.ToString(stOcrInfo.nOCRAllNum) + "]"); + for (int i = 0; i < stOcrInfo.nOCRAllNum; i++) + { + string strOcrCharCode = Encoding.UTF8.GetString(stOcrInfo.stOcrRowInfo[i].chOcr); + Console.WriteLine("Get OcrInfo:" + "ocrNum[" + i.ToString() + "], ocrLen[" + Convert.ToString(stOcrInfo.stOcrRowInfo[i].nOcrLen) + "], ocrChar[" + strOcrCharCode.Trim().TrimEnd('\0') + "]"); + } + } + catch (Exception ex) { - string strOcrCharCode = Encoding.UTF8.GetString(stOcrInfo.stOcrRowInfo[i].chOcr); - Console.WriteLine("Get OcrInfo:" + "ocrNum[" + i.ToString() + "], ocrLen[" + Convert.ToString(stOcrInfo.stOcrRowInfo[i].nOcrLen) + "], ocrChar[" + strOcrCharCode.Trim().TrimEnd('\0') + "]"); + log.Info("mes扫码器回调函数异常:" + ex.Message.ToString()); + } + finally + { + MesCode = string.Empty; + SNCode = string.Empty; } } - + public static void Shell() { diff --git a/Aucma.Core.CodeBinding/ViewModels/IndexPageViewModel.cs b/Aucma.Core.CodeBinding/ViewModels/IndexPageViewModel.cs index 6088145c..cb6b1579 100644 --- a/Aucma.Core.CodeBinding/ViewModels/IndexPageViewModel.cs +++ b/Aucma.Core.CodeBinding/ViewModels/IndexPageViewModel.cs @@ -54,10 +54,10 @@ namespace Aucma.Core.CodeBinding.ViewModels // 过点数据表,物料完成记录MaterialCompletion private readonly IMaterialCompletionServices? _iMaterialCompletionServices; // 静态变量存code1 - private static string code1Str = string.Empty; + // private static string code1Str = string.Empty; // 静态变量存code2 - private static string code2Str = string.Empty; + // private static string code2Str = string.Empty; //配置文件扫码器列表 private static List allScanners = Appsettings.app("ScannerServer").ToList(); public IndexPageViewModel() @@ -70,10 +70,12 @@ namespace Aucma.Core.CodeBinding.ViewModels _printBarCodeServices = App.ServiceProvider.GetService(); _iMaterialCompletionServices = App.ServiceProvider.GetService(); - Business.MvCodeHelper.BindingReceiveCodeEvent += ReceiveCode; - GunBusiness.RefreshMaterialCodeStrEvent += ReceiveCode; + Business.MvCodeHelper.ReceiveCodeEvent += ReceiveCode; + Business.MvCodeHelper.BindingCodeEvent += BindingCode; Business.MvCodeHelper.NoReadEvent += ReceiveNoRead; - + GunBusiness.BindingReceiveCodeEvent += BindingCode; + GunBusiness.RefreshMaterialCodeStrEvent += ReceiveCode; + LoadData(); @@ -104,29 +106,29 @@ namespace Aucma.Core.CodeBinding.ViewModels { string tempCode1 ; string tempCode2; - // 实时绑定条码 - Task.Run(() => - { - while(true) - { - try - { - if (!string.IsNullOrEmpty(code1Str) && !string.IsNullOrEmpty(code2Str)) - { - tempCode1 = code1Str; - tempCode2 = code2Str; - code1Str = string.Empty; - code2Str = string.Empty; - BindingCode(tempCode1, tempCode2); - } - } - catch (Exception ex) - { - Console.WriteLine(ex.ToString()); - } - Thread.Sleep(500); - } - }); + //// 实时绑定条码 + //Task.Run(() => + //{ + // while(true) + // { + // try + // { + // if (!string.IsNullOrEmpty(code1Str) && !string.IsNullOrEmpty(code2Str)) + // { + // tempCode1 = code1Str; + // tempCode2 = code2Str; + // code1Str = string.Empty; + // code2Str = string.Empty; + // BindingCode(tempCode1, tempCode2); + // } + // } + // catch (Exception ex) + // { + // Console.WriteLine(ex.ToString()); + // } + // Thread.Sleep(500); + // } + //}); // 心跳线程 Task.Run(() => @@ -449,11 +451,11 @@ namespace Aucma.Core.CodeBinding.ViewModels { log.Info("MES条码NoRead:"); // 全局变量赋值 - code1Str = string.Empty ; - code2Str = string.Empty; + // code1Str = string.Empty ; + // code2Str = string.Empty; // 页面扫码信息清空 - Code1 = code1Str; - Code2 = code2Str; + Code1 = string.Empty; + Code2 = string.Empty; Code1Time = string.Empty ; Code2Time = string.Empty ; SendPlcStop(); @@ -462,11 +464,11 @@ namespace Aucma.Core.CodeBinding.ViewModels else if(model.Id == 2) { RefreshAndWriteLog("SN条码未扫描到,NoRead报警,线体停止,请重新扫描!"); - code1Str = string.Empty; - code2Str = string.Empty; + // code1Str = string.Empty; + // code2Str = string.Empty; // 页面扫码信息清空 - Code1 = code1Str; - Code2 = code2Str; + Code1 = string.Empty; + Code2 = string.Empty; Code1Time = string.Empty; Code2Time = string.Empty; SendPlcStop(); @@ -487,7 +489,7 @@ namespace Aucma.Core.CodeBinding.ViewModels { log.Info("扫描到MES条码:" + codeStr); // 全局变量赋值,SN码扫描后使用 - code1Str = codeStr; + // code1Str = codeStr; RefreshCode1(codeStr); } else @@ -495,7 +497,7 @@ namespace Aucma.Core.CodeBinding.ViewModels log.Info("扫描到成品条码:" + codeStr); // 1.刷新界面条码信息 // 全局变量赋值,mes条码扫描后使用 - code2Str = codeStr; + // code2Str = codeStr; RefreshCode2(codeStr); } @@ -554,8 +556,8 @@ namespace Aucma.Core.CodeBinding.ViewModels /// /// 条码绑定 /// - /// - /// + /// MES码 + /// SN码 public void BindingCode(string code1,string code2) { try @@ -656,8 +658,8 @@ namespace Aucma.Core.CodeBinding.ViewModels } finally { - code1Str = string.Empty; - code2Str = string.Empty; + // code1Str = string.Empty; + // code2Str = string.Empty; } } @@ -705,7 +707,7 @@ namespace Aucma.Core.CodeBinding.ViewModels var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("条码绑定Plc")); if (obj != null) { - log.Info("停止信号D7102写1"); + log.Info("下发放行,停止信号D7102写0"); // 停止信号复位 obj.plc.WriteInt16("D7102", "0"); // 报警信号复位