From 0dba9ef18733828d7fec14803b78c5c687702c3b Mon Sep 17 00:00:00 2001 From: liuwf Date: Thu, 20 Jun 2024 15:51:59 +0800 Subject: [PATCH] =?UTF-8?q?change-=E6=B7=BB=E5=8A=A0=E5=AE=9E=E9=99=85PLC?= =?UTF-8?q?=E7=82=B9=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Business/LogoBusiness.cs | 10 ++-- SlnMesnac.Common/GunHelper.cs | 37 ++++++++------ SlnMesnac.Plc/Factory/InovanceFactory.cs | 38 ++++++++++++++ SlnMesnac.Plc/Factory/MelsecBinaryFactory.cs | 49 +++++++++++++++++++ SlnMesnac.Plc/Factory/OmronNJFactory.cs | 46 +++++++++++++++++ SlnMesnac.Plc/Factory/SiemensFactory.cs | 49 +++++++++++++++++++ SlnMesnac.Plc/PlcAbsractFactory.cs | 21 +++++++- SlnMesnac.WPF/App.xaml.cs | 5 +- SlnMesnac.WPF/ViewModel/IndexViewModel.cs | 3 ++ .../ViewModel/MainWindowViewModel.cs | 9 ++-- SlnMesnac.WPF/appsettings.json | 28 +++++------ 11 files changed, 255 insertions(+), 40 deletions(-) diff --git a/SlnMesnac.Business/LogoBusiness.cs b/SlnMesnac.Business/LogoBusiness.cs index bd675af..ea198ac 100644 --- a/SlnMesnac.Business/LogoBusiness.cs +++ b/SlnMesnac.Business/LogoBusiness.cs @@ -273,7 +273,7 @@ namespace SlnMesnac.Business { if (plc != null && plc.IsConnected) { - plc.writeInt16ByAddress("M100", 8); + plc.writeInt32ByAddress("DB22.DBW2", 8); Task.Run(() => { // 设置计时器 @@ -281,7 +281,7 @@ namespace SlnMesnac.Business stopwatch.Start(); while (true) { - if (plc.readInt16ByAddress("M100") == 0) + if (plc.readInt32ByAddress("DB22.DBW2") == 0) { logger.LogInformation("PLC复位成功,启动线体"); RefreshMessageEvent?.Invoke("PLC复位成功,启动线体", true); @@ -291,6 +291,7 @@ namespace SlnMesnac.Business { logger.LogError("PLC复位超时"); RefreshMessageEvent?.Invoke("PLC复位超时", true); + break; } Thread.Sleep(100); } @@ -318,7 +319,7 @@ namespace SlnMesnac.Business { if (plc != null && plc.IsConnected) { - plc.writeInt16ByAddress("M100", 9); + plc.writeInt32ByAddress("DB22.DBW2", 9); Task.Run(() => { // 设置计时器 @@ -326,7 +327,7 @@ namespace SlnMesnac.Business stopwatch.Start(); while (true) { - if (plc.readInt16ByAddress("M100") == 1) + if (plc.readInt32ByAddress("DB22.DBW2") == 1) { logger.LogInformation("PLC复位成功,启动线体"); RefreshMessageEvent?.Invoke("PLC复位成功,启动线体", true); @@ -336,6 +337,7 @@ namespace SlnMesnac.Business { logger.LogError("PLC复位超时"); RefreshMessageEvent?.Invoke("PLC复位超时", true); + break; } Thread.Sleep(100); } diff --git a/SlnMesnac.Common/GunHelper.cs b/SlnMesnac.Common/GunHelper.cs index 3e7f722..a37a800 100644 --- a/SlnMesnac.Common/GunHelper.cs +++ b/SlnMesnac.Common/GunHelper.cs @@ -109,16 +109,27 @@ namespace SlnMesnac.Common { if (serialPort.IsOpen) { - byte[] buffer = null; + if (data == "NG") { - buffer = GetBytesByCommand("OpenRed"); + byte[] buffer = GetBytesByCommand("CloseRed"); + serialPort.Write(buffer, 0x0, 0x4); + byte[] buffer1 = GetBytesByCommand("OpenRed"); + serialPort.Write(buffer1, 0x0, 0x4); } else if (data == "OK") { - buffer = GetBytesByCommand("CloseRed"); + byte[] buffer = GetBytesByCommand("CloseRed"); + serialPort.Write(buffer, 0x0, 0x4); + byte[] buffer1 = GetBytesByCommand("OpenGreen"); + serialPort.Write(buffer1, 0x0, 0x4); } - serialPort.Write(buffer, 0x0, 0x4); + else if(data == "Exit") + { + byte[] buffer = GetBytesByCommand("CloseRed"); + serialPort.Write(buffer, 0x0, 0x4); + } + } else { @@ -142,18 +153,16 @@ namespace SlnMesnac.Common byte[] buffer = null; switch (command) { - // 打开红灯+蜂鸣 - case "OpenRed": buffer = new byte[] { 0xA0, 0x00, 0x02, 0xA2 }; break; - // 关闭红灯+蜂鸣 + // 打开红灯闪烁+蜂鸣 + case "OpenRed": buffer = new byte[] { 0xA0, 0x07, 0x02, 0xA9 }; break; + // 打开红灯闪烁 + //case "OpenRed": buffer = new byte[] { 0xA0, 0x03, 0x02, 0xA5 }; break; + // 全部关闭 case "CloseRed": buffer = new byte[] { 0xA0, 0x00, 0x00, 0xA0 }; break; - ////闪烁红灯+蜂鸣 - //case "FlashRed": buffer = new byte[] { 0xA0, 0x07, 0x02, 0xA9 }; break; + //// 打开绿灯 - //case "OpenGreen": buffer = new byte[] { 0xA0, 0x00, 0x01, 0xA1 }; break; - //// 关闭绿灯 - //case "CloseGreen": buffer = new byte[] { 0xA0, 0x00, 0x00, 0xA0 }; break; - //// 闪烁绿灯 - //case "FlashGreen": buffer = new byte[] { 0xA0, 0x00, 0x02, 0xA2 }; break; + case "OpenGreen": buffer = new byte[] { 0xA0, 0x02, 0x01, 0xA3 }; break; + default:return null; } return buffer; diff --git a/SlnMesnac.Plc/Factory/InovanceFactory.cs b/SlnMesnac.Plc/Factory/InovanceFactory.cs index 7cf57a7..b8a8e9e 100644 --- a/SlnMesnac.Plc/Factory/InovanceFactory.cs +++ b/SlnMesnac.Plc/Factory/InovanceFactory.cs @@ -130,6 +130,30 @@ namespace SlnMesnac.Plc.Factory } } + /// + /// 根据地址读取int32数据 + /// + /// + /// + /// + public override int readInt32ByAddress(string address) + { + try + { + OperateResult read = inovanceTcp.ReadInt32(address); + if (!read.IsSuccess) + { + throw new InvalidOperationException($"根据地址:{address};读取int32数据失败:{read.Content}"); + } + return read.Content; + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据地址:{address};读取int32数据异常:{ex.Message}"); + } + } + + /// /// 根据地址写入int16数据 /// @@ -203,6 +227,20 @@ namespace SlnMesnac.Plc.Factory } } + /// + /// 根据地址写入int16数据 + /// + /// + /// + /// + /// + public override bool writeInt32ByAddress(string address, int value) + { + + throw new NotImplementedException("未实现异常"); + + } + /// /// 通过PLC地址读取string类型数据 /// diff --git a/SlnMesnac.Plc/Factory/MelsecBinaryFactory.cs b/SlnMesnac.Plc/Factory/MelsecBinaryFactory.cs index 65ef7c1..5176900 100644 --- a/SlnMesnac.Plc/Factory/MelsecBinaryFactory.cs +++ b/SlnMesnac.Plc/Factory/MelsecBinaryFactory.cs @@ -119,6 +119,30 @@ namespace SlnMesnac.Plc.Factory } } + /// + /// 根据地址读取int32数据 + /// + /// + /// + /// + public override int readInt32ByAddress(string address) + { + try + { + OperateResult read = melsec_net.ReadInt32(address); + if (!read.IsSuccess) + { + throw new InvalidOperationException($"根据地址:{address};读取int16数据失败:{read.Content}"); + } + return read.Content; + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据地址:{address};读取int16数据异常:{ex.Message}"); + } + } + + /// /// 根据地址写入int16数据 /// @@ -143,6 +167,31 @@ namespace SlnMesnac.Plc.Factory } } + /// + /// 根据地址写入int16数据 + /// + /// + /// + /// + /// + public override bool writeInt32ByAddress(string address, int value) + { + try + { + OperateResult operateResult = melsec_net.Write(address, value); + if (!operateResult.IsSuccess) + { + throw new InvalidOperationException($"根据地址:{address};写入int32数据失败:{operateResult.Message}"); + } + return operateResult.IsSuccess; + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据地址:{address};写入int32数据异常:{ex.Message}"); + } + } + + /// /// 通过PLC地址读取string类型数据 /// diff --git a/SlnMesnac.Plc/Factory/OmronNJFactory.cs b/SlnMesnac.Plc/Factory/OmronNJFactory.cs index 10d5991..73a0845 100644 --- a/SlnMesnac.Plc/Factory/OmronNJFactory.cs +++ b/SlnMesnac.Plc/Factory/OmronNJFactory.cs @@ -122,6 +122,29 @@ namespace SlnMesnac.Plc.Factory } } + /// + /// 根据地址读取int32数据 + /// + /// + /// + /// + public override int readInt32ByAddress(string address) + { + try + { + OperateResult read = omronFinsNet.ReadInt32(address); + if (!read.IsSuccess) + { + throw new InvalidOperationException($"根据地址:{address};读取int32数据失败:{read.Content}"); + } + return read.Content; + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据地址:{address};读取int32数据异常:{ex.Message}"); + } + } + /// /// 根据地址写入int16数据 /// @@ -146,6 +169,29 @@ namespace SlnMesnac.Plc.Factory } } + /// + /// 根据地址写入int32数据 + /// + /// + /// + /// + /// + public override bool writeInt32ByAddress(string address, int value) + { + try + { + OperateResult operateResult = omronFinsNet.Write(address, value); + if (!operateResult.IsSuccess) + { + throw new InvalidOperationException($"根据地址:{address};写入int32数据失败:{operateResult.Message}"); + } + return operateResult.IsSuccess; + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据地址:{address};写入int32数据异常:{ex.Message}"); + } + } /// /// 通过PLC地址读取string类型数据 /// diff --git a/SlnMesnac.Plc/Factory/SiemensFactory.cs b/SlnMesnac.Plc/Factory/SiemensFactory.cs index 5807ec2..e81c38a 100644 --- a/SlnMesnac.Plc/Factory/SiemensFactory.cs +++ b/SlnMesnac.Plc/Factory/SiemensFactory.cs @@ -120,6 +120,29 @@ namespace SlnMesnac.Plc.Factory } } + /// + /// 根据地址读取int32数据 + /// + /// + /// + /// + public override int readInt32ByAddress(string address) + { + try + { + OperateResult read = s7.ReadInt32(address); + if (!read.IsSuccess) + { + throw new InvalidOperationException($"根据地址:{address};读取int32数据失败:{read.Content}"); + } + return read.Content; + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据地址:{address};读取int32数据异常:{ex.Message}"); + } + } + /// /// 根据地址写入int16数据 /// @@ -144,6 +167,32 @@ namespace SlnMesnac.Plc.Factory } } + + /// + /// 根据地址写入int32数据 + /// + /// + /// + /// + /// + public override bool writeInt32ByAddress(string address, int value) + { + try + { + OperateResult operateResult = s7.Write(address, value); + if (!operateResult.IsSuccess) + { + throw new InvalidOperationException($"根据地址:{address};写入int32数据失败:{operateResult.Message}"); + } + return operateResult.IsSuccess; + } + catch (Exception ex) + { + throw new InvalidOperationException($"根据地址:{address};写入int32数据异常:{ex.Message}"); + } + } + + /// /// 通过PLC地址读取string类型数据 /// diff --git a/SlnMesnac.Plc/PlcAbsractFactory.cs b/SlnMesnac.Plc/PlcAbsractFactory.cs index b5e59d2..39060b6 100644 --- a/SlnMesnac.Plc/PlcAbsractFactory.cs +++ b/SlnMesnac.Plc/PlcAbsractFactory.cs @@ -35,7 +35,16 @@ /// /// public abstract int readInt16ByAddress(string address); - + + + /// + /// 通过PLC地址读取int32类型数据 + /// + /// + /// + public abstract int readInt32ByAddress(string address); + + /// /// 通过PLC地址写入int16类型数据 /// @@ -43,7 +52,15 @@ /// /// public abstract bool writeInt16ByAddress(string address,int value); - + + /// + /// 通过PLC地址写入int32类型数据 + /// + /// + /// + /// + public abstract bool writeInt32ByAddress(string address, int value); + /// /// 通过PLC地址读取string类型数据 /// diff --git a/SlnMesnac.WPF/App.xaml.cs b/SlnMesnac.WPF/App.xaml.cs index 78c0506..e9d1775 100644 --- a/SlnMesnac.WPF/App.xaml.cs +++ b/SlnMesnac.WPF/App.xaml.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; +using SlnMesnac.Common; using SlnMesnac.Config; using System; using System.Collections.Generic; @@ -23,7 +24,7 @@ namespace SlnMesnac.WPF private System.Threading.Mutex mutex; private LierdaCracker cracker = new LierdaCracker(); public static IServiceProvider ServiceProvider; - + private GunHelper gunHelper = GunHelper.Instance; // Startup事件 protected override async void OnStartup(StartupEventArgs e) { @@ -71,6 +72,8 @@ namespace SlnMesnac.WPF Log.Information($"系统退出,当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); // 释放资源 // ... + // 关闭指示灯 + gunHelper.SendData("Exit"); } diff --git a/SlnMesnac.WPF/ViewModel/IndexViewModel.cs b/SlnMesnac.WPF/ViewModel/IndexViewModel.cs index d9a37a8..bb58b76 100644 --- a/SlnMesnac.WPF/ViewModel/IndexViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/IndexViewModel.cs @@ -59,6 +59,9 @@ namespace SlnMesnac.WPF.ViewModel logoConfigService = App.ServiceProvider.GetService(); _logger2 = App.ServiceProvider.GetService>(); logoBusiness = LogoBusiness.GetInstance(_logger2, logoConfigService, baseMaterialService, ocrVerfiyService, plcPool, tcpServer); + + //程序启动,打开绿色指示灯 + gunHelper.SendData("OK"); } /// diff --git a/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs b/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs index a6cd1fa..b789fea 100644 --- a/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/MainWindowViewModel.cs @@ -49,7 +49,7 @@ namespace SlnMesnac.WPF.ViewModel private StatisticsPageView statisticsPageView = new StatisticsPageView(); private ConfigPage configPage = new ConfigPage(); private DebugConfig config = DebugConfig.Instance; - + private GunHelper gunHelper = GunHelper.Instance; public delegate void RefreDataGrid(); public static event RefreDataGrid RefreDataGridEvent; public MainWindowViewModel() @@ -68,8 +68,8 @@ namespace SlnMesnac.WPF.ViewModel FormControlCommand = new RelayCommand(x => FormControl(x)); TcpServer.RefreshStateEvent += RefreshScanner; - - + + //PLC状态刷新 StartState(); RefreshTime(); @@ -166,7 +166,6 @@ namespace SlnMesnac.WPF.ViewModel #endregion - public void StartState() @@ -227,7 +226,7 @@ namespace SlnMesnac.WPF.ViewModel // 关闭当前窗口 case "Exit": { - + gunHelper.SendData("Exit"); Environment.Exit(0); } diff --git a/SlnMesnac.WPF/appsettings.json b/SlnMesnac.WPF/appsettings.json index f3b0738..6c9f23c 100644 --- a/SlnMesnac.WPF/appsettings.json +++ b/SlnMesnac.WPF/appsettings.json @@ -26,7 +26,7 @@ "configId": 1, "plcType": "SiemensPlc", "plcIp": "127.0.0.1", - "plcPort": 6000, + "plcPort": 102, "plcKey": "plc", "isFlage": true }, @@ -52,20 +52,20 @@ "equipIp": "127.0.0.1", "equipPort": 6009, "equipKey": "test2", - "isFlage": true + "isFlage": false } ], - "ScannerConfig": [ - { - "Id": 1, - "Ip": "10.10.92.142", - "Name": "test1" - }, - { - "Id": 2, - "Ip": "10.10.92.143", - "Name": "test2" - } - ] + //"ScannerConfig": [ + // { + // "Id": 1, + // "Ip": "10.10.92.142", + // "Name": "test1" + // }, + // { + // "Id": 2, + // "Ip": "10.10.92.143", + // "Name": "test2" + // } + //] } }