From e8a0b342996ef64df6f23e710ceee0f1c87dc058 Mon Sep 17 00:00:00 2001 From: liuwf Date: Tue, 14 Nov 2023 16:59:46 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E6=9D=A1=E7=A0=81=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E6=89=AB=E7=A0=81=E5=99=A8=E8=AE=BE=E5=A4=87=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/MainWindowViewModel.cs | 159 +++++++++++++++++- Aucma.Core.CodeBinding/Views/MainWindow.xaml | 8 +- Aucma.Core.PLc/Impl/MelsecPlc.cs | 5 +- Aucma.Core.RunPlc/RunPlcService.cs | 24 +-- Aucma.Core.Scanner/Helper/MvCodeHelper.cs | 11 +- 5 files changed, 180 insertions(+), 27 deletions(-) diff --git a/Aucma.Core.CodeBinding/ViewModels/MainWindowViewModel.cs b/Aucma.Core.CodeBinding/ViewModels/MainWindowViewModel.cs index 75a4acb0..efffd75d 100644 --- a/Aucma.Core.CodeBinding/ViewModels/MainWindowViewModel.cs +++ b/Aucma.Core.CodeBinding/ViewModels/MainWindowViewModel.cs @@ -10,6 +10,8 @@ using NPOI.HSSF.Record; using System.Threading.Tasks; using Aucma.Core.PLc; using System.Linq; +using Admin.Core.Common; +using Aucma.Core.Scanner; namespace Aucma.Core.CodeBinding.ViewModels { @@ -30,26 +32,40 @@ namespace Aucma.Core.CodeBinding.ViewModels public MainWindowViewModel() { UserContent = firstPage; - PlcState(false); + PlcState(true); + Scanner1State(true); + Scanner2State(true); init(); } public void init() { - // plc状态刷新定时器 + // 设备状态刷新定时器 System.Timers.Timer timer = new System.Timers.Timer(1000 * 5); - timer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshPlcStatus); + timer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshStatus); timer.AutoReset = true; timer.Enabled = true; timer.Start(); } + #region 设备状态刷新 + /// + /// 设备状态刷新 + /// + /// + /// + public void RefreshStatus(object sender, System.Timers.ElapsedEventArgs e) + { + RefreshPlc(); + RefreshScanner(); + } + /// /// plc状态刷新 /// /// /// - public void RefreshPlcStatus(object sender, System.Timers.ElapsedEventArgs e) + public void RefreshPlc() { var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("条码绑定Plc")); if (obj != null) @@ -67,10 +83,23 @@ namespace Aucma.Core.CodeBinding.ViewModels { PlcState(false); } - } - + /// + /// 扫码器状态刷新 + /// + /// + /// + public void RefreshScanner() + { + string ip1 = Appsettings.app("Middleware", "Scanner1", "Ip"); + string ip2 = Appsettings.app("Middleware", "Scanner2", "Ip"); + bool flag1 = MvCodeHelper.ConnectionStatus(ip1); + bool flag2 = MvCodeHelper.ConnectionStatus(ip2); + Scanner1State(flag1); + Scanner2State(flag2); + } + #endregion #region 更换界面 public System.Windows.Controls.UserControl _content; @@ -80,9 +109,9 @@ namespace Aucma.Core.CodeBinding.ViewModels get => _content; set => SetProperty(ref _content, value); } - #endregion + #region 窗口操作 /// /// 窗口操作 @@ -232,9 +261,125 @@ namespace Aucma.Core.CodeBinding.ViewModels PlcUIIcon = "Assets/Images/Red.png"; } }); + } + #endregion + #region 扫码器1状态 + /// + /// UI 展示状态-文字 + /// + public string _scanner1UIStatusWb; + public string Scanner1UIStatusWb + { + //get { return plcUIStatusWb; } + //set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); } + get => _scanner1UIStatusWb; + set => SetProperty(ref _scanner1UIStatusWb, value); + } + /// + /// UI 展示状态-颜色 + /// + public string _scanner1UIColor; + public string Scanner1UIColor + { + //get { return plcUIColor; } + //set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); } + get => _scanner1UIColor; + set => SetProperty(ref _scanner1UIColor, value); + } + /// + /// UI 展示状态-图标 + /// + public string _scanner1UIIcon; + public string Scanner1UIIcon + { + //get { return plcUIIcon; } + //set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); } + get => _scanner1UIIcon; + set => SetProperty(ref _scanner1UIIcon, value); + } + + /// + /// 扫码器1连接状态-true:连接成功;false:失败 + /// + /// + public void Scanner1State(bool type) + { + Application.Current.Dispatcher.Invoke(() => + { + if (type) + { + Scanner1UIStatusWb = "扫码器1连接成功"; + Scanner1UIColor = "Green"; + Scanner1UIIcon = "Assets/Images/Green.png"; + } + else + { + Scanner1UIStatusWb = "扫码器1状态异常"; + Scanner1UIColor = "Red"; + Scanner1UIIcon = "Assets/Images/Red.png"; + } + }); } #endregion + #region 扫码器2状态 + /// + /// UI 展示状态-文字 + /// + public string _scanner2UIStatusWb; + public string Scanner2UIStatusWb + { + //get { return plcUIStatusWb; } + //set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); } + get => _scanner2UIStatusWb; + set => SetProperty(ref _scanner2UIStatusWb, value); + } + /// + /// UI 展示状态-颜色 + /// + public string _scanner2UIColor; + public string Scanner2UIColor + { + //get { return plcUIColor; } + //set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); } + get => _scanner2UIColor; + set => SetProperty(ref _scanner2UIColor, value); + } + /// + /// UI 展示状态-图标 + /// + public string _scanner2UIIcon; + public string Scanner2UIIcon + { + //get { return plcUIIcon; } + //set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); } + get => _scanner2UIIcon; + set => SetProperty(ref _scanner2UIIcon, value); + } + + /// + /// 扫码器2连接状态-true:连接成功;false:失败 + /// + /// + public void Scanner2State(bool type) + { + Application.Current.Dispatcher.Invoke(() => + { + if (type) + { + Scanner2UIStatusWb = "扫码器2连接成功"; + Scanner2UIColor = "Green"; + Scanner2UIIcon = "Assets/Images/Green.png"; + } + else + { + Scanner2UIStatusWb = "扫码器2状态异常"; + Scanner2UIColor = "Red"; + Scanner2UIIcon = "Assets/Images/Red.png"; + } + }); + } + #endregion } } diff --git a/Aucma.Core.CodeBinding/Views/MainWindow.xaml b/Aucma.Core.CodeBinding/Views/MainWindow.xaml index 1a8da6ff..cbfd6352 100644 --- a/Aucma.Core.CodeBinding/Views/MainWindow.xaml +++ b/Aucma.Core.CodeBinding/Views/MainWindow.xaml @@ -77,16 +77,16 @@ - + - + - + - + diff --git a/Aucma.Core.PLc/Impl/MelsecPlc.cs b/Aucma.Core.PLc/Impl/MelsecPlc.cs index bb8f22e8..c5299861 100644 --- a/Aucma.Core.PLc/Impl/MelsecPlc.cs +++ b/Aucma.Core.PLc/Impl/MelsecPlc.cs @@ -80,6 +80,8 @@ namespace Aucma.Core.PLc //melsecMcNet.GetPipeSocket().SetMultiPorts(new int[] { 6000, 6001 }); try { + // 先关闭再重连,防止多次重连 + melsecMcNet.ConnectClose(); OperateResult connect = melsecMcNet.ConnectServer(); if (connect.IsSuccess) { @@ -444,7 +446,8 @@ namespace Aucma.Core.PLc try { melsecMcNet.ReceiveTimeOut = 2000; - OperateResult read = await melsecMcNet.ReadBoolAsync(address); + // 2023-11-14变成同步方法,异步心跳导致信号滞后,会重复请求重连 + OperateResult read = melsecMcNet.ReadBool(address); if (read.IsSuccess) { IsConnected = true; diff --git a/Aucma.Core.RunPlc/RunPlcService.cs b/Aucma.Core.RunPlc/RunPlcService.cs index a027d3cf..626ca94c 100644 --- a/Aucma.Core.RunPlc/RunPlcService.cs +++ b/Aucma.Core.RunPlc/RunPlcService.cs @@ -81,18 +81,18 @@ namespace Aucma.Core.RunPlc { item.IsConnect = false; //Console.WriteLine($"{item.EquipName}:PLC连接失败!"); - System.GC.Collect(); - bool r = item.plc.Connect(item.IP, item.Port); - if (r) - { - item.plc.IsConnected = r; - item.IsConnect = r; - } - else - { - item.plc.IsConnected = r; - item.IsConnect = r; - } + // System.GC.Collect(); + bool r = item.plc.Connect(item.IP, item.Port); + if (r) + { + item.plc.IsConnected = r; + item.IsConnect = r; + } + else + { + item.plc.IsConnected = r; + item.IsConnect = r; + } } } } diff --git a/Aucma.Core.Scanner/Helper/MvCodeHelper.cs b/Aucma.Core.Scanner/Helper/MvCodeHelper.cs index 2d7f22c2..7fe8735e 100644 --- a/Aucma.Core.Scanner/Helper/MvCodeHelper.cs +++ b/Aucma.Core.Scanner/Helper/MvCodeHelper.cs @@ -56,7 +56,7 @@ namespace Aucma.Core.Scanner /// 获取不到任务设备即连接失败 /// /// - public bool ConnectionStatus(string ip) + public static bool ConnectionStatus(string ip) { // 遍历所有已打开相机 foreach (KeyValuePair hashmap in m_cMyDevices) @@ -66,11 +66,16 @@ namespace Aucma.Core.Scanner return true; } } + // 没有连接上,重新获取并打开设备 + Task.Run(() => + { + DeviceListAcqAndOpen(); + }); return false; } #endregion - #region 获取设备列表 + #region 获取并打开设备列表 /// /// 获取设备列表 /// @@ -96,7 +101,7 @@ namespace Aucma.Core.Scanner } MvCodeReader m_cMyDevice = new MvCodeReader(); - + //打开所有设备 for (int i = 0; i < m_stDeviceList.nDeviceNum; i++) {