|
-
- using BPASmartClient.CustomResource.Pages.Model;
- using BPA.Helper;
-
- 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 ??(_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;
- MessageNotify.GetInstance.ShowRunLog("PLC变量配置加载成功");
- }
- catch (Exception)
- {
- IsConfig = false;
- MessageNotify.GetInstance.ShowRunLog("PLC变量配置加载错误,请重新配置并重启软件");
- //throw;
- }
- }
- else
- {
- IsConfig = false;
- MessageNotify.GetInstance.ShowRunLog("PLC变量配置加载失败:文件无数据,请重新配置并重启软件");
- }
- //消息注册
- modbus.ShowEx += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryOneModbus.ShowEx += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryTwoModbus.ShowEx += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryThreeModbus.ShowEx += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryFourModbus.ShowEx += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryFiveModbus.ShowEx += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
-
- modbus.Show += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryOneModbus.Show += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryTwoModbus.Show += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryThreeModbus.Show += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryFourModbus.Show += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- fryFiveModbus.Show += new Action<string>((s) =>
- {
- if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
- });
- }
- 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 "滚筒输送线": modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
- case "炒锅1": fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
- case "炒锅2": fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
- case "炒锅3": fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
- case "炒锅4": fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
- case "炒锅5": fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
- }
-
- }
-
- if (modbus.Connected)
- MessageNotify.GetInstance.ShowRunLog("滚筒线PLC连接成功");
- if (fryOneModbus.Connected)
- MessageNotify.GetInstance.ShowRunLog("1号炒锅PLC连接成功");
- if (fryTwoModbus.Connected)
- MessageNotify.GetInstance.ShowRunLog("2号炒锅PLC连接成功");
- if (fryThreeModbus.Connected)
- MessageNotify.GetInstance.ShowRunLog("3号炒锅PLC连接成功");
- if (fryFourModbus.Connected)
- MessageNotify.GetInstance.ShowRunLog("4号炒锅PLC连接成功");
- if (fryFiveModbus.Connected)
- MessageNotify.GetInstance.ShowRunLog("5号炒锅PLC连接成功");
- }
- }
- }
- }
- public void ReadData()
- {
- if (IsConfig)
- {
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- //滚筒线
- try
- {
- Connected = modbus.Connected;
- if (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);
- }
- }
-
-
- }
- else
- {
- DeviceManage devices = Json<DeviceManage>.Data;
- var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "滚筒输送线");
- if (res != null)
- modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
- Thread.Sleep(10000);
- }
- }
- catch (Exception)
- {
-
- // throw;
- }
- Thread.Sleep(10);
- }), $"滚筒线实时数据读取线程");
-
-
-
-
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- try
- {
- //炒锅1
- FryOneConnected = fryOneModbus.Connected;
- if (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);
-
- }
- else
- {
- DeviceManage devices = Json<DeviceManage>.Data;
- var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅1");
- if (res != null)
- fryOneModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
-
- Thread.Sleep(10000);
- }
- }
- catch (Exception)
- {
-
- //throw;
- }
- Thread.Sleep(10);
- }), $"炒锅1实时数据读取线程");
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- try
- {
- //炒锅2
- FryTwoConnected = fryTwoModbus.Connected;
- if (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);
- }
- }
- }
- else
- {
- DeviceManage devices = Json<DeviceManage>.Data;
- var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅2");
- if (res != null)
- fryTwoModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
- Thread.Sleep(10000);
- }
- }
- catch (Exception)
- {
-
- //throw;
- }
- Thread.Sleep(10);
- }), $"炒锅2实时数据读取线程");
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- //炒锅3
- try
- {
- FryThreeConnected = fryThreeModbus.Connected;
- if (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);
-
- }
- else
- {
- DeviceManage devices = Json<DeviceManage>.Data;
- var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅3");
- if (res != null)
- fryThreeModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
- Thread.Sleep(10000);
- }
- }
- catch (Exception)
- {
-
- //throw;
- }
- Thread.Sleep(10);
- }), $"炒锅3实时数据读取线程");
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- try
- {
- //炒锅4
- FryFourConnected = fryFourModbus.Connected;
- if (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);
-
- }
- else
- {
- DeviceManage devices = Json<DeviceManage>.Data;
- var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅4");
- if (res != null)
- fryFourModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
- Thread.Sleep(10000);
- }
- }
- catch (Exception)
- {
-
- //throw;
- }
- Thread.Sleep(10);
- }), $"炒锅4实时数据读取线程");
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- try
- {
- //炒锅5
- FryFiveConnected = fryFiveModbus.Connected;
- if (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);
-
- }
- else
- {
- DeviceManage devices = Json<DeviceManage>.Data;
- var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅5");
- if (res != null)
- fryFiveModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
- Thread.Sleep(10000);
- }
- }
- catch (Exception)
- {
-
- // throw;
- }
- Thread.Sleep(10);
- }), $"炒锅5实时数据读取线程");
- }
- }
- /// <summary>
- /// 滚筒线写ushort数据
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePlcData(string address, ushort value)
- {
- lock (this)
- {
- modbus.Write(address, value);
- }
- }
- /// <summary>
- /// 滚筒线写float数据
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WriteRealPlcData(string address, float value)
- {
- lock (this)
- {
- modbus.SetReal(address,value);
- }
- }
- /// <summary>
- /// 炒锅1工艺数据
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotOnePlcData(string address,ushort value )
- {
- fryOneModbus.Write(address,value);
- }
- /// <summary>
- /// 炒锅1工艺目标重量偏差
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotOneRealPlcData(string address, float value)
- {
- fryOneModbus.SetReal(address, value);
- }
-
- /// <summary>
- /// 炒锅2工艺数据
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotTwoPlcData(string address, ushort value)
- {
- fryTwoModbus.Write(address, value);
-
- }
- /// <summary>
- /// 炒锅2工艺目标重量偏差
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotTwoRealPlcData(string address, float value)
- {
- fryTwoModbus.SetReal(address, value);
- }
- /// <summary>
- /// 炒锅3工艺数据
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotThreePlcData(string address, ushort value)
- {
- fryThreeModbus.Write(address, value);
-
- }
- /// <summary>
- /// 炒锅3工艺目标重量偏差
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotThreeRealPlcData(string address, float value)
- {
- fryThreeModbus.SetReal(address, value);
- }
- /// <summary>
- /// 炒锅4工艺数据
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotFourPlcData(string address, ushort value)
- {
-
- fryFourModbus.Write(address,value);
- }
- /// <summary>
- /// 炒锅4工艺目标重量偏差
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotFourRealPlcData(string address, float value)
- {
- fryFourModbus.SetReal(address, value);
- }
- /// <summary>
- /// 炒锅5工艺数据
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotFivePlcData(string address, object value)
- {
- fryFiveModbus.Write(address, value);
-
- }
- /// <summary>
- /// 炒锅5工艺目标重量偏差
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- public void WritePotFiveRealPlcData(string address, float value)
- {
- fryFiveModbus.SetReal(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;
- }
- }
- }
|