using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.Helper; using BPASmartClient.Message; using BPASmartClient.Modbus; using FryPot_DosingSystem.Model; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace FryPot_DosingSystem.Control { internal class DeviceOperate { private static DeviceOperate _instance; public static DeviceOperate GetInstance => _instance ??= new DeviceOperate(); public bool IsConfig { get; set; }//设备plc数据是否配置 ModbusTcp modbus = new ModbusTcp();//滚筒线Modbus通讯对象 ModbusTcp fryOneModbus = new ModbusTcp();//炒锅1Modbus通讯对象 ModbusTcp fryTwoModbus = new ModbusTcp();//炒锅2Modbus通讯对象 ModbusTcp fryThreeModbus = new ModbusTcp();//炒锅3Modbus通讯对象 ModbusTcp fryFourModbus = new ModbusTcp();//炒锅4Modbus通讯对象 ModbusTcp fryFiveModbus = new ModbusTcp();//炒锅5Modbus通讯对象 // private string Ip { get; set; } // private string Port { get; set; } public bool Connected { get; set; } public bool FryOneConnected { get; set; } public bool FryTwoConnected { get; set; } public bool FryThreeConnected { get; set; } public bool FryFourConnected { get; set; } public bool FryFiveConnected { get; set; } // private string DeviceName { get; set; } public ConcurrentDictionary Data { get; set; } = new ConcurrentDictionary(); public ConcurrentDictionary FryOneData { get; set; } = new ConcurrentDictionary(); public ConcurrentDictionary FryTwoData { get; set; } = new ConcurrentDictionary(); public ConcurrentDictionary FryThreeData { get; set; } = new ConcurrentDictionary(); public ConcurrentDictionary FryFourData { get; set; } = new ConcurrentDictionary(); public ConcurrentDictionary FryFiveData { get; set; } = new ConcurrentDictionary(); public ObservableCollection Variables { get; set; } = new ObservableCollection(); public ObservableCollection FryOneVariables { get; set; } = new ObservableCollection(); public ObservableCollection FryTwoVariables { get; set; } = new ObservableCollection(); public ObservableCollection FryThreeVariables { get; set; } = new ObservableCollection(); public ObservableCollection FryFourVariables { get; set; } = new ObservableCollection(); public ObservableCollection FryFiveVariables { get; set; } = new ObservableCollection(); public DeviceOperate() { Init(); Connect(); ReadData(); } public void Init() { Variables.Clear(); FryOneVariables.Clear(); FryTwoVariables.Clear(); FryThreeVariables.Clear(); FryFourVariables.Clear(); FryFiveVariables.Clear(); Json.Read(); if (Json.Data.VariablesInfo.Count > 0) { try { if (Json.Data.VariablesInfo["滚筒输送线"].Count>0) { //foreach (var item in Json.Data.VariablesInfo["滚筒运输线"]) //{ // Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) }); //} foreach (KeyValuePair> dic in Json.Data.VariablesInfo) { if (string.Equals(dic.Key, "滚筒输送线")) { foreach (var item in dic.Value) { Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) }); } } if (string.Equals(dic.Key, "炒锅1")) { foreach (var item in dic.Value) { FryOneVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) }); } } if (string.Equals(dic.Key, "炒锅2")) { foreach (var item in dic.Value) { FryTwoVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) }); } } if (string.Equals(dic.Key, "炒锅3")) { foreach (var item in dic.Value) { FryThreeVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) }); } } if (string.Equals(dic.Key, "炒锅4")) { foreach (var item in dic.Value) { FryFourVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) }); } } if (string.Equals(dic.Key, "炒锅5")) { foreach (var item in dic.Value) { FryFiveVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) }); } } } } IsConfig = true; } catch (Exception) { IsConfig = false; //throw; } } else { IsConfig = false; } //Variables.Add(new PlcVariableModel() { Address = "D2001", Length = 8 });//1号线体滚筒工位号 //Variables.Add(new PlcVariableModel() { Address = "D2011", Length = 8 });//2号线体滚筒工位号 //Variables.Add(new PlcVariableModel() { Address = "D2021", Length = 8 });//3号线体滚筒工位号 //Variables.Add(new PlcVariableModel() { Address = "D2031", Length = 9 });//输送线出料状态 //Variables.Add(new PlcVariableModel() { Address = "D2040", Length = 5 });//炒锅1-5进料滚筒运行 //Variables.Add(new PlcVariableModel() { Address = "D2045", Length = 5 });//炒锅1-5进料到位信号 //Variables.Add(new PlcVariableModel() { Address = "D2050", Length = 5 });//炒锅1-5空桶到位信号 //Variables.Add(new PlcVariableModel() { Address = "D2055", Length = 5 });//炒锅1-5空桶呼叫AGV //Variables.Add(new PlcVariableModel() { Address = "D2060", Length = 5 });//炒锅1空桶洗桶呼叫AGV //Variables.Add(new PlcVariableModel() { Address = "D2065", Length = 5 });//炒锅1-5空桶滚筒运行 //Variables.Add(new PlcVariableModel() { Address = "D2070", Length = 5 });//炒锅1-5滚筒故障信号 //Variables.Add(new PlcVariableModel() { Address = "D2075", Length = 1 });//洗桶进桶滚筒运行信号 //Variables.Add(new PlcVariableModel() { Address = "D2076", Length = 1 });//洗桶出桶呼叫AGV //Variables.Add(new PlcVariableModel() { Address = "D2077", Length = 1 });// 洗桶出桶滚筒运行信号 //Variables.Add(new PlcVariableModel() { Address = "D2078", Length = 3 });//1-3滚筒线体配方完成信号 } public void Connect() { if (IsConfig) { Json.Read(); DeviceManage devices = Json.Data; if (devices != null) { if (devices.Devices.Count > 0) { for (int i = 0; i < devices.Devices.Count; i++) { string Ip = devices.Devices[i].Ip; string Port = devices.Devices[i].Port; string DeviceName = devices.Devices[i].DeviceName; switch (DeviceName) { case "滚筒输送线": Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break; case "炒锅1": Task.Run(() => { fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break; case "炒锅2": Task.Run(() => { fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break; case "炒锅3": Task.Run(() => { fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break; case "炒锅4": Task.Run(() => { fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break; case "炒锅5": Task.Run(() => { fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break; } } // Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); App.Current.Dispatcher.Invoke(new Action(() => { BPASmartClient.CustomResource.Pages.Model.MessageLog.GetInstance.RunLog("PLC连接成功"); })); }); } } } } public void ReadData() { if (IsConfig) { ThreadManage.GetInstance().StartLong(new Action(() => { //滚筒线 Connected = modbus.Connected; while (Connected) { foreach (var item in Variables) { var res = modbus.Read(item.Address, item.Length);//读取plc数据 if (Data.ContainsKey(item.Address)) { Data[item.Address] = res; } else { Data.TryAdd(item.Address, res); } } Thread.Sleep(50); } //炒锅1 FryOneConnected = fryOneModbus.Connected; while (FryOneConnected) { foreach (var item in FryOneVariables) { var res = fryOneModbus.Read(item.Address, item.Length);//读取plc数据 if (FryOneData.ContainsKey(item.Address)) { FryOneData[item.Address] = res; } else { FryOneData.TryAdd(item.Address, res); } } Thread.Sleep(50); } //炒锅2 FryTwoConnected = fryTwoModbus.Connected; while (FryTwoConnected) { foreach (var item in FryTwoVariables) { var res = fryTwoModbus.Read(item.Address, item.Length);//读取plc数据 if (FryTwoData.ContainsKey(item.Address)) { FryTwoData[item.Address] = res; } else { FryTwoData.TryAdd(item.Address, res); } } Thread.Sleep(50); } //炒锅3 FryThreeConnected = fryThreeModbus.Connected; while (FryThreeConnected) { foreach (var item in FryThreeVariables) { var res = fryThreeModbus.Read(item.Address, item.Length);//读取plc数据 if (FryThreeData.ContainsKey(item.Address)) { FryThreeData[item.Address] = res; } else { FryThreeData.TryAdd(item.Address, res); } } Thread.Sleep(50); } //炒锅4 FryFourConnected = fryFourModbus.Connected; while (FryFourConnected) { foreach (var item in FryFourVariables) { var res = fryFourModbus.Read(item.Address, item.Length);//读取plc数据 if (FryFourData.ContainsKey(item.Address)) { FryFourData[item.Address] = res; } else { FryFourData.TryAdd(item.Address, res); } } Thread.Sleep(50); } //炒锅5 FryFiveConnected = fryFiveModbus.Connected; while (FryFiveConnected) { foreach (var item in FryFiveVariables) { var res = fryFiveModbus.Read(item.Address, item.Length);//读取plc数据 if (FryFiveData.ContainsKey(item.Address)) { FryFiveData[item.Address] = res; } else { FryFiveData.TryAdd(item.Address, res); } } Thread.Sleep(50); } Thread.Sleep(500); }), $"PLC实时数据读取线程"); } } public void WritePlcData(string address, ushort value) { lock (this) { modbus.Write(address, value); } } public ConcurrentDictionary GetAllData() { return Data; } public ConcurrentDictionary GetFryOneData() { return FryOneData; } public ConcurrentDictionary GetFryTwoData() { return FryTwoData; } public ConcurrentDictionary GetFryThreeData() { return FryThreeData; } public ConcurrentDictionary GetFryFourData() { return FryFourData; } public ConcurrentDictionary GetFryFiveData() { return FryFiveData; } } }