From 7d57ce7841ac463cc96872b9e724753cb65b5500 Mon Sep 17 00:00:00 2001 From: liuwf Date: Tue, 18 Jun 2024 16:54:55 +0800 Subject: [PATCH] =?UTF-8?q?change-=E6=94=B9=E5=8F=98PLC=E4=BA=A4=E4=BA=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Business/LogoBusiness.cs | 99 +++++++++++++++++++---- SlnMesnac.WPF/ViewModel/IndexViewModel.cs | 32 ++++++-- 2 files changed, 110 insertions(+), 21 deletions(-) diff --git a/SlnMesnac.Business/LogoBusiness.cs b/SlnMesnac.Business/LogoBusiness.cs index e8b59b7..bd675af 100644 --- a/SlnMesnac.Business/LogoBusiness.cs +++ b/SlnMesnac.Business/LogoBusiness.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Security.Cryptography.Xml; +using System.ServiceModel.Channels; using System.Threading; using System.Threading.Tasks; using TouchSocket.Core; @@ -50,6 +51,8 @@ namespace SlnMesnac.Business public delegate void RefreshMessage(string message, bool isWarning = false); public static event RefreshMessage? RefreshMessageEvent; + + #endregion @@ -124,14 +127,14 @@ namespace SlnMesnac.Business hikFlag = await JudgeIsSuccessAsync(); if (hikFlag) { - //校验成功下发放行 - Pass(); + //校验成功放行 + RefreshMessageEvent?.Invoke("Logo识别成功,下发放行"); logger.LogInformation($"箱体码:{materialCodeStr},Logo识别成功,下发放行"); } else { - //校验成功下发放行 + //校验失败禁止放行 WarningAndStop($"Logo识别失败,禁止放行"); } } @@ -139,8 +142,7 @@ namespace SlnMesnac.Business { // 不需要校验 hikFlag = true; - - Pass(); + RefreshMessageEvent?.Invoke("Logo无需识别,下发放行"); logger.LogInformation($"箱体码:{materialCodeStr},Logo无需识别,下发放行"); } @@ -267,28 +269,93 @@ namespace SlnMesnac.Business //M100停止点位 public void Pass() { - if(plc!=null && plc.IsConnected) + try + { + if (plc != null && plc.IsConnected) + { + plc.writeInt16ByAddress("M100", 8); + Task.Run(() => + { + // 设置计时器 + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + while (true) + { + if (plc.readInt16ByAddress("M100") == 0) + { + logger.LogInformation("PLC复位成功,启动线体"); + RefreshMessageEvent?.Invoke("PLC复位成功,启动线体", true); + } + // 检查是否超过两秒 + if (stopwatch.ElapsedMilliseconds > 3000) + { + logger.LogError("PLC复位超时"); + RefreshMessageEvent?.Invoke("PLC复位超时", true); + } + Thread.Sleep(100); + } + }); + + } + else + { + logger.LogError("PLC未连接,请检查连接"); + RefreshMessageEvent?.Invoke("PLC未连接,请检查连接", true); + } + // 声光电报警复位 + gunHelper.SendData("OK"); + } + catch (Exception ex) { - plc.writeInt16ByAddress("M100",0); + logger.LogError($"下发PLC复位方法Pass()异常:{ex.Message}"); } - // 声光电报警复位 - gunHelper.SendData("OK"); } public void Stop() { - if (plc != null && plc.IsConnected) + try + { + if (plc != null && plc.IsConnected) + { + plc.writeInt16ByAddress("M100", 9); + Task.Run(() => + { + // 设置计时器 + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + while (true) + { + if (plc.readInt16ByAddress("M100") == 1) + { + logger.LogInformation("PLC复位成功,启动线体"); + RefreshMessageEvent?.Invoke("PLC复位成功,启动线体", true); + } + // 检查是否超过两秒 + if (stopwatch.ElapsedMilliseconds > 3000) + { + logger.LogError("PLC复位超时"); + RefreshMessageEvent?.Invoke("PLC复位超时", true); + } + Thread.Sleep(100); + } + }); + + + } + // 声光电报警 + gunHelper.SendData("NG"); + } + catch (Exception ex) { - plc.writeInt16ByAddress("M100", 1); + logger.LogError($"下发PLC报警方法Stop()异常:{ex.Message}"); } - // 声光电报警复位 - gunHelper.SendData("NG"); + } - #endregion + #endregion - #region 记录日志,刷新界面及下发PLC报警 - public void WarningAndStop(string message) + #region 记录日志,刷新界面及下发PLC报警 + public void WarningAndStop(string message) { logger.LogError(message); RefreshMessageEvent?.Invoke(message, true); diff --git a/SlnMesnac.WPF/ViewModel/IndexViewModel.cs b/SlnMesnac.WPF/ViewModel/IndexViewModel.cs index df34adb..d9a37a8 100644 --- a/SlnMesnac.WPF/ViewModel/IndexViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/IndexViewModel.cs @@ -1,12 +1,18 @@ using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; +using HslCommunication.Enthernet; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Microsoft.Win32; using SlnMesnac.Business; using SlnMesnac.Common; +using SlnMesnac.Config; using SlnMesnac.Model.domain; +using SlnMesnac.Plc; using SlnMesnac.Repository.service; +using SlnMesnac.TouchSocket; +using SlnMesnac.WPF.Views; using SqlSugar; using System; using System.Collections.Generic; @@ -26,6 +32,18 @@ namespace SlnMesnac.WPF.ViewModel private ILogoIdentifyService logoIdentifyService; private GunHelper gunHelper = GunHelper.Instance; public RelayCommand ResetCommand { get; set;} + + private LogoBusiness logoBusiness = null; + + + private readonly ILogger _logger2; + private IBaseMaterialService? baseMaterialService; + private ILogoIdentifyService? ocrVerfiyService; + private ILogoConfigService? logoConfigService; + private TcpServer? tcpServer; + private PlcPool plcPool = null; + + public IndexViewModel() { logoIdentifyService = App.ServiceProvider.GetService(); @@ -33,7 +51,14 @@ namespace SlnMesnac.WPF.ViewModel LogoBusiness.RefreshBoxInfoEvent += RefreshBoxInfo; ResetCommand = new RelayCommand(Reset); RefreshDataGrid(); - + + plcPool = App.ServiceProvider.GetService(); + tcpServer = App.ServiceProvider.GetService(); + ocrVerfiyService = App.ServiceProvider.GetService(); + baseMaterialService = App.ServiceProvider.GetService(); + logoConfigService = App.ServiceProvider.GetService(); + _logger2 = App.ServiceProvider.GetService>(); + logoBusiness = LogoBusiness.GetInstance(_logger2, logoConfigService, baseMaterialService, ocrVerfiyService, plcPool, tcpServer); } /// @@ -43,10 +68,7 @@ namespace SlnMesnac.WPF.ViewModel /// private void Reset() { - // TODO 复位PLC放行 - - // 复位报警灯 - gunHelper.SendData("OK"); + logoBusiness.Pass(); } ///