From 0bdfe55b390722d2193409fc3412a04ccc2011c9 Mon Sep 17 00:00:00 2001 From: liuwf Date: Tue, 25 Jun 2024 21:17:31 +0800 Subject: [PATCH] =?UTF-8?q?push-=E6=8F=90=E4=BA=A4=E7=8E=B0=E5=9C=BA?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Business/LogoBusiness.cs | 48 ++++++++++++++--------- SlnMesnac.Common/FileHelper.cs | 2 +- SlnMesnac.Common/GunHelper.cs | 4 +- SlnMesnac.Config/DebugConfig.cs | 26 ++++++++++++ SlnMesnac.TouchSocket/TcpServer.cs | 10 ++++- SlnMesnac.WPF/ViewModel/IndexViewModel.cs | 26 ++++++++++-- SlnMesnac.WPF/Views/IndexPage.xaml | 8 ++-- SlnMesnac.WPF/appsettings.json | 4 +- 8 files changed, 95 insertions(+), 33 deletions(-) diff --git a/SlnMesnac.Business/LogoBusiness.cs b/SlnMesnac.Business/LogoBusiness.cs index 8bbdf83..0180775 100644 --- a/SlnMesnac.Business/LogoBusiness.cs +++ b/SlnMesnac.Business/LogoBusiness.cs @@ -45,9 +45,11 @@ namespace SlnMesnac.Business private static bool HikCameraResult = false; #region 委托定义 刷新界面扫描信息 - public delegate void RefreshBoxInfo(string boxCode, string boxTime, string model, byte[] imageData, bool isSuccess); + public delegate void RefreshBoxInfo(string boxCode, string boxTime, string model, bool isSuccess); public static event RefreshBoxInfo? RefreshBoxInfoEvent; + public delegate void RefreshPicture(byte[] imageData); + public static event RefreshPicture? RefreshPictureEvent; public delegate void RefreshMessage(string message, bool isWarning = false); public static event RefreshMessage? RefreshMessageEvent; @@ -102,7 +104,7 @@ namespace SlnMesnac.Business /// public async void BarCodeHandler(string materialCodeStr, string ip) { - + Console.WriteLine("相机流程开始:"); // 传入照片 byte[] ImageData = null; try @@ -129,6 +131,7 @@ namespace SlnMesnac.Business if (logoConfig.IsChecked == 1) { + Thread.Sleep(int.Parse(config.SleepStr)); // 1.触发相机拍照 tcpServer.SendCommand(config.CameraIP, logoConfig.CheckKind.ToString()); // 需要校验 @@ -166,18 +169,25 @@ namespace SlnMesnac.Business record.RecordTime = DateTime.Now; logoIdentifyService.InsertRecord(record); #endregion - ImageData = FileHelper.FindPhoto(config.CameraFilePath); - if (ImageData != null) - { - FileHelper.SaveImage(ImageData, materialCodeStr + ".jpg", PicturePath); - // 刷新界面、刷新图片,照片按照日期存储本地 - RefreshBoxInfoEvent?.Invoke(materialCodeStr, DateTime.Now.ToString(), logoConfig.MaterialName, ImageData, hikFlag); - } - else + + // 刷新界面、刷新图片,照片按照日期存储本地 + RefreshBoxInfoEvent?.Invoke(materialCodeStr, DateTime.Now.ToString(), logoConfig.MaterialName, hikFlag); + + await Task.Run(() => { - WarningAndStop($"读取相机保存照片文件夹失败,请检查文件夹:{config.CameraFilePath}是否存在"); - } + Thread.Sleep(int.Parse(config.PictureSleep)); + ImageData = FileHelper.FindPhoto(config.CameraFilePath); + if (ImageData != null) + { + FileHelper.SaveImage(ImageData, materialCodeStr + ".jpg", PicturePath); + RefreshPictureEvent?.Invoke(ImageData); + } + else + { + // WarningAndStop($"读取相机保存照片文件夹失败,请检查文件夹:{config.CameraFilePath}是否存在"); + } + }); } catch (Exception ex) @@ -283,7 +293,7 @@ namespace SlnMesnac.Business { if (plc != null && plc.IsConnected) { - plc.writeInt32ByAddress("DB22.DBW2", 8); + plc.writeInt16ByAddress("DB22.DBW2", 8); Task.Run(() => { // 设置计时器 @@ -291,10 +301,11 @@ namespace SlnMesnac.Business stopwatch.Start(); while (true) { - if (plc.readInt32ByAddress("DB22.DBW2") == 0) + if (plc.readInt16ByAddress("DB22.DBW2") == 0) { logger.LogInformation("PLC复位成功,启动线体"); - RefreshMessageEvent?.Invoke("PLC复位成功,启动线体", true); + RefreshMessageEvent?.Invoke("PLC复位成功,启动线体"); + break; } // 检查是否超过两秒 if (stopwatch.ElapsedMilliseconds > 3000) @@ -329,7 +340,7 @@ namespace SlnMesnac.Business { if (plc != null && plc.IsConnected) { - plc.writeInt32ByAddress("DB22.DBW2", 9); + plc.writeInt16ByAddress("DB22.DBW2", 9); Task.Run(() => { // 设置计时器 @@ -337,10 +348,11 @@ namespace SlnMesnac.Business stopwatch.Start(); while (true) { - if (plc.readInt32ByAddress("DB22.DBW2") == 1) + if (plc.readInt16ByAddress("DB22.DBW2") == 1) { logger.LogInformation("PLC复位成功,启动线体"); - RefreshMessageEvent?.Invoke("PLC复位成功,启动线体", true); + RefreshMessageEvent?.Invoke("PLC复位成功,启动线体"); + break; } // 检查是否超过两秒 if (stopwatch.ElapsedMilliseconds > 3000) diff --git a/SlnMesnac.Common/FileHelper.cs b/SlnMesnac.Common/FileHelper.cs index 2eef00e..86d7d9f 100644 --- a/SlnMesnac.Common/FileHelper.cs +++ b/SlnMesnac.Common/FileHelper.cs @@ -20,7 +20,7 @@ namespace SlnMesnac.Common try { // 获取目录中的所有文件 - string[] files = Directory.GetFiles(directoryPath, "*.jpg"); + string[] files = Directory.GetFiles(directoryPath, "*.bmp"); // 检查是否有文件 if (files.Length == 0) diff --git a/SlnMesnac.Common/GunHelper.cs b/SlnMesnac.Common/GunHelper.cs index a37a800..5dfbd5e 100644 --- a/SlnMesnac.Common/GunHelper.cs +++ b/SlnMesnac.Common/GunHelper.cs @@ -38,8 +38,8 @@ namespace SlnMesnac.Common } #endregion + private DebugConfig config = DebugConfig.Instance; - //初始化串口并启动接收数据 public void InstanceSerialPort() { @@ -49,7 +49,7 @@ namespace SlnMesnac.Common //实例化串行端口 //端口名 注:因为使用的是USB转RS232 所以去设备管理器中查看一下虚拟com口的名字 - serialPort.PortName = port;// portName; + serialPort.PortName = config.Port;// portName; //波特率 霍尼威尔扫码枪115200,普通9600 serialPort.BaudRate = 0x2580; //奇偶校验 diff --git a/SlnMesnac.Config/DebugConfig.cs b/SlnMesnac.Config/DebugConfig.cs index 60d14b4..894fd86 100644 --- a/SlnMesnac.Config/DebugConfig.cs +++ b/SlnMesnac.Config/DebugConfig.cs @@ -53,5 +53,31 @@ namespace SlnMesnac.Config set { iniHelper.IniWriteValue("system", "CameraFilePath", value); } } + /// + /// 拍照延迟 + /// + public string SleepStr + { + get { return iniHelper.IniReadValue("system", "Sleep"); } + set { iniHelper.IniWriteValue("system", "Sleep", value); } + } + + /// + /// 读取照片延迟 + /// + public string PictureSleep + { + get { return iniHelper.IniReadValue("system", "PictureSleep"); } + set { iniHelper.IniWriteValue("system", "PictureSleep", value); } + } + + /// + /// Com口 + /// + public string Port + { + get { return iniHelper.IniReadValue("system", "ComPort"); } + set { iniHelper.IniWriteValue("system", "ComPort", value); } + } } } diff --git a/SlnMesnac.TouchSocket/TcpServer.cs b/SlnMesnac.TouchSocket/TcpServer.cs index deb1791..a5ff2aa 100644 --- a/SlnMesnac.TouchSocket/TcpServer.cs +++ b/SlnMesnac.TouchSocket/TcpServer.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; using TouchSocket.Core; using TouchSocket.Sockets; @@ -83,16 +84,21 @@ namespace SlnMesnac.TouchSocket } else { + string code = mes.Trim().TrimEnd('\0'); client.Logger.Info($"客户端{client.IP}:{client.Port}》接收到信息:{mes}"); // 区分相机扫码器数据 if (client.IP == config.ScannerIP) { - RefreshMaterialCodeStrEvent?.Invoke(mes.Trim().TrimEnd('\0'), client.IP); + if (code.Contains("heart")) + { // 粘包丢弃 + return Task.CompletedTask; + } + RefreshMaterialCodeStrEvent?.Invoke(code, client.IP); } else if (client.IP == config.CameraIP) { // 相机返回数据 - CameraResultEvent?.Invoke(mes.Trim().TrimEnd('\0')); + CameraResultEvent?.Invoke(code); } diff --git a/SlnMesnac.WPF/ViewModel/IndexViewModel.cs b/SlnMesnac.WPF/ViewModel/IndexViewModel.cs index e55ca67..2a58476 100644 --- a/SlnMesnac.WPF/ViewModel/IndexViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/IndexViewModel.cs @@ -49,6 +49,7 @@ namespace SlnMesnac.WPF.ViewModel logoIdentifyService = App.ServiceProvider.GetService(); LogoBusiness.RefreshMessageEvent += RefreshMessage; LogoBusiness.RefreshBoxInfoEvent += RefreshBoxInfo; + LogoBusiness.RefreshPictureEvent += RefreshPicture; ResetCommand = new RelayCommand(Reset); RefreshDataGrid(); @@ -71,8 +72,8 @@ namespace SlnMesnac.WPF.ViewModel /// private void Reset() { - logoBusiness.BarCodeHandler("2737001000021E6M0627", "111"); - // logoBusiness.Pass(); + //FileHelper.FindPhoto(DebugConfig.Instance.CameraFilePath); + logoBusiness.Pass(); } /// @@ -134,16 +135,33 @@ namespace SlnMesnac.WPF.ViewModel /// /// /// - public void RefreshBoxInfo(string boxCode,string boxTime,string model,byte[] imageData,bool isSuccess) + public void RefreshBoxInfo(string boxCode,string boxTime,string model,bool isSuccess) { BoxCode = boxCode; BoxTime = boxTime; ProductModel = model; - ImageData = imageData; + RefreshResultColor(isSuccess); RefreshDataGrid(); } + + /// + /// 刷新界面照片 + /// + /// + /// + /// + /// + public void RefreshPicture(byte[] imageData) + { + + ImageData = imageData; + + + } + + /// /// 刷新界面提示信息 /// diff --git a/SlnMesnac.WPF/Views/IndexPage.xaml b/SlnMesnac.WPF/Views/IndexPage.xaml index 806c982..8898c83 100644 --- a/SlnMesnac.WPF/Views/IndexPage.xaml +++ b/SlnMesnac.WPF/Views/IndexPage.xaml @@ -112,9 +112,9 @@ - - - + + + @@ -126,7 +126,7 @@ -