diff --git a/FryPot_DosingSystem/App.xaml.cs b/FryPot_DosingSystem/App.xaml.cs index 78dfaeb9..741c67dc 100644 --- a/FryPot_DosingSystem/App.xaml.cs +++ b/FryPot_DosingSystem/App.xaml.cs @@ -39,7 +39,7 @@ namespace FryPot_DosingSystem { mv.Show(); MessageLog.GetInstance.ShowUserLog("用户登录"); - DeviceOperate deviceOperate = DeviceOperate.GetInstance;//开启实时PLC数据读取 + Task.Run(new Action(() => { DeviceOperate deviceOperate = DeviceOperate.GetInstance; }));//开启实时PLC数据读取 DosingLogicControl logigControl = DosingLogicControl.GetInstance;//开启逻辑控制任务程序 HubHelper.GetInstance.Connect("192.168.1.20", 8089); AlarmHelper.Init();//报警实时监控 diff --git a/FryPot_DosingSystem/Control/DeviceOperate.cs b/FryPot_DosingSystem/Control/DeviceOperate.cs index 0b97b402..4bd3edf6 100644 --- a/FryPot_DosingSystem/Control/DeviceOperate.cs +++ b/FryPot_DosingSystem/Control/DeviceOperate.cs @@ -18,7 +18,7 @@ namespace FryPot_DosingSystem.Control internal class DeviceOperate { private static DeviceOperate _instance; - public static DeviceOperate GetInstance => _instance ??= new DeviceOperate(); + public static DeviceOperate GetInstance => _instance ??(_instance= new DeviceOperate()); public bool IsConfig { get; set; }//设备plc数据是否配置 ModbusTcp modbus = new ModbusTcp();//滚筒线Modbus通讯对象 ModbusTcp fryOneModbus = new ModbusTcp();//炒锅1Modbus通讯对象 @@ -54,7 +54,15 @@ namespace FryPot_DosingSystem.Control Init(); Connect(); ReadData(); + ConnectStatusMonitor(); } + + private void ConnectStatusMonitor() + { + + + } + public void Init() { Variables.Clear(); @@ -162,23 +170,32 @@ namespace FryPot_DosingSystem.Control { if (devices.Devices.Count > 0) { - for (int i = 0; i < devices.Devices.Count; i++) + + try { - string Ip = devices.Devices[i].Ip; - string Port = devices.Devices[i].Port; - string DeviceName = devices.Devices[i].DeviceName; - switch (DeviceName) + for (int i = 0; i < devices.Devices.Count; i++) { - case "滚筒输送线": Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("滚筒线PLC连接成功"); }); break; - case "炒锅1": Task.Run(() => { fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("1号炒锅PLC连接成功"); }); break; - case "炒锅2": Task.Run(() => { fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("2号炒锅PLC连接成功"); }); break; - case "炒锅3": Task.Run(() => { fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("3号炒锅PLC连接成功"); }); break; - case "炒锅4": Task.Run(() => { fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("4号炒锅PLC连接成功"); }); break; - case "炒锅5": Task.Run(() => { fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("5号炒锅PLC连接成功"); }); break; + string Ip = devices.Devices[i].Ip; + string Port = devices.Devices[i].Port; + string DeviceName = devices.Devices[i].DeviceName; + switch (DeviceName) + { + case "滚筒输送线": modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("滚筒线PLC连接成功"); break; + case "炒锅1": fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("1号炒锅PLC连接成功"); break; + case "炒锅2": fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("2号炒锅PLC连接成功"); break; + case "炒锅3": fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("3号炒锅PLC连接成功"); break; + case "炒锅4": fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("4号炒锅PLC连接成功"); break; + case "炒锅5": fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("5号炒锅PLC连接成功"); break; + } + } + } + catch (Exception) + { + MessageLog.GetInstance.ShowRunLog("PLC连接失败"); } - // Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); App.Current.Dispatcher.Invoke(new Action(() => { BPASmartClient.CustomResource.Pages.Model.MessageLog.GetInstance.ShowRunLog("PLC连接成功"); })); }); + } } } @@ -191,7 +208,7 @@ namespace FryPot_DosingSystem.Control { //滚筒线 Connected = modbus.Connected; - while (Connected) + if (Connected) { foreach (var item in Variables) { @@ -205,16 +222,27 @@ namespace FryPot_DosingSystem.Control Data.TryAdd(item.Address, res); } } - Thread.Sleep(50); + + } + else + { + DeviceManage devices = Json.Data; + var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "滚筒输送线"); + if (res != null) + modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageLog.GetInstance.ShowRunLog("滚筒线PLC重连成功"); } Thread.Sleep(10); }), $"滚筒线实时数据读取线程"); + + + + ThreadManage.GetInstance().StartLong(new Action(() => { //炒锅1 FryOneConnected = fryOneModbus.Connected; - while (FryOneConnected) + if (FryOneConnected) { foreach (var item in FryOneVariables) { @@ -228,16 +256,23 @@ namespace FryPot_DosingSystem.Control FryOneData.TryAdd(item.Address, res); } } - Thread.Sleep(50); + //Thread.Sleep(50); } + else + { + DeviceManage devices = Json.Data; + var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅1"); + if (res != null) + modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageLog.GetInstance.ShowRunLog("1号炒锅PLC重连成功"); + } Thread.Sleep(10); }), $"炒锅1实时数据读取线程"); ThreadManage.GetInstance().StartLong(new Action(() => { //炒锅2 FryTwoConnected = fryTwoModbus.Connected; - while (FryTwoConnected) + if (FryTwoConnected) { foreach (var item in FryTwoVariables) { @@ -251,16 +286,23 @@ namespace FryPot_DosingSystem.Control FryTwoData.TryAdd(item.Address, res); } } - Thread.Sleep(50); + + } + else + { + DeviceManage devices = Json.Data; + var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅2"); + if (res != null) + modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageLog.GetInstance.ShowRunLog("2号炒锅PLC重连成功"); } Thread.Sleep(10); }), $"炒锅2实时数据读取线程"); ThreadManage.GetInstance().StartLong(new Action(() => { - //炒锅3 + //炒锅3 FryThreeConnected = fryThreeModbus.Connected; - while (FryThreeConnected) + if (FryThreeConnected) { foreach (var item in FryThreeVariables) { @@ -274,16 +316,23 @@ namespace FryPot_DosingSystem.Control FryThreeData.TryAdd(item.Address, res); } } - Thread.Sleep(50); + //Thread.Sleep(50); } + else + { + DeviceManage devices = Json.Data; + var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅3"); + if (res != null) + modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageLog.GetInstance.ShowRunLog("3号炒锅PLC重连成功"); + } Thread.Sleep(10); }), $"炒锅3实时数据读取线程"); ThreadManage.GetInstance().StartLong(new Action(() => { - //炒锅4 + //炒锅4 FryFourConnected = fryFourModbus.Connected; - while (FryFourConnected) + if (FryFourConnected) { foreach (var item in FryFourVariables) { @@ -297,16 +346,23 @@ namespace FryPot_DosingSystem.Control FryFourData.TryAdd(item.Address, res); } } - Thread.Sleep(50); + //Thread.Sleep(50); } + else + { + DeviceManage devices = Json.Data; + var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅4"); + if (res != null) + modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageLog.GetInstance.ShowRunLog("4号炒锅PLC重连成功"); + } Thread.Sleep(10); }), $"炒锅4实时数据读取线程"); ThreadManage.GetInstance().StartLong(new Action(() => { - //炒锅5 + //炒锅5 FryFiveConnected = fryFiveModbus.Connected; - while (FryFiveConnected) + if (FryFiveConnected) { foreach (var item in FryFiveVariables) { @@ -320,9 +376,16 @@ namespace FryPot_DosingSystem.Control FryFiveData.TryAdd(item.Address, res); } } - Thread.Sleep(50); + // Thread.Sleep(50); } + else + { + DeviceManage devices = Json.Data; + var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅5"); + if (res != null) + modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageLog.GetInstance.ShowRunLog("5号炒锅PLC重连成功"); + } Thread.Sleep(10); }), $"炒锅5实时数据读取线程"); } diff --git a/FryPot_DosingSystem/ViewModel/MainViewModel.cs b/FryPot_DosingSystem/ViewModel/MainViewModel.cs index 517a2094..cc9b417a 100644 --- a/FryPot_DosingSystem/ViewModel/MainViewModel.cs +++ b/FryPot_DosingSystem/ViewModel/MainViewModel.cs @@ -88,7 +88,7 @@ namespace FryPot_DosingSystem.ViewModel public MainViewModel() { LogViewModel model = LogViewModel.GetInstance; - DeviceOperate deviceOperate = DeviceOperate.GetInstance;//开启实时PLC数据读取 + // DeviceOperate deviceOperate = DeviceOperate.GetInstance;//开启实时PLC数据读取 DosingLogicControl logigControl = DosingLogicControl.GetInstance;//开启逻辑控制任务程序 TogglePag = new RelayCommand(DoNavChanged); Login = new RelayCommand(() =>