|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
-
- 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<string, object> Data { get; set; } = new ConcurrentDictionary<string, object>();
- public ConcurrentDictionary<string, object> FryOneData { get; set; } = new ConcurrentDictionary<string, object>();
- public ConcurrentDictionary<string, object> FryTwoData { get; set; } = new ConcurrentDictionary<string, object>();
- public ConcurrentDictionary<string, object> FryThreeData { get; set; } = new ConcurrentDictionary<string, object>();
- public ConcurrentDictionary<string, object> FryFourData { get; set; } = new ConcurrentDictionary<string, object>();
- public ConcurrentDictionary<string, object> FryFiveData { get; set; } = new ConcurrentDictionary<string, object>();
- public ObservableCollection<PlcVariableModel> Variables { get; set; } = new ObservableCollection<PlcVariableModel>();
- public ObservableCollection<PlcVariableModel> FryOneVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
- public ObservableCollection<PlcVariableModel> FryTwoVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
- public ObservableCollection<PlcVariableModel> FryThreeVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
- public ObservableCollection<PlcVariableModel> FryFourVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
- public ObservableCollection<PlcVariableModel> FryFiveVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
- public DeviceOperate()
- {
- Init();
- Connect();
- ReadData();
- }
- public void Init()
- {
- Variables.Clear();
- FryOneVariables.Clear();
- FryTwoVariables.Clear();
- FryThreeVariables.Clear();
- FryFourVariables.Clear();
- FryFiveVariables.Clear();
-
- Json<PlcVariableInfoManage>.Read();
- if (Json<PlcVariableInfoManage>.Data.VariablesInfo.Count > 0)
- {
- try
- {
- if (Json<PlcVariableInfoManage>.Data.VariablesInfo["滚筒输送线"].Count>0)
- {
- //foreach (var item in Json<PlcVariableInfoManage>.Data.VariablesInfo["滚筒运输线"])
- //{
- // Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
- //}
- foreach (KeyValuePair<string,ObservableCollection<PlcVariableInfoModel>> dic in Json<PlcVariableInfoManage>.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<DeviceManage>.Read();
- DeviceManage devices = Json<DeviceManage>.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<string, object> GetAllData()
- {
- return Data;
- }
- public ConcurrentDictionary<string, object> GetFryOneData()
- {
- return FryOneData;
- }
- public ConcurrentDictionary<string, object> GetFryTwoData()
- {
- return FryTwoData;
- }
- public ConcurrentDictionary<string, object> GetFryThreeData()
- {
- return FryThreeData;
- }
- public ConcurrentDictionary<string, object> GetFryFourData()
- {
- return FryFourData;
- }
- public ConcurrentDictionary<string, object> GetFryFiveData()
- {
- return FryFiveData;
- }
- }
- }
|